Skip to content

Commit 667a030

Browse files
committed
add setLineClass method
1 parent cf1facb commit 667a030

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

lib/codemirror.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var CodeMirror = (function() {
2222
'<div style="position: relative">' + // Set to the height of the text, causes scrolling
2323
'<pre style="position: absolute; visibility: hidden">' + // To measure line/char size
2424
'<span>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</span></pre>' +
25-
'<div style="position: absolute">' + // Moved around its parent to cover visible view
25+
'<div style="position: relative">' + // Moved around its parent to cover visible view
2626
'<div class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div>' +
2727
'<div style="overflow: hidden; position: absolute; width: 0; left: 0">' + // Wraps and hides input textarea
2828
'<textarea style="height: 1px; position: absolute;" wrap="off"></textarea></div>' +
@@ -134,6 +134,7 @@ var CodeMirror = (function() {
134134
markText: operation(function(a, b, c){return operation(markText(a, b, c));}),
135135
setMarker: addGutterMarker,
136136
clearMarker: removeGutterMarker,
137+
setLineClass: operation(setLineClass),
137138
lineInfo: lineInfo,
138139
addWidget: function(pos, node, scroll) {
139140
var pos = localCoords(clipPos(pos), true);
@@ -728,6 +729,7 @@ var CodeMirror = (function() {
728729
}
729730
else {
730731
node.innerHTML = lines[j].getHTML(ch1, ch2, false);
732+
node.className = lines[j].className || "";
731733
node = node.nextSibling;
732734
}
733735
}
@@ -957,6 +959,20 @@ var CodeMirror = (function() {
957959
line.gutterMarker = null;
958960
updateGutter();
959961
}
962+
function setLineClass(line, className) {
963+
if (typeof line == "number") {
964+
var no = line;
965+
line = lines[clipLine(line)];
966+
}
967+
else {
968+
var no = indexOf(lines, line);
969+
if (no == -1) return null;
970+
}
971+
line.className = className;
972+
changes.push({from: no, to: no + 1});
973+
return line;
974+
}
975+
960976
function lineInfo(line) {
961977
if (typeof line == "number") {
962978
var n = line;
@@ -1459,7 +1475,7 @@ var CodeMirror = (function() {
14591475
this.styles = styles || [text, null];
14601476
this.stateAfter = null;
14611477
this.text = text;
1462-
this.marked = null; this.gutterMarker = null;
1478+
this.marked = this.gutterMarker = this.className = null;
14631479
}
14641480
Line.prototype = {
14651481
// Replace a piece of a line, keeping the styles around it intact.
@@ -1543,7 +1559,9 @@ var CodeMirror = (function() {
15431559
// Produces an HTML fragment for the line, taking selection,
15441560
// marking, and highlighting into account.
15451561
getHTML: function(sfrom, sto, includePre) {
1546-
var html = includePre ? ["<pre>"] : [];
1562+
var html = [];
1563+
if (includePre)
1564+
html.push(this.className ? '<pre class="' + this.className + '">': "<pre>");
15471565
function span(text, style) {
15481566
if (!text) return;
15491567
if (style) html.push('<span class="', style, '">', htmlEscape(text), "</span>");

manual.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,13 +441,17 @@ <h2 id="api">Programming API</h2>
441441
If this is not what you want, you can include the
442442
string <code>%N%</code> in the text, which will be replaced by
443443
the line number.</dd>
444-
445444
<dt id="clearMarker"><code>clearMarker(line)</code></dt>
446445
<dd>Clears a marker created
447446
with <code>setMarker</code>. <code>line</code> can be either a
448447
number or a handle returned by <code>setMarker</code> (since a
449448
number may now refer to a different line if something was added
450449
or deleted).</dd>
450+
<dt id="setLineClass"><code>setLineClass(line, className) → lineHandle</code></dt>
451+
<dd>Set a CSS class name for the given line. <code>line</code>
452+
can be a number or a line handle (as returned
453+
by <code>setMarker</code> or this function).
454+
Pass <code>null</code> to clear the class for a line.</dd>
451455

452456
<dt id="lineInfo"><code>lineInfo(line) → object</code></dt>
453457
<dd>Returns the line number, text content, and marker status of

0 commit comments

Comments
 (0)