Skip to content

Commit

Permalink
Improve auto-completion in _"My filters"_ editor
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed Dec 19, 2021
1 parent a86e804 commit e4a7df3
Showing 1 changed file with 82 additions and 16 deletions.
98 changes: 82 additions & 16 deletions src/js/codemirror/ubo-static-filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,23 +443,89 @@ const initHints = function() {
if ( text.startsWith(seed) === false ) { continue; }
out.push(hint);
}
// 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);
}
if ( out.length !== 0 ) {
return {
from: { line: cursor.line, ch: cursor.ch - seedLeft.length },
to: { line: cursor.line, ch: cursor.ch + seedRight.length },
list: out,
};
}
// If no match, try again with a different heuristic: valid hints are
// those matching left seed, not matching right seed but right seed is
// found to be a valid hint. This is to take care of cases like:
//
// *$script,redomain=example.org
// ^
// + cursor position
//
// In such case, [ redirect=, redirect-rule= ] should be returned
// as valid hints.
for ( const hint of hints ) {
const text = hint instanceof Object
? hint.displayText || hint.text
: hint;
if ( seedLeft.length === 0 ) { continue; }
if ( text.startsWith(seedLeft) === false ) { continue; }
if ( hints.includes(seedRight) === false ) { continue; }
out.push(hint);
}
if ( out.length !== 0 ) {
return {
from: { line: cursor.line, ch: cursor.ch - seedLeft.length },
to: { line: cursor.line, ch: cursor.ch },
list: out,
};
}
// If no match, try again with a different heuristic: valid hints are
// those containing seed as a substring. This is to take care of cases
// like:
//
// *$script,redirect=gif
// ^
// + cursor position
//
// In such case, [ 1x1.gif, 1x1-transparent.gif ] should be returned
// as valid hints.
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);
}
if ( out.length !== 0 ) {
return {
from: { line: cursor.line, ch: cursor.ch - seedLeft.length },
to: { line: cursor.line, ch: cursor.ch + seedRight.length },
list: out,
};
}
// If still no match, try again with a different heuristic: valid hints
// are those containing left seed as a substring. This is to take care
// of cases like:
//
// *$script,redirect=gifdomain=example.org
// ^
// + cursor position
//
// In such case, [ 1x1.gif, 1x1-transparent.gif ] should be returned
// as valid hints.
for ( const hint of hints ) {
const text = hint instanceof Object
? hint.displayText || hint.text
: hint;
if ( text.includes(seedLeft) === false ) { continue; }
out.push(hint);
}
if ( out.length !== 0 ) {
return {
from: { line: cursor.line, ch: cursor.ch - seedLeft.length },
to: { line: cursor.line, ch: cursor.ch },
list: out,
};
}
return {
from: { line: cursor.line, ch: cursor.ch - seedLeft.length },
to: { line: cursor.line, ch: cursor.ch + seedRight.length },
list: out,
};
};

const getOriginHints = function(cursor, line, suffix = '') {
Expand Down

0 comments on commit e4a7df3

Please sign in to comment.