mirror of
https://github.com/nodejs/node.git
synced 2026-11-30 11:55:44 -05:00
readline: allow completer to rewrite existing input
Sometimes, it makes sense for a completer to change the existing input, e.g. by adjusting the casing (imagine a completer that corrects `Number.isNan` to `Number.IsNaN`, for example). This commit allows that in the readline implemention. PR-URL: https://github.com/nodejs/node/pull/39178 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
This commit is contained in:
1 parent
1e065a0a43
commit
6c87b591d9
2 files changed
+49
-1
No files matched your search
+13
-1
@@ -632,9 +632,21 @@ Interface.prototype._tabComplete = function(lastKeypressWasTab) {
|
||||
// If there is a common prefix to all matches, then apply that portion.
|
||||
const prefix = commonPrefix(ArrayPrototypeFilter(completions,
|
||||
(e) => e !== ''));
|
||||
if (prefix.length > completeOn.length) {
|
||||
if (StringPrototypeStartsWith(prefix, completeOn) &&
|
||||
prefix.length > completeOn.length) {
|
||||
this._insertString(StringPrototypeSlice(prefix, completeOn.length));
|
||||
return;
|
||||
} else if (!StringPrototypeStartsWith(completeOn, prefix)) {
|
||||
this.line = StringPrototypeSlice(this.line,
|
||||
0,
|
||||
this.cursor - completeOn.length) +
|
||||
prefix +
|
||||
StringPrototypeSlice(this.line,
|
||||
this.cursor,
|
||||
this.line.length);
|
||||
this.cursor = this.cursor - completeOn.length + prefix.length;
|
||||
this._refreshLine();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!lastKeypressWasTab) {
|
||||
|
||||
@@ -100,3 +100,39 @@ common.skipIfDumbTerminal();
|
||||
});
|
||||
rli.close();
|
||||
}
|
||||
|
||||
{
|
||||
let output = '';
|
||||
class FakeInput extends EventEmitter {
|
||||
columns = 80
|
||||
|
||||
write = common.mustCall((data) => {
|
||||
output += data;
|
||||
}, 9)
|
||||
|
||||
resume() {}
|
||||
pause() {}
|
||||
end() {}
|
||||
}
|
||||
|
||||
const fi = new FakeInput();
|
||||
const rli = new readline.Interface({
|
||||
input: fi,
|
||||
output: fi,
|
||||
terminal: true,
|
||||
completer: common.mustCall((input, cb) => {
|
||||
cb(null, [[input[0].toUpperCase() + input.slice(1)], input]);
|
||||
}),
|
||||
});
|
||||
|
||||
rli.on('line', common.mustNotCall());
|
||||
fi.emit('data', 'input');
|
||||
queueMicrotask(() => {
|
||||
fi.emit('data', '\t');
|
||||
queueMicrotask(() => {
|
||||
assert.match(output, /> Input/);
|
||||
output = '';
|
||||
rli.close();
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user