1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-10-06 09:37:12 +02:00

Code review of new linter code

As per CodeMirror's documentation, eachLine() iterator is
faster, so use this. Also no need to keep track or marked
lines, we can just find them on demand, this makes the code
simpler.
This commit is contained in:
Raymond Hill 2023-04-02 09:19:32 -04:00
parent dd8031a508
commit 46e90b21e9
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
2 changed files with 56 additions and 48 deletions

View File

@ -144,7 +144,6 @@ import { i18n$ } from '../i18n.js';
this.queryTimer = null; this.queryTimer = null;
this.dirty = true; this.dirty = true;
this.lines = []; this.lines = [];
this.errorLines = [];
cm.on('changes', (cm, changes) => { cm.on('changes', (cm, changes) => {
for ( const change of changes ) { for ( const change of changes ) {
if ( change.text.length !== 0 || change.removed !== 0 ) { if ( change.text.length !== 0 || change.removed !== 0 ) {
@ -374,23 +373,28 @@ import { i18n$ } from '../i18n.js';
}; };
const findNextError = function(cm, dir) { const findNextError = function(cm, dir) {
const state = getSearchState(cm); const doc = cm.getDoc();
const lines = state.errorLines;
if ( lines.length === 0 ) { return; }
const cursor = cm.getCursor('from'); const cursor = cm.getCursor('from');
const start = cursor.line; const cursorLine = cursor.line;
const next = lines.reduce((best, v) => { const start = dir < 0 ? 0 : cursorLine + 1;
const end = dir < 0 ? cursorLine : doc.lineCount();
let found = -1;
doc.eachLine(start, end, lineHandle => {
const markers = lineHandle.gutterMarkers || null;
if ( markers === null ) { return; }
if ( markers['CodeMirror-lintgutter'] === undefined ) { return; }
const line = lineHandle.lineNo();
if ( dir < 0 ) { if ( dir < 0 ) {
if ( v < start && (best === -1 || v > best) ) { return v; } found = line;
return best; return;
} }
if ( v > start && (best === -1 || v < best) ) { return v; } found = line;
return best; return true;
}, -1); });
if ( next === -1 || next === start ) { return; } if ( found === -1 || found === cursorLine ) { return; }
cm.getDoc().setCursor(next); cm.getDoc().setCursor(found);
const { clientHeight } = cm.getScrollInfo(); const { clientHeight } = cm.getScrollInfo();
cm.scrollIntoView({ line: next, ch: 0 }, clientHeight >>> 1); cm.scrollIntoView({ line: found, ch: 0 }, clientHeight >>> 1);
}; };
const clearSearch = function(cm, hard) { const clearSearch = function(cm, hard) {
@ -493,8 +497,7 @@ import { i18n$ } from '../i18n.js';
CodeMirror.defineInitHook(function(cm) { CodeMirror.defineInitHook(function(cm) {
getSearchState(cm); getSearchState(cm);
cm.on('linterDone', details => { cm.on('linterDone', details => {
const count = details.lines.length; const count = details.errorCount;
getSearchState(cm).errorLines = details.lines;
dom.cl.toggle('.cm-linter-widget', 'hasErrors', count !== 0); dom.cl.toggle('.cm-linter-widget', 'hasErrors', count !== 0);
dom.text( dom.text(
'.cm-linter-widget .cm-linter-widget-count', '.cm-linter-widget .cm-linter-widget-count',

View File

@ -671,7 +671,7 @@ CodeMirror.registerHelper('fold', 'ubo-static-filtering', (( ) => {
nativeCssHas: vAPI.webextFlavor.env.includes('native_css_has'), nativeCssHas: vAPI.webextFlavor.env.includes('native_css_has'),
}); });
const markedset = []; let errorCount = 0;
let markedsetStart = 0; let markedsetStart = 0;
let markedsetTimer; let markedsetTimer;
@ -689,18 +689,14 @@ CodeMirror.registerHelper('fold', 'ubo-static-filtering', (( ) => {
const line = markedsetStart++; const line = markedsetStart++;
const markers = lineHandle.gutterMarkers || null; const markers = lineHandle.gutterMarkers || null;
if ( markers && markers['CodeMirror-lintgutter'] ) { if ( markers && markers['CodeMirror-lintgutter'] ) {
markedset.push(lineHandle.lineNo()); errorCount += 1;
} }
if ( (line & 0x0F) === 0 && deadline.timeRemaining() === 0 ) { if ( (line & 0x0F) === 0 && deadline.timeRemaining() === 0 ) {
processMarkedsetAsync(doc); processMarkedsetAsync(doc);
return true; return true;
} }
if ( markedsetStart === lineCount ) { if ( markedsetStart === lineCount ) {
CodeMirror.signal( CodeMirror.signal(doc.getEditor(), 'linterDone', { errorCount });
doc.getEditor(),
'linterDone',
{ lines: markedset }
);
} }
}); });
}; };
@ -708,7 +704,7 @@ CodeMirror.registerHelper('fold', 'ubo-static-filtering', (( ) => {
const changeset = []; const changeset = [];
let changesetTimer; let changesetTimer;
const addChanges = (doc, change) => { const addChange = (doc, change) => {
changeset.push(change); changeset.push(change);
processChangesetAsync(doc); processChangesetAsync(doc);
}; };
@ -736,9 +732,10 @@ CodeMirror.registerHelper('fold', 'ubo-static-filtering', (( ) => {
return error; return error;
}; };
const extractMarker = lineInfo => { const extractMarker = lineHandle => {
if ( lineInfo.gutterMarkers instanceof Object === false ) { return; } const markers = lineHandle.gutterMarkers || null;
return lineInfo.gutterMarkers['CodeMirror-lintgutter']; if ( markers === null ) { return; }
return markers['CodeMirror-lintgutter'] || undefined;
}; };
const markerTemplate = (( ) => { const markerTemplate = (( ) => {
@ -750,41 +747,49 @@ CodeMirror.registerHelper('fold', 'ubo-static-filtering', (( ) => {
return marker; return marker;
})(); })();
const makeMarker = (doc, line, marker, error) => { const makeMarker = (doc, lineHandle, marker, error) => {
if ( marker === undefined ) { if ( marker === undefined ) {
marker = markerTemplate.cloneNode(true); marker = markerTemplate.cloneNode(true);
doc.setGutterMarker(line, 'CodeMirror-lintgutter', marker); doc.setGutterMarker(lineHandle, 'CodeMirror-lintgutter', marker);
} }
marker.children[0].textContent = error; marker.children[0].textContent = error;
}; };
const processChange = (doc, deadline, change) => {
let { from, to } = change;
doc.eachLine(from, to, lineHandle => {
astParser.parse(lineHandle.text);
const error = extractError();
const marker = extractMarker(lineHandle);
if ( error === undefined && marker ) {
doc.setGutterMarker(lineHandle, 'CodeMirror-lintgutter', null);
} else if ( error !== undefined ) {
makeMarker(doc, lineHandle, marker, error);
}
from += 1;
if ( (from & 0x0F) !== 0 ) { return; }
if ( deadline.timeRemaining() !== 0 ) { return; }
return true;
});
if ( from !== to ) {
return { from, to };
}
};
const processChangeset = (doc, deadline) => { const processChangeset = (doc, deadline) => {
const cm = doc.getEditor(); const cm = doc.getEditor();
cm.startOperation(); cm.startOperation();
while ( changeset.length !== 0 ) { while ( changeset.length !== 0 ) {
const { from, to } = changeset.shift(); const change = processChange(doc, deadline, changeset.shift());
for ( let line = from; line < to; line++ ) { if ( change === undefined ) { continue; }
const lineInfo = doc.lineInfo(line); changeset.unshift(change);
if ( lineInfo === null ) { continue; } break;
astParser.parse(lineInfo.text);
const error = extractError();
let marker = extractMarker(lineInfo);
if ( error === undefined && marker ) {
doc.setGutterMarker(line, 'CodeMirror-lintgutter', null);
} else if ( error !== undefined ) {
makeMarker(doc, line, marker, error);
}
if ( (line & 0x0F) === 0 && deadline.timeRemaining() === 0 ) {
changeset.unshift({ doc, from: line+1, to });
break;
}
}
} }
cm.endOperation(); cm.endOperation();
if ( changeset.length !== 0 ) { if ( changeset.length !== 0 ) {
return processChangesetAsync(doc); return processChangesetAsync(doc);
} }
markedset.length = 0; errorCount = 0;
markedsetStart = 0; markedsetStart = 0;
processMarkedsetAsync(doc); processMarkedsetAsync(doc);
}; };
@ -795,7 +800,7 @@ CodeMirror.registerHelper('fold', 'ubo-static-filtering', (( ) => {
for ( const change of changes ) { for ( const change of changes ) {
const from = change.from.line; const from = change.from.line;
const to = from + change.text.length; const to = from + change.text.length;
addChanges(doc, { from, to }); addChange(doc, { from, to });
} }
}); });
}); });