Skip to content

feat: add multiline editing in the REPL #2347

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

Merged
merged 11 commits into from
Jun 21, 2024
Merged

Conversation

Snehil-Shah
Copy link
Member

@Snehil-Shah Snehil-Shah commented Jun 9, 2024

Resolves #2060

Description

What is the purpose of this pull request?

This pull request:

  • adds support for manually adding a newline using CTRL+O keybinding.
  • adds support for navigating through lines using arrow keys.
  • adds support for continuous backspaces.

Related Issues

Does this pull request have any related issues?

This pull request:

Questions

Any questions for reviewers of this pull request?

Testing strategy?

Other

Any other information relevant to this pull request? This may include screenshots, references, and/or implementation notes.

I wanted to clarify a mistake in the original RFC and that hitting the up arrow auto-completes the previous commands only if the line is empty. After this PR, this behavior is preserved. Only when you have an empty input, the up arrow completions will work.

I will add the ability to cycle through multiline commands using the up arrow, in a separate PR.

Checklist

Please ensure the following tasks are completed before submitting this pull request.


@stdlib-js/reviewers

Signed-off-by: Snehil Shah <[email protected]>
@kgryte kgryte added Enhancement Issue or pull request for enhancing existing functionality. REPL Issue or pull request specific to the project REPL. labels Jun 9, 2024
@Snehil-Shah Snehil-Shah requested a review from kgryte June 10, 2024 07:42
@Snehil-Shah Snehil-Shah requested a review from Planeshifter June 13, 2024 15:33
Signed-off-by: Snehil Shah <[email protected]>
@kgryte
Copy link
Member

kgryte commented Jun 15, 2024

@Snehil-Shah After pulling this down and playing with things, I was able to get the REPL into a state where I was stuck in multi-line edit mode and the command cache was not being cleared. Even after executing subsequent commands, anytime I hit enter from a new command prompt, rather than execute an empty statement, the REPL would re-enter multi-line mode. Looks like some edge cases are not being fully handled.

@kgryte
Copy link
Member

kgryte commented Jun 15, 2024

Also, when I did the following

In [1]: {<|>}

and hit Ctrl+E, the REPL did not break the line at the cursor. Instead, it did

In [1]: {}
<|>

which is not what is typically desired..

@Snehil-Shah
Copy link
Member Author

Snehil-Shah commented Jun 15, 2024

@kgryte I think I found out what's really going on. So, the thing is, I have been testing this out with CTRL+O combination all this time because apparently CTRL+E was clashing with my editor's internal keybinding (yes I got lazy there in changing my editor preferences, I apologize). I now tried CTRL+E. Apparently, in node's readline, CTRL+E has a default keypress action of "to end of line". So everytime we do ctrl+e, we go to the end of the line and then we enter multiline editing. That's the reason your curly braces example is doing what it's doing.
I suggest we change to CTRL+O as the keybinding. Or move CTRL+E check to beforeKeypress handler and overwrite default behaviour. Again, so sorry for the oversight.

rather than execute an empty statement, the REPL would re-enter multi-line mode. Looks like some edge cases are not being fully handled.

image

That was intentional and not a problem with the cache. I thought empty statements don't mean anything so it would be better to assume they are trying to type more, maybe to have equal indentation by starting from below the prompt and they can always use backspace to go back.

In [1]: 
function a(){
// ...
}

I'll change it if it's not desired or correct.

@kgryte
Copy link
Member

kgryte commented Jun 16, 2024

Re: keybinding. Ctrl-O is fine.

Re: empty statements. I found the updated behavior surprising. My feeling is that we should treat statements (including empty statements) consistently. Meaning,

In [1]: <|>

and

In [1]: a<|>

should behave consistently when hitting ENTER.

And to enter multi-line mode in both instances, one needs to use the Ctrl-O keybinding. Special casing empty statements doesn't seem like enough of a win to be worth the inconsistency.

@Snehil-Shah
Copy link
Member Author

@kgryte done.

Copy link
Member

@kgryte kgryte left a comment

Choose a reason for hiding this comment

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

LGTM. I pulled down locally and tested out. Seems to work as stated.

As stated in the OP, we'll want to figure out the previous command/history issue. And moving from processLine to a dedicated multi-line handler class significantly increases complexity, requiring fairly tight coupling between various components. This class is a good candidate for a future refactor in order to disentangle logic. For now, however, I'm fine with punting this to another day.

Thanks for working on this, @Snehil-Shah!

@kgryte kgryte merged commit 32b9ebf into stdlib-js:develop Jun 21, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Issue or pull request for enhancing existing functionality. REPL Issue or pull request specific to the project REPL.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[RFC]: add support for multi-line editing to the REPL
3 participants