Skip to content

Commit f3e3330

Browse files
authoredSep 19, 2023
feat: Allow not showing inline preview for completers when inlineEnabled 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.
1 parent 76b725c commit f3e3330

File tree

5 files changed

+116
-2
lines changed

5 files changed

+116
-2
lines changed
 

‎ace.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,8 @@ export namespace Ace {
10131013
getDocTooltip?(item: Completion): undefined | string | Completion;
10141014
cancel?(): void;
10151015
id?: string;
1016-
triggerCharacters?: string[]
1016+
triggerCharacters?: string[];
1017+
hideInlinePreview?: boolean;
10171018
}
10181019

10191020
export class AceInline {

‎src/autocomplete.js

+5
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,11 @@ class CompletionProvider {
675675
var total = editor.completers.length;
676676
editor.completers.forEach(function(completer, i) {
677677
completer.getCompletions(editor, session, pos, prefix, function(err, results) {
678+
if (completer.hideInlinePreview)
679+
results = results.map((result) => {
680+
return Object.assign(result, {hideInlinePreview: completer.hideInlinePreview});
681+
});
682+
678683
if (!err && results)
679684
matches = matches.concat(results);
680685
// Fetch prefix again, because they may have changed by now

‎src/autocomplete/inline.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class AceInline {
3030
return false;
3131
}
3232
var displayText = completion.snippet ? snippetManager.getDisplayTextForSnippet(editor, completion.snippet) : completion.value;
33-
if (!displayText || !displayText.startsWith(prefix)) {
33+
if (completion.hideInlinePreview || !displayText || !displayText.startsWith(prefix)) {
3434
return false;
3535
}
3636
this.editor = editor;

‎src/autocomplete/inline_test.js

+39
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ var completions = [
3737
{
3838
snippet: "foobar2",
3939
score: 0
40+
},
41+
{
42+
value: "f should not show inline",
43+
score: 0,
44+
hideInlinePreview: true
4045
}
4146
];
4247

@@ -222,6 +227,40 @@ module.exports = {
222227
assert.strictEqual(getAllLines(), textBase + "f");
223228
done();
224229
},
230+
"test: should respect hideInlinePreview": function(done) {
231+
// By default, this option is set to hide.
232+
inline.show(editor, completions[5], "f");
233+
editor.renderer.$loop._flush();
234+
assert.equal(getAllLines(), textBase + "f");
235+
assert.strictEqual(inline.isOpen(), false);
236+
inline.hide();
237+
editor.renderer.$loop._flush();
238+
239+
// Now it should be shown.
240+
completions[5].hideInlinePreview = false;
241+
242+
inline.show(editor, completions[5], "f");
243+
editor.renderer.$loop._flush();
244+
assert.equal(getAllLines(), textBase + "f should not show inline");
245+
assert.strictEqual(inline.isOpen(), true);
246+
inline.hide();
247+
editor.renderer.$loop._flush();
248+
249+
// Now it should be shown.
250+
completions[5].hideInlinePreview = undefined;
251+
252+
inline.show(editor, completions[5], "f");
253+
editor.renderer.$loop._flush();
254+
assert.equal(getAllLines(), textBase + "f should not show inline");
255+
assert.strictEqual(inline.isOpen(), true);
256+
inline.hide();
257+
editor.renderer.$loop._flush();
258+
259+
// Reset to state before test.
260+
completions[5].hideInlinePreview = true;
261+
262+
done();
263+
},
225264
tearDown: function() {
226265
inline.destroy();
227266
editor.destroy();

‎src/autocomplete_test.js

+69
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,75 @@ module.exports = {
544544
editor.completer.popup.renderer.$loop._flush();
545545
assert.equal(completer.popup.getRow(), 0);
546546

547+
done();
548+
},
549+
"test: should respect hideInlinePreview": function(done) {
550+
var editor = initEditor("hello world\n");
551+
552+
editor.completers = [
553+
{
554+
getCompletions: function (editor, session, pos, prefix, callback) {
555+
var completions = [
556+
{
557+
caption: "option 1",
558+
value: "one",
559+
score: 3
560+
}
561+
];
562+
callback(null, completions);
563+
},
564+
hideInlinePreview: true
565+
}, {
566+
getCompletions: function (editor, session, pos, prefix, callback) {
567+
var completions = [
568+
{
569+
caption: "option 2",
570+
value: "two",
571+
score: 2
572+
}
573+
];
574+
callback(null, completions);
575+
},
576+
hideInlinePreview: false
577+
}, {
578+
getCompletions: function (editor, session, pos, prefix, callback) {
579+
var completions = [
580+
{
581+
caption: "option 3",
582+
value: "three",
583+
score: 1
584+
}
585+
];
586+
callback(null, completions);
587+
}
588+
}
589+
];
590+
591+
var completer = Autocomplete.for(editor);
592+
completer.inlineEnabled = true;
593+
594+
user.type("Ctrl-Space");
595+
var inline = completer.inlineRenderer;
596+
597+
assert.equal(editor.completer.popup.isOpen, true);
598+
599+
// Row 0, should hide inline preview.
600+
assert.equal(completer.popup.getRow(), 0);
601+
assert.strictEqual(inline.isOpen(), false);
602+
603+
sendKey("Down");
604+
605+
// Row 1, should show inline preview.
606+
assert.equal(completer.popup.getRow(), 1);
607+
assert.strictEqual(inline.isOpen(), true);
608+
609+
sendKey("Down");
610+
611+
// Row 2, should show inline preview.
612+
assert.equal(completer.popup.getRow(), 2);
613+
assert.strictEqual(inline.isOpen(), true);
614+
615+
547616
done();
548617
}
549618
};

0 commit comments

Comments
 (0)
Please sign in to comment.