Skip to content

Commit 2a9eb02

Browse files
committed
JS formatting
1 parent 1d8e04c commit 2a9eb02

File tree

10 files changed

+41083
-14647
lines changed

10 files changed

+41083
-14647
lines changed
Lines changed: 289 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,289 @@
1+
// CodeMirror, copyright (c) by Marijn Haverbeke and others
2+
// Distributed under an MIT license: https://codemirror.net/5/LICENSE
3+
4+
(function (mod) {
5+
if (typeof exports == "object" && typeof module == "object")
6+
// CommonJS
7+
mod(require("../../lib/codemirror"));
8+
else if (typeof define == "function" && define.amd)
9+
// AMD
10+
define(["../../lib/codemirror"], mod);
11+
// Plain browser env
12+
else mod(CodeMirror);
13+
})(function (CodeMirror) {
14+
"use strict";
15+
16+
var noOptions = {};
17+
var nonWS = /[^\s\u00a0]/;
18+
var Pos = CodeMirror.Pos,
19+
cmp = CodeMirror.cmpPos;
20+
21+
function firstNonWS(str) {
22+
var found = str.search(nonWS);
23+
return found == -1 ? 0 : found;
24+
}
25+
26+
CodeMirror.commands.toggleComment = function (cm) {
27+
cm.toggleComment();
28+
};
29+
30+
CodeMirror.defineExtension("toggleComment", function (options) {
31+
if (!options) options = noOptions;
32+
var cm = this;
33+
var minLine = Infinity,
34+
ranges = this.listSelections(),
35+
mode = null;
36+
for (var i = ranges.length - 1; i >= 0; i--) {
37+
var from = ranges[i].from(),
38+
to = ranges[i].to();
39+
if (from.line >= minLine) continue;
40+
if (to.line >= minLine) to = Pos(minLine, 0);
41+
minLine = from.line;
42+
if (mode == null) {
43+
if (cm.uncomment(from, to, options)) mode = "un";
44+
else {
45+
cm.lineComment(from, to, options);
46+
mode = "line";
47+
}
48+
} else if (mode == "un") {
49+
cm.uncomment(from, to, options);
50+
} else {
51+
cm.lineComment(from, to, options);
52+
}
53+
}
54+
});
55+
56+
// Rough heuristic to try and detect lines that are part of multi-line string
57+
function probablyInsideString(cm, pos, line) {
58+
return (
59+
/\bstring\b/.test(cm.getTokenTypeAt(Pos(pos.line, 0))) &&
60+
!/^[\'\"\`]/.test(line)
61+
);
62+
}
63+
64+
function getMode(cm, pos) {
65+
var mode = cm.getMode();
66+
return mode.useInnerComments === false || !mode.innerMode
67+
? mode
68+
: cm.getModeAt(pos);
69+
}
70+
71+
CodeMirror.defineExtension("lineComment", function (from, to, options) {
72+
if (!options) options = noOptions;
73+
var self = this,
74+
mode = getMode(self, from);
75+
var firstLine = self.getLine(from.line);
76+
if (firstLine == null || probablyInsideString(self, from, firstLine))
77+
return;
78+
79+
var commentString = options.lineComment || mode.lineComment;
80+
if (!commentString) {
81+
if (options.blockCommentStart || mode.blockCommentStart) {
82+
options.fullLines = true;
83+
self.blockComment(from, to, options);
84+
}
85+
return;
86+
}
87+
88+
var end = Math.min(
89+
to.ch != 0 || to.line == from.line ? to.line + 1 : to.line,
90+
self.lastLine() + 1
91+
);
92+
var pad = options.padding == null ? " " : options.padding;
93+
var blankLines = options.commentBlankLines || from.line == to.line;
94+
95+
self.operation(function () {
96+
if (options.indent) {
97+
var baseString = null;
98+
for (var i = from.line; i < end; ++i) {
99+
var line = self.getLine(i);
100+
var whitespace =
101+
line.search(nonWS) === -1 ? line : line.slice(0, firstNonWS(line));
102+
if (baseString == null || baseString.length > whitespace.length) {
103+
baseString = whitespace;
104+
}
105+
}
106+
for (var i = from.line; i < end; ++i) {
107+
var line = self.getLine(i),
108+
cut = baseString.length;
109+
if (!blankLines && !nonWS.test(line)) continue;
110+
if (line.slice(0, cut) != baseString) cut = firstNonWS(line);
111+
self.replaceRange(
112+
baseString + commentString + pad,
113+
Pos(i, 0),
114+
Pos(i, cut)
115+
);
116+
}
117+
} else {
118+
for (var i = from.line; i < end; ++i) {
119+
if (blankLines || nonWS.test(self.getLine(i)))
120+
self.replaceRange(commentString + pad, Pos(i, 0));
121+
}
122+
}
123+
});
124+
});
125+
126+
CodeMirror.defineExtension("blockComment", function (from, to, options) {
127+
if (!options) options = noOptions;
128+
var self = this,
129+
mode = getMode(self, from);
130+
var startString = options.blockCommentStart || mode.blockCommentStart;
131+
var endString = options.blockCommentEnd || mode.blockCommentEnd;
132+
if (!startString || !endString) {
133+
if (
134+
(options.lineComment || mode.lineComment) &&
135+
options.fullLines != false
136+
)
137+
self.lineComment(from, to, options);
138+
return;
139+
}
140+
if (/\bcomment\b/.test(self.getTokenTypeAt(Pos(from.line, 0)))) return;
141+
142+
var end = Math.min(to.line, self.lastLine());
143+
if (end != from.line && to.ch == 0 && nonWS.test(self.getLine(end))) --end;
144+
145+
var pad = options.padding == null ? " " : options.padding;
146+
if (from.line > end) return;
147+
148+
self.operation(function () {
149+
if (options.fullLines != false) {
150+
var lastLineHasText = nonWS.test(self.getLine(end));
151+
self.replaceRange(pad + endString, Pos(end));
152+
self.replaceRange(startString + pad, Pos(from.line, 0));
153+
var lead = options.blockCommentLead || mode.blockCommentLead;
154+
if (lead != null)
155+
for (var i = from.line + 1; i <= end; ++i)
156+
if (i != end || lastLineHasText)
157+
self.replaceRange(lead + pad, Pos(i, 0));
158+
} else {
159+
var atCursor = cmp(self.getCursor("to"), to) == 0,
160+
empty = !self.somethingSelected();
161+
self.replaceRange(endString, to);
162+
if (atCursor)
163+
self.setSelection(empty ? to : self.getCursor("from"), to);
164+
self.replaceRange(startString, from);
165+
}
166+
});
167+
});
168+
169+
CodeMirror.defineExtension("uncomment", function (from, to, options) {
170+
if (!options) options = noOptions;
171+
var self = this,
172+
mode = getMode(self, from);
173+
var end = Math.min(
174+
to.ch != 0 || to.line == from.line ? to.line : to.line - 1,
175+
self.lastLine()
176+
),
177+
start = Math.min(from.line, end);
178+
179+
// Try finding line comments
180+
var lineString = options.lineComment || mode.lineComment,
181+
lines = [];
182+
var pad = options.padding == null ? " " : options.padding,
183+
didSomething;
184+
lineComment: {
185+
if (!lineString) break lineComment;
186+
for (var i = start; i <= end; ++i) {
187+
var line = self.getLine(i);
188+
var found = line.indexOf(lineString);
189+
if (
190+
found > -1 &&
191+
!/comment/.test(self.getTokenTypeAt(Pos(i, found + 1)))
192+
)
193+
found = -1;
194+
if (found == -1 && nonWS.test(line)) break lineComment;
195+
if (found > -1 && nonWS.test(line.slice(0, found))) break lineComment;
196+
lines.push(line);
197+
}
198+
self.operation(function () {
199+
for (var i = start; i <= end; ++i) {
200+
var line = lines[i - start];
201+
var pos = line.indexOf(lineString),
202+
endPos = pos + lineString.length;
203+
if (pos < 0) continue;
204+
if (line.slice(endPos, endPos + pad.length) == pad)
205+
endPos += pad.length;
206+
didSomething = true;
207+
self.replaceRange("", Pos(i, pos), Pos(i, endPos));
208+
}
209+
});
210+
if (didSomething) return true;
211+
}
212+
213+
// Try block comments
214+
var startString = options.blockCommentStart || mode.blockCommentStart;
215+
var endString = options.blockCommentEnd || mode.blockCommentEnd;
216+
if (!startString || !endString) return false;
217+
var lead = options.blockCommentLead || mode.blockCommentLead;
218+
var startLine = self.getLine(start),
219+
open = startLine.indexOf(startString);
220+
if (open == -1) return false;
221+
var endLine = end == start ? startLine : self.getLine(end);
222+
var close = endLine.indexOf(
223+
endString,
224+
end == start ? open + startString.length : 0
225+
);
226+
var insideStart = Pos(start, open + 1),
227+
insideEnd = Pos(end, close + 1);
228+
if (
229+
close == -1 ||
230+
!/comment/.test(self.getTokenTypeAt(insideStart)) ||
231+
!/comment/.test(self.getTokenTypeAt(insideEnd)) ||
232+
self.getRange(insideStart, insideEnd, "\n").indexOf(endString) > -1
233+
)
234+
return false;
235+
236+
// Avoid killing block comments completely outside the selection.
237+
// Positions of the last startString before the start of the selection, and the first endString after it.
238+
var lastStart = startLine.lastIndexOf(startString, from.ch);
239+
var firstEnd =
240+
lastStart == -1
241+
? -1
242+
: startLine
243+
.slice(0, from.ch)
244+
.indexOf(endString, lastStart + startString.length);
245+
if (
246+
lastStart != -1 &&
247+
firstEnd != -1 &&
248+
firstEnd + endString.length != from.ch
249+
)
250+
return false;
251+
// Positions of the first endString after the end of the selection, and the last startString before it.
252+
firstEnd = endLine.indexOf(endString, to.ch);
253+
var almostLastStart = endLine
254+
.slice(to.ch)
255+
.lastIndexOf(startString, firstEnd - to.ch);
256+
lastStart =
257+
firstEnd == -1 || almostLastStart == -1 ? -1 : to.ch + almostLastStart;
258+
if (firstEnd != -1 && lastStart != -1 && lastStart != to.ch) return false;
259+
260+
self.operation(function () {
261+
self.replaceRange(
262+
"",
263+
Pos(
264+
end,
265+
close -
266+
(pad && endLine.slice(close - pad.length, close) == pad
267+
? pad.length
268+
: 0)
269+
),
270+
Pos(end, close + endString.length)
271+
);
272+
var openEnd = open + startString.length;
273+
if (pad && startLine.slice(openEnd, openEnd + pad.length) == pad)
274+
openEnd += pad.length;
275+
self.replaceRange("", Pos(start, open), Pos(start, openEnd));
276+
if (lead)
277+
for (var i = start + 1; i <= end; ++i) {
278+
var line = self.getLine(i),
279+
found = line.indexOf(lead);
280+
if (found == -1 || nonWS.test(line.slice(0, found))) continue;
281+
var foundEnd = found + lead.length;
282+
if (pad && line.slice(foundEnd, foundEnd + pad.length) == pad)
283+
foundEnd += pad.length;
284+
self.replaceRange("", Pos(i, found), Pos(i, foundEnd));
285+
}
286+
});
287+
return true;
288+
});
289+
});

0 commit comments

Comments
 (0)