Skip to content

Commit b318c21

Browse files
committed
Merge pull request #175 from parallaxinc/highlight-syntax
Syntax check shortcut and highlight syntax errors
2 parents 6dec6b8 + 7d7e720 commit b318c21

File tree

12 files changed

+177
-63
lines changed

12 files changed

+177
-63
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"frylord": "^0.6.0",
1414
"holovisor": "^0.2.0",
1515
"iggins": "^0.2.1",
16-
"irken": "^0.7.0",
16+
"irken": "^0.7.1",
1717
"lodash": "^3.9.1",
1818
"react": "^0.13.1",
1919
"react-loader": "^1.2.0",

plugins/editor/index.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,24 @@ require('codemirror/addon/selection/mark-selection');
99
require('codemirror/lib/codemirror.css');
1010
require('../../assets/theme/parallax.css');
1111

12+
const React = require('react');
1213
const CodeMirror = require('codemirror');
1314
require('./pbasic')(CodeMirror);
1415

1516
const keyExtension = require('./key-extension');
17+
1618
const consoleStore = require('../../src/stores/console');
1719
const editorStore = require('../../src/stores/editor');
20+
const deviceStore = require('../../src/stores/device');
1821
const fileStore = require('../../src/stores/file');
22+
1923
const { handleInput } = require('../../src/actions/editor');
2024
const DocumentsStore = require('../../src/stores/documents');
2125

22-
const React = require('react');
2326
const TransmissionBar = require('./transmission-bar');
2427

28+
const makeToasts = require('../../src/lib/toasts');
29+
2530
function editor(app, opts, done){
2631

2732
var codeEditor;
@@ -37,9 +42,39 @@ function editor(app, opts, done){
3742
}
3843
}
3944

45+
function highlighter(position, length) {
46+
if(!codeEditor){
47+
return;
48+
}
49+
50+
const doc = codeEditor.getDoc();
51+
52+
const anchor = doc.posFromIndex(position);
53+
const head = doc.posFromIndex(position + length);
54+
55+
doc.setSelection(anchor, head, { scroll: false });
56+
57+
const charRect = codeEditor.charCoords(anchor, 'local');
58+
const halfHeight = codeEditor.getScrollerElement().offsetHeight / 2;
59+
const halfTextHeight = Math.floor((charRect.bottom - charRect.top) / 2);
60+
codeEditor.scrollTo(null, charRect.top - halfHeight - halfTextHeight);
61+
}
62+
4063
consoleStore.listen(refreshConsole);
4164

42-
var space = app.workspace;
65+
const space = app.workspace;
66+
const compile = app.compile.bind(app);
67+
// seems strange to pass highlighter to toasts
68+
// maybe this should be named "handlers" or something
69+
const toasts = makeToasts(app.toast, highlighter);
70+
71+
editorStore.toasts = toasts;
72+
editorStore.compile = compile;
73+
editorStore.workspace = space;
74+
75+
// really stinks to attach these in here
76+
fileStore.toasts = toasts;
77+
deviceStore.toasts = toasts;
4378

4479
app.view('editor', function(el, cb){
4580
console.log('editor render');
@@ -67,7 +102,8 @@ function editor(app, opts, done){
67102
'Ctrl-Up': false,
68103
'Ctrl-Down': false,
69104
'Tab': false,
70-
'Shift-Tab': false
105+
'Shift-Tab': false,
106+
'Ctrl-T': false
71107
});
72108
keyExtension.setup(app);
73109
editorStore.cm = codeEditor;

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/overlays/index.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const overlayStore = require('../../src/stores/overlay');
1111
const projectStore = require('../../src/stores/project');
1212

1313
const { confirmDelete, changeProject, deleteProject } = require('../../src/actions/project');
14-
const { handleError, handleSuccess, deleteFile, saveFileAs } = require('../../src/actions/file');
14+
const { deleteFile, saveFileAs } = require('../../src/actions/file');
1515
const { hideSave, hideDelete, hideDownload, showProjects, hideProjects } = require('../../src/actions/overlay');
1616

1717
function overlays(app, opts, done){
@@ -61,10 +61,7 @@ function overlays(app, opts, done){
6161
if(showDownloadOverlay){
6262
component = (
6363
<DownloadOverlay
64-
onCancel={hideDownload}
65-
handleSuccess={handleSuccess}
66-
handleError={handleError}
67-
handleComplete={hideDownload} />
64+
onCancel={hideDownload} />
6865
);
6966
}
7067

plugins/sidebar/index.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,18 @@ const FileOperations = require('./file-operations');
1010
const ProjectOperations = require('./project-operations');
1111

1212
const deviceStore = require('../../src/stores/device');
13-
const editorStore = require('../../src/stores/editor');
1413
const fileStore = require('../../src/stores/file');
1514

1615
const { loadFile } = require('../../src/actions/file');
1716

17+
const makeToasts = require('../../src/lib/toasts');
18+
1819
function sidebar(app, opts, done){
1920

2021
const space = app.workspace;
21-
const toast = app.toast;
22-
const overlay = app.overlay;
2322
const userConfig = app.userConfig;
24-
const irken = app;
25-
const getBoard = app.getBoard.bind(irken);
26-
const scanBoards = app.scanBoards.bind(irken);
23+
const getBoard = app.getBoard.bind(app);
24+
const scanBoards = app.scanBoards.bind(app);
2725

2826
function refreshDirectory(){
2927
// TODO: expose a method to refresh directory without changing it
@@ -58,16 +56,11 @@ function sidebar(app, opts, done){
5856

5957
// Store bindings
6058
deviceStore.workspace = space;
61-
deviceStore.toast = toast;
62-
deviceStore.overlay = overlay;
6359
deviceStore.getBoard = getBoard;
6460
deviceStore.scanBoards = scanBoards;
6561

66-
editorStore.workspace = space;
67-
6862
fileStore.workspace = space;
6963
fileStore.userConfig = userConfig;
70-
fileStore.toast = toast;
7164

7265
done();
7366
}

plugins/sidebar/styles.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@ const styles = {
3131
},
3232
fileHasTemp: {
3333
backgroundColor: red
34-
},
35-
errorToast: {
36-
backgroundColor: red
37-
},
38-
successToast: {
39-
backgroundColor: green
4034
}
4135
};
4236

src/actions/editor.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ class EditorActions {
66
handleInput(inst) {
77
this.dispatch(inst);
88
}
9+
10+
syntaxCheck(){
11+
this.dispatch();
12+
}
913
}
1014

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

src/actions/file.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,6 @@ class FileActions {
3030
saveFileAs(name) {
3131
this.dispatch(name);
3232
}
33-
34-
handleError(err) {
35-
this.dispatch(err);
36-
}
37-
38-
handleSuccess(msg) {
39-
this.dispatch(msg);
40-
}
4133
}
4234

4335
module.exports = alt.createActions(FileActions);

src/lib/toasts.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict';
2+
3+
const red = '#da2100';
4+
const green = '#159600';
5+
6+
const styles = {
7+
errorToast: {
8+
backgroundColor: red
9+
},
10+
successToast: {
11+
backgroundColor: green
12+
}
13+
};
14+
15+
function toasts(api, highlighter){
16+
17+
function success(msg){
18+
api.show(msg, { style: styles.successToast, timeout: 5000 });
19+
}
20+
21+
function error(err){
22+
// leaving this in for better debugging of errors
23+
console.log(err);
24+
25+
api.show(err.message, { style: styles.errorToast });
26+
27+
if(typeof highlighter !== 'function'){
28+
return;
29+
}
30+
31+
if(err && err.errorLength){
32+
highlighter(err.errorPosition, err.errorLength);
33+
}
34+
}
35+
36+
function clear(){
37+
api.clear();
38+
}
39+
40+
return {
41+
success,
42+
error,
43+
clear
44+
};
45+
}
46+
47+
module.exports = toasts;

src/stores/device.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
'use strict';
22

3-
const alt = require('../alt');
43
const _ = require('lodash');
54

5+
const alt = require('../alt');
6+
67
const { rx, tx } = require('../actions/transmission');
78
const { hideDownload, showDownload } = require('../actions/overlay');
89
const { clearOutput, output } = require('../actions/console');
910
const { enableAuto, disableAuto, reloadDevices, updateSelected } = require('../actions/device');
10-
const { handleSuccess, handleError } = require('../actions/file');
1111

1212
class DeviceStore {
1313
constructor() {
@@ -134,7 +134,7 @@ class DeviceStore {
134134
this.setState({ progress: progress });
135135
}
136136

137-
const { workspace, toast, getBoard } = this.getInstance();
137+
const { workspace, getBoard } = this.getInstance();
138138
const { selectedDevice } = this.state;
139139

140140
const name = workspace.filename.deref();
@@ -155,16 +155,34 @@ class DeviceStore {
155155
.tap(() => clearOutput())
156156
.then(() => board.on('terminal', output))
157157
.then(() => board.on('terminal', rx))
158-
.tap(() => toast.clear())
159-
.tap(() => handleSuccess(`'${name}' downloaded successfully`))
160-
.catch(handleError)
158+
.tap(() => this._handleClear())
159+
.tap(() => this._handleSuccess(`'${name}' downloaded successfully`))
160+
.catch((err) => this._handleError(err))
161161
.finally(() => {
162162
board.removeListener('progress', updateProgress);
163163
this.setState({ progress: 0 });
164164
hideDownload();
165165
});
166166
}
167167

168+
_handleClear(){
169+
const { toasts } = this.getInstance();
170+
171+
toasts.clear();
172+
}
173+
174+
_handleError(err){
175+
const { toasts } = this.getInstance();
176+
177+
toasts.error(err);
178+
}
179+
180+
_handleSuccess(msg){
181+
const { toasts } = this.getInstance();
182+
183+
toasts.success(msg);
184+
}
185+
168186
}
169187

170188
DeviceStore.config = {

0 commit comments

Comments
 (0)