-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdemo.js
98 lines (88 loc) · 3.32 KB
/
demo.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
function codemirror_grammar_demo(code, langs)
{
document.getElementById('editor-version').innerHTML = CodeMirror.version;
document.getElementById('grammar-version').innerHTML = CodeMirrorGrammar.VERSION;
if ( langs.mode )
{
var opts = {
mode: langs.mode,
lineNumbers: true,
indentUnit: 4,
indentWithTabs: false,
localVars: {},
gutters: ["CodeMirror-lint-markers", "CodeMirror-linenumbers", "CodeMirror-foldgutter"],
foldGutter: true
};
var editor = CodeMirror.fromTextArea(code, opts);
editor.setSize(null, 500);
return editor;
}
var main_lang, main_mode;
for (var i=0,l=langs.length; i<l; i++)
{
var lang = langs[i].language, grammar = langs[i].grammar, Mode;
// 2. parse the grammar into a Codemirror syntax-highlight mode
Mode = CodeMirrorGrammar.getMode( grammar );
Mode.name = lang;
// 3. register the mode with Codemirror
CodeMirror.defineMode(lang, Mode);
if ( 0 === i )
{
// main mode
main_lang = lang; main_mode = Mode;
// enable syntax validation
main_mode.supportGrammarAnnotations = true;
// enable code folding
main_mode.supportCodeFolding = true;
// enable code matching
main_mode.supportCodeMatching = true;
// enable autocomplete, have a unique cmd to not interfere with any default autocompletes
main_mode.supportAutoCompletion = true;
main_mode.autocompleter.options = {prefixMatch:true, caseInsensitiveMatch:false, inContext:true, dynamic:true};
CodeMirror.registerHelper("lint", main_lang, main_mode.linter);
CodeMirror.registerHelper("fold", main_mode.foldType, main_mode.folder);
CodeMirror.defineOption(main_mode.matchType, false, function( cm, val, old ) {
if ( old && old != CodeMirror.Init )
{
cm.off( "cursorActivity", main_mode.matcher );
main_mode.matcher.clear( cm );
}
if ( val )
{
cm.on( "cursorActivity", main_mode.matcher );
main_mode.matcher( cm );
}
});
}
else
{
// submodes
// add any sub/inner modes to main mode
main_mode.submode(lang, CodeMirror.getMode({}, lang));
}
}
// use it!
var autocomplete_cmd = 'autocomplete_grammar_'+main_lang, togglecomment_cmd = 'togglecomment_grammar_'+main_lang;
CodeMirror.commands[autocomplete_cmd] = function( cm ) {
CodeMirror.showHint(cm, main_mode.autocompleter);
};
/*CodeMirror.commands[togglecomment_cmd] = function( cm ) {
cm.toggleComment( main_mode.options() )
};*/
var opts = {
mode: main_lang,
lineNumbers: true,
indentUnit: 4,
indentWithTabs: false,
extraKeys: {"Ctrl-Space": autocomplete_cmd, "Ctrl-L": "toggleComment"},
gutters: ["CodeMirror-lint-markers", "CodeMirror-linenumbers", "CodeMirror-foldgutter"],
foldGutter: true,
// enable syntax validation
lint: true
};
// enable code matching
opts[main_mode.matchType] = true;
var editor = CodeMirror.fromTextArea(code, opts);
editor.setSize(null, 500);
return editor;
}