Skip to content

Commit

Permalink
Regions are not folding correctly in off-side languages. Fixes #35091
Browse files Browse the repository at this point in the history
  • Loading branch information
aeschli committed Sep 28, 2017
1 parent 93dfa35 commit 2295b67
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
23 changes: 10 additions & 13 deletions src/vs/editor/common/model/indentRanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class IndentRange {
}
}

interface PreviousRegion { indent: number; line: number; marker: RegExp; };
interface PreviousRegion { indent: number; line: number; marker: boolean; };

export function computeRanges(model: ITextModel, offSide: boolean, markers?: FoldingMarkers, minimumRangeSize: number = 1): IndentRange[] {

Expand All @@ -44,13 +44,13 @@ export function computeRanges(model: ITextModel, offSide: boolean, markers?: Fol
}

let previousRegions: PreviousRegion[] = [];
previousRegions.push({ indent: -1, line: model.getLineCount() + 1, marker: null }); // sentinel, to make sure there's at least one entry
previousRegions.push({ indent: -1, line: model.getLineCount() + 1, marker: false }); // sentinel, to make sure there's at least one entry

for (let line = model.getLineCount(); line > 0; line--) {
let indent = model.getIndentLevel(line);
let previous = previousRegions[previousRegions.length - 1];
if (indent === -1) {
if (offSide) {
if (offSide && !previous.marker) {
// for offSide languages, empty lines are associated to the next block
previous.line = line;
}
Expand All @@ -60,23 +60,20 @@ export function computeRanges(model: ITextModel, offSide: boolean, markers?: Fol
if (pattern && (m = model.getLineContent(line).match(pattern))) {
// folding pattern match
if (m[1]) { // start pattern match
if (previous.indent >= 0 && !previous.marker) {

// discard all regions until the folding pattern
do {
previousRegions.pop();
previous = previousRegions[previousRegions.length - 1];
} while (previous.indent >= 0 && !previous.marker);
// discard all regions until the folding pattern
while (previous.indent >= 0 && !previous.marker) {
previousRegions.pop();
previous = previousRegions[previousRegions.length - 1];
}
if (previous.marker) {
// new folding range from pattern, includes the end line
result.push(new IndentRange(line, previous.line, indent, true));
previous.marker = null;
previous.marker = false;
previous.indent = indent;
previous.line = line;
}
} else { // end pattern match
previousRegions.push({ indent: -2, line, marker: pattern });
previousRegions.push({ indent: -2, line, marker: true });
}
} else {
if (previous.indent > indent) {
Expand All @@ -96,7 +93,7 @@ export function computeRanges(model: ITextModel, offSide: boolean, markers?: Fol
previous.line = line;
} else { // previous.indent < indent
// new region with a bigger indent
previousRegions.push({ indent, line, marker: null });
previousRegions.push({ indent, line, marker: false });
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions src/vs/editor/test/common/model/indentRanges.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,25 @@ suite('Folding with regions', () => {
/* 6*/ ' return;',
], [r(1, 4, 0, true), r(5, 6, 2)], false, markers);
});
test('With off-side', () => {
assertRanges([
/* 1*/ '#region',
/* 2*/ ' ',
/* 3*/ '',
/* 4*/ '#endregion',
/* 5*/ '',
], [r(1, 4, 0, true)], true, markers);
});
test('Nested with off-side', () => {
assertRanges([
/* 1*/ '#region',
/* 2*/ ' ',
/* 3*/ '#region',
/* 4*/ '',
/* 5*/ '#endregion',
/* 6*/ '',
/* 7*/ '#endregion',
/* 8*/ '',
], [r(1, 7, 0, true), r(3, 5, 0, true)], true, markers);
});
});

0 comments on commit 2295b67

Please sign in to comment.