Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / devtools / front_end / cm / activeline.js
blob22da2e0aace1e527a8c71b67604714849c663de0
1 // CodeMirror, copyright (c) by Marijn Haverbeke and others
2 // Distributed under an MIT license: http://codemirror.net/LICENSE
4 // Because sometimes you need to style the cursor's line.
5 //
6 // Adds an option 'styleActiveLine' which, when enabled, gives the
7 // active line's wrapping <div> the CSS class "CodeMirror-activeline",
8 // and gives its background <div> the class "CodeMirror-activeline-background".
10 (function(mod) {
11 if (typeof exports == "object" && typeof module == "object") // CommonJS
12 mod(require("../../lib/codemirror"));
13 else if (typeof define == "function" && define.amd) // AMD
14 define(["../../lib/codemirror"], mod);
15 else // Plain browser env
16 mod(CodeMirror);
17 })(function(CodeMirror) {
18 "use strict";
19 var WRAP_CLASS = "CodeMirror-activeline";
20 var BACK_CLASS = "CodeMirror-activeline-background";
22 CodeMirror.defineOption("styleActiveLine", false, function(cm, val, old) {
23 var prev = old && old != CodeMirror.Init;
24 if (val && !prev) {
25 cm.state.activeLines = [];
26 updateActiveLines(cm, cm.listSelections());
27 cm.on("beforeSelectionChange", selectionChange);
28 } else if (!val && prev) {
29 cm.off("beforeSelectionChange", selectionChange);
30 clearActiveLines(cm);
31 delete cm.state.activeLines;
33 });
35 function clearActiveLines(cm) {
36 for (var i = 0; i < cm.state.activeLines.length; i++) {
37 cm.removeLineClass(cm.state.activeLines[i], "wrap", WRAP_CLASS);
38 cm.removeLineClass(cm.state.activeLines[i], "background", BACK_CLASS);
42 function sameArray(a, b) {
43 if (a.length != b.length) return false;
44 for (var i = 0; i < a.length; i++)
45 if (a[i] != b[i]) return false;
46 return true;
49 function updateActiveLines(cm, ranges) {
50 var active = [];
51 for (var i = 0; i < ranges.length; i++) {
52 var range = ranges[i];
53 if (!range.empty()) continue;
54 var line = cm.getLineHandleVisualStart(range.head.line);
55 if (active[active.length - 1] != line) active.push(line);
57 if (sameArray(cm.state.activeLines, active)) return;
58 cm.operation(function() {
59 clearActiveLines(cm);
60 for (var i = 0; i < active.length; i++) {
61 cm.addLineClass(active[i], "wrap", WRAP_CLASS);
62 cm.addLineClass(active[i], "background", BACK_CLASS);
64 cm.state.activeLines = active;
65 });
68 function selectionChange(cm, sel) {
69 updateActiveLines(cm, sel.ranges);
71 });