Skip to content

RFC: Revamp the REPL Tab completion feature

This feature reworks the tab complete feature in the repl module.

Rather than using regular expressions to guess the completion options, the tab completion will call a method on the function through a Symbol attached on these methods, which returns an array of possible completion options.

const repl = require('repl');
var fnWithTabComplete = function(someParam) {
   //fn body
}

fnWithTabComplete[repl.replComplete] = function(inputParams, paramsText) {
  // inputParams => [param1, param2, ...]
  // paramsText => "param1, param2 ..."

  return ['tabComplete1', 'tabComplete2'];
}


fnWithTabComplete('...<Tab>'
/*
  tabComplete1    tabComplete2
*/

Notes

  • This tab completion feature works on all methods and functions with the repl.replComplete Symbol attached on them.
  • The completer function gets an array of the parameters the user added. This way the implementers can specify different tab completion based on the parameter the user is trying auto complete.

Benefits

  • more scalable than using regex
  • exposes the tab completion for both external and internal modules
  • move tab completion implementations into the concerned modules rather than bloating the repl module.
  • Implementing tab completion in other modules becomes a breeze rather than trying to craft a usually-morbid regular expression.
  • Additionally, using the current approach for tab completion renders the feature rigid:
    • When I added the tab completion feature for the fs module methods, a concern about inconsistency of having the tab completion work in some places while not in others. This approach makes it the concern of the consuming modules to implement the tab completion as needed rather than having it
    • All the methods supported by tab completion will have to be maintained by the repl module, its test cases, and any updated on the main module will have to be reflected. Basically the repl will have a large dictionary ot method names in other modules.

Context

Recently i submitted a PR to implement tab completion for the fs module, and i was faced with many design issues mainly because of how impractical the regexp approach is for expanding the tab completion feature.

@starkwang @apapirovski @benjamingr can you provide feedback on this please, since you are in context with this issue already. Thanks!

Semver: Major or Minor

I believe this does not introduce any breaking changes so it could be introduced as a minor semver.

RFC

I am requesting comments and feedback on the approach and some general guidance. This PR is far from complete but it contains the initial direction of implementation. Also I have an already opened PR for the fs path autocomplete, if this PR receives positive feedback i will refactor the other one to use this feature.

Final notes

Finally, I will leave you with this piece of wisdom my brain crafted while implementing the feature 😄

I feel that the regular expression approach is similar to the foreign key constrain in SQL, using constrains usually introduces rigidity and impracticality.

Cheers!

Merge request reports

Loading