From 32649818b8e584eaee716f21efc86dbf81c8fd5d Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Wed, 21 Mar 2012 10:42:17 +0100 Subject: [PATCH] Add findMarksAt method --- doc/manual.html | 4 ++++ lib/codemirror.js | 36 +++++++++++++++++++++++++----------- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/doc/manual.html b/doc/manual.html index e5ca572b2a..8867b2f7d1 100644 --- a/doc/manual.html +++ b/doc/manual.html @@ -545,6 +545,10 @@

Programming API

the document, and the second explicitly removes the bookmark. +
findMarksAt(pos) → array
+
Returns an array of all the bookmarks and marked ranges + present at the given position.
+
setMarker(line, text, className) → lineHandle
Add a gutter marker for the given line. Gutter markers are shown in the line-number area (instead of the number for this diff --git a/lib/codemirror.js b/lib/codemirror.js index d9ed8264b8..45079c78b0 100644 --- a/lib/codemirror.js +++ b/lib/codemirror.js @@ -201,6 +201,7 @@ var CodeMirror = (function() { }, markText: operation(markText), setBookmark: setBookmark, + findMarksAt: findMarksAt, setMarker: operation(addGutterMarker), clearMarker: operation(removeGutterMarker), setLineClass: operation(setLineClass), @@ -1354,7 +1355,7 @@ var CodeMirror = (function() { var lineN = lineNo(line); min = Math.min(min, lineN); max = Math.max(max, lineN); for (var j = 0; j < mk.length; ++j) - if (mk[j].set == this.set) mk.splice(j--, 1); + if (mk[j].marker == this) mk.splice(j--, 1); } if (min != Infinity) changes.push({from: min, to: max + 1}); @@ -1365,7 +1366,7 @@ var CodeMirror = (function() { var line = this.set[i], mk = line.marked; for (var j = 0; j < mk.length; ++j) { var mark = mk[j]; - if (mark.set == this.set) { + if (mark.marker == this) { if (mark.from != null || mark.to != null) { var found = lineNo(line); if (found != null) { @@ -1384,7 +1385,7 @@ var CodeMirror = (function() { var tm = new TextMarker(); if (!posLess(from, to)) return tm; function add(line, from, to, className) { - getLine(line).addMark(new MarkedText(from, to, className, tm.set)); + getLine(line).addMark(new MarkedText(from, to, className, tm)); } if (from.line == to.line) add(from.line, from.ch, to.ch, className); else { @@ -1404,6 +1405,19 @@ var CodeMirror = (function() { return bm; } + function findMarksAt(pos) { + pos = clipPos(pos); + var markers = [], marked = getLine(pos.line).marked; + if (!marked) return markers; + for (var i = 0, e = marked.length; i < e; ++i) { + var m = marked[i]; + if ((m.from == null || m.from <= pos.ch) && + (m.to == null || m.to >= pos.ch)) + markers.push(m.marker || m); + } + return markers; + } + function addGutterMarker(line, text, className) { if (typeof line == "number") line = getLine(clipLine(line)); line.gutterMarker = {text: text, style: className}; @@ -2147,22 +2161,22 @@ var CodeMirror = (function() { }; CodeMirror.StringStream = StringStream; - function MarkedText(from, to, className, set) { - this.from = from; this.to = to; this.style = className; this.set = set; + function MarkedText(from, to, className, marker) { + this.from = from; this.to = to; this.style = className; this.marker = marker; } MarkedText.prototype = { - attach: function(line) { this.set.push(line); }, + attach: function(line) { this.marker.set.push(line); }, detach: function(line) { - var ix = indexOf(this.set, line); - if (ix > -1) this.set.splice(ix, 1); + var ix = indexOf(this.marker.set, line); + if (ix > -1) this.marker.set.splice(ix, 1); }, split: function(pos, lenBefore) { if (this.to <= pos && this.to != null) return null; var from = this.from < pos || this.from == null ? null : this.from - pos + lenBefore; var to = this.to == null ? null : this.to - pos + lenBefore; - return new MarkedText(from, to, this.style, this.set); + return new MarkedText(from, to, this.style, this.marker); }, - dup: function() { return new MarkedText(null, null, this.style, this.set); }, + dup: function() { return new MarkedText(null, null, this.style, this.marker); }, clipTo: function(fromOpen, from, toOpen, to, diff) { if (fromOpen && to > this.from && (to < this.to || this.to == null)) this.from = null; @@ -2174,7 +2188,7 @@ var CodeMirror = (function() { this.to = to < this.to ? this.to + diff : from; }, isDead: function() { return this.from != null && this.to != null && this.from >= this.to; }, - sameSet: function(x) { return this.set == x.set; } + sameSet: function(x) { return this.marker == x.marker; } }; function Bookmark(pos) {