Skip to content

Commit

Permalink
fix: ignore initial whitespaces when modifying words
Browse files Browse the repository at this point in the history
Signed-off-by: Snehil Shah <[email protected]>
  • Loading branch information
Snehil-Shah committed Sep 20, 2024
1 parent 5d21f07 commit 5394596
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions lib/node_modules/@stdlib/repl/lib/editor_actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var parseKey = require( './parse_key.js' );

// VARIABLES //

var RE_NON_WHITESPACE = /\S+/;
var EDITOR_ACTIONS = [
'moveRight',
'moveLeft',
Expand Down Expand Up @@ -167,14 +168,22 @@ setNonEnumerableReadOnly( EditorActions.prototype, '_replaceLine', function repl
setNonEnumerableReadOnly( EditorActions.prototype, '_modifyNextWord', function modifyNextWord( modifier ) {
var updatedLine;
var substring;
var match;
var start;
var end;

end = this._rli.line.indexOf( ' ', this._rli.cursor + 1 );
if ( end === -1 ) {
// Use regex to find the first non-whitespace character and the end of the word after the cursor:
match = this._rli.line.slice( this._rli.cursor ).match( RE_NON_WHITESPACE );
if ( match ) {
start = this._rli.cursor + match.index;
end = start + match[ 0 ].length;
} else {
start = this._rli.cursor;
end = this._rli.line.length;
}
substring = this._rli.line.slice( this._rli.cursor, end );
updatedLine = this._rli.line.slice( 0, this._rli.cursor ) + modifier( substring ) + this._rli.line.slice( end ); // eslint-disable-line max-len
// Extract the word, apply the modifier, and reconstruct the line:
substring = this._rli.line.slice( start, end );
updatedLine = this._rli.line.slice( 0, start ) + modifier( substring ) + this._rli.line.slice( end ); // eslint-disable-line max-len
this._replaceLine( updatedLine );
this._moveCursorX( end - this._rli.line.length );
});
Expand Down

0 comments on commit 5394596

Please sign in to comment.