Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion build_config/IntelEdison.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
cc.flags << %w(-O2 -pipe -g -feliminate-unused-debug-types)
cc.flags << "--sysroot=#{POKY_EDISON_SYSROOT}"
cc.compile_options = %Q[%{flags} -o "%{outfile}" -c "%{infile}"]
cc.defines = %w(MRB_USE_READLINE)
end

conf.cxx do |cxx|
Expand Down
31 changes: 7 additions & 24 deletions mrbgems/mruby-bin-mirb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ mirb-prism (mruby interactive) is an interactive Ruby shell for mruby, using the

## Tab Completion

mirb supports context-aware tab completion when built with a readline library.
mirb supports context-aware tab completion in its built-in multi-line editor.
The editor is used when standard input is a terminal; no external line-editing
library is required or consulted.

### Supported Completions

Expand Down Expand Up @@ -53,30 +55,11 @@ mirb supports context-aware tab completion when built with a readline library.
class
```

### Readline Library Support
### Behavior

Tab completion works with:

- **GNU readline** (default on Linux)
- **libedit** (default on macOS/BSD)
- **linenoise** (lightweight alternative)

### Configuration

The readline library can be configured via the `MRUBY_MIRB_READLINE` environment variable:

```bash
# Auto-detect (default)
rake

# Force specific library
MRUBY_MIRB_READLINE=readline rake # GNU readline
MRUBY_MIRB_READLINE=libedit rake # libedit
MRUBY_MIRB_READLINE=linenoise rake # linenoise

# Disable readline (plain input mode)
MRUBY_MIRB_READLINE=none rake
```
- A single match is inserted in place of the typed prefix
- Multiple matches sharing a longer common prefix extend the input to that prefix
- Otherwise the candidates are listed below the input line

### Notes

Expand Down
122 changes: 0 additions & 122 deletions mrbgems/mruby-bin-mirb/tools/mirb/mirb_completion.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,6 @@ strndup(const char *s, size_t n)
}
#endif

#ifdef MRB_USE_READLINE
#ifndef MRB_USE_LINENOISE
#include MRB_READLINE_HEADER
#endif
#endif

#ifdef MRB_USE_LINENOISE
#include <linenoise.h>
#endif

/* ============================================================
* Core Completion Engine
* ============================================================ */
Expand Down Expand Up @@ -84,7 +74,6 @@ mirb_completion_free(mirb_completion_ctx *ctx)

ctx->completion_count = 0;
ctx->completion_alloc = 0;
ctx->current_index = 0;
}

/* ============================================================
Expand Down Expand Up @@ -586,117 +575,6 @@ mirb_cleanup_completion(void)
}
}

/* ============================================================
* Readline/Libedit Adapter
* ============================================================ */

#ifdef MRB_USE_READLINE
#ifndef MRB_USE_LINENOISE

static char *
mirb_readline_generator(const char *text, int state)
{
(void)text; /* text is already in match_prefix */

/* state == 0: first call, generate completions */
if (state == 0) {
mirb_completion_free(g_ctx);

/* Generate completions based on full line */
mirb_generate_completions(g_ctx, rl_line_buffer, rl_point);

g_ctx->current_index = 0;
}

/* Return next completion or NULL when done */
if (g_ctx->current_index < g_ctx->completion_count) {
char *completion = g_ctx->completions[g_ctx->current_index];
g_ctx->current_index++;

/* readline will free this, so duplicate */
return strdup(completion);
}

return NULL;
}

static char **
mirb_readline_completion(const char *text, int start, int end)
{
(void)start;
(void)end;

/* Prevent default filename completion */
rl_attempted_completion_over = 1;

/* Use our generator */
return rl_completion_matches(text, mirb_readline_generator);
}

void
mirb_setup_readline_completion(mrb_state *mrb, mrb_ccontext *cxt)
{
if (!init_completion_ctx(mrb, cxt)) return;

/* Set completion function */
rl_attempted_completion_function = mirb_readline_completion;

/* Configure readline behavior - include . so "obj.method" are separate words */
rl_basic_word_break_characters = " \t\n\"\\'`@$><=;|&{(.";
rl_completer_word_break_characters = " \t\n\"\\'`@$><=;|&{(.";
}

#endif
#endif

/* ============================================================
* Linenoise Adapter
* ============================================================ */

#ifdef MRB_USE_LINENOISE

static void
mirb_linenoise_completion(const char *buf, linenoiseCompletions *lc)
{
int cursor_pos = (int)strlen(buf); /* linenoise completes at end */
int i, prefix_start;
char completion_line[1024];

/* Clear previous completions */
mirb_completion_free(g_ctx);

/* Generate completions */
mirb_generate_completions(g_ctx, buf, cursor_pos);

/* Need to build full line with completion */
prefix_start = cursor_pos - g_ctx->prefix_len;
if (prefix_start < 0) return;

/* Add each completion to linenoise */
for (i = 0; i < g_ctx->completion_count; i++) {
size_t len = strlen(g_ctx->completions[i]);

if ((size_t)prefix_start + len + 1 > sizeof(completion_line)) continue;

/* Copy line up to prefix, then the completion */
memcpy(completion_line, buf, prefix_start);
memcpy(completion_line + prefix_start, g_ctx->completions[i], len+1);

linenoiseAddCompletion(lc, completion_line);
}
}

void
mirb_setup_linenoise_completion(mrb_state *mrb, mrb_ccontext *cxt)
{
if (!init_completion_ctx(mrb, cxt)) return;

/* Set completion callback */
linenoiseSetCompletionCallback(mirb_linenoise_completion);
}

#endif

/* ============================================================
* Custom Editor Adapter
* ============================================================ */
Expand Down
17 changes: 2 additions & 15 deletions mrbgems/mruby-bin-mirb/tools/mirb/mirb_completion.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*
* Architecture:
* - Core engine is library-agnostic
* - Adapters for readline/libedit and linenoise
* - Adapter for the built-in multi-line editor
* - Context detection based on input line analysis
* - Safe evaluation of receiver expressions
*
Expand Down Expand Up @@ -58,7 +58,6 @@ typedef struct mirb_completion_ctx {
char **completions; /* Array of completion strings */
int completion_count; /* Number of completions */
int completion_alloc; /* Allocated size */
int current_index; /* For generator pattern (readline) */
} mirb_completion_ctx;

/* Core Completion Engine Interface */
Expand Down Expand Up @@ -100,21 +99,9 @@ mrb_value mirb_eval_receiver(mrb_state *mrb, const char *receiver_expr, mrb_ccon
/* Check if in file completion context */
mrb_bool mirb_in_file_context(const char *line, int quote_pos);

/* Cleanup completion context (shared by all adapters) */
/* Cleanup completion context */
void mirb_cleanup_completion(void);

/* Readline/Libedit adapter setup */
#ifdef MRB_USE_READLINE
#ifndef MRB_USE_LINENOISE
void mirb_setup_readline_completion(mrb_state *mrb, mrb_ccontext *cxt);
#endif
#endif

/* Linenoise adapter setup */
#ifdef MRB_USE_LINENOISE
void mirb_setup_linenoise_completion(mrb_state *mrb, mrb_ccontext *cxt);
#endif

/* Custom editor adapter */
void mirb_setup_editor_completion(mrb_state *mrb, mrb_ccontext *cxt);

Expand Down
Loading