Skip to content

Commit

Permalink
feat: Allow not showing inline preview for completers when `inlineEna…
Browse files Browse the repository at this point in the history
…bled` is set to `true`. (#5315)

Now, we have a property at the AutoComplete level to enable inline preview for all completion items within that AutoComplete. For some use-cases, users might want to have more granularity and only show inline previews for some of the registered completers.
  • Loading branch information
akoreman authored Sep 19, 2023
1 parent 76b725c commit f3e3330
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 2 deletions.
3 changes: 2 additions & 1 deletion ace.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,8 @@ export namespace Ace {
getDocTooltip?(item: Completion): undefined | string | Completion;
cancel?(): void;
id?: string;
triggerCharacters?: string[]
triggerCharacters?: string[];
hideInlinePreview?: boolean;
}

export class AceInline {
Expand Down
5 changes: 5 additions & 0 deletions src/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,11 @@ class CompletionProvider {
var total = editor.completers.length;
editor.completers.forEach(function(completer, i) {
completer.getCompletions(editor, session, pos, prefix, function(err, results) {
if (completer.hideInlinePreview)
results = results.map((result) => {
return Object.assign(result, {hideInlinePreview: completer.hideInlinePreview});
});

if (!err && results)
matches = matches.concat(results);
// Fetch prefix again, because they may have changed by now
Expand Down
2 changes: 1 addition & 1 deletion src/autocomplete/inline.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class AceInline {
return false;
}
var displayText = completion.snippet ? snippetManager.getDisplayTextForSnippet(editor, completion.snippet) : completion.value;
if (!displayText || !displayText.startsWith(prefix)) {
if (completion.hideInlinePreview || !displayText || !displayText.startsWith(prefix)) {
return false;
}
this.editor = editor;
Expand Down
39 changes: 39 additions & 0 deletions src/autocomplete/inline_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ var completions = [
{
snippet: "foobar2",
score: 0
},
{
value: "f should not show inline",
score: 0,
hideInlinePreview: true
}
];

Expand Down Expand Up @@ -222,6 +227,40 @@ module.exports = {
assert.strictEqual(getAllLines(), textBase + "f");
done();
},
"test: should respect hideInlinePreview": function(done) {
// By default, this option is set to hide.
inline.show(editor, completions[5], "f");
editor.renderer.$loop._flush();
assert.equal(getAllLines(), textBase + "f");
assert.strictEqual(inline.isOpen(), false);
inline.hide();
editor.renderer.$loop._flush();

// Now it should be shown.
completions[5].hideInlinePreview = false;

inline.show(editor, completions[5], "f");
editor.renderer.$loop._flush();
assert.equal(getAllLines(), textBase + "f should not show inline");
assert.strictEqual(inline.isOpen(), true);
inline.hide();
editor.renderer.$loop._flush();

// Now it should be shown.
completions[5].hideInlinePreview = undefined;

inline.show(editor, completions[5], "f");
editor.renderer.$loop._flush();
assert.equal(getAllLines(), textBase + "f should not show inline");
assert.strictEqual(inline.isOpen(), true);
inline.hide();
editor.renderer.$loop._flush();

// Reset to state before test.
completions[5].hideInlinePreview = true;

done();
},
tearDown: function() {
inline.destroy();
editor.destroy();
Expand Down
69 changes: 69 additions & 0 deletions src/autocomplete_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,75 @@ module.exports = {
editor.completer.popup.renderer.$loop._flush();
assert.equal(completer.popup.getRow(), 0);

done();
},
"test: should respect hideInlinePreview": function(done) {
var editor = initEditor("hello world\n");

editor.completers = [
{
getCompletions: function (editor, session, pos, prefix, callback) {
var completions = [
{
caption: "option 1",
value: "one",
score: 3
}
];
callback(null, completions);
},
hideInlinePreview: true
}, {
getCompletions: function (editor, session, pos, prefix, callback) {
var completions = [
{
caption: "option 2",
value: "two",
score: 2
}
];
callback(null, completions);
},
hideInlinePreview: false
}, {
getCompletions: function (editor, session, pos, prefix, callback) {
var completions = [
{
caption: "option 3",
value: "three",
score: 1
}
];
callback(null, completions);
}
}
];

var completer = Autocomplete.for(editor);
completer.inlineEnabled = true;

user.type("Ctrl-Space");
var inline = completer.inlineRenderer;

assert.equal(editor.completer.popup.isOpen, true);

// Row 0, should hide inline preview.
assert.equal(completer.popup.getRow(), 0);
assert.strictEqual(inline.isOpen(), false);

sendKey("Down");

// Row 1, should show inline preview.
assert.equal(completer.popup.getRow(), 1);
assert.strictEqual(inline.isOpen(), true);

sendKey("Down");

// Row 2, should show inline preview.
assert.equal(completer.popup.getRow(), 2);
assert.strictEqual(inline.isOpen(), true);


done();
}
};
Expand Down

0 comments on commit f3e3330

Please sign in to comment.