3 CodeMirror.extendMode("css", {
6 newlineAfterToken: function(_type, content) {
7 return /^[;{}]$/.test(content);
11 CodeMirror.extendMode("javascript", {
14 // FIXME semicolons inside of for
15 newlineAfterToken: function(_type, content, textAfter, state) {
17 return /^[\[,{]$/.test(content) || /^}/.test(textAfter);
19 if (content == ";" && state.lexical && state.lexical.type == ")") return false;
20 return /^[;{}]$/.test(content) && !/^;/.test(textAfter);
25 CodeMirror.extendMode("xml", {
28 newlineAfterToken: function(type, content, textAfter) {
29 return type == "tag" && />$/.test(content) || /^</.test(textAfter);
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);
54 cm.replaceRange(selText, from, to);
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");
69 // Applies automatic formatting to the specified range
70 CodeMirror.defineExtension("autoFormatRange", function (from, to) {
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");
76 var out = "", lines = 0, atSol = from.ch == 0;
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)) {
93 if (!atSol && inner.mode.newlineAfterToken &&
94 inner.mode.newlineAfterToken(style, cur, stream.string.slice(stream.pos) || text[i+1] || "", inner.state))
97 if (!stream.pos && outer.blankLine) outer.blankLine(state);
98 if (!atSol) newline();
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));