Skip to content

Commit

Permalink
Add findMarksAt method
Browse files Browse the repository at this point in the history
  • Loading branch information
marijnh committed Mar 21, 2012
1 parent 9b900e5 commit 3264981
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
4 changes: 4 additions & 0 deletions doc/manual.html
Expand Up @@ -545,6 +545,10 @@ <h2 id="api">Programming API</h2>
the document, and the second explicitly removes the
bookmark.</dd>

<dt id="findMarksAt"><code>findMarksAt(pos) → array</code></dt>
<dd>Returns an array of all the bookmarks and marked ranges
present at the given position.</dd>

<dt id="setMarker"><code>setMarker(line, text, className) → lineHandle</code></dt>
<dd>Add a gutter marker for the given line. Gutter markers are
shown in the line-number area (instead of the number for this
Expand Down
36 changes: 25 additions & 11 deletions lib/codemirror.js
Expand Up @@ -201,6 +201,7 @@ var CodeMirror = (function() {
},
markText: operation(markText),
setBookmark: setBookmark,
findMarksAt: findMarksAt,
setMarker: operation(addGutterMarker),
clearMarker: operation(removeGutterMarker),
setLineClass: operation(setLineClass),
Expand Down Expand Up @@ -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});
Expand All @@ -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) {
Expand All @@ -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 {
Expand All @@ -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};
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down

0 comments on commit 3264981

Please sign in to comment.