Skip to content

Commit

Permalink
Improve auto-complete of hostname values in "My filters"
Browse files Browse the repository at this point in the history
Related issue:
- uBlockOrigin/uBlock-issues#1134

Related commit:
- daf464b
  • Loading branch information
gorhill committed Dec 2, 2020
1 parent cb7ec8a commit 262a1a0
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 35 deletions.
45 changes: 33 additions & 12 deletions src/js/1p-filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,43 @@ const cmEditor = new CodeMirror(document.getElementById('userFilters'), {

uBlockDashboard.patchCodeMirrorEditor(cmEditor);

vAPI.messaging.send('dashboard', {
what: 'getAutoCompleteDetails'
}).then(response => {
if ( response instanceof Object === false ) { return; }
const mode = cmEditor.getMode();
// TODO: listen to changes in currently opened set of tabs?
if ( mode.setHints instanceof Function ) {
mode.setHints(response);
}
mode.parser.expertMode = response.expertMode !== false;
});

let cachedUserFilters = '';

/******************************************************************************/

// Add auto-complete ability to the editor.

{
let hintUpdateToken = 0;

const responseHandler = function(response) {
if ( response instanceof Object === false ) { return; }
if ( response.hintUpdateToken !== undefined ) {
const firstVisit = hintUpdateToken === 0;
const mode = cmEditor.getMode();
if ( mode.setHints instanceof Function ) {
mode.setHints(response, firstVisit);
}
if ( firstVisit ) {
mode.parser.expertMode = response.expertMode !== false;
}
hintUpdateToken = response.hintUpdateToken;
}
vAPI.setTimeout(getHints, 2503);
};

const getHints = function() {
vAPI.messaging.send('dashboard', {
what: 'getAutoCompleteDetails',
hintUpdateToken
}).then(responseHandler);
};

getHints();
}

/******************************************************************************/

const getEditorText = function() {
const text = cmEditor.getValue().replace(/\s+$/, '');
return text === '' ? text : text + '\n';
Expand Down
43 changes: 27 additions & 16 deletions src/js/codemirror/ubo-static-filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,26 +361,37 @@ CodeMirror.defineMode('ubo-static-filtering', function() {
style = style.trim();
return style !== '' ? style : null;
},
setHints: function(details) {
for ( const [ name, desc ] of details.redirectResources ) {
const displayText = desc.aliasOf !== ''
? `${name} (${desc.aliasOf})`
: '';
if ( desc.canRedirect ) {
redirectNames.set(name, displayText);
setHints: function(details, firstVisit = false) {
if ( Array.isArray(details.redirectResources) ) {
for ( const [ name, desc ] of details.redirectResources ) {
const displayText = desc.aliasOf !== ''
? `${name} (${desc.aliasOf})`
: '';
if ( desc.canRedirect ) {
redirectNames.set(name, displayText);
}
if ( desc.canInject && name.endsWith('.js') ) {
scriptletNames.set(name.slice(0, -3), displayText);
}
}
if ( desc.canInject && name.endsWith('.js') ) {
scriptletNames.set(name.slice(0, -3), displayText);
}
if ( Array.isArray(details.preparseDirectiveTokens)) {
details.preparseDirectiveTokens.forEach(([ a, b ]) => {
preparseDirectiveTokens.set(a, b);
});
}
if ( Array.isArray(details.preparseDirectiveHints)) {
preparseDirectiveHints.push(...details.preparseDirectiveHints);
}
if ( Array.isArray(details.originHints) ) {
originHints.length = 0;
for ( const hint of details.originHints ) {
originHints.push(hint);
}
}
details.preparseDirectiveTokens.forEach(([ a, b ]) => {
preparseDirectiveTokens.set(a, b);
});
preparseDirectiveHints.push(...details.preparseDirectiveHints);
for ( const hint of details.originHints ) {
originHints.push(hint);
if ( firstVisit ) {
initHints();
}
initHints();
},
get parser() {
return parser;
Expand Down
18 changes: 11 additions & 7 deletions src/js/messaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -1201,13 +1201,17 @@ const onMessage = function(request, sender, callback) {
break;

case 'getAutoCompleteDetails':
response = {
redirectResources: µb.redirectEngine.getResourceDetails(),
preparseDirectiveTokens: µb.preparseDirectives.getTokens(),
preparseDirectiveHints: µb.preparseDirectives.getHints(),
originHints: getOriginHints(),
expertMode: µb.hiddenSettings.filterAuthorMode,
};
response = {};
if ( request.hintUpdateToken === 0 ) {
response.redirectResources = µb.redirectEngine.getResourceDetails();
response.preparseDirectiveTokens = µb.preparseDirectives.getTokens();
response.preparseDirectiveHints = µb.preparseDirectives.getHints();
response.expertMode = µb.hiddenSettings.filterAuthorMode;
}
if ( request.hintUpdateToken !== µb.pageStoresToken ) {
response.originHints = getOriginHints();
response.hintUpdateToken = µb.pageStoresToken;
}
break;

case 'getRules':
Expand Down

0 comments on commit 262a1a0

Please sign in to comment.