Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Ability to filter comments and strings from text completer #5233

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
26 changes: 25 additions & 1 deletion src/autocomplete/text_completer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var Range = require("../range").Range;

var splitRegex = /[^a-zA-Z_0-9\$\-\u00C0-\u1FFF\u2C00-\uD7FF\w]+/;
var identifierRe = /[a-zA-Z\$_\u00a1-\uffff][a-zA-Z\\d\\$_\u00a1-\uffff]*/;

function getWordIndex(doc, pos) {
var textBefore = doc.getTextRange(Range.fromPoints({
Expand Down Expand Up @@ -36,9 +37,32 @@ function wordDistance(doc, pos) {
return wordScores;
}

function filterStringsFromCompletions(session, pos) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this has a different behaviour to wordDistance, I think it would make sense to add a basic test that verifies getting completions works as expected with filterStringsCompletions enabled.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the name of the function does not really represent what's going on, it returns wordScores for filtered strings so maybe getWordScoresForFilteredStrings would better suit it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This name really better suits purpose of function, but could you, please, clarify, which tests you are expecting? Current test is checking scenario with and without filterStringsCompletions

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only test that with filterStringsCompletions enabled comments and doc comments are filtered out, but we don't test that completion works as expected with this option enabled and returns results in expected order (or something like that), since the logic now differs significantly.

var filterRegExp = /string|comment|^comment\.doc.*/;
var lines = session.bgTokenizer.lines;
var exclude = lines[pos.row] && lines[pos.row].find(el => el.start === pos.column - el.value.length);
var wordScores = Object.create(null);

var flatLines = lines.flat();
var linesLength = flatLines.length;
for (var i = 0; i < linesLength; i++) {
var token = flatLines[i];
if (!token || exclude && token.value === exclude.value) {
continue;
}
if (!filterRegExp.test(token.type) && identifierRe.test(token.value)) {
wordScores[token.value] = 0;
}
}

return wordScores;
}

exports.getCompletions = function (editor, session, pos, prefix, callback) {
var wordScore = wordDistance(session, pos);
var wordScore = editor.$filterStringsCompletions ? filterStringsFromCompletions(session, pos) : wordDistance(
session, pos);
var wordList = Object.keys(wordScore);

callback(null, wordList.map(function (word) {
return {
caption: word,
Expand Down
27 changes: 25 additions & 2 deletions src/autocomplete_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ var user = require("./test/user");
var Range = require("./range").Range;
require("./ext/language_tools");
var Autocomplete = require("./autocomplete").Autocomplete;
var textCompleter = require("./autocomplete/text_completer");
var JavaScriptMode = require("./mode/javascript").Mode;

var editor;
function initEditor(value) {
function initEditor(value, mode) {
if (editor) {
editor.destroy();
editor.container.remove();
Expand All @@ -24,7 +26,8 @@ function initEditor(value) {
value: value,
maxLines: 10,
enableBasicAutocompletion: true,
enableLiveAutocompletion: true
enableLiveAutocompletion: true,
mode: mode
});
document.body.appendChild(editor.container);
editor.focus();
Expand Down Expand Up @@ -162,6 +165,26 @@ module.exports = {
done();
});
},
"test: filter strings and comments from local completions list": function (done) {
var editor = initEditor("//comment here\n /**\n * doc comment\n**/'string'\nsomeIdentifier\n", new JavaScriptMode());
editor.completers = [textCompleter];
editor.moveCursorTo(3, 0);
editor.renderer.$loop._flush();

sendKey("o");
var popup = editor.completer.popup;
afterRenderCheck(popup, function () {
assert.equal(popup.data.length, 3);
editor.setOption("filterStringsCompletions", true);
editor.onCommandKey(null, 0, 13);
sendKey(" ");
sendKey("o");
afterRenderCheck(popup, function () {
assert.equal(popup.data.length, 1); //only identifier left
done();
});
});
},
"test: different completers tooltips": function (done) {
var editor = initEditor("");
var firstDoc = "<b>First</b>";
Expand Down
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