Skip to content

Commit cf7aed9

Browse files
committed
Rename coordsFromIndex to posFromIndex, add indexFromPos
1 parent 8654204 commit cf7aed9

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

lib/codemirror.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ var CodeMirror = (function() {
250250
moveV: operation(moveV),
251251
toggleOverwrite: function() {overwrite = !overwrite;},
252252

253-
coordsFromIndex: function(off) {
253+
posFromIndex: function(off) {
254254
var lineNo = 0, ch;
255255
doc.iter(0, doc.size, function(line) {
256256
var sz = line.text.length + 1;
@@ -260,6 +260,14 @@ var CodeMirror = (function() {
260260
});
261261
return clipPos({line: lineNo, ch: ch});
262262
},
263+
indexFromPos: function (coords) {
264+
if (coords.line < 0 || coords.ch < 0) return 0;
265+
var index = coords.ch;
266+
doc.iter(0, coords.line, function (line) {
267+
index += line.text.length + 1;
268+
});
269+
return index;
270+
},
263271

264272
operation: function(f){return operation(f)();},
265273
refresh: function(){updateDisplay(true);},

manual.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,12 +569,14 @@ <h2 id="api">Programming API</h2>
569569
objects. <code>to</code> can be left off to simply insert the
570570
string at position <code>from</code>.</dd>
571571

572-
<dt id="coordsFromIndex"><code>coordsFromIndex(index) → object</code></dt>
572+
<dt id="posFromIndex"><code>posFromIndex(index) → object</code></dt>
573573
<dd>Calculates and returns a <code>{line, ch}</code> object for a
574574
zero-based <code>index</code> who's value is relative to the start of the
575575
editor's text. If the <code>index</code> is out of range of the text then
576576
the returned object is clipped to start or end of the text
577577
respectively.</dd>
578+
<dt id="indexFromPos"><code>indexFromPos(object) → number</code></dt>
579+
<dd>The reverse of <a href="#posFromIndex"><code>posFromIndex</code></a>.</dd>
578580
</dl>
579581

580582
<p>The following are more low-level methods:</p>

test/test.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ test("defaults", function() {
9494
eq(defs.indentUnit, 5);
9595
eq(cm.getValue(), "uu");
9696
eq(cm.getOption("enterMode"), "keep");
97-
eq(cm.getInputField().tabindex, 55);
97+
eq(cm.getInputField().tabIndex, 55);
9898
}
9999
finally {
100100
CodeMirror.defaults = olddefaults;
@@ -147,7 +147,7 @@ testCM("coordsChar", function(cm) {
147147
}
148148
});
149149

150-
testCM("coordsFromIndex", function(cm) {
150+
testCM("posFromIndex", function(cm) {
151151
cm.setValue(
152152
"This function should\n" +
153153
"convert a zero based index\n" +
@@ -166,9 +166,11 @@ testCM("coordsFromIndex", function(cm) {
166166

167167
for (var i = 0; i < examples.length; i++) {
168168
var example = examples[i];
169-
var pos = cm.coordsFromIndex(example.index);
169+
var pos = cm.posFromIndex(example.index);
170170
eq(pos.line, example.line);
171171
eq(pos.ch, example.ch);
172+
if (example.index >= 0 && example.index < 64)
173+
eq(cm.indexFromPos(pos), example.index);
172174
}
173175
});
174176

0 commit comments

Comments
 (0)