Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(GH-1523) Remove duplicate/overlapping folding regions #1525

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/features/Folding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ export class FoldingProvider implements vscode.FoldingRangeProvider {
// Sort the list of matched tokens, starting at the top of the document,
// and ensure that, in the case of multiple ranges starting the same line,
// that the largest range (i.e. most number of lines spanned) is sorted
// first. This is needed as vscode will just ignore any duplicate folding
// ranges.
// first. This is needed to detect duplicate regions. The first in the list
// will be used and subsequent duplicates ignored.
foldableRegions.sort((a: LineNumberRange, b: LineNumberRange) => {
// Initially look at the start line
if (a.startline > b.startline) { return 1; }
Expand All @@ -252,11 +252,13 @@ export class FoldingProvider implements vscode.FoldingRangeProvider {
return 0;
});

// Convert the matched token list into a FoldingRange[]
const foldingRanges = [];
foldableRegions.forEach((item) => { foldingRanges.push(item.toFoldingRange()); });

return foldingRanges;
return foldableRegions
// It's possible to have duplicate or overlapping ranges, that is, regions which have the same starting
// line number as the previous region. Therefore only emit ranges which have a different starting line
// than the previous range.
.filter((item, index, src) => index === 0 || item.startline !== src[index - 1].startline)
// Convert the internal representation into the VSCode expected type
.map((item) => item.toFoldingRange());
}

/**
Expand Down
15 changes: 15 additions & 0 deletions test/features/folding.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,21 @@ suite("Features", () => {

assertFoldingRegions(result, expectedMismatchedFoldingRegions);
});

test("Does not return duplicate or overlapping regions", async () => {
const expectedMismatchedFoldingRegions = [
{ start: 1, end: 2, kind: null },
{ start: 2, end: 4, kind: null },
];

// Integration test against the test fixture 'folding-mismatch.ps1' that contains
// duplicate/overlapping ranges due to the `(` and `{` characters
const uri = vscode.Uri.file(path.join(fixturePath, "folding-duplicate.ps1"));
const document = await vscode.workspace.openTextDocument(uri);
const result = await provider.provideFoldingRanges(document, null, null);

assertFoldingRegions(result, expectedMismatchedFoldingRegions);
});
});
});
});
5 changes: 5 additions & 0 deletions test/fixtures/folding-duplicate.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# This script causes duplicate/overlapping ranges due to the `(` and `{` characters
$AnArray = @(Get-ChildItem -Path C:\ -Include *.ps1 -File).Where({
$_.FullName -ne 'foo'}).ForEach({
# Do Something
})