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
Latest commit
76 lines (63 loc) · 1.95 KB
/
Copy pathscope.js
File metadata and controls
76 lines (63 loc) · 1.95 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
74
75
76
/*global CodeMirror */
CodeMirror.defineMode('scope',function(config,parserConfig){
'use strict';
varlevels=[],fullyParsed;
//request levels from parserConfig
functiongetLevels(){
//this gets called a lot so check first
if(parserConfig.hasChanged()){
levels=parserConfig.getLevels();
}
}
functionparseLine(stream,state){
varlevel;
//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();
returnnull;
}
//check if we need to skip ahead within the line
if((level.from-1)>stream.pos){
stream.pos+=(level.from-stream.pos-1);
returnnull;
}
//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();
returnnull;
}
return{
startState: function(){
fullyParsed=false;
return{
//source code line
line: 0,
//levels array index
index: 0
};
},
token: function(stream,state){
returnparseLine(stream,state);
},
blankLine: function(state){
returnparseLine(newCodeMirror.StringStream(''),state);
}
};
});