@@ -550,22 +550,37 @@ exports.textLinesMutator = (lines) => {
550
550
// is not actually a newline, but for the purposes of N and L values,
551
551
// the caller should pretend it is, and for things to work right in that case, the input
552
552
// 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)
553
558
const curSplice = [ 0 , 0 ] ;
554
559
let inSplice = false ;
555
- // position in document after curSplice is applied:
560
+
561
+ // position in lines after curSplice is applied:
556
562
let curLine = 0 ;
557
563
let curCol = 0 ;
558
564
// invariant: if (inSplice) then (curLine is in curSplice[0] + curSplice.length - {2,3}) &&
559
565
// curLine >= curSplice[0]
560
566
// invariant: if (inSplice && (curLine >= curSplice[0] + curSplice.length - 2)) then
561
567
// curCol == 0
562
568
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
+ */
563
574
const lines_applySplice = ( s ) => {
564
575
lines . splice . apply ( lines , s ) ;
565
576
} ;
566
577
567
578
const lines_toSource = ( ) => lines . toSource ( ) ;
568
579
580
+ /**
581
+ * Get a line from lines at given index
582
+ * @param {Number } idx an index
583
+ */
569
584
const lines_get = ( idx ) => {
570
585
if ( lines . get ) {
571
586
return lines . get ( idx ) ;
@@ -575,6 +590,11 @@ exports.textLinesMutator = (lines) => {
575
590
} ;
576
591
// can be unimplemented if removeLines's return value not needed
577
592
593
+ /**
594
+ * Return a slice from lines array
595
+ * @param {Number } start the start index
596
+ * @param {Number } end the end index
597
+ */
578
598
const lines_slice = ( start , end ) => {
579
599
if ( lines . slice ) {
580
600
return lines . slice ( start , end ) ;
@@ -583,6 +603,9 @@ exports.textLinesMutator = (lines) => {
583
603
}
584
604
} ;
585
605
606
+ /**
607
+ * Return the length of lines array
608
+ */
586
609
const lines_length = ( ) => {
587
610
if ( ( typeof lines . length ) === 'number' ) {
588
611
return lines . length ;
@@ -591,15 +614,25 @@ exports.textLinesMutator = (lines) => {
591
614
}
592
615
} ;
593
616
617
+ /**
618
+ * Starts a new splice.
619
+ */
594
620
const enterSplice = ( ) => {
595
621
curSplice [ 0 ] = curLine ;
596
622
curSplice [ 1 ] = 0 ;
623
+ // TODO(doc) when is this the case?
624
+ // check all enterSplice calls and changes to curCol
597
625
if ( curCol > 0 ) {
598
626
putCurLineInSplice ( ) ;
599
627
}
600
628
inSplice = true ;
601
629
} ;
602
630
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
+ */
603
636
const leaveSplice = ( ) => {
604
637
lines_applySplice ( curSplice ) ;
605
638
curSplice . length = 2 ;
0 commit comments