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

Side by Side Diff: Source/devtools/front_end/cm/markselection.js

Issue 216973004: DevTools: roll CodeMirror to v4.0.3 (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: support autocomplete with multiselections Created 6 years, 8 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 | Annotate | Revision Log
OLDNEW
1 // Because sometimes you need to mark the selected *text*. 1 // Because sometimes you need to mark the selected *text*.
2 // 2 //
3 // Adds an option 'styleSelectedText' which, when enabled, gives 3 // Adds an option 'styleSelectedText' which, when enabled, gives
4 // selected text the CSS class given as option value, or 4 // selected text the CSS class given as option value, or
5 // "CodeMirror-selectedtext" when the value is not a string. 5 // "CodeMirror-selectedtext" when the value is not a string.
6 6
7 (function() { 7 (function(mod) {
8 if (typeof exports == "object" && typeof module == "object") // CommonJS
9 mod(require("../../lib/codemirror"));
10 else if (typeof define == "function" && define.amd) // AMD
11 define(["../../lib/codemirror"], mod);
12 else // Plain browser env
13 mod(CodeMirror);
14 })(function(CodeMirror) {
8 "use strict"; 15 "use strict";
9 16
10 CodeMirror.defineOption("styleSelectedText", false, function(cm, val, old) { 17 CodeMirror.defineOption("styleSelectedText", false, function(cm, val, old) {
11 var prev = old && old != CodeMirror.Init; 18 var prev = old && old != CodeMirror.Init;
12 if (val && !prev) { 19 if (val && !prev) {
13 cm.state.markedSelection = []; 20 cm.state.markedSelection = [];
14 cm.state.markedSelectionStyle = typeof val == "string" ? val : "CodeMirror -selectedtext"; 21 cm.state.markedSelectionStyle = typeof val == "string" ? val : "CodeMirror -selectedtext";
15 reset(cm); 22 reset(cm);
16 cm.on("cursorActivity", onCursorActivity); 23 cm.on("cursorActivity", onCursorActivity);
17 cm.on("change", onChange); 24 cm.on("change", onChange);
18 } else if (!val && prev) { 25 } else if (!val && prev) {
19 cm.off("cursorActivity", onCursorActivity); 26 cm.off("cursorActivity", onCursorActivity);
20 cm.off("change", onChange); 27 cm.off("change", onChange);
21 clear(cm); 28 clear(cm);
22 cm.state.markedSelection = cm.state.markedSelectionStyle = null; 29 cm.state.markedSelection = cm.state.markedSelectionStyle = null;
23 } 30 }
24 }); 31 });
25 32
26 function onCursorActivity(cm) { 33 function onCursorActivity(cm) {
27 cm.operation(function() { update(cm); }); 34 cm.operation(function() { update(cm); });
28 } 35 }
29 36
30 function onChange(cm) { 37 function onChange(cm) {
31 if (cm.state.markedSelection.length) 38 if (cm.state.markedSelection.length)
32 cm.operation(function() { clear(cm); }); 39 cm.operation(function() { clear(cm); });
33 } 40 }
34 41
35 var CHUNK_SIZE = 8; 42 var CHUNK_SIZE = 8;
36 var Pos = CodeMirror.Pos; 43 var Pos = CodeMirror.Pos;
37 44 var cmp = CodeMirror.cmpPos;
38 function cmp(pos1, pos2) {
39 return pos1.line - pos2.line || pos1.ch - pos2.ch;
40 }
41 45
42 function coverRange(cm, from, to, addAt) { 46 function coverRange(cm, from, to, addAt) {
43 if (cmp(from, to) == 0) return; 47 if (cmp(from, to) == 0) return;
44 var array = cm.state.markedSelection; 48 var array = cm.state.markedSelection;
45 var cls = cm.state.markedSelectionStyle; 49 var cls = cm.state.markedSelectionStyle;
46 for (var line = from.line;;) { 50 for (var line = from.line;;) {
47 var start = line == from.line ? from : Pos(line, 0); 51 var start = line == from.line ? from : Pos(line, 0);
48 var endLine = line + CHUNK_SIZE, atEnd = endLine >= to.line; 52 var endLine = line + CHUNK_SIZE, atEnd = endLine >= to.line;
49 var end = atEnd ? to : Pos(endLine, 0); 53 var end = atEnd ? to : Pos(endLine, 0);
50 var mark = cm.markText(start, end, {className: cls}); 54 var mark = cm.markText(start, end, {className: cls});
51 if (addAt == null) array.push(mark); 55 if (addAt == null) array.push(mark);
52 else array.splice(addAt++, 0, mark); 56 else array.splice(addAt++, 0, mark);
53 if (atEnd) break; 57 if (atEnd) break;
54 line = endLine; 58 line = endLine;
55 } 59 }
56 } 60 }
57 61
58 function clear(cm) { 62 function clear(cm) {
59 var array = cm.state.markedSelection; 63 var array = cm.state.markedSelection;
60 for (var i = 0; i < array.length; ++i) array[i].clear(); 64 for (var i = 0; i < array.length; ++i) array[i].clear();
61 array.length = 0; 65 array.length = 0;
62 } 66 }
63 67
64 function reset(cm) { 68 function reset(cm) {
65 clear(cm); 69 clear(cm);
66 var from = cm.getCursor("start"), to = cm.getCursor("end"); 70 var ranges = cm.listSelections();
67 coverRange(cm, from, to); 71 for (var i = 0; i < ranges.length; i++)
72 coverRange(cm, ranges[i].from(), ranges[i].to());
68 } 73 }
69 74
70 function update(cm) { 75 function update(cm) {
76 if (!cm.somethingSelected()) return clear(cm);
77 if (cm.listSelections().length > 1) return reset(cm);
78
71 var from = cm.getCursor("start"), to = cm.getCursor("end"); 79 var from = cm.getCursor("start"), to = cm.getCursor("end");
72 if (cmp(from, to) == 0) return clear(cm);
73 80
74 var array = cm.state.markedSelection; 81 var array = cm.state.markedSelection;
75 if (!array.length) return coverRange(cm, from, to); 82 if (!array.length) return coverRange(cm, from, to);
76 83
77 var coverStart = array[0].find(), coverEnd = array[array.length - 1].find(); 84 var coverStart = array[0].find(), coverEnd = array[array.length - 1].find();
78 if (!coverStart || !coverEnd || to.line - from.line < CHUNK_SIZE || 85 if (!coverStart || !coverEnd || to.line - from.line < CHUNK_SIZE ||
79 cmp(from, coverEnd.to) >= 0 || cmp(to, coverStart.from) <= 0) 86 cmp(from, coverEnd.to) >= 0 || cmp(to, coverStart.from) <= 0)
80 return reset(cm); 87 return reset(cm);
81 88
82 while (cmp(from, coverStart.from) > 0) { 89 while (cmp(from, coverStart.from) > 0) {
(...skipping 15 matching lines...) Expand all
98 } 105 }
99 if (cmp(to, coverEnd.to) > 0) { 106 if (cmp(to, coverEnd.to) > 0) {
100 if (to.line - coverEnd.from.line < CHUNK_SIZE) { 107 if (to.line - coverEnd.from.line < CHUNK_SIZE) {
101 array.pop().clear(); 108 array.pop().clear();
102 coverRange(cm, coverEnd.from, to); 109 coverRange(cm, coverEnd.from, to);
103 } else { 110 } else {
104 coverRange(cm, coverEnd.to, to); 111 coverRange(cm, coverEnd.to, to);
105 } 112 }
106 } 113 }
107 } 114 }
108 })(); 115 });
OLDNEW
« no previous file with comments | « Source/devtools/front_end/cm/headlesscodemirror.js ('k') | Source/devtools/front_end/cm/matchbrackets.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698