From ebf7fb145e6dd1e909cb1c89f95c8be103557b7d Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Thu, 9 Jul 2020 08:09:51 -0400 Subject: [PATCH] Fine tune auto-completion for `!#if` directives Auto-completion will work only for uBO's own tokens, compatibility-related tokens[1] will not be taken into account for auto-completion. The reason is to not have the compatibility-related tokens get in the way of auto-completion in order to not inconvenience uBO's filter list maintainers. [1] `adguard_ext_chromium`, `adguard_ext_firefox`, etc. --- src/js/codemirror/ubo-static-filtering.js | 23 +++++++++++++---------- src/js/messaging.js | 3 ++- src/js/storage.js | 15 +++++++++++++++ 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/js/codemirror/ubo-static-filtering.js b/src/js/codemirror/ubo-static-filtering.js index 2d21c0176..bbccf41a7 100644 --- a/src/js/codemirror/ubo-static-filtering.js +++ b/src/js/codemirror/ubo-static-filtering.js @@ -32,7 +32,8 @@ const redirectNames = new Map(); const scriptletNames = new Map(); -const preparseDirectiveNames = new Set(); +const preparseDirectiveTokens = new Set(); +const preparseDirectiveHints = []; /******************************************************************************/ @@ -64,8 +65,8 @@ CodeMirror.defineMode('ubo-static-filtering', function() { } stream.skipToEnd(); if ( - preparseDirectiveNames.size === 0 || - preparseDirectiveNames.has(match[2].trim()) + preparseDirectiveTokens.size === 0 || + preparseDirectiveTokens.has(match[2].trim()) ) { return 'variable strong'; } @@ -288,9 +289,10 @@ CodeMirror.defineMode('ubo-static-filtering', function() { scriptletNames.set(name.slice(0, -3), displayText); } } - details.preparseDirectives.forEach(a => { - preparseDirectiveNames.add(a); + details.preparseDirectiveTokens.forEach(a => { + preparseDirectiveTokens.add(a); }); + preparseDirectiveHints.push(...details.preparseDirectiveHints); initHints(); }, }; @@ -432,11 +434,12 @@ const initHints = function() { const matchLeft = /^!#if !?(\w*)$/.exec(line.slice(0, beg)); const matchRight = /^\w*/.exec(line.slice(beg)); if ( matchLeft === null || matchRight === null ) { return; } - const hints = []; - for ( const hint of preparseDirectiveNames ) { - hints.push(hint); - } - return pickBestHints(cursor, matchLeft[1], matchRight[0], hints); + return pickBestHints( + cursor, + matchLeft[1], + matchRight[0], + preparseDirectiveHints + ); } if ( line.startsWith('!#') && line !== '!#endif' ) { const matchLeft = /^!#(\w*)$/.exec(line.slice(0, beg)); diff --git a/src/js/messaging.js b/src/js/messaging.js index a814a3727..d0e18a1f2 100644 --- a/src/js/messaging.js +++ b/src/js/messaging.js @@ -1154,7 +1154,8 @@ const onMessage = function(request, sender, callback) { case 'getAutoCompleteDetails': response = { redirectResources: µb.redirectEngine.getResourceDetails(), - preparseDirectives: Array.from(µb.preparseDirectives.tokens.keys()), + preparseDirectiveTokens: µb.preparseDirectives.getTokens(), + preparseDirectiveHints: µb.preparseDirectives.getHints(), }; break; diff --git a/src/js/storage.js b/src/js/storage.js index 3ac401e3a..750079194 100644 --- a/src/js/storage.js +++ b/src/js/storage.js @@ -918,6 +918,21 @@ self.addEventListener('hiddenSettingsChanged', ( ) => { return out.join('\n'); }, + getHints: function() { + const out = []; + const vals = new Set(); + for ( const [ key, val ] of this.tokens ) { + if ( vals.has(val) ) { continue; } + vals.add(val); + out.push(key); + } + return out; + }, + + getTokens: function() { + return Array.from(this.tokens.keys()); + }, + tokens: new Map([ [ 'ext_ublock', 'ublock' ], [ 'env_chromium', 'chromium' ],