Skip to content

repl: add possibility to edit multiline commands while adding them #58003

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

Closed

Conversation

puskin
Copy link
Contributor

@puskin puskin commented Apr 24, 2025

Summary

This PR builds upon the multiline editing support added to the REPL history in #57400, enhancing it with the ability to edit multiline input as you're writing it, before it even reaches the history.

Key Features

  1. Inline Multiline Editing: You can now insert, delete, edit, and split lines while composing a REPL command. This brings a more fluid editing experience, similar to what you'd expect from a basic code editor. See the GIF below for a demo.

  2. TTY-only Support: This feature makes heavy use of cursor movement to efficiently draw and clear parts of the current input. As a result, it currently only works in TTY environments.

  3. Unchanged Execution Behavior: Pressing Enter behaves as usual:

    • If the current input is a complete and valid command, it executes.
    • If not, a new line is added, continuing multiline input.
  4. Limitations:

    • If you want to insert a new line in the middle of a complete multiline command, you’ll need to first make the command incomplete (e.g. by removing a closing } or `).
    • The logic for inserting new lines in complete commands is in place, but I ran into trouble capturing a reliable shortcut like Shift + Enter. I’ve left a TODO to revisit this and debug further.

multiline-while-adding

EDIT

Implemented vertical cursor movement as well

vertical-movement

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. readline Issues and PRs related to the built-in readline module. repl Issues and PRs related to the REPL subsystem. labels Apr 24, 2025
@puskin puskin force-pushed the repl-multiline-refactor-and-while-typing branch from 7353efd to 9aca5ed Compare April 24, 2025 12:12
Copy link

codecov bot commented Apr 24, 2025

Codecov Report

Attention: Patch coverage is 99.54751% with 1 line in your changes missing coverage. Please review.

Project coverage is 90.22%. Comparing base (c11c7be) to head (1030ce5).
Report is 455 commits behind head on main.

Files with missing lines Patch % Lines
lib/internal/readline/interface.js 99.48% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #58003      +/-   ##
==========================================
- Coverage   90.24%   90.22%   -0.02%     
==========================================
  Files         630      630              
  Lines      185788   186589     +801     
  Branches    36429    36639     +210     
==========================================
+ Hits       167659   168358     +699     
- Misses      11004    11051      +47     
- Partials     7125     7180      +55     
Files with missing lines Coverage Δ
lib/internal/readline/utils.js 98.33% <100.00%> (+0.06%) ⬆️
lib/repl.js 94.98% <100.00%> (-0.05%) ⬇️
lib/internal/readline/interface.js 97.25% <99.48%> (+0.45%) ⬆️

... and 95 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@puskin puskin force-pushed the repl-multiline-refactor-and-while-typing branch 6 times, most recently from fe64915 to 4088f89 Compare April 30, 2025 14:24
Copy link
Member

@BridgeAR BridgeAR left a comment

Choose a reason for hiding this comment

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

This is great work and almost LGTM! I would just like to have some more tests for positions pressing enter before the end.

One behavior that I noticed that could be improved after landing is the cursor positioning while moving upwards. Imagine line 12\n1\n123 UP, UP. The cursor should actually be at the end of line 1 (12) instead of in the middle. It is a typical behavior when moving the cursor up, so keeping that would be nice.

Fixing faulty entries does not work fully anymore for multiline entries. Those do show up in my history after editing the former part. That does not have to block this PR though, since it's a nice to have.

const repl = require('internal/repl');
const stream = require('stream');

class ActionStream extends stream.Stream {
Copy link
Member

Choose a reason for hiding this comment

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

We repeat this in pretty much all REPL tests. Maybe it's time for a helper method? :)
I am a bit uncertain where to place that though. Mostly common is a place for helpers but this is very specific for the REPL, so maybe somewhere else.

This is not blocking.

@puskin
Copy link
Contributor Author

puskin commented Apr 30, 2025

Fixing faulty entries does not work fully anymore for multiline entries. Those do show up in my history after editing the former part. That does not have to block this PR though, since it's a nice to have.

interesting, I cannot replicate the issue (we also have a specific test for this).
What I am doing to test the behavior is to paste this

let lineWithMistake = `I have some
problem with` my syntax'

and then fixing it editing the history, and it is working just fine. What are you typing to make the functionality break?

@puskin puskin force-pushed the repl-multiline-refactor-and-while-typing branch from 4088f89 to 7b3c67f Compare April 30, 2025 19:32
@BridgeAR
Copy link
Member

interesting, I cannot replicate the issue (we also have a specific test for this).
What I am doing to test the behavior is to paste this

I just had another look and it and it is actually working! I created a ReferenceError, not a SyntaxError. We could probably handle it identically but that is something for another PR as further improvement :)

Copy link
Member

@BridgeAR BridgeAR left a comment

Choose a reason for hiding this comment

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

LGTM with my last comment addressed. This is really good work!

@BridgeAR BridgeAR added the request-ci Add this label to start a Jenkins CI on a PR. label Apr 30, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Apr 30, 2025
@nodejs-github-bot
Copy link
Collaborator

@puskin puskin force-pushed the repl-multiline-refactor-and-while-typing branch from 7b3c67f to ff4639e Compare May 1, 2025 06:07
@puskin puskin force-pushed the repl-multiline-refactor-and-while-typing branch from 210b0da to 1030ce5 Compare May 1, 2025 17:31
@BridgeAR BridgeAR added the request-ci Add this label to start a Jenkins CI on a PR. label May 2, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label May 2, 2025
@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@BridgeAR BridgeAR added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label May 3, 2025
@nodejs-github-bot
Copy link
Collaborator

@BridgeAR BridgeAR added the commit-queue Add this label to land a pull request using GitHub Actions. label May 3, 2025
@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot nodejs-github-bot added commit-queue-failed An error occurred while landing this pull request using GitHub Actions. and removed commit-queue Add this label to land a pull request using GitHub Actions. labels May 4, 2025
@nodejs-github-bot
Copy link
Collaborator

Commit Queue failed
- Loading data for nodejs/node/pull/58003
✔  Done loading data for nodejs/node/pull/58003
----------------------------------- PR info ------------------------------------
Title      repl: add possibility to edit multiline commands while adding them (#58003)
   ⚠  Could not retrieve the email or name of the PR author's from user's GitHub profile!
Branch     puskin:repl-multiline-refactor-and-while-typing -> nodejs:main
Labels     readline, repl, author ready, needs-ci
Commits    2
 - repl: add possibility to edit multiline commands while adding them
 - repl: add proper vertical cursor movements
Committers 1
 - Giovanni <github@puskin.it>
PR-URL: https://github.com/nodejs/node/pull/58003
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com>
------------------------------ Generated metadata ------------------------------
PR-URL: https://github.com/nodejs/node/pull/58003
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com>
--------------------------------------------------------------------------------
   ℹ  This PR was created on Thu, 24 Apr 2025 12:10:53 GMT
   ✔  Approvals: 2
   ✔  - Ruben Bridgewater (@BridgeAR) (TSC): https://github.com/nodejs/node/pull/58003#pullrequestreview-2812512679
   ✔  - Pietro Marchini (@pmarchini): https://github.com/nodejs/node/pull/58003#pullrequestreview-2813299994
   ✔  Last GitHub CI successful
   ℹ  Last Full PR CI on 2025-05-04T17:35:22Z: https://ci.nodejs.org/job/node-test-pull-request/66590/
- Querying data for job/node-test-pull-request/66590/
   ✔  Last Jenkins CI successful
--------------------------------------------------------------------------------
   ✔  No git cherry-pick in progress
   ✔  No git am in progress
   ✔  No git rebase in progress
--------------------------------------------------------------------------------
- Bringing origin/main up to date...
From https://github.com/nodejs/node
 * branch                  main       -> FETCH_HEAD
✔  origin/main is now up-to-date
- Downloading patch for 58003
From https://github.com/nodejs/node
 * branch                  refs/pull/58003/merge -> FETCH_HEAD
✔  Fetched commits as 5fb879c4584c..1030ce5cbc8a
--------------------------------------------------------------------------------
[main 745059a9c5] repl: add possibility to edit multiline commands while adding them
 Author: Giovanni <github@puskin.it>
 Date: Wed Apr 23 15:05:56 2025 +0200
 7 files changed, 506 insertions(+), 53 deletions(-)
 create mode 100644 test/parallel/test-repl-multiline-navigation-while-adding.js
[main 48af18f664] repl: add proper vertical cursor movements
 Author: Giovanni <github@puskin.it>
 Date: Thu May 1 10:20:17 2025 +0200
 3 files changed, 121 insertions(+), 38 deletions(-)
   ✔  Patches applied
There are 2 commits in the PR. Attempting autorebase.
Rebasing (2/4)
Executing: git node land --amend --yes
--------------------------------- New Message ----------------------------------
repl: add possibility to edit multiline commands while adding them

PR-URL: #58003
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com>

[detached HEAD 62eef37ac7] repl: add possibility to edit multiline commands while adding them
Author: Giovanni <github@puskin.it>
Date: Wed Apr 23 15:05:56 2025 +0200
7 files changed, 506 insertions(+), 53 deletions(-)
create mode 100644 test/parallel/test-repl-multiline-navigation-while-adding.js
Rebasing (3/4)
Rebasing (4/4)
Executing: git node land --amend --yes
--------------------------------- New Message ----------------------------------
repl: add proper vertical cursor movements

PR-URL: #58003
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com>

[detached HEAD 7b279215aa] repl: add proper vertical cursor movements
Author: Giovanni <github@puskin.it>
Date: Thu May 1 10:20:17 2025 +0200
3 files changed, 121 insertions(+), 38 deletions(-)
Successfully rebased and updated refs/heads/main.

ℹ Add commit-queue-squash label to land the PR as one commit, or commit-queue-rebase to land as separate commits.

https://github.com/nodejs/node/actions/runs/14823896908

@BridgeAR BridgeAR added commit-queue Add this label to land a pull request using GitHub Actions. commit-queue-rebase Add this label to allow the Commit Queue to land a PR in several commits. and removed commit-queue-failed An error occurred while landing this pull request using GitHub Actions. labels May 4, 2025
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label May 4, 2025
@nodejs-github-bot
Copy link
Collaborator

Landed in 5fb879c...995ad2b

nodejs-github-bot pushed a commit that referenced this pull request May 4, 2025
PR-URL: #58003
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com>
nodejs-github-bot pushed a commit that referenced this pull request May 4, 2025
PR-URL: #58003
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com>
targos pushed a commit that referenced this pull request May 16, 2025
PR-URL: #58003
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com>
targos pushed a commit that referenced this pull request May 16, 2025
PR-URL: #58003
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com>
@aduh95
Copy link
Contributor

aduh95 commented Jun 10, 2025

This does not land cleanly on v22.x-staging, and would require a manual backport PR if we want it on the 22.x line.

@aduh95 aduh95 added the backport-requested-v22.x PRs awaiting manual backport to the v22.x-staging branch. label Jun 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. backport-requested-v22.x PRs awaiting manual backport to the v22.x-staging branch. commit-queue-rebase Add this label to allow the Commit Queue to land a PR in several commits. needs-ci PRs that need a full CI run. readline Issues and PRs related to the built-in readline module. repl Issues and PRs related to the REPL subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants