Skip to content

Commit a3fa5db

Browse files
ghaiklorMyles Borins
authored andcommitted
repl: copying tabs shouldn't trigger completion
PR-URL: #5958Fixes: #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‎

Lines changed: 7 additions & 0 deletions
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‎

Lines changed: 12 additions & 3 deletions
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
varhistorySize;
@@ -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(typeofthis.completer==='function'){
872+
if(typeofthis.completer==='function'&&this.isCompletionEnabled){
872873
this._tabComplete();
873874
break;
874875
}
@@ -902,7 +903,7 @@ exports.Interface = Interface;
902903
constKEYPRESS_DECODER=Symbol('keypress-decoder');
903904
constESCAPE_DECODER=Symbol('escape-decoder');
904905

905-
functionemitKeypressEvents(stream){
906+
functionemitKeypressEvents(stream,iface){
906907
if(stream[KEYPRESS_DECODER])return;
907908
varStringDecoder=require('string_decoder').StringDecoder;// lazy load
908909
stream[KEYPRESS_DECODER]=newStringDecoder('utf8');
@@ -915,6 +916,10 @@ function emitKeypressEvents(stream){
915916
varr=stream[KEYPRESS_DECODER].write(b);
916917
if(r){
917918
for(vari=0;i<r.length;i++){
919+
if(r[i]==='\t'&&typeofr[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
throwerr;
931+
}finally{
932+
if(iface){
933+
iface.isCompletionEnabled=true;
934+
}
926935
}
927936
}
928937
}

‎test/parallel/test-readline-interface.js‎

Lines changed: 3 additions & 1 deletion
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(varcharacterof'\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)