Skip to content

Commit

Permalink
Fixed folding while moving #62
Browse files Browse the repository at this point in the history
  • Loading branch information
vslinko committed Apr 10, 2021
1 parent e9c539b commit 9f9c591
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
21 changes: 20 additions & 1 deletion src/list_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ export class ListUtils {
line,
indentSign
);
const folded = (editor as any).isFolded({
line: l,
ch: 0,
});

if (indentLevel === currentLevel.getLevel() + 1) {
currentLevel = lastList;
Expand All @@ -87,7 +91,7 @@ export class ListUtils {
return null;
}

const list = new List(indentSign, bullet, content);
const list = new List(indentSign, bullet, content, folded);
currentLevel.addAfterAll(list);
lastList = list;
}
Expand All @@ -102,6 +106,13 @@ export class ListUtils {
);
const newString = root.print();

const fromLine = root.getListStartPosition().line;
const toLine = root.getListEndPosition().line;

for (let l = fromLine; l <= toLine; l++) {
(editor as any).foldCode(l, null, "unfold");
}

const diff = diffLines(oldString, newString);
let l = root.getListStartPosition().line;
for (const change of diff) {
Expand All @@ -128,6 +139,14 @@ export class ListUtils {
if (oldCursor.line != newCursor.line || oldCursor.ch != newCursor.ch) {
editor.setCursor(newCursor);
}

for (let l = fromLine; l <= toLine; l++) {
const line = root.getListUnderLine(l);
if (line.isFoldRoot()) {
// TODO: why working only with -1?
(editor as any).foldCode(l - 1);
}
}
}

detectListIndentSign(editor: CodeMirror.Editor, cursor: CodeMirror.Position) {
Expand Down
27 changes: 25 additions & 2 deletions src/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,40 @@ export class List implements IList {
private indentSign: string;
private bullet: string;
private content: string;
private folded: boolean;
private children: List[];
private parent: List;

constructor(indentSign: string, bullet: string, content: string) {
constructor(
indentSign: string,
bullet: string,
content: string,
folded: boolean
) {
this.indentSign = indentSign;
this.bullet = bullet;
this.content = content;
this.folded = folded;
this.children = [];
this.parent = null;
}

isFolded() {
return this.folded;
}

isFoldRoot() {
let parent = this.getParent();
while (parent) {
if (parent.isFolded()) {
return false;
}
parent = parent.getParent();
}

return this.isFolded();
}

getChildren() {
return this.children.concat();
}
Expand Down Expand Up @@ -133,7 +156,7 @@ export class Root implements IList {
this.start = start;
this.end = end;
this.cursor = cursor;
this.rootList = new List("", "", "");
this.rootList = new List("", "", "", false);
}

replaceCursor(cursor: CodeMirror.Position) {
Expand Down

0 comments on commit 9f9c591

Please sign in to comment.