Skip to content

Commit 9d297d3

Browse files
committed
feat: filter comment and strings completions option
1 parent 30979cb commit 9d297d3

File tree

5 files changed

+30
-17
lines changed

5 files changed

+30
-17
lines changed

ace.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ export namespace Ace {
234234
relativeLineNumbers: boolean;
235235
enableMultiselect: boolean;
236236
enableKeyboardAccessibility: boolean;
237+
filterStringsCompletions: boolean;
237238
}
238239

239240
export interface SearchOptions {

src/autocomplete/text_completer.js

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,23 @@ function wordDistance(doc, pos) {
3737
return wordScores;
3838
}
3939

40-
function completionsFromMode(session, pos) {
41-
var completerTokens = session.$mode.$completerTokens;
40+
function filterStringsFromCompletions(session, pos) {
41+
var filterRegExp = /string|comment|^comment\.doc.*/;
4242
var lines = session.bgTokenizer.lines;
43+
if (!lines[pos.row]) {
44+
return;
45+
}
4346
var exclude = lines[pos.row].find(el => el.start === pos.column - el.value.length);
4447
var wordScores = Object.create(null);
4548

46-
lines = lines.flat();
47-
var linesLength = lines.length;
49+
var flatLines = lines.flat();
50+
var linesLength = flatLines.length;
4851
for (var i = 0; i < linesLength; i++) {
49-
var token = lines[i];
52+
var token = flatLines[i];
5053
if (!token || exclude && token.value === exclude.value) {
5154
continue;
5255
}
53-
if (completerTokens.includes(token.type) && identifierRe.test(token.value)) {
56+
if (!filterRegExp.test(token.type) && identifierRe.test(token.value)) {
5457
wordScores[token.value] = 0;
5558
}
5659
}
@@ -59,15 +62,19 @@ function completionsFromMode(session, pos) {
5962
}
6063

6164
exports.getCompletions = function (editor, session, pos, prefix, callback) {
62-
var wordScore = session.$mode.$completerTokens ? completionsFromMode(session, pos) : wordDistance(session, pos);
63-
var wordList = Object.keys(wordScore);
65+
var wordScore = editor.$filterStringsCompletions ? filterStringsFromCompletions(session, pos) : wordDistance(session, pos);
66+
if (!wordScore) {
67+
callback(null, null)
68+
} else {
69+
var wordList = Object.keys(wordScore);
6470

65-
callback(null, wordList.map(function (word) {
66-
return {
67-
caption: word,
68-
value: word,
69-
score: wordScore[word],
70-
meta: "local"
71-
};
72-
}));
71+
callback(null, wordList.map(function (word) {
72+
return {
73+
caption: word,
74+
value: word,
75+
score: wordScore[word],
76+
meta: "local"
77+
};
78+
}));
79+
}
7380
};

src/ext/language_tools.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,5 +221,8 @@ require("../config").defineOptions(Editor.prototype, "editor", {
221221
}
222222
},
223223
value: false
224+
},
225+
filterStringsCompletions: {
226+
initialValue: false
224227
}
225228
});

src/ext/options.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ var optionGroups = {
192192
"Live Autocompletion": {
193193
path: "enableLiveAutocompletion"
194194
},
195+
"Filter Comments and String Completions": {
196+
path: "filterStringsCompletions"
197+
},
195198
"Custom scrollbar": {
196199
path: "customScrollbar"
197200
},

src/mode/javascript.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ oop.inherits(Mode, TextMode);
2525
this.$pairQuotesAfter = {
2626
"`": /\w/
2727
};
28-
this.$completerTokens = ["identifier", "entity.name.function", "storage.type", "variable.parameter"];
2928

3029
this.getNextLineIndent = function(state, line, tab) {
3130
var indent = this.$getIndent(line);

0 commit comments

Comments
 (0)