1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-09 12:22:33 +01:00

Improve auto-completion heuristics

Related commit:
- 3e72a47c1f
This commit is contained in:
Raymond Hill 2020-06-17 08:12:47 -04:00
parent 001e1ea21e
commit bbf1cb22a0
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2

View File

@ -268,24 +268,28 @@ CodeMirror.defineMode('ubo-static-filtering', function() {
}) })
); );
const getNetOptionHints = function(cursor, isNegated, seedLeft, seedRight) { const pickBestHints = function(cursor, seedLeft, seedRight, hints) {
const assignPos = seedRight.indexOf('=');
if ( assignPos !== -1 ) { seedRight = seedRight.slice(0, assignPos); }
const seed = (seedLeft + seedRight).trim(); const seed = (seedLeft + seedRight).trim();
const isException = parser.isException();
const out = []; const out = [];
for ( let [ name, bits ] of parser.netOptionTokens ) { // First, compare against whole seed
if ( name.startsWith(seed) === false ) { continue; } for ( const hint of hints ) {
if ( isNegated && (bits & parser.OPTCanNegate) === 0 ) { continue; } const text = hint instanceof Object
if ( isException ) { ? hint.displayText || hint.text
if ( (bits & parser.OPTBlockOnly) !== 0 ) { continue; } : hint;
} else { if ( text.startsWith(seed) === false ) { continue; }
if ( (bits & parser.OPTAllowOnly) !== 0 ) { continue; } out.push(hint);
if ( (assignPos === -1) && (bits & parser.OPTMustAssign) !== 0 ) {
name += '=';
} }
// If no match, try again with a different heuristic
if ( out.length === 0 ) {
for ( const hint of hints ) {
const text = hint instanceof Object
? hint.displayText || hint.text
: hint;
if ( seedLeft.length === 1 ) {
if ( text.startsWith(seedLeft) === false ) { continue; }
} else if ( text.includes(seed) === false ) { continue; }
out.push(hint);
} }
out.push(name);
} }
return { return {
from: { line: cursor.line, ch: cursor.ch - seedLeft.length }, from: { line: cursor.line, ch: cursor.ch - seedLeft.length },
@ -294,18 +298,32 @@ CodeMirror.defineMode('ubo-static-filtering', function() {
}; };
}; };
const getNetRedirectHints = function(cursor, seedLeft, seedRight) { const getNetOptionHints = function(cursor, isNegated, seedLeft, seedRight) {
const seed = (seedLeft + seedRight).trim(); const assignPos = seedRight.indexOf('=');
const out = []; if ( assignPos !== -1 ) { seedRight = seedRight.slice(0, assignPos); }
for ( let text of redirectNames.keys() ) { const isException = parser.isException();
if ( text.startsWith(seed) === false ) { continue; } const hints = [];
out.push(text); for ( let [ text, bits ] of parser.netOptionTokens ) {
if ( isNegated && (bits & parser.OPTCanNegate) === 0 ) { continue; }
if ( isException ) {
if ( (bits & parser.OPTBlockOnly) !== 0 ) { continue; }
} else {
if ( (bits & parser.OPTAllowOnly) !== 0 ) { continue; }
if ( (assignPos === -1) && (bits & parser.OPTMustAssign) !== 0 ) {
text += '=';
} }
return { }
from: { line: cursor.line, ch: cursor.ch - seedLeft.length }, hints.push(text);
to: { line: cursor.line, ch: cursor.ch + seedRight.length }, }
list: out, return pickBestHints(cursor, seedLeft, seedRight, hints);
}; };
const getNetRedirectHints = function(cursor, seedLeft, seedRight) {
const hints = [];
for ( const text of redirectNames.keys() ) {
hints.push(text);
}
return pickBestHints(cursor, seedLeft, seedRight, hints);
}; };
const getNetHints = function(cursor, line) { const getNetHints = function(cursor, line) {
@ -338,18 +356,12 @@ CodeMirror.defineMode('ubo-static-filtering', function() {
const matchRight = /^([a-z-]*)\(?/.exec(line.slice(beg)); const matchRight = /^([a-z-]*)\(?/.exec(line.slice(beg));
if ( matchLeft === null || matchRight === null ) { return; } if ( matchLeft === null || matchRight === null ) { return; }
const isStaticDOM = matchLeft[0].indexOf('^') !== -1; const isStaticDOM = matchLeft[0].indexOf('^') !== -1;
const seed = (matchLeft[1] + matchRight[1]).trim(); const hints = [];
const out = [];
for ( let [ text, bits ] of proceduralOperatorNames ) { for ( let [ text, bits ] of proceduralOperatorNames ) {
if ( text.startsWith(seed) === false ) { continue; }
if ( isStaticDOM && (bits & 0b10) !== 0 ) { continue; } if ( isStaticDOM && (bits & 0b10) !== 0 ) { continue; }
out.push(text); hints.push(text);
} }
return { return pickBestHints(cursor, matchLeft[1], matchRight[1], hints);
from: { line: cursor.line, ch: cursor.ch - matchLeft[1].length },
to: { line: cursor.line, ch: cursor.ch + matchRight[1].length },
list: out,
};
}; };
const getExtScriptletHints = function(cursor, line) { const getExtScriptletHints = function(cursor, line) {
@ -357,21 +369,15 @@ CodeMirror.defineMode('ubo-static-filtering', function() {
const matchLeft = /#\+\js\(([^,]*)$/.exec(line.slice(0, beg)); const matchLeft = /#\+\js\(([^,]*)$/.exec(line.slice(0, beg));
const matchRight = /^([^,)]*)/.exec(line.slice(beg)); const matchRight = /^([^,)]*)/.exec(line.slice(beg));
if ( matchLeft === null || matchRight === null ) { return; } if ( matchLeft === null || matchRight === null ) { return; }
const seed = (matchLeft[1] + matchRight[1]).trim(); const hints = [];
const out = [];
for ( const [ text, displayText ] of scriptletNames ) { for ( const [ text, displayText ] of scriptletNames ) {
if ( text.startsWith(seed) === false ) { continue; }
const hint = { text }; const hint = { text };
if ( displayText !== '' ) { if ( displayText !== '' ) {
hint.displayText = displayText; hint.displayText = displayText;
} }
out.push(hint); hints.push(hint);
} }
return { return pickBestHints(cursor, matchLeft[1], matchRight[1], hints);
from: { line: cursor.line, ch: cursor.ch - matchLeft[1].length },
to: { line: cursor.line, ch: cursor.ch + matchRight[1].length },
list: out,
};
}; };
const getHints = function(cm) { const getHints = function(cm) {