]>
Commit | Line | Data |
---|---|---|
06a3d686 BB |
1 | (function() { |
2 | ||
3 | CodeMirror.extendMode("css", { | |
4 | commentStart: "/*", | |
5 | commentEnd: "*/", | |
6 | newlineAfterToken: function(_type, content) { | |
7 | return /^[;{}]$/.test(content); | |
8 | } | |
9 | }); | |
10 | ||
11 | CodeMirror.extendMode("javascript", { | |
12 | commentStart: "/*", | |
13 | commentEnd: "*/", | |
14 | // FIXME semicolons inside of for | |
15 | newlineAfterToken: function(_type, content, textAfter, state) { | |
16 | if (this.jsonMode) { | |
17 | return /^[\[,{]$/.test(content) || /^}/.test(textAfter); | |
18 | } else { | |
19 | if (content == ";" && state.lexical && state.lexical.type == ")") return false; | |
20 | return /^[;{}]$/.test(content) && !/^;/.test(textAfter); | |
21 | } | |
22 | } | |
23 | }); | |
24 | ||
25 | CodeMirror.extendMode("xml", { | |
26 | commentStart: "<!--", | |
27 | commentEnd: "-->", | |
28 | newlineAfterToken: function(type, content, textAfter) { | |
29 | return type == "tag" && />$/.test(content) || /^</.test(textAfter); | |
30 | } | |
31 | }); | |
32 | ||
33 | // Comment/uncomment the specified range | |
34 | CodeMirror.defineExtension("commentRange", function (isComment, from, to) { | |
35 | var cm = this, curMode = CodeMirror.innerMode(cm.getMode(), cm.getTokenAt(from).state).mode; | |
36 | cm.operation(function() { | |
37 | if (isComment) { // Comment range | |
38 | cm.replaceRange(curMode.commentEnd, to); | |
39 | cm.replaceRange(curMode.commentStart, from); | |
40 | if (from.line == to.line && from.ch == to.ch) // An empty comment inserted - put cursor inside | |
41 | cm.setCursor(from.line, from.ch + curMode.commentStart.length); | |
42 | } else { // Uncomment range | |
43 | var selText = cm.getRange(from, to); | |
44 | var startIndex = selText.indexOf(curMode.commentStart); | |
45 | var endIndex = selText.lastIndexOf(curMode.commentEnd); | |
46 | if (startIndex > -1 && endIndex > -1 && endIndex > startIndex) { | |
47 | // Take string till comment start | |
48 | selText = selText.substr(0, startIndex) | |
49 | // From comment start till comment end | |
50 | + selText.substring(startIndex + curMode.commentStart.length, endIndex) | |
51 | // From comment end till string end | |
52 | + selText.substr(endIndex + curMode.commentEnd.length); | |
53 | } | |
54 | cm.replaceRange(selText, from, to); | |
55 | } | |
56 | }); | |
57 | }); | |
58 | ||
59 | // Applies automatic mode-aware indentation to the specified range | |
60 | CodeMirror.defineExtension("autoIndentRange", function (from, to) { | |
61 | var cmInstance = this; | |
62 | this.operation(function () { | |
63 | for (var i = from.line; i <= to.line; i++) { | |
64 | cmInstance.indentLine(i, "smart"); | |
65 | } | |
66 | }); | |
67 | }); | |
68 | ||
69 | // Applies automatic formatting to the specified range | |
70 | CodeMirror.defineExtension("autoFormatRange", function (from, to) { | |
71 | var cm = this; | |
72 | var outer = cm.getMode(), text = cm.getRange(from, to).split("\n"); | |
73 | var state = CodeMirror.copyState(outer, cm.getTokenAt(from).state); | |
74 | var tabSize = cm.getOption("tabSize"); | |
75 | ||
76 | var out = "", lines = 0, atSol = from.ch == 0; | |
77 | function newline() { | |
78 | out += "\n"; | |
79 | atSol = true; | |
80 | ++lines; | |
81 | } | |
82 | ||
83 | for (var i = 0; i < text.length; ++i) { | |
84 | var stream = new CodeMirror.StringStream(text[i], tabSize); | |
85 | while (!stream.eol()) { | |
86 | var inner = CodeMirror.innerMode(outer, state); | |
87 | var style = outer.token(stream, state), cur = stream.current(); | |
88 | stream.start = stream.pos; | |
89 | if (!atSol || /\S/.test(cur)) { | |
90 | out += cur; | |
91 | atSol = false; | |
92 | } | |
93 | if (!atSol && inner.mode.newlineAfterToken && | |
94 | inner.mode.newlineAfterToken(style, cur, stream.string.slice(stream.pos) || text[i+1] || "", inner.state)) | |
95 | newline(); | |
96 | } | |
97 | if (!stream.pos && outer.blankLine) outer.blankLine(state); | |
98 | if (!atSol) newline(); | |
99 | } | |
100 | ||
101 | cm.operation(function () { | |
102 | cm.replaceRange(out, from, to); | |
103 | for (var cur = from.line + 1, end = from.line + lines; cur <= end; ++cur) | |
104 | cm.indentLine(cur, "smart"); | |
105 | cm.setSelection(from, cm.getCursor(false)); | |
106 | }); | |
107 | }); | |
108 | })(); |