Skip to content

Commit 487296c

Browse files
committed
Implement syntax highlighting on compiler error
1 parent 6dec6b8 commit 487296c

File tree

5 files changed

+72
-2
lines changed

5 files changed

+72
-2
lines changed

plugins/editor/key-extension.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const { findNext, findPrevious, replace } = require('../../src/actions/find');
66
const { moveByScrollUpLine, moveByScrollDownLine } = require('../../src/actions/editor-move');
77
const { dedent, indent } = require('../../src/actions/text-move');
88
const { print } = require('../../src/actions/system');
9+
const { syntaxCheck } = require('../../src/actions/editor');
910
const { newFile, saveFile } = require('../../src/actions/file');
1011
const { hideOverlays, showSave, showDownload, showProjects } = require('../../src/actions/overlay');
1112
const { disableAuto, enableAuto } = require('../../src/actions/device');
@@ -120,6 +121,13 @@ const keyExtension = {
120121
evt.preventDefault();
121122
showProjects();
122123
}
124+
},
125+
syntaxCheck: {
126+
code: ['CTRL_T', 'F7'],
127+
exec(evt){
128+
evt.preventDefault();
129+
syntaxCheck();
130+
}
123131
}
124132
};
125133

plugins/sidebar/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ function sidebar(app, opts, done){
2222
const overlay = app.overlay;
2323
const userConfig = app.userConfig;
2424
const irken = app;
25+
const compile = app.compile.bind(irken);
2526
const getBoard = app.getBoard.bind(irken);
2627
const scanBoards = app.scanBoards.bind(irken);
2728

@@ -63,6 +64,8 @@ function sidebar(app, opts, done){
6364
deviceStore.getBoard = getBoard;
6465
deviceStore.scanBoards = scanBoards;
6566

67+
editorStore.toast = toast;
68+
editorStore.compile = compile;
6669
editorStore.workspace = space;
6770

6871
fileStore.workspace = space;

src/actions/editor.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ class EditorActions {
66
handleInput(inst) {
77
this.dispatch(inst);
88
}
9+
highlight(position, len) {
10+
this.dispatch({
11+
position: position,
12+
length: len
13+
});
14+
}
15+
syntaxCheck(){
16+
this.dispatch();
17+
}
918
}
1019

1120
module.exports = alt.createActions(EditorActions);

src/stores/editor.js

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
'use strict';
22

33
const alt = require('../alt');
4+
const styles = require('../../plugins/sidebar/styles');
45

56
const { findNext, findPrevious, replace } = require('../actions/find');
6-
const { handleInput } = require('../actions/editor');
7+
const { handleError, handleSuccess } = require('../actions/file');
8+
const { handleInput, highlight, syntaxCheck } = require('../actions/editor');
79
const { moveByScrollUpLine, moveByScrollDownLine } = require('../actions/editor-move');
810
const { dedent, indent } = require('../actions/text-move');
911
const { print } = require('../actions/system');
@@ -17,10 +19,12 @@ class EditorStore {
1719
onFindPrevious: findPrevious,
1820
onHandleInput: handleInput,
1921
onIndent: indent,
22+
onHighlight: highlight,
2023
onMoveByScrollUpLine: moveByScrollUpLine,
2124
onMoveByScrollDownLine: moveByScrollDownLine,
2225
onPrint: print,
23-
onReplace: replace
26+
onReplace: replace,
27+
onSyntaxCheck: syntaxCheck
2428
});
2529

2630
}
@@ -77,7 +81,48 @@ class EditorStore {
7781

7882
cm.execCommand('replace');
7983
}
84+
onSyntaxCheck() {
85+
const { workspace, compile } = this.getInstance();
86+
const result = compile({
87+
type: 'bs2',
88+
source: workspace.current.deref()
89+
});
90+
if(result.error){
91+
this.handleError(result.error);
92+
}else{
93+
this.handleSuccess('Tokenization successful!');
94+
}
95+
}
96+
onHighlight(opts) {
97+
const { cm } = this.getInstance();
98+
const doc = cm.getDoc();
99+
100+
const anchor = doc.posFromIndex(opts.position);
101+
const head = doc.posFromIndex(opts.position + opts.length);
102+
103+
console.log('onHighlight', opts, anchor, head);
104+
doc.setSelection(anchor, head);
105+
}
80106

107+
108+
//duplicated from file store due to dispatch->dispatch invariant
109+
handleError(err){
110+
// leaving this in for better debugging of errors
111+
console.log(err);
112+
const { toast } = this.getInstance();
113+
114+
toast.show(err.message, { style: styles.errorToast });
115+
if(err && err.errorLength){
116+
this.onHighlight({ position: err.errorPosition, length: err.errorLength });
117+
}
118+
}
119+
120+
//duplicated from file store due to dispatch->dispatch invariant
121+
handleSuccess(msg){
122+
const { toast } = this.getInstance();
123+
124+
toast.show(msg, { style: styles.successToast, timeout: 5000 });
125+
}
81126
}
82127

83128
EditorStore.config = {

src/stores/file.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const alt = require('../alt');
88
const styles = require('../../plugins/sidebar/styles');
99

1010
const { hideOverlays, hideSave } = require('../actions/overlay');
11+
const { highlight } = require('../actions/editor.js');
12+
1113
const {
1214
clearName,
1315
updateName,
@@ -207,6 +209,9 @@ class FileStore {
207209
const { toast } = this.getInstance();
208210

209211
toast.show(err.message, { style: styles.errorToast });
212+
if(err && err.errorLength){
213+
highlight(err.errorPosition, err.errorLength);
214+
}
210215
}
211216

212217
onHandleSuccess(msg){

0 commit comments

Comments
 (0)