Skip to content

Commit 7b0dd47

Browse files
webzwo0irhansen
authored andcommitted
add more documentation
1 parent e7a1df0 commit 7b0dd47

File tree

1 file changed

+64
-1
lines changed

1 file changed

+64
-1
lines changed

src/static/js/Changeset.js

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,26 +640,45 @@ exports.textLinesMutator = (lines) => {
640640
inSplice = false;
641641
};
642642

643+
/**
644+
* Indicates if curLine is already in the splice. This is necessary because the last element
645+
* in curSplice is curLine when this line is currently worked on (e.g. when skipping are inserting)
646+
*
647+
* TODO(doc) why aren't removals considered?
648+
* @returns {Boolean} true if curLine is in splice
649+
*/
643650
const isCurLineInSplice = () => (curLine - curSplice[0] < (curSplice.length - 2));
644651

645652
const debugPrint = (typ) => { /* eslint-disable-line no-unused-vars */
646653
print(`${typ}: ${curSplice.toSource()} / ${curLine},${curCol} / ${lines_toSource()}`);
647654
};
648655

656+
/**
657+
* Incorporates current line into the splice
658+
* and marks its old position to be deleted.
659+
*
660+
* @returns {Number} the index of the added line in curSplice
661+
*/
649662
const putCurLineInSplice = () => {
650663
if (!isCurLineInSplice()) {
651664
curSplice.push(lines_get(curSplice[0] + curSplice[1]));
652665
curSplice[1]++;
653666
}
654-
return 2 + curLine - curSplice[0];
667+
return 2 + curLine - curSplice[0]; // TODO should be the same as curSplice.length - 1
655668
};
656669

670+
/**
671+
* It will skip some newlines by putting them into the splice.
672+
*
673+
* @param {Boolean} includeInSplice indicates if attributes are present
674+
*/
657675
const skipLines = (L, includeInSplice) => {
658676
if (L) {
659677
if (includeInSplice) {
660678
if (!inSplice) {
661679
enterSplice();
662680
}
681+
// TODO(doc) should this count the number of characters that are skipped to check?
663682
for (let i = 0; i < L; i++) {
664683
curCol = 0;
665684
putCurLineInSplice();
@@ -668,6 +687,7 @@ exports.textLinesMutator = (lines) => {
668687
} else {
669688
if (inSplice) {
670689
if (L > 1) {
690+
// TODO(doc) figure out why single lines are incorporated into splice instead of ignored
671691
leaveSplice();
672692
} else {
673693
putCurLineInSplice();
@@ -680,6 +700,13 @@ exports.textLinesMutator = (lines) => {
680700
}
681701
};
682702

703+
/**
704+
* Skip some characters. Can contain newlines.
705+
*
706+
* @param {Number} N number of characters to skip
707+
* @param {Number} L number of newlines to skip
708+
* @param {Boolean} includeInSplice indicates if attributes are present
709+
*/
683710
const skip = (N, L, includeInSplice) => {
684711
if (N) {
685712
if (L) {
@@ -689,20 +716,33 @@ exports.textLinesMutator = (lines) => {
689716
enterSplice();
690717
}
691718
if (inSplice) {
719+
// although the line is put into splice curLine is not increased, because
720+
// only some chars are skipped, not the whole line
692721
putCurLineInSplice();
693722
}
694723
curCol += N;
695724
}
696725
}
697726
};
698727

728+
/**
729+
* Remove whole lines from lines array
730+
*
731+
* @param {Number} L number of lines to be removed
732+
*/
699733
const removeLines = (L) => {
700734
let removed = '';
701735
if (L) {
702736
if (!inSplice) {
703737
enterSplice();
704738
}
705739

740+
/**
741+
* Gets a string of joined lines after the end of the splice
742+
*
743+
* @param k {Number} number of lines
744+
* @returns {String} joined lines
745+
*/
706746
const nextKLinesText = (k) => {
707747
const m = curSplice[0] + curSplice[1];
708748
return lines_slice(m, m + k).join('');
@@ -730,6 +770,12 @@ exports.textLinesMutator = (lines) => {
730770
return removed;
731771
};
732772

773+
/**
774+
* Remove text from lines array
775+
*
776+
* @param N {Number} characters to delete
777+
* @param L {Number} lines to delete
778+
*/
733779
const remove = (N, L) => {
734780
let removed = '';
735781
if (N) {
@@ -739,6 +785,8 @@ exports.textLinesMutator = (lines) => {
739785
if (!inSplice) {
740786
enterSplice();
741787
}
788+
// although the line is put into splice, curLine is not increased, because
789+
// only some chars are removed not the whole line
742790
const sline = putCurLineInSplice();
743791
removed = curSplice[sline].substring(curCol, curCol + N);
744792
curSplice[sline] = curSplice[sline].substring(0, curCol) +
@@ -748,6 +796,12 @@ exports.textLinesMutator = (lines) => {
748796
return removed;
749797
};
750798

799+
/**
800+
* Inserts text into lines array.
801+
*
802+
* @param text {String} the text to insert
803+
* @param L {Number} number of newlines in text
804+
*/
751805
const insert = (text, L) => {
752806
if (text) {
753807
if (!inSplice) {
@@ -782,6 +836,12 @@ exports.textLinesMutator = (lines) => {
782836
}
783837
};
784838

839+
/**
840+
* Checks if curLine (the line we are in when curSplice is applied) is the last line
841+
* in lines.
842+
*
843+
* @return {Boolean} indicates if there are lines left
844+
*/
785845
const hasMore = () => {
786846
let docLines = lines_length();
787847
if (inSplice) {
@@ -790,6 +850,9 @@ exports.textLinesMutator = (lines) => {
790850
return curLine < docLines;
791851
};
792852

853+
/**
854+
* Closes the splice
855+
*/
793856
const close = () => {
794857
if (inSplice) {
795858
leaveSplice();

0 commit comments

Comments
 (0)