Skip to content

Commit 51006bb

Browse files
authored
fix: improve prefixing for inline preview (#5439)
Use the string of text between the start of the autocomplete prefix and the current cursor position, regardless of the characters in between, to determine whether inline preview should be rendered. This allows inline preview to stay visible when typing ahead with whitespace.
1 parent c433537 commit 51006bb

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

src/autocomplete.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,14 @@ class Autocomplete {
197197
}
198198

199199
$updateGhostText(completion) {
200-
var prefix = util.getCompletionPrefix(this.editor);
200+
// Ghost text can include characters normally not part of the prefix (e.g. whitespace).
201+
// When typing ahead with ghost text however, we want to simply prefix with respect to the
202+
// base of the completion.
203+
var row = this.base.row;
204+
var column = this.base.column;
205+
var cursorColumn = this.editor.getCursorPosition().column;
206+
var prefix = this.editor.session.getLine(row).slice(column, cursorColumn);
207+
201208
if (!this.inlineRenderer.show(this.editor, completion, prefix)) {
202209
this.inlineRenderer.hide();
203210
} else {

src/autocomplete_test.js

+45
Original file line numberDiff line numberDiff line change
@@ -1383,6 +1383,51 @@ module.exports = {
13831383
assert.strictEqual(inline.isOpen(), true);
13841384
assert.strictEqual(editor.renderer.$ghostText.text, "n\nthat does something\ncool");
13851385

1386+
done();
1387+
}, 100);
1388+
}, 100);
1389+
},
1390+
"test: should keep showing ghost text when typing ahead with whitespace": function(done) {
1391+
var editor = initEditor("");
1392+
1393+
editor.completers = [
1394+
{
1395+
getCompletions: function (editor, session, pos, prefix, callback) {
1396+
var completions = [
1397+
{
1398+
value: "function that does something cool"
1399+
}
1400+
];
1401+
callback(null, completions);
1402+
}
1403+
}
1404+
];
1405+
1406+
var completer = Autocomplete.for(editor);
1407+
completer.inlineEnabled = true;
1408+
1409+
user.type("f");
1410+
var inline = completer.inlineRenderer;
1411+
1412+
// Popup should be open, with inline text renderered.
1413+
assert.equal(completer.popup.isOpen, true);
1414+
assert.equal(completer.popup.getRow(), 0);
1415+
assert.strictEqual(inline.isOpen(), true);
1416+
assert.strictEqual(editor.renderer.$ghostText.text, "unction that does something cool");
1417+
1418+
// when you keep typing, the ghost text should update accordingly
1419+
user.type("unction th");
1420+
1421+
setTimeout(() => {
1422+
assert.strictEqual(inline.isOpen(), true);
1423+
assert.strictEqual(editor.renderer.$ghostText.text, "at does something cool");
1424+
1425+
user.type("at do");
1426+
1427+
setTimeout(() => {
1428+
assert.strictEqual(inline.isOpen(), true);
1429+
assert.strictEqual(editor.renderer.$ghostText.text, "es something cool");
1430+
13861431
done();
13871432
}, 100);
13881433
}, 100);

0 commit comments

Comments
 (0)