Repository navigation
Expand file tree
/
Copy pathtext-cursor.js
More file actions
64 lines (57 loc) · 1.84 KB
/
Copy pathtext-cursor.js
File metadata and controls
64 lines (57 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
module.exports = TextCursor;
function TextCursor (code) {
this.lines = [null].concat(code.split('\n'));
this.cursor = {
line: 1,
column: 0
};
}
TextCursor.prototype.stepTo = function (line, column) {
if (line === 'end') {
line = this.lines.length - 1;
}
if (!column && typeof line === 'object') {
column = line.column;
line = line.line;
}
var output = '';
var currentSlice;
//If we're going to a different line, grab the remainder of all
//preceeding lines
while (this.cursor.line < line) {
currentSlice = this.lines[this.cursor.line].slice(this.cursor.column);
//if (currentSlice.length) output += currentSlice + '\n';
output += currentSlice + '\n';
this.cursor.line++;
this.cursor.column = 0;
}
//Grab from where we are on the line we're jumping to, to the desired
//column
currentSlice = this.lines[this.cursor.line].slice(this.cursor.column, column);
if (currentSlice.length) output += currentSlice;
// slice = lines[cursor.line].slice(cursor.column, pos.column);
// if (slice.length) output += slice;
if (this.lines[this.cursor.line].length <= column) output += '\n';
this.cursor.column = column;
return output;
};
//var cursor = {
// line: 1,
// column: 0
//};
//
//var piecesToPosition = function (pos) {
// var output = '';
// var slice;
// while(cursor.line < pos.line) {
// slice = lines[cursor.line].slice(cursor.column);
// if (slice.length) output += slice + '\n';
// cursor.line++;
// cursor.column = 0;
// }
// slice = lines[cursor.line].slice(cursor.column, pos.column);
// if (slice.length) output += slice;
// if (lines[cursor.line].length === pos.column) output += '\n';
// cursor.column = pos.column;
// return output;
//};