nodejs / help

:sparkles: Need help with Node.js? File an Issue here. :rocket:
1.47k stars 282 forks source link

Extend node.js REPL completer #4171

Closed RuslanGlaznyov closed 5 months ago

RuslanGlaznyov commented 1 year ago

Details

I implement my custom REPL, I see that we can specify completer function. But I don't wont to replace the existing completer I want to extend prev one. For instance, I would like to add additional autocompletion (function, let, const), but leave all perv autocompletion such as defined vars

Node.js version

v18.12.1 and above

Example code

const customRepl = repl.start({
  prompt: '> ',
  preview: true,
  useColors: true,
  terminal: true,
  completer: extendedCompleter
});

Operating system

Mac OS. m1

Scope

code, repl

Module and version

Not applicable.

preveen-stack commented 1 year ago

pls check if this meets your purpose

// Create a custom completer function
function customCompleter(line) {
  // Get the default completer function
  const defaultCompleter = repl.completer;

  // Call the default completer to get its completions
  const [completions, originalLine] = defaultCompleter(line);

  // Add your custom completions or modify the existing ones
  const customCompletions = ['apple', 'banana', 'cherry', 'date'];
  const combinedCompletions = completions.concat(customCompletions);

  // Return the combined completions and the original line
  return [combinedCompletions, originalLine];
}
preveen-stack commented 1 year ago

cc @nodejs/repl

github-actions[bot] commented 5 months ago

It seems there has been no activity on this issue for a while, and it is being closed in 30 days. If you believe this issue should remain open, please leave a comment. If you need further assistance or have questions, you can also search for similar issues on Stack Overflow. Make sure to look at the README file for the most updated links.

zen0wu commented 5 months ago

pls check if this meets your purpose

// Create a custom completer function
function customCompleter(line) {
  // Get the default completer function
  const defaultCompleter = repl.completer;

  // Call the default completer to get its completions
  const [completions, originalLine] = defaultCompleter(line);

  // Add your custom completions or modify the existing ones
  const customCompletions = ['apple', 'banana', 'cherry', 'date'];
  const combinedCompletions = completions.concat(customCompletions);

  // Return the combined completions and the original line
  return [combinedCompletions, originalLine];
}

having the same issue, where does this repl instance come from? if it's a constructed REPLServer, there's no way to override completer later, if it's the repl module it doesn't have the completer property, so i don't think this works

zen0wu commented 5 months ago

what i've tried that might work is this, but it doesn't feel a great solution

    const tmpReplServer = repl.start({
        input: fs.createReadStream("/dev/null"),
        output: fs.createWriteStream("/dev/null"),
    })
    const defaultCompleter = tmpReplServer.completer
    tmpReplServer.close()

    const replServer = repl.start({ 
        completer:  defaultCompleter, // can customize
    })