Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

repl: added support for custom completions #8484

Closed

Conversation

diosney
Copy link

@diosney diosney commented Oct 1, 2014

Currently I'm working on a project that uses the REPL to implement a mid complex CLI app found out that the REPL module doesn't have a way to override the default complete() function, which is used for TAB completions of commands.

I think that the REPL module can benefit from this addition, and specifically the repl applications that may wants to use this functionality.

I added the docs & tests, and the tests passed fine in my environment, this is just a little change, after all.

@diosney
Copy link
Author

diosney commented Oct 1, 2014

Simple example:

var repl = require('repl')
  .start({
    terminal : true,
    completer: function completer(line) {
      var completions = 'aaa aa1 aa2 bbb ccc ddd eee'.split(' ');

      var hits = completions.filter(function (item) {
        return item.indexOf(line) == 0;
      });

      // Show all completions if none was found.
      return [
        hits.length
          ? hits
          : completions,
        line
      ];
    }
  });

@diosney
Copy link
Author

diosney commented Dec 5, 2014

Hi, any decision on this?.

@@ -429,6 +428,9 @@ var requireRE = /\brequire\s*\(['"](([\w\.\/-]+\/)?([\w\.\/-]*))/;
var simpleExpressionRE =
/(([a-zA-Z_$](?:\w|\$)*)\.)*([a-zA-Z_$](?:\w|\$)*)\.?$/;

REPLServer.prototype.complete = function() {
this.completer.apply(this, arguments);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just pass line and callback by name?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to avoid in the future if anyone adds more parameters to the original function but forgot to put them there. A little silly but removes complexity by having only one source of truth.

@cjihrig
Copy link

cjihrig commented Dec 5, 2014

It's not up to me, but I'm OK with this change (assuming it works).

@diosney
Copy link
Author

diosney commented Dec 5, 2014

OK, thanks for your opinion. I added the related tests too, so test if it works can be done automatically.

@diosney
Copy link
Author

diosney commented Feb 18, 2015

Hi, any plans to review this? Thanks

@jasnell
Copy link
Member

jasnell commented Feb 20, 2015

Also not up to me to accept it or not but this looks like a good addition.

completer: function customCompleter(line, cb) {
var completions = 'aaa aa1 aa2 bbb bb1 bb2 bb3 ccc ddd eee'.split(' ');
var hits = completions.filter(function (item) {
return item.indexOf(line) == 0;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make this ===

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Thanks.

@cjihrig
Copy link

cjihrig commented Feb 24, 2015

A few comments, but mostly LGTM. Please squash this down to a single commit. If no one has a problem with this, I'll land it.

@diosney diosney force-pushed the repl-add-custom-completion-support branch from 266bd4f to 231d34d Compare June 13, 2015 21:45
@diosney
Copy link
Author

diosney commented Jun 13, 2015

@cjihrig Sorry for the delay, somehow I got noticed right now of your comments about this :( I don't know why was until now that my email client delayed the related github notifications o_O.

Thanks for your comments, I fixed all the pointed issues and squashed the commits into a single one, so, do you think that this feature can land now?

Thanks

@@ -64,6 +64,9 @@ the following values:
returns the formatting (including coloring) to display. Defaults to
`util.inspect`.

- `completer` - an optional function that is used for Tab autocompletion.
See [Readline Interface][] for an example of using this.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we forgetting to include a link here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, at the time I made the PR, that was the way in the other docs that the linking was done, maybe this changed in the meantime. Search in https://raw.githubusercontent.com/joyent/node/master/doc/api/https.markdown for [tls.createServer()][] for an example.

If there is other way to do it, just point me in the rigth direction please, but looking at the docs code I didn't found an example of direct linking.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thefourtheye Well, I ended direct linking to the page, I searched a bit more through the docs I found a way to do it. Compiled the docs and the links worked as expected.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it seems that it had to be the relative path, something like readline.html#readline_readline_createinterface_options.

@diosney diosney force-pushed the repl-add-custom-completion-support branch from 231d34d to 92d779e Compare June 15, 2015 14:01
@diosney
Copy link
Author

diosney commented Jun 15, 2015

@cjihrig OK, links in the docs fixed too, waiting for your approval.

self.complete(text, callback);
}
// Figure out which "complete" function to use.
self.completer = options.completer || complete;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change this to self.completer = typeof options.completer === 'function' ? options.completer : complete; just to ensure that completer is a function.

Edit: You may have to split this across lines if it goes past 80 characters.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@diosney diosney force-pushed the repl-add-custom-completion-support branch from 92d779e to 823dacb Compare June 16, 2015 19:05
@diosney
Copy link
Author

diosney commented Jun 18, 2015

@cjihrig Is everything okey now? I hope so :D

return item.indexOf(line) === 0;
});
// Show all completions if none was found.
cb([hits.length ? hits : completions, line]);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you not need to pass an error argument here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean? Can you be more explicit?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most callbacks take an error as their first argument. Is that not the case here?

@cjihrig
Copy link

cjihrig commented Jun 24, 2015

Can you please open this PR against io.js. Since this is a new feature, it no longer makes sense to have it in joyent/node.

@cjihrig cjihrig closed this Jun 24, 2015
@cjihrig
Copy link

cjihrig commented Jun 24, 2015

LGTM, by the way.

@diosney
Copy link
Author

diosney commented Jun 25, 2015

@cjihrig This is really frustrating man. Seriously. It seems that the Contributing docs have to be updated.

@cjihrig
Copy link

cjihrig commented Jun 25, 2015

Sorry, I'm not sure how closely you've been following along, but Node.js is moving into a foundation and merging with io.js. This repo (joyent/node) is going to become a maintenance branch. The new repo will live at nodejs/node. However, the converged repo is not quite ready yet, so new features (such as the one in this PR) are going into io.js.

@jasnell
Copy link
Member

jasnell commented Jun 25, 2015

@diosney ... definitely can appreciate the frustration. We're currently in mid transition working on bringing node.js and io.js back together and it is a bit confusing where new contributions need to go (or even where old contributions need to land if they haven't already). The documentation will get updated but there are quite a few moving parts. All I can ask is a bit of patience ;-). The patch looks good, btw. Would love to see it land.

lance pushed a commit to nodejs/node that referenced this pull request Jul 8, 2016
Allow user code to override the default `complete()` function from
`readline.Interface`. See:
https://nodejs.org/api/readline.html#readline_use_of_the_completer_function

Ref: nodejs/node-v0.x-archive#8484

PR-URL: #7527
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Lance Ball <lball@redhat.com>
cjihrig pushed a commit to nodejs/node that referenced this pull request Aug 10, 2016
Allow user code to override the default `complete()` function from
`readline.Interface`. See:
https://nodejs.org/api/readline.html#readline_use_of_the_completer_function

Ref: nodejs/node-v0.x-archive#8484

PR-URL: #7527
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Lance Ball <lball@redhat.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants