Skip to content
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

repl: Auto alignment for .editor mode #8241

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ function Interface(input, output, completer, terminal) {

this._sawReturn = false;
this.isCompletionEnabled = true;
this._sawKeyPress = false;
this._previousKey = null;

EventEmitter.call(this);
Expand Down Expand Up @@ -247,6 +248,10 @@ Interface.prototype._addHistory = function() {
// if the history is disabled then return the line
if (this.historySize === 0) return this.line;

const line = this.line.replace(/^\s+|\s+$/, '');
Copy link
Member

Choose a reason for hiding this comment

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

Why not just this.line.trim()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@addaleax Updated

// if the trimmed line is empty then return the line
if (line.length === 0) return this.line;

Copy link
Member

Choose a reason for hiding this comment

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

Just to make sure I understand, the changes here are technically independent from the rest of the changes, right? Like, it’s only in order not to clobber the history with whitespace-only lines that come from editor mode?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. Incorporated test to ensure that whitespace-only lines are not added in history.

if (this.history.length === 0 || this.history[0] !== this.line) {
this.history.unshift(this.line);

Expand Down Expand Up @@ -947,6 +952,10 @@ function emitKeypressEvents(stream, iface) {
if (r) {
clearTimeout(timeoutId);

if (iface) {
iface._sawKeyPress = r.length === 1;
Copy link
Member

Choose a reason for hiding this comment

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

Is this guarding against pasted code?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes

}

for (var i = 0; i < r.length; i++) {
if (r[i] === '\t' && typeof r[i + 1] === 'string' && iface) {
iface.isCompletionEnabled = false;
Expand Down
9 changes: 9 additions & 0 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,15 @@ function REPLServer(prompt,

if (self.editorMode) {
self.bufferedCommand += cmd + '\n';

// code alignment
const matches = self._sawKeyPress ? cmd.match(/^\s+/) : null;
if (matches) {
const prefix = matches[0];
self.inputStream.write(prefix);
self.line = prefix;
self.cursor = prefix.length;
}
self.memory(cmd);
return;
}
Expand Down
47 changes: 47 additions & 0 deletions test/parallel/test-repl-.editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,54 @@ const tests = [
input: 'var i = 1;\ni + 3',
output: '\n4',
event: {ctrl: true, name: 'd'}
},
{
input: ' var i = 1;\ni + 3',
output: '\n4',
event: {ctrl: true, name: 'd'}
}
];

tests.forEach(({input, output, event}) => run(input, output, event));

// Auto code alignment for .editor mode
function testCodeAligment({input, cursor = 0, line = ''}) {
const stream = new common.ArrayStream();

const replServer = repl.start({
prompt: '> ',
terminal: true,
input: stream,
output: stream,
useColors: false
});

stream.emit('data', '.editor\n');
input.split('').forEach((ch) => stream.emit('data', ch));
replServer.close();
assert.strictEqual(line, replServer.line);
assert.strictEqual(cursor, replServer.cursor);
}

const codeAlignmentTests = [
{
input: 'var i = 1;\n'
},
{
input: ' var i = 1;\n',
cursor: 2,
line: ' '
},
{
input: ' var i = 1;\n',
cursor: 5,
line: ' '
},
{
input: ' var i = 1;\n var j = 2\n',
cursor: 2,
line: ' '
}
];

codeAlignmentTests.forEach(testCodeAligment);