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: Copying tab characters into the repl calls tabComplete #5958

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion doc/api/readline.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,12 @@ a `'resize'` event on the `output` if/when the columns ever change

Move cursor to the specified position in a given TTY stream.

## readline.emitKeypressEvents(stream)
## readline.emitKeypressEvents(stream[, interface])

Causes `stream` to begin emitting `'keypress'` events corresponding to its
input.
Optionally, `interface` specifies a `readline.Interface` instance for which
autocompletion is disabled when copy-pasted input is detected.

## readline.moveCursor(stream, dx, dy)

Expand Down
15 changes: 12 additions & 3 deletions lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function Interface(input, output, completer, terminal) {
}

this._sawReturn = false;
this.isCompletionEnabled = true;

EventEmitter.call(this);
var historySize;
Expand Down Expand Up @@ -129,7 +130,7 @@ function Interface(input, output, completer, terminal) {

} else {

emitKeypressEvents(input);
emitKeypressEvents(input, this);

// input usually refers to stdin
input.on('keypress', onkeypress);
Expand Down Expand Up @@ -878,7 +879,7 @@ Interface.prototype._ttyWrite = function(s, key) {

case 'tab':
// If tab completion enabled, do that...
if (typeof this.completer === 'function') {
if (typeof this.completer === 'function' && this.isCompletionEnabled) {
this._tabComplete();
break;
}
Expand Down Expand Up @@ -912,7 +913,7 @@ exports.Interface = Interface;
const KEYPRESS_DECODER = Symbol('keypress-decoder');
const ESCAPE_DECODER = Symbol('escape-decoder');

function emitKeypressEvents(stream) {
function emitKeypressEvents(stream, iface) {
Copy link
Contributor

Choose a reason for hiding this comment

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

if (stream[KEYPRESS_DECODER]) return;
var StringDecoder = require('string_decoder').StringDecoder; // lazy load
stream[KEYPRESS_DECODER] = new StringDecoder('utf8');
Expand All @@ -925,6 +926,10 @@ function emitKeypressEvents(stream) {
var r = stream[KEYPRESS_DECODER].write(b);
if (r) {
for (var i = 0; i < r.length; i++) {
if (r[i] === '\t' && typeof r[i + 1] === 'string' && iface) {
iface.isCompletionEnabled = false;
Copy link
Contributor

@Fishrock123 Fishrock123 May 10, 2016

Choose a reason for hiding this comment

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

This will throw if iface is undefined though. Need to:

a. Document that iface is an option (probably)
b. Ignore it if iface is undefined

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm... Why? We have ... && iface) condition.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ahhh sorry I didn't see the checks for it 😥

All good to go if there is a docs note.

}

try {
stream[ESCAPE_DECODER].next(r[i]);
} catch (err) {
Expand All @@ -933,6 +938,10 @@ function emitKeypressEvents(stream) {
stream[ESCAPE_DECODER] = emitKeys(stream);
stream[ESCAPE_DECODER].next();
throw err;
} finally {
if (iface) {
iface.isCompletionEnabled = true;
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion test/parallel/test-readline-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,9 @@ function isWarned(emitter) {
assert.strictEqual(called, false);
called = true;
});
fi.emit('data', '\tfo\to\t');
for (var character of '\tfo\to\t') {
fi.emit('data', character);
}
fi.emit('data', '\n');
assert.ok(called);
rli.close();
Expand Down