-
-
Notifications
You must be signed in to change notification settings - Fork 823
feat: add auto-completion preview for REPL #1832
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Change repl to show a gray preview of the possible tab completion, whenever there is only one available. This feature is modeled after the preview in the node.js REPL, and should enhance the user experience.
Adds several special interactions inspired by the node REPL, to enhance user experience. When the user moves the caret into the preview it is now automatically filled. Also, when the user presses enter while the preview is showing, it will be filled in before the line is processed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👋 Hi there! 👋
And thank you for opening your first pull request! We will review it shortly. 🏃 💨
/stdlib update-copyright-years |
Refactor preview_completer from factory function to small class. Also, use repeat from stdlib instead of the string prototype, and make some small style fixes.
@tudor-pagu A separate PR. We can discuss more on #1794, as that RFC is still under discussion. |
Make the beforeKeypress and onKeypress methods be defined in the prototype of PreviewCompleter so that they are not created again for every instance.
The clearPreview function in completerCallback() was accidentally removed, which caused some tests to fail. This commit puts it back.
@tudor-pagu I've updated your PR. I encourage you to look through the changes in order to better understand expectations. This stated, the preview completion does work; however, there is a bug which I did not have time to investigate or fix. Namely, when a completion preview is displayed, if you hit space one or more times, the preview is still displayed. This should not be the case. Upon hitting space, the preview should be cleared. I also did not update the tests, which also need some clean-up. |
@kgryte I think there's 2 separate issues here:
Also, I've found another bug with the completer: (from before my commits) I also want to ask: If every possible completion has a common prefix, should we display that prefix as a preview? I noticed that typing
Leads to no preview, because the completions are ['unary','unaryBy','unary...', etc], but pressing tab completes to unary (common prefix). Should the preview also display unary here? My worry is it might be slow to check the common prefix of all completions when the number of completions is high, maybe we should only do this check if there are less than ~10 completions? |
Fixes a bug where the preview would still show after user pressed space.
Indeed, it looks like the introduction of the preview completer broke the ability to handle brackets. |
Ah, okay, was able to reproduce the TAB crash behavior when using the existing TAB completion functionality. |
This commit addresses a bug wherein, if a user types `var x = [` and hits TAB, the REPL crashes, as it assumes that the expression is a valid expression.
Okay. I think I've fixed the various bugs associated with TAB completion which were affecting preview completion. |
I also updated the logic for handling trailing whitespace. I had forgotten about the scenario such as
where you'd want to display a preview for properties containing whitespace. |
@tudor-pagu Okay, so I think this is coming along. Re: common prefix preview completion. Well, I'd say go ahead and try to implement it without restrictions and see how slow it is. If it makes the REPL unusable, then can try restricting to, say, when there are 10 completions or fewer. |
Extract the logic for setting up the repl with the debug streams into a separate fixture. Also, add comments to the tests.
If there are multiple completion candidates, but they all have some non-empty common prefix, that prefix will now be shown as a preview. This works well with the tab completion which also fills in the common prefix when the user presses tab.
@kgryte I've also found a small bug with the completer: variable names are only tab completed if they are initialized.
|
A couple of comments:
In order to support TAB completion of I think this is doable (albeit imperfect; e.g., |
@kgryte |
@tudor-pagu Thanks for the ping. It is on my todos. Hopefully, I'll have a bit more feedback on the tests by this weekend, so we can get this over the finish line. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tudor-pagu I refactored and cleaned up the tests. I believe this PR should be good to merge. Will merge once CI passes. Thanks for working on this!
Resolves #1775 .
Description
This pull request implements the preview for auto-completion feature, inspired by the node.js REPL. Whenever there is only one possible tab completion, it will be shown to the user as a gray preview. This preview is shown to the user regardless of where the cursor is. There are a few special interactions related to the preview:
Screencast from 11.03.2024 17:34:20.webm
Screencast from 11.03.2024 17:37:36.webm
Related Issues
#1794 aims to implement a related feature, and might depend on this PR (depending on the implementation).
Questions
Some things I was not sure about when implementing this:
completionCache
variable. If there is a better way of structuring this I would be happy to implement it.Other
In
preview_completer.js
, theno-underscore-dangle
eslint rule is disabled in order to access the _rli attribute of theREPL
object. This is also done in already existing code, such as inmain.js
andcompleter.js
, for similar reasons.In order to implement the auto-completion when the user presses enter, I had to override the internal _ttyWrite attribute of the readLine interface. This is because any other event listener only fires after the character is processed, so by that point the line is cleared and already submitted. While it is potentially dangerous to override internal methods, this is the only way I could find, and this has also been done in other established projects, such as this implementation of the node.js REPL:
Checklist
@stdlib-js/reviewers