forked from daniellmb/JavaScript-Scope-Context-Coloring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscope.js
More file actions
73 lines (60 loc) · 1.92 KB
/
Copy pathscope.js
File metadata and controls
73 lines (60 loc) · 1.92 KB
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
/*global CodeMirror */
CodeMirror.defineMode('scope', function (config, parserConfig) {
'use strict';
var levels = [], fullyParsed;
//lint the code to get the scope information
function getLevels() {
//this gets called a lot so check first
if (parserConfig.hasChanged()) {
levels = parserConfig.getLevels();
}
}
//get the level information
getLevels();
return {
startState: function () {
fullyParsed = false;
return {
line: 0,
index: 0
};
},
token: function (stream, state) {
var level, next;
//get the latest levels
getLevels();
//check for valid levels data
if (state.index < levels.length) {
//check stream for 'start of line'
if (stream.sol()) {
if (!fullyParsed) {
state.line += 1;
}
}
//get the level information
level = levels[state.index];
//check if we need to skip ahead
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);
//check if this is the last line
if (state.index == levels.length) {
fullyParsed = true;
}
else
{
//step forward in the list
state.index += 1;
}
//return the CSS class to apply
return 'level' + level.level;
}
//handle race conditions
stream.skipToEnd();
return null;
}
};
});