Skip to content

Commit 0c6aef8

Browse files
committed
add some documentation for textLinesMutator
1 parent e8814a9 commit 0c6aef8

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

src/static/js/Changeset.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,16 +581,27 @@ exports.textLinesMutator = function (lines) {
581581
// is not actually a newline, but for the purposes of N and L values,
582582
// the caller should pretend it is, and for things to work right in that case, the input
583583
// to insert() should be a single line with no newlines.
584+
585+
// The splice holds information which lines are to be deleted or changed.
586+
// curSplice[0] is an index into the lines array
587+
// curSplice[1] is the number of lines that will be removed from lines
588+
// the other elements represent mutated (changed by ops) lines or new lines (added by ops)
584589
var curSplice = [0, 0];
585590
var inSplice = false;
586-
// position in document after curSplice is applied:
591+
592+
// position in lines after curSplice is applied:
587593
var curLine = 0,
588594
curCol = 0;
589595
// invariant: if (inSplice) then (curLine is in curSplice[0] + curSplice.length - {2,3}) &&
590596
// curLine >= curSplice[0]
591597
// invariant: if (inSplice && (curLine >= curSplice[0] + curSplice.length - 2)) then
592598
// curCol == 0
593599

600+
/**
601+
* Adds and/or removes entries at a specific offset in lines array
602+
* It is called when leaving the splice
603+
* @param {Array} s curSplice
604+
*/
594605
function lines_applySplice(s) {
595606
lines.splice.apply(lines, s);
596607
}
@@ -599,6 +610,10 @@ exports.textLinesMutator = function (lines) {
599610
return lines.toSource();
600611
}
601612

613+
/**
614+
* Get a line from lines at given index
615+
* @param {Number} idx an index
616+
*/
602617
function lines_get(idx) {
603618
if (lines.get) {
604619
return lines.get(idx);
@@ -608,6 +623,11 @@ exports.textLinesMutator = function (lines) {
608623
}
609624
// can be unimplemented if removeLines's return value not needed
610625

626+
/**
627+
* Return a slice from lines array
628+
* @param {Number} start the start index
629+
* @param {Number} end the end index
630+
*/
611631
function lines_slice(start, end) {
612632
if (lines.slice) {
613633
return lines.slice(start, end);
@@ -616,6 +636,9 @@ exports.textLinesMutator = function (lines) {
616636
}
617637
}
618638

639+
/**
640+
* Return the length of lines array
641+
*/
619642
function lines_length() {
620643
if ((typeof lines.length) == "number") {
621644
return lines.length;
@@ -624,15 +647,25 @@ exports.textLinesMutator = function (lines) {
624647
}
625648
}
626649

650+
/**
651+
* Starts a new splice.
652+
*/
627653
function enterSplice() {
628654
curSplice[0] = curLine;
629655
curSplice[1] = 0;
656+
//TODO(doc) when is this the case?
657+
// check all enterSplice calls and changes to curCol
630658
if (curCol > 0) {
631659
putCurLineInSplice();
632660
}
633661
inSplice = true;
634662
}
635663

664+
/**
665+
* Changes the lines array according to the values in curSplice
666+
* and resets curSplice.
667+
* This is called via close or TODO(doc)
668+
*/
636669
function leaveSplice() {
637670
lines_applySplice(curSplice);
638671
curSplice.length = 2;

0 commit comments

Comments
 (0)