Skip to content

(GH-1417) Fix code folding on CRLF documents #1418

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

Merged
merged 5 commits into from
Jul 11, 2018
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
21 changes: 19 additions & 2 deletions src/features/Folding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as vscode from "vscode";
import {
DocumentSelector,
LanguageClient,
Position,
} from "vscode-languageclient";
import { IFeature } from "../feature";
import { ILogger } from "../logging";
Expand Down Expand Up @@ -193,8 +194,24 @@ export class FoldingProvider implements vscode.FoldingRangeProvider {
// If the grammar hasn't been setup correctly, return empty result
if (this.powershellGrammar == null) { return []; }

// Convert the document text into a series of grammar tokens
const tokens: ITokenList = this.powershellGrammar.tokenizeLine(document.getText(), null).tokens;
// Tokenize each line and build up an array of document-wide tokens
// Note that line endings (CRLF/LF/CR) have interpolation issues so don't
// tokenize an entire document if the line endings are variable.
const tokens: ITokenList = new Array<IToken>();
let tokenizationState = null;
Copy link
Contributor Author

@glennsarti glennsarti Jul 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I create an interface for this...Doesn't seem like I really need to....

for (let i = 0; i < document.lineCount; i++) {
const result = this.powershellGrammar.tokenizeLine(document.lineAt(i).text, tokenizationState);
const offset = document.offsetAt(new vscode.Position(i, 0)) ;

for (const item of result.tokens) {
// Add the offset of the line to translate a character offset into
// a document based index
item.startIndex += offset;
item.endIndex += offset;
tokens.push(item);
}
tokenizationState = result.ruleStack;
}

// Parse the token list looking for matching tokens and return
// a list of LineNumberRange objects. Then filter the list and only return matches
Expand Down
55 changes: 38 additions & 17 deletions test/features/folding.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import { MockLogger } from "../test_utils";
const fixturePath = path.join(__dirname, "..", "..", "..", "test", "fixtures");

function assertFoldingRegions(result, expected): void {
assert.equal(result.length, expected.length);

for (let i = 0; i < expected.length; i++) {
const failMessage = `expected ${JSON.stringify(expected[i])}, actual ${JSON.stringify(result[i])}`;
assert.equal(result[i].start, expected[i].start, failMessage);
assert.equal(result[i].end, expected[i].end, failMessage);
assert.equal(result[i].kind, expected[i].kind, failMessage);
}

assert.equal(result.length, expected.length);
}

suite("Features", () => {
Expand All @@ -36,26 +36,47 @@ suite("Features", () => {
assert.notEqual(psGrammar, null);
});

test("Can detect all of the foldable regions in a document", async () => {
// Integration test against the test fixture 'folding.ps1' that contains
// all of the different types of folding available
const uri = vscode.Uri.file(path.join(fixturePath, "folding.ps1"));
const document = await vscode.workspace.openTextDocument(uri);
const result = await provider.provideFoldingRanges(document, null, null);

const expected = [
suite("For a single document", async () => {
const expectedFoldingRegions = [
{ start: 1, end: 6, kind: 1 },
{ start: 7, end: 46, kind: 3 },
{ start: 7, end: 51, kind: 3 },
{ start: 8, end: 13, kind: 1 },
{ start: 14, end: 17, kind: 3 },
{ start: 21, end: 23, kind: 1 },
{ start: 25, end: 35, kind: 3 },
{ start: 27, end: 31, kind: 3 },
{ start: 37, end: 39, kind: 3 },
{ start: 42, end: 45, kind: 3 },
{ start: 19, end: 22, kind: 3 },
{ start: 26, end: 28, kind: 1 },
{ start: 30, end: 40, kind: 3 },
{ start: 32, end: 36, kind: 3 },
{ start: 42, end: 44, kind: 3 },
{ start: 47, end: 50, kind: 3 },
];

assertFoldingRegions(result, expected);
test("Can detect all of the foldable regions in a document with CRLF line endings", async () => {
// Integration test against the test fixture 'folding-crlf.ps1' that contains
// all of the different types of folding available
const uri = vscode.Uri.file(path.join(fixturePath, "folding-crlf.ps1"));
const document = await vscode.workspace.openTextDocument(uri);
const result = await provider.provideFoldingRanges(document, null, null);

// Ensure we have CRLF line endings as we're depending on git
// to clone the test fixtures correctly
assert.notEqual(document.getText().indexOf("\r\n"), -1);

assertFoldingRegions(result, expectedFoldingRegions);
});

test("Can detect all of the foldable regions in a document with LF line endings", async () => {
// Integration test against the test fixture 'folding-lf.ps1' that contains
// all of the different types of folding available
const uri = vscode.Uri.file(path.join(fixturePath, "folding-lf.ps1"));
const document = await vscode.workspace.openTextDocument(uri);
const result = await provider.provideFoldingRanges(document, null, null);

// Ensure we do not CRLF line endings as we're depending on git
// to clone the test fixtures correctly
assert.equal(document.getText().indexOf("\r\n"), -1);

assertFoldingRegions(result, expectedFoldingRegions);
});
});
});
});
8 changes: 8 additions & 0 deletions test/fixtures/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Set the default behavior, in case people don't have core.autocrlf set.
* text=auto

# These test fixtures require crlf
folding-crlf.ps1 text eol=crlf

# These test fixtures require lf
folding-lf.ps1 text eol=lf
52 changes: 52 additions & 0 deletions test/fixtures/folding-crlf.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
function short-func-not-fold {};
<#
.SYNOPSIS
This whole comment block should fold, not just the SYNOPSIS
.EXAMPLE
This whole comment block should fold, not just the EXAMPLE
#>
function New-VSCodeShouldFold {
<#
.SYNOPSIS
This whole comment block should fold, not just the SYNOPSIS
.EXAMPLE
This whole comment block should fold, not just the EXAMPLE
#>
$I = @'
herestrings should fold

'@

$I = @"
double quoted herestrings should also fold

"@

# this won't be folded

# This block of comments should be foldable as a single block
# This block of comments should be foldable as a single block
# This block of comments should be foldable as a single block

#region This fools the indentation folding.
Write-Host "Hello"
# region Nested regions should be foldable
Write-Host "Hello"
# comment1
Write-Host "Hello"
#endregion
Write-Host "Hello"
# comment2
Write-Host "Hello"
# endregion

$c = {
Write-Host "Script blocks should be foldable"
}

# Array fools indentation folding
$d = @(
'should fold1',
'should fold2'
)
}
52 changes: 52 additions & 0 deletions test/fixtures/folding-lf.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
function short-func-not-fold {};
<#
.SYNOPSIS
This whole comment block should fold, not just the SYNOPSIS
.EXAMPLE
This whole comment block should fold, not just the EXAMPLE
#>
function New-VSCodeShouldFold {
<#
.SYNOPSIS
This whole comment block should fold, not just the SYNOPSIS
.EXAMPLE
This whole comment block should fold, not just the EXAMPLE
#>
$I = @'
herestrings should fold

'@

$I = @"
double quoted herestrings should also fold

"@

# this won't be folded

# This block of comments should be foldable as a single block
# This block of comments should be foldable as a single block
# This block of comments should be foldable as a single block

#region This fools the indentation folding.
Write-Host "Hello"
# region Nested regions should be foldable
Write-Host "Hello"
# comment1
Write-Host "Hello"
#endregion
Write-Host "Hello"
# comment2
Write-Host "Hello"
# endregion

$c = {
Write-Host "Script blocks should be foldable"
}

# Array fools indentation folding
$d = @(
'should fold1',
'should fold2'
)
}
47 changes: 0 additions & 47 deletions test/fixtures/folding.ps1

This file was deleted.