Add: draw line pointer for parsing error

This commit is contained in:
eight 2017-11-09 15:55:06 +08:00
parent 79a7a218c4
commit a1c1eb328b

View File

@ -2,7 +2,7 @@
/* global showToggleStyleHelp goBackToManage updateLintReportIfEnabled */ /* global showToggleStyleHelp goBackToManage updateLintReportIfEnabled */
/* global hotkeyRerouter setupAutocomplete setupOptionsExpand */ /* global hotkeyRerouter setupAutocomplete setupOptionsExpand */
/* global editors linterConfig updateLinter regExpTester mozParser */ /* global editors linterConfig updateLinter regExpTester mozParser */
/* global makeLink createAppliesToLineWidget */ /* global makeLink createAppliesToLineWidget messageBox */
'use strict'; 'use strict';
function createSourceEditor(style) { function createSourceEditor(style) {
@ -243,9 +243,30 @@ ${section}
hadBeenSaved = true; hadBeenSaved = true;
}) })
.catch(err => { .catch(err => {
const contents = [String(err)];
if (Number.isInteger(err.index)) {
const pos = cm.posFromIndex(err.index);
contents[0] += ` (line ${pos.line + 1} col ${pos.ch + 1})`;
contents.push($element({
tag: 'pre',
textContent: drawLinePointer(pos)
}));
}
console.error(err); console.error(err);
alert(err); messageBox.alert(contents);
}); });
function drawLinePointer(pos) {
const SIZE = 60;
const line = cm.getLine(pos.line);
const pointer = ' '.repeat(pos.ch) + '^';
const start = Math.max(Math.min(pos.ch - SIZE / 2, line.length - SIZE), 0);
const end = Math.min(Math.max(pos.ch + SIZE / 2, SIZE), line.length);
const leftPad = start !== 0 ? '...' : '';
const rightPad = end !== line.length ? '...' : '';
return leftPad + line.slice(start, end) + rightPad + '\n' +
' '.repeat(leftPad.length) + pointer.slice(start, end);
}
} }
function isTouched() { function isTouched() {