Skip to content

Commit

Permalink
feat: filter comment and strings completions option
Browse files Browse the repository at this point in the history
  • Loading branch information
mkslanc committed Jul 12, 2023
1 parent 30979cb commit 9d297d3
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 17 deletions.
1 change: 1 addition & 0 deletions ace.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ export namespace Ace {
relativeLineNumbers: boolean;
enableMultiselect: boolean;
enableKeyboardAccessibility: boolean;
filterStringsCompletions: boolean;
}

export interface SearchOptions {
Expand Down
39 changes: 23 additions & 16 deletions src/autocomplete/text_completer.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,23 @@ function wordDistance(doc, pos) {
return wordScores;
}

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

lines = lines.flat();
var linesLength = lines.length;
var flatLines = lines.flat();
var linesLength = flatLines.length;
for (var i = 0; i < linesLength; i++) {
var token = lines[i];
var token = flatLines[i];
if (!token || exclude && token.value === exclude.value) {
continue;
}
if (completerTokens.includes(token.type) && identifierRe.test(token.value)) {
if (!filterRegExp.test(token.type) && identifierRe.test(token.value)) {
wordScores[token.value] = 0;
}
}
Expand All @@ -59,15 +62,19 @@ function completionsFromMode(session, pos) {
}

exports.getCompletions = function (editor, session, pos, prefix, callback) {
var wordScore = session.$mode.$completerTokens ? completionsFromMode(session, pos) : wordDistance(session, pos);
var wordList = Object.keys(wordScore);
var wordScore = editor.$filterStringsCompletions ? filterStringsFromCompletions(session, pos) : wordDistance(session, pos);
if (!wordScore) {
callback(null, null)
} else {
var wordList = Object.keys(wordScore);

callback(null, wordList.map(function (word) {
return {
caption: word,
value: word,
score: wordScore[word],
meta: "local"
};
}));
callback(null, wordList.map(function (word) {
return {
caption: word,
value: word,
score: wordScore[word],
meta: "local"
};
}));
}
};
3 changes: 3 additions & 0 deletions src/ext/language_tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,5 +221,8 @@ require("../config").defineOptions(Editor.prototype, "editor", {
}
},
value: false
},
filterStringsCompletions: {
initialValue: false
}
});
3 changes: 3 additions & 0 deletions src/ext/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ var optionGroups = {
"Live Autocompletion": {
path: "enableLiveAutocompletion"
},
"Filter Comments and String Completions": {
path: "filterStringsCompletions"
},
"Custom scrollbar": {
path: "customScrollbar"
},
Expand Down
1 change: 0 additions & 1 deletion src/mode/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ oop.inherits(Mode, TextMode);
this.$pairQuotesAfter = {
"`": /\w/
};
this.$completerTokens = ["identifier", "entity.name.function", "storage.type", "variable.parameter"];

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

0 comments on commit 9d297d3

Please sign in to comment.