-
Notifications
You must be signed in to change notification settings - Fork 68
/
scope.js
76 lines (63 loc) · 1.95 KB
/
scope.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*global CodeMirror */
CodeMirror.defineMode('scope', function (config, parserConfig) {
'use strict';
var levels = [], fullyParsed;
//request levels from parserConfig
function getLevels() {
//this gets called a lot so check first
if (parserConfig.hasChanged()) {
levels = parserConfig.getLevels();
}
}
function parseLine(stream, state) {
var level;
//get the latest levels
getLevels();
//check for valid levels data
if (state.index < levels.length) {
//check stream for 'start of line'
if (stream.sol()) {
state.line += 1;
}
//get the level information
level = levels[state.index];
//check if we need to skip the line
if (state.line !== level.line) {
//do nothing
stream.skipToEnd();
return null;
}
//check if we need to skip ahead within the line
if ((level.from - 1) > stream.pos) {
stream.pos += (level.from - stream.pos - 1);
return null;
}
//move stream to scope change
stream.pos += (level.thru - level.from);
//step forward in the list
state.index += 1;
//return the CSS class to apply
return 'level' + level.level;
}
//do nothing
stream.skipToEnd();
return null;
}
return {
startState: function () {
fullyParsed = false;
return {
//source code line
line: 0,
//levels array index
index: 0
};
},
token: function (stream, state) {
return parseLine(stream, state);
},
blankLine: function(state) {
return parseLine(new CodeMirror.StringStream(''), state);
}
};
});