Skip to content

Commit a3fa5db

Browse files
ghaiklorMyles Borins
authored and
Myles Borins
committed
repl: copying tabs shouldn't trigger completion
PR-URL: #5958 Fixes: #5954 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]>
1 parent f0edf87 commit a3fa5db

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

doc/api/readline.md

+7
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,13 @@ a `'resize'` event on the `output` if/when the columns ever change
354354

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

357+
## readline.emitKeypressEvents(stream[, interface])
358+
359+
Causes `stream` to begin emitting `'keypress'` events corresponding to its
360+
input.
361+
Optionally, `interface` specifies a `readline.Interface` instance for which
362+
autocompletion is disabled when copy-pasted input is detected.
363+
357364
## readline.moveCursor(stream, dx, dy)
358365

359366
Move cursor relative to it's current position in a given TTY stream.

lib/readline.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ function Interface(input, output, completer, terminal) {
3636
}
3737

3838
this._sawReturn = false;
39+
this.isCompletionEnabled = true;
3940

4041
EventEmitter.call(this);
4142
var historySize;
@@ -122,7 +123,7 @@ function Interface(input, output, completer, terminal) {
122123

123124
} else {
124125

125-
exports.emitKeypressEvents(input);
126+
exports.emitKeypressEvents(input, this);
126127

127128
// input usually refers to stdin
128129
input.on('keypress', onkeypress);
@@ -868,7 +869,7 @@ Interface.prototype._ttyWrite = function(s, key) {
868869

869870
case 'tab':
870871
// If tab completion enabled, do that...
871-
if (typeof this.completer === 'function') {
872+
if (typeof this.completer === 'function' && this.isCompletionEnabled) {
872873
this._tabComplete();
873874
break;
874875
}
@@ -902,7 +903,7 @@ exports.Interface = Interface;
902903
const KEYPRESS_DECODER = Symbol('keypress-decoder');
903904
const ESCAPE_DECODER = Symbol('escape-decoder');
904905

905-
function emitKeypressEvents(stream) {
906+
function emitKeypressEvents(stream, iface) {
906907
if (stream[KEYPRESS_DECODER]) return;
907908
var StringDecoder = require('string_decoder').StringDecoder; // lazy load
908909
stream[KEYPRESS_DECODER] = new StringDecoder('utf8');
@@ -915,6 +916,10 @@ function emitKeypressEvents(stream) {
915916
var r = stream[KEYPRESS_DECODER].write(b);
916917
if (r) {
917918
for (var i = 0; i < r.length; i++) {
919+
if (r[i] === '\t' && typeof r[i + 1] === 'string' && iface) {
920+
iface.isCompletionEnabled = false;
921+
}
922+
918923
try {
919924
stream[ESCAPE_DECODER].next(r[i]);
920925
} catch (err) {
@@ -923,6 +928,10 @@ function emitKeypressEvents(stream) {
923928
stream[ESCAPE_DECODER] = emitKeys(stream);
924929
stream[ESCAPE_DECODER].next();
925930
throw err;
931+
} finally {
932+
if (iface) {
933+
iface.isCompletionEnabled = true;
934+
}
926935
}
927936
}
928937
}

test/parallel/test-readline-interface.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,9 @@ function isWarned(emitter) {
208208
assert.strictEqual(called, false);
209209
called = true;
210210
});
211-
fi.emit('data', '\tfo\to\t');
211+
for (var character of '\tfo\to\t') {
212+
fi.emit('data', character);
213+
}
212214
fi.emit('data', '\n');
213215
assert.ok(called);
214216
rli.close();

0 commit comments

Comments
 (0)