Skip to content

Commit d7bfadd

Browse files
webzwo0irhansen
authored andcommitted
add some documentation for textLinesMutator
1 parent 1354f6c commit d7bfadd

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

Diff for: src/static/js/Changeset.js

+34-1
Original file line numberDiff line numberDiff line change
@@ -550,22 +550,37 @@ exports.textLinesMutator = (lines) => {
550550
// is not actually a newline, but for the purposes of N and L values,
551551
// the caller should pretend it is, and for things to work right in that case, the input
552552
// to insert() should be a single line with no newlines.
553+
554+
// The splice holds information which lines are to be deleted or changed.
555+
// curSplice[0] is an index into the lines array
556+
// curSplice[1] is the number of lines that will be removed from lines
557+
// the other elements represent mutated (changed by ops) lines or new lines (added by ops)
553558
const curSplice = [0, 0];
554559
let inSplice = false;
555-
// position in document after curSplice is applied:
560+
561+
// position in lines after curSplice is applied:
556562
let curLine = 0;
557563
let curCol = 0;
558564
// invariant: if (inSplice) then (curLine is in curSplice[0] + curSplice.length - {2,3}) &&
559565
// curLine >= curSplice[0]
560566
// invariant: if (inSplice && (curLine >= curSplice[0] + curSplice.length - 2)) then
561567
// curCol == 0
562568

569+
/**
570+
* Adds and/or removes entries at a specific offset in lines array
571+
* It is called when leaving the splice
572+
* @param {Array} s curSplice
573+
*/
563574
const lines_applySplice = (s) => {
564575
lines.splice.apply(lines, s);
565576
};
566577

567578
const lines_toSource = () => lines.toSource();
568579

580+
/**
581+
* Get a line from lines at given index
582+
* @param {Number} idx an index
583+
*/
569584
const lines_get = (idx) => {
570585
if (lines.get) {
571586
return lines.get(idx);
@@ -575,6 +590,11 @@ exports.textLinesMutator = (lines) => {
575590
};
576591
// can be unimplemented if removeLines's return value not needed
577592

593+
/**
594+
* Return a slice from lines array
595+
* @param {Number} start the start index
596+
* @param {Number} end the end index
597+
*/
578598
const lines_slice = (start, end) => {
579599
if (lines.slice) {
580600
return lines.slice(start, end);
@@ -583,6 +603,9 @@ exports.textLinesMutator = (lines) => {
583603
}
584604
};
585605

606+
/**
607+
* Return the length of lines array
608+
*/
586609
const lines_length = () => {
587610
if ((typeof lines.length) === 'number') {
588611
return lines.length;
@@ -591,15 +614,25 @@ exports.textLinesMutator = (lines) => {
591614
}
592615
};
593616

617+
/**
618+
* Starts a new splice.
619+
*/
594620
const enterSplice = () => {
595621
curSplice[0] = curLine;
596622
curSplice[1] = 0;
623+
// TODO(doc) when is this the case?
624+
// check all enterSplice calls and changes to curCol
597625
if (curCol > 0) {
598626
putCurLineInSplice();
599627
}
600628
inSplice = true;
601629
};
602630

631+
/**
632+
* Changes the lines array according to the values in curSplice
633+
* and resets curSplice.
634+
* This is called via close or TODO(doc)
635+
*/
603636
const leaveSplice = () => {
604637
lines_applySplice(curSplice);
605638
curSplice.length = 2;

0 commit comments

Comments
 (0)