From c1cdbe0081955e456e3a2be2043cf140a9653e3c Mon Sep 17 00:00:00 2001 From: mkslanc Date: Wed, 12 Jul 2023 17:56:06 +0400 Subject: [PATCH] feat: filter comment and strings completions option --- ace.d.ts | 1 + src/autocomplete/text_completer.js | 38 +++++++++++++++++------------- src/autocomplete_test.js | 27 +++++++++++++++++++-- src/ext/language_tools.js | 3 +++ src/ext/options.js | 3 +++ src/mode/javascript.js | 1 - 6 files changed, 53 insertions(+), 20 deletions(-) diff --git a/ace.d.ts b/ace.d.ts index 81ab5adb784..c1dfce7d6a0 100644 --- a/ace.d.ts +++ b/ace.d.ts @@ -234,6 +234,7 @@ export namespace Ace { relativeLineNumbers: boolean; enableMultiselect: boolean; enableKeyboardAccessibility: boolean; + filterStringsCompletions: boolean; } export interface SearchOptions { diff --git a/src/autocomplete/text_completer.js b/src/autocomplete/text_completer.js index 583182da51f..25131a926de 100644 --- a/src/autocomplete/text_completer.js +++ b/src/autocomplete/text_completer.js @@ -37,20 +37,20 @@ 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; - var exclude = lines[pos.row].find(el => el.start === pos.column - el.value.length); + var exclude = lines[pos.row] && 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; } } @@ -59,15 +59,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" + }; + })); + } }; diff --git a/src/autocomplete_test.js b/src/autocomplete_test.js index 34a1e479e00..3b8ff27335a 100644 --- a/src/autocomplete_test.js +++ b/src/autocomplete_test.js @@ -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(); @@ -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(); @@ -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 = "First"; diff --git a/src/ext/language_tools.js b/src/ext/language_tools.js index 0f6b9941b5e..d5384f366c5 100644 --- a/src/ext/language_tools.js +++ b/src/ext/language_tools.js @@ -221,5 +221,8 @@ require("../config").defineOptions(Editor.prototype, "editor", { } }, value: false + }, + filterStringsCompletions: { + initialValue: false } }); diff --git a/src/ext/options.js b/src/ext/options.js index f82b82dcc5e..75c774c9a44 100644 --- a/src/ext/options.js +++ b/src/ext/options.js @@ -192,6 +192,9 @@ var optionGroups = { "Live Autocompletion": { path: "enableLiveAutocompletion" }, + "Filter Comments and String Completions": { + path: "filterStringsCompletions" + }, "Custom scrollbar": { path: "customScrollbar" }, diff --git a/src/mode/javascript.js b/src/mode/javascript.js index 7db7a892cc9..8f3ba56da7c 100644 --- a/src/mode/javascript.js +++ b/src/mode/javascript.js @@ -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);