1
// ============== Formatting extensions ============================
3
// Define extensions for a few modes
4
CodeMirror.extendMode("css", {
7
wordWrapChars: [";", "\\{", "\\}"],
8
autoFormatLineBreaks: function (text) {
9
return text.replace(new RegExp("(;|\\{|\\})([^\r\n])", "g"), "$1\n$2");
13
function jsNonBreakableBlocks(text) {
14
var nonBreakableRegexes = [/for\s*?\((.*?)\)/,
19
var nonBreakableBlocks = [];
20
for (var i = 0; i < nonBreakableRegexes.length; i++) {
22
while (curPos < text.length) {
23
var m = text.substr(curPos).match(nonBreakableRegexes[i]);
25
nonBreakableBlocks.push({
26
start: curPos + m.index,
27
end: curPos + m.index + m[0].length
29
curPos += m.index + Math.max(1, m[0].length);
31
else { // No more matches
36
nonBreakableBlocks.sort(function (a, b) {
37
return a.start - b.start;
40
return nonBreakableBlocks;
43
CodeMirror.extendMode("javascript", {
46
wordWrapChars: [";", "\\{", "\\}"],
48
autoFormatLineBreaks: function (text) {
50
var reLinesSplitter = /(;|\{|\})([^\r\n;])/g;
51
var nonBreakableBlocks = jsNonBreakableBlocks(text);
52
if (nonBreakableBlocks != null) {
54
for (var i = 0; i < nonBreakableBlocks.length; i++) {
55
if (nonBreakableBlocks[i].start > curPos) { // Break lines till the block
56
res += text.substring(curPos, nonBreakableBlocks[i].start).replace(reLinesSplitter, "$1\n$2");
57
curPos = nonBreakableBlocks[i].start;
59
if (nonBreakableBlocks[i].start <= curPos
60
&& nonBreakableBlocks[i].end >= curPos) { // Skip non-breakable block
61
res += text.substring(curPos, nonBreakableBlocks[i].end);
62
curPos = nonBreakableBlocks[i].end;
65
if (curPos < text.length)
66
res += text.substr(curPos).replace(reLinesSplitter, "$1\n$2");
69
return text.replace(reLinesSplitter, "$1\n$2");
74
CodeMirror.extendMode("xml", {
79
autoFormatLineBreaks: function (text) {
80
var lines = text.split("\n");
81
var reProcessedPortion = new RegExp("(^\\s*?<|^[^<]*?)(.+)(>\\s*?$|[^>]*?$)");
82
var reOpenBrackets = new RegExp("<", "g");
83
var reCloseBrackets = new RegExp("(>)([^\r\n])", "g");
84
for (var i = 0; i < lines.length; i++) {
85
var mToProcess = lines[i].match(reProcessedPortion);
86
if (mToProcess != null && mToProcess.length > 3) { // The line starts with whitespaces and ends with whitespaces
87
lines[i] = mToProcess[1]
88
+ mToProcess[2].replace(reOpenBrackets, "\n$&").replace(reCloseBrackets, "$1\n$2")
93
return lines.join("\n");
97
function localModeAt(cm, pos) {
98
return CodeMirror.innerMode(cm.getMode(), cm.getTokenAt(pos).state).mode;
101
function enumerateModesBetween(cm, line, start, end) {
102
var outer = cm.getMode(), text = cm.getLine(line);
103
if (end == null) end = text.length;
104
if (!outer.innerMode) return [{from: start, to: end, mode: outer}];
105
var state = cm.getTokenAt({line: line, ch: start}).state;
106
var mode = CodeMirror.innerMode(outer, state).mode;
107
var found = [], stream = new CodeMirror.StringStream(text);
108
stream.pos = stream.start = start;
110
outer.token(stream, state);
111
var curMode = CodeMirror.innerMode(outer, state).mode;
112
if (curMode != mode) {
113
var cut = stream.start;
114
// Crappy heuristic to deal with the fact that a change in
115
// mode can occur both at the end and the start of a token,
116
// and we don't know which it was.
117
if (mode.name == "xml" && text.charAt(stream.pos - 1) == ">") cut = stream.pos;
118
found.push({from: start, to: cut, mode: mode});
122
if (stream.pos >= end) break;
123
stream.start = stream.pos;
125
if (start < end) found.push({from: start, to: end, mode: mode});
129
// Comment/uncomment the specified range
130
CodeMirror.defineExtension("commentRange", function (isComment, from, to) {
131
var curMode = localModeAt(this, from), cm = this;
132
this.operation(function() {
133
if (isComment) { // Comment range
134
cm.replaceRange(curMode.commentEnd, to);
135
cm.replaceRange(curMode.commentStart, from);
136
if (from.line == to.line && from.ch == to.ch) // An empty comment inserted - put cursor inside
137
cm.setCursor(from.line, from.ch + curMode.commentStart.length);
138
} else { // Uncomment range
139
var selText = cm.getRange(from, to);
140
var startIndex = selText.indexOf(curMode.commentStart);
141
var endIndex = selText.lastIndexOf(curMode.commentEnd);
142
if (startIndex > -1 && endIndex > -1 && endIndex > startIndex) {
143
// Take string till comment start
144
selText = selText.substr(0, startIndex)
145
// From comment start till comment end
146
+ selText.substring(startIndex + curMode.commentStart.length, endIndex)
147
// From comment end till string end
148
+ selText.substr(endIndex + curMode.commentEnd.length);
150
cm.replaceRange(selText, from, to);
155
// Applies automatic mode-aware indentation to the specified range
156
CodeMirror.defineExtension("autoIndentRange", function (from, to) {
157
var cmInstance = this;
158
this.operation(function () {
159
for (var i = from.line; i <= to.line; i++) {
160
cmInstance.indentLine(i, "smart");
165
// Applies automatic formatting to the specified range
166
CodeMirror.defineExtension("autoFormatRange", function (from, to) {
168
cm.operation(function () {
169
for (var cur = from.line, end = to.line; cur <= end; ++cur) {
170
var f = {line: cur, ch: cur == from.line ? from.ch : 0};
171
var t = {line: cur, ch: cur == end ? to.ch : null};
172
var modes = enumerateModesBetween(cm, cur, f.ch, t.ch), mangled = "";
173
var text = cm.getRange(f, t);
174
for (var i = 0; i < modes.length; ++i) {
175
var part = modes.length > 1 ? text.slice(modes[i].from, modes[i].to) : text;
176
if (mangled) mangled += "\n";
177
if (modes[i].mode.autoFormatLineBreaks) {
178
mangled += modes[i].mode.autoFormatLineBreaks(part);
179
} else mangled += text;
181
if (mangled != text) {
182
for (var count = 0, pos = mangled.indexOf("\n"); pos != -1; pos = mangled.indexOf("\n", pos + 1), ++count) {}
183
cm.replaceRange(mangled, f, t);
188
for (var cur = from.line + 1; cur <= end; ++cur)
189
cm.indentLine(cur, "smart");
190
cm.setSelection(from, cm.getCursor(false));