Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(74)

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/cm/comment.js

Issue 2862603003: Revert of DevTools: Roll CodeMirror to 5.25.1
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // CodeMirror, copyright (c) by Marijn Haverbeke and others 1 // CodeMirror, copyright (c) by Marijn Haverbeke and others
2 // Distributed under an MIT license: http://codemirror.net/LICENSE 2 // Distributed under an MIT license: http://codemirror.net/LICENSE
3 3
4 (function(mod) { 4 (function(mod) {
5 if (typeof exports == "object" && typeof module == "object") // CommonJS 5 if (typeof exports == "object" && typeof module == "object") // CommonJS
6 mod(require("../../lib/codemirror")); 6 mod(require("../../lib/codemirror"));
7 else if (typeof define == "function" && define.amd) // AMD 7 else if (typeof define == "function" && define.amd) // AMD
8 define(["../../lib/codemirror"], mod); 8 define(["../../lib/codemirror"], mod);
9 else // Plain browser env 9 else // Plain browser env
10 mod(CodeMirror); 10 mod(CodeMirror);
(...skipping 28 matching lines...) Expand all
39 } else if (mode == "un") { 39 } else if (mode == "un") {
40 cm.uncomment(from, to, options); 40 cm.uncomment(from, to, options);
41 } else { 41 } else {
42 cm.lineComment(from, to, options); 42 cm.lineComment(from, to, options);
43 } 43 }
44 } 44 }
45 }); 45 });
46 46
47 // Rough heuristic to try and detect lines that are part of multi-line string 47 // Rough heuristic to try and detect lines that are part of multi-line string
48 function probablyInsideString(cm, pos, line) { 48 function probablyInsideString(cm, pos, line) {
49 return /\bstring\b/.test(cm.getTokenTypeAt(Pos(pos.line, 0))) && !/^[\'\"\`] /.test(line) 49 return /\bstring\b/.test(cm.getTokenTypeAt(Pos(pos.line, 0))) && !/^[\'\"`]/ .test(line)
50 }
51
52 function getMode(cm, pos) {
53 var mode = cm.getMode()
54 return mode.useInnerComments === false || !mode.innerMode ? mode : cm.getMod eAt(pos)
55 } 50 }
56 51
57 CodeMirror.defineExtension("lineComment", function(from, to, options) { 52 CodeMirror.defineExtension("lineComment", function(from, to, options) {
58 if (!options) options = noOptions; 53 if (!options) options = noOptions;
59 var self = this, mode = getMode(self, from); 54 var self = this, mode = self.getModeAt(from);
60 var firstLine = self.getLine(from.line); 55 var firstLine = self.getLine(from.line);
61 if (firstLine == null || probablyInsideString(self, from, firstLine)) return ; 56 if (firstLine == null || probablyInsideString(self, from, firstLine)) return ;
62 57
63 var commentString = options.lineComment || mode.lineComment; 58 var commentString = options.lineComment || mode.lineComment;
64 if (!commentString) { 59 if (!commentString) {
65 if (options.blockCommentStart || mode.blockCommentStart) { 60 if (options.blockCommentStart || mode.blockCommentStart) {
66 options.fullLines = true; 61 options.fullLines = true;
67 self.blockComment(from, to, options); 62 self.blockComment(from, to, options);
68 } 63 }
69 return; 64 return;
(...skipping 23 matching lines...) Expand all
93 for (var i = from.line; i < end; ++i) { 88 for (var i = from.line; i < end; ++i) {
94 if (blankLines || nonWS.test(self.getLine(i))) 89 if (blankLines || nonWS.test(self.getLine(i)))
95 self.replaceRange(commentString + pad, Pos(i, 0)); 90 self.replaceRange(commentString + pad, Pos(i, 0));
96 } 91 }
97 } 92 }
98 }); 93 });
99 }); 94 });
100 95
101 CodeMirror.defineExtension("blockComment", function(from, to, options) { 96 CodeMirror.defineExtension("blockComment", function(from, to, options) {
102 if (!options) options = noOptions; 97 if (!options) options = noOptions;
103 var self = this, mode = getMode(self, from); 98 var self = this, mode = self.getModeAt(from);
104 var startString = options.blockCommentStart || mode.blockCommentStart; 99 var startString = options.blockCommentStart || mode.blockCommentStart;
105 var endString = options.blockCommentEnd || mode.blockCommentEnd; 100 var endString = options.blockCommentEnd || mode.blockCommentEnd;
106 if (!startString || !endString) { 101 if (!startString || !endString) {
107 if ((options.lineComment || mode.lineComment) && options.fullLines != fals e) 102 if ((options.lineComment || mode.lineComment) && options.fullLines != fals e)
108 self.lineComment(from, to, options); 103 self.lineComment(from, to, options);
109 return; 104 return;
110 } 105 }
111 if (/\bcomment\b/.test(self.getTokenTypeAt(Pos(from.line, 0)))) return
112 106
113 var end = Math.min(to.line, self.lastLine()); 107 var end = Math.min(to.line, self.lastLine());
114 if (end != from.line && to.ch == 0 && nonWS.test(self.getLine(end))) --end; 108 if (end != from.line && to.ch == 0 && nonWS.test(self.getLine(end))) --end;
115 109
116 var pad = options.padding == null ? " " : options.padding; 110 var pad = options.padding == null ? " " : options.padding;
117 if (from.line > end) return; 111 if (from.line > end) return;
118 112
119 self.operation(function() { 113 self.operation(function() {
120 if (options.fullLines != false) { 114 if (options.fullLines != false) {
121 var lastLineHasText = nonWS.test(self.getLine(end)); 115 var lastLineHasText = nonWS.test(self.getLine(end));
122 self.replaceRange(pad + endString, Pos(end)); 116 self.replaceRange(pad + endString, Pos(end));
123 self.replaceRange(startString + pad, Pos(from.line, 0)); 117 self.replaceRange(startString + pad, Pos(from.line, 0));
124 var lead = options.blockCommentLead || mode.blockCommentLead; 118 var lead = options.blockCommentLead || mode.blockCommentLead;
125 if (lead != null) for (var i = from.line + 1; i <= end; ++i) 119 if (lead != null) for (var i = from.line + 1; i <= end; ++i)
126 if (i != end || lastLineHasText) 120 if (i != end || lastLineHasText)
127 self.replaceRange(lead + pad, Pos(i, 0)); 121 self.replaceRange(lead + pad, Pos(i, 0));
128 } else { 122 } else {
129 self.replaceRange(endString, to); 123 self.replaceRange(endString, to);
130 self.replaceRange(startString, from); 124 self.replaceRange(startString, from);
131 } 125 }
132 }); 126 });
133 }); 127 });
134 128
135 CodeMirror.defineExtension("uncomment", function(from, to, options) { 129 CodeMirror.defineExtension("uncomment", function(from, to, options) {
136 if (!options) options = noOptions; 130 if (!options) options = noOptions;
137 var self = this, mode = getMode(self, from); 131 var self = this, mode = self.getModeAt(from);
138 var end = Math.min(to.ch != 0 || to.line == from.line ? to.line : to.line - 1, self.lastLine()), start = Math.min(from.line, end); 132 var end = Math.min(to.ch != 0 || to.line == from.line ? to.line : to.line - 1, self.lastLine()), start = Math.min(from.line, end);
139 133
140 // Try finding line comments 134 // Try finding line comments
141 var lineString = options.lineComment || mode.lineComment, lines = []; 135 var lineString = options.lineComment || mode.lineComment, lines = [];
142 var pad = options.padding == null ? " " : options.padding, didSomething; 136 var pad = options.padding == null ? " " : options.padding, didSomething;
143 lineComment: { 137 lineComment: {
144 if (!lineString) break lineComment; 138 if (!lineString) break lineComment;
145 for (var i = start; i <= end; ++i) { 139 for (var i = start; i <= end; ++i) {
146 var line = self.getLine(i); 140 var line = self.getLine(i);
147 var found = line.indexOf(lineString); 141 var found = line.indexOf(lineString);
148 if (found > -1 && !/comment/.test(self.getTokenTypeAt(Pos(i, found + 1)) )) found = -1; 142 if (found > -1 && !/comment/.test(self.getTokenTypeAt(Pos(i, found + 1)) )) found = -1;
149 if (found == -1 && nonWS.test(line)) break lineComment; 143 if (found == -1 && (i != end || i == start) && nonWS.test(line)) break l ineComment;
150 if (found > -1 && nonWS.test(line.slice(0, found))) break lineComment; 144 if (found > -1 && nonWS.test(line.slice(0, found))) break lineComment;
151 lines.push(line); 145 lines.push(line);
152 } 146 }
153 self.operation(function() { 147 self.operation(function() {
154 for (var i = start; i <= end; ++i) { 148 for (var i = start; i <= end; ++i) {
155 var line = lines[i - start]; 149 var line = lines[i - start];
156 var pos = line.indexOf(lineString), endPos = pos + lineString.length; 150 var pos = line.indexOf(lineString), endPos = pos + lineString.length;
157 if (pos < 0) continue; 151 if (pos < 0) continue;
158 if (line.slice(endPos, endPos + pad.length) == pad) endPos += pad.leng th; 152 if (line.slice(endPos, endPos + pad.length) == pad) endPos += pad.leng th;
159 didSomething = true; 153 didSomething = true;
160 self.replaceRange("", Pos(i, pos), Pos(i, endPos)); 154 self.replaceRange("", Pos(i, pos), Pos(i, endPos));
161 } 155 }
162 }); 156 });
163 if (didSomething) return true; 157 if (didSomething) return true;
164 } 158 }
165 159
166 // Try block comments 160 // Try block comments
167 var startString = options.blockCommentStart || mode.blockCommentStart; 161 var startString = options.blockCommentStart || mode.blockCommentStart;
168 var endString = options.blockCommentEnd || mode.blockCommentEnd; 162 var endString = options.blockCommentEnd || mode.blockCommentEnd;
169 if (!startString || !endString) return false; 163 if (!startString || !endString) return false;
170 var lead = options.blockCommentLead || mode.blockCommentLead; 164 var lead = options.blockCommentLead || mode.blockCommentLead;
171 var startLine = self.getLine(start), open = startLine.indexOf(startString) 165 var startLine = self.getLine(start), endLine = end == start ? startLine : se lf.getLine(end);
172 if (open == -1) return false 166 var open = startLine.indexOf(startString), close = endLine.lastIndexOf(endSt ring);
173 var endLine = end == start ? startLine : self.getLine(end)
174 var close = endLine.indexOf(endString, end == start ? open + startString.len gth : 0);
175 if (close == -1 && start != end) { 167 if (close == -1 && start != end) {
176 endLine = self.getLine(--end); 168 endLine = self.getLine(--end);
177 close = endLine.indexOf(endString); 169 close = endLine.lastIndexOf(endString);
178 } 170 }
179 var insideStart = Pos(start, open + 1), insideEnd = Pos(end, close + 1) 171 if (open == -1 || close == -1 ||
180 if (close == -1 || 172 !/comment/.test(self.getTokenTypeAt(Pos(start, open + 1))) ||
181 !/comment/.test(self.getTokenTypeAt(insideStart)) || 173 !/comment/.test(self.getTokenTypeAt(Pos(end, close + 1))))
182 !/comment/.test(self.getTokenTypeAt(insideEnd)) ||
183 self.getRange(insideStart, insideEnd, "\n").indexOf(endString) > -1)
184 return false; 174 return false;
185 175
186 // Avoid killing block comments completely outside the selection. 176 // Avoid killing block comments completely outside the selection.
187 // Positions of the last startString before the start of the selection, and the first endString after it. 177 // Positions of the last startString before the start of the selection, and the first endString after it.
188 var lastStart = startLine.lastIndexOf(startString, from.ch); 178 var lastStart = startLine.lastIndexOf(startString, from.ch);
189 var firstEnd = lastStart == -1 ? -1 : startLine.slice(0, from.ch).indexOf(en dString, lastStart + startString.length); 179 var firstEnd = lastStart == -1 ? -1 : startLine.slice(0, from.ch).indexOf(en dString, lastStart + startString.length);
190 if (lastStart != -1 && firstEnd != -1 && firstEnd + endString.length != from .ch) return false; 180 if (lastStart != -1 && firstEnd != -1 && firstEnd + endString.length != from .ch) return false;
191 // Positions of the first endString after the end of the selection, and the last startString before it. 181 // Positions of the first endString after the end of the selection, and the last startString before it.
192 firstEnd = endLine.indexOf(endString, to.ch); 182 firstEnd = endLine.indexOf(endString, to.ch);
193 var almostLastStart = endLine.slice(to.ch).lastIndexOf(startString, firstEnd - to.ch); 183 var almostLastStart = endLine.slice(to.ch).lastIndexOf(startString, firstEnd - to.ch);
(...skipping 10 matching lines...) Expand all
204 var line = self.getLine(i), found = line.indexOf(lead); 194 var line = self.getLine(i), found = line.indexOf(lead);
205 if (found == -1 || nonWS.test(line.slice(0, found))) continue; 195 if (found == -1 || nonWS.test(line.slice(0, found))) continue;
206 var foundEnd = found + lead.length; 196 var foundEnd = found + lead.length;
207 if (pad && line.slice(foundEnd, foundEnd + pad.length) == pad) foundEnd += pad.length; 197 if (pad && line.slice(foundEnd, foundEnd + pad.length) == pad) foundEnd += pad.length;
208 self.replaceRange("", Pos(i, found), Pos(i, foundEnd)); 198 self.replaceRange("", Pos(i, found), Pos(i, foundEnd));
209 } 199 }
210 }); 200 });
211 return true; 201 return true;
212 }); 202 });
213 }); 203 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698