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
Changes from 1 commit
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