From a1c1eb328ba7cd984f8f8dd91c33a89bd8b5a5df Mon Sep 17 00:00:00 2001 From: eight Date: Thu, 9 Nov 2017 15:55:06 +0800 Subject: [PATCH] Add: draw line pointer for parsing error --- edit/source-editor.js | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/edit/source-editor.js b/edit/source-editor.js index 970af308..922b301d 100644 --- a/edit/source-editor.js +++ b/edit/source-editor.js @@ -2,7 +2,7 @@ /* global showToggleStyleHelp goBackToManage updateLintReportIfEnabled */ /* global hotkeyRerouter setupAutocomplete setupOptionsExpand */ /* global editors linterConfig updateLinter regExpTester mozParser */ -/* global makeLink createAppliesToLineWidget */ +/* global makeLink createAppliesToLineWidget messageBox */ 'use strict'; function createSourceEditor(style) { @@ -243,9 +243,30 @@ ${section} hadBeenSaved = true; }) .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); - 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() {