From ee32e32bfd57ea48d81d5bbfb2b18aea40960c8d Mon Sep 17 00:00:00 2001 From: ens-rpitcher Date: Wed, 19 Sep 2018 16:54:30 +0100 Subject: [PATCH 1/4] Initial attempt --- CHANGELOG.md | 3 +++ package.json | 5 +++++ src/features/Folding.ts | 6 ++++++ src/settings.ts | 2 ++ 4 files changed, 16 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 27d46c26f7..b6f89e3338 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # vscode-powershell Release History +- [vscode-PowerShell #????](https://github.com/PowerShell/vscode-PowerShell/pull/????) - + New code folding option - 'powershell.codeFolding.showLastLine' - Set true to show the last line of a folded section similar to the default VSCode folding style + ## v1.8.4 ### Friday, August 31, 2018 #### [vscode-powershell](https://github.com/powershell/vscode-powershell) diff --git a/package.json b/package.json index 61f20bacb0..535e77b9a5 100644 --- a/package.json +++ b/package.json @@ -478,6 +478,11 @@ "default": true, "description": "Enables syntax based code folding. When disabled, the default indentation based code folding is used." }, + "powershell.codeFolding.showLastLine": { + "type": "boolean", + "default": false, + "description": "Shows the last line of a folded section similar to the default VSCode folding style. When disabled, the entire folded region is hidden." + }, "powershell.codeFormatting.preset": { "type": "string", "enum": [ diff --git a/src/features/Folding.ts b/src/features/Folding.ts index 1e68f3b33b..9c4bc0fad9 100644 --- a/src/features/Folding.ts +++ b/src/features/Folding.ts @@ -101,10 +101,13 @@ class LineNumberRange { */ public rangeKind: vscode.FoldingRangeKind; + private settings: Settings.ISettings; + constructor( rangeKind: vscode.FoldingRangeKind, ) { this.rangeKind = rangeKind; + this.settings = Settings.load(); } /** @@ -121,6 +124,9 @@ class LineNumberRange { ): LineNumberRange { this.startline = document.positionAt(start.startIndex).line; this.endline = document.positionAt(end.startIndex).line; + if (this.settings.codeFolding && this.settings.codeFolding.showLastLine) { + this.endline-- + } return this; } diff --git a/src/settings.ts b/src/settings.ts index 42fe34f803..c7a87ffef7 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -31,6 +31,7 @@ export interface IBugReportingSettings { export interface ICodeFoldingSettings { enable?: boolean; + showLastLine?: boolean; } export interface ICodeFormattingSettings { @@ -116,6 +117,7 @@ export function load(): ISettings { const defaultCodeFoldingSettings: ICodeFoldingSettings = { enable: true, + showLastLine: false, }; const defaultCodeFormattingSettings: ICodeFormattingSettings = { From f66b7a06bf3784245678d0d579f53cfa3fb56e68 Mon Sep 17 00:00:00 2001 From: ens-rpitcher Date: Wed, 26 Sep 2018 11:07:01 +0100 Subject: [PATCH 2/4] Add missing semicolon --- src/features/Folding.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/features/Folding.ts b/src/features/Folding.ts index 9c4bc0fad9..05e79298e3 100644 --- a/src/features/Folding.ts +++ b/src/features/Folding.ts @@ -125,7 +125,7 @@ class LineNumberRange { this.startline = document.positionAt(start.startIndex).line; this.endline = document.positionAt(end.startIndex).line; if (this.settings.codeFolding && this.settings.codeFolding.showLastLine) { - this.endline-- + this.endline--; } return this; } From e93381acab6f055f997ae029a83e834bcde4fa99 Mon Sep 17 00:00:00 2001 From: ens-rpitcher Date: Wed, 26 Sep 2018 11:12:00 +0100 Subject: [PATCH 3/4] f --- CHANGELOG.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b6f89e3338..27d46c26f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,5 @@ # vscode-powershell Release History -- [vscode-PowerShell #????](https://github.com/PowerShell/vscode-PowerShell/pull/????) - - New code folding option - 'powershell.codeFolding.showLastLine' - Set true to show the last line of a folded section similar to the default VSCode folding style - ## v1.8.4 ### Friday, August 31, 2018 #### [vscode-powershell](https://github.com/powershell/vscode-powershell) From 177e05eed9634538d6d65edc14b38133534d5130 Mon Sep 17 00:00:00 2001 From: Glenn Sarti Date: Fri, 28 Sep 2018 09:32:33 +0800 Subject: [PATCH 4/4] (GH-1514) Show the last line of folding regions as per default VSCode Changes the default to true Adds tests --- package.json | 2 +- src/features/Folding.ts | 22 +++++++----- test/features/folding.test.ts | 66 +++++++++++++++++++++++++---------- 3 files changed, 62 insertions(+), 28 deletions(-) diff --git a/package.json b/package.json index 535e77b9a5..250e8a7ad1 100644 --- a/package.json +++ b/package.json @@ -480,7 +480,7 @@ }, "powershell.codeFolding.showLastLine": { "type": "boolean", - "default": false, + "default": true, "description": "Shows the last line of a folded section similar to the default VSCode folding style. When disabled, the entire folded region is hidden." }, "powershell.codeFormatting.preset": { diff --git a/src/features/Folding.ts b/src/features/Folding.ts index 05e79298e3..894663735e 100644 --- a/src/features/Folding.ts +++ b/src/features/Folding.ts @@ -101,13 +101,10 @@ class LineNumberRange { */ public rangeKind: vscode.FoldingRangeKind; - private settings: Settings.ISettings; - constructor( rangeKind: vscode.FoldingRangeKind, ) { this.rangeKind = rangeKind; - this.settings = Settings.load(); } /** @@ -124,9 +121,6 @@ class LineNumberRange { ): LineNumberRange { this.startline = document.positionAt(start.startIndex).line; this.endline = document.positionAt(end.startIndex).line; - if (this.settings.codeFolding && this.settings.codeFolding.showLastLine) { - this.endline--; - } return this; } @@ -163,8 +157,12 @@ class LineNumberRange { * Creates a vscode.FoldingRange object based on this object * @returns A Folding Range object for use with the Folding Provider */ - public toFoldingRange(): vscode.FoldingRange { - return new vscode.FoldingRange(this.startline, this.endline, this.rangeKind); + public toFoldingRange(settings: Settings.ISettings): vscode.FoldingRange { + if (settings.codeFolding && settings.codeFolding.showLastLine) { + return new vscode.FoldingRange(this.startline, this.endline - 1, this.rangeKind); + } else { + return new vscode.FoldingRange(this.startline, this.endline, this.rangeKind); + } } } @@ -258,15 +256,21 @@ export class FoldingProvider implements vscode.FoldingRangeProvider { return 0; }); + const settings = this.currentSettings(); 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()); + .map((item) => item.toFoldingRange(settings)); } + /** + * A helper to return the current extension settings + */ + public currentSettings(): Settings.ISettings { return Settings.load(); } + /** * Given a start and end textmate scope name, find matching grammar tokens * and pair them together. Uses a simple stack to take into account nested regions. diff --git a/test/features/folding.test.ts b/test/features/folding.test.ts index 8f94f96c65..02c15510ad 100644 --- a/test/features/folding.test.ts +++ b/test/features/folding.test.ts @@ -7,6 +7,7 @@ import * as path from "path"; import * as vscode from "vscode"; import { DocumentSelector } from "vscode-languageclient"; import * as folding from "../../src/features/Folding"; +import * as Settings from "../../src/settings"; import { MockLogger } from "../test_utils"; const fixturePath = path.join(__dirname, "..", "..", "..", "test", "fixtures"); @@ -22,6 +23,13 @@ function assertFoldingRegions(result, expected): void { assert.equal(result.length, expected.length); } +// Wrap the FoldingProvider class with our own custom settings for testing +class CustomSettingFoldingProvider extends folding.FoldingProvider { + public customSettings: Settings.ISettings = Settings.load(); + // Overridde the super currentSettings method with our own custom test settings + public currentSettings(): Settings.ISettings { return this.customSettings; } +} + suite("Features", () => { suite("Folding Provider", async () => { @@ -38,21 +46,21 @@ suite("Features", () => { suite("For a single document", async () => { const expectedFoldingRegions = [ - { start: 0, end: 4, kind: 3 }, - { start: 1, end: 3, kind: 1 }, - { start: 10, end: 15, kind: 1 }, - { start: 16, end: 60, kind: null }, - { start: 17, end: 22, kind: 1 }, - { start: 23, end: 26, kind: null }, - { start: 28, end: 31, kind: null }, - { start: 35, end: 37, kind: 1 }, - { start: 39, end: 49, kind: 3 }, - { start: 41, end: 45, kind: 3 }, - { start: 51, end: 53, kind: null }, - { start: 56, end: 59, kind: null }, - { start: 64, end: 66, kind: 1 }, - { start: 67, end: 72, kind: 3 }, - { start: 68, end: 70, kind: 1 }, + { start: 0, end: 3, kind: 3 }, + { start: 1, end: 2, kind: 1 }, + { start: 10, end: 14, kind: 1 }, + { start: 16, end: 59, kind: null }, + { start: 17, end: 21, kind: 1 }, + { start: 23, end: 25, kind: null }, + { start: 28, end: 30, kind: null }, + { start: 35, end: 36, kind: 1 }, + { start: 39, end: 48, kind: 3 }, + { start: 41, end: 44, kind: 3 }, + { start: 51, end: 52, kind: null }, + { start: 56, end: 58, kind: null }, + { start: 64, end: 65, kind: 1 }, + { start: 67, end: 71, kind: 3 }, + { start: 68, end: 69, kind: 1 }, ]; test("Can detect all of the foldable regions in a document with CRLF line endings", async () => { @@ -83,9 +91,31 @@ suite("Features", () => { assertFoldingRegions(result, expectedFoldingRegions); }); + suite("Where showLastLine setting is false", async () => { + const customprovider = (new CustomSettingFoldingProvider(psGrammar)); + customprovider.customSettings.codeFolding.showLastLine = false; + + test("Can detect all foldable regions in a document", 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 customprovider.provideFoldingRanges(document, null, null); + + // Incrememnt the end line of the expected regions by one as we will + // be hiding the last line + const expectedLastLineRegions = expectedFoldingRegions.map( (item) => { + item.end++; + return item; + }); + + assertFoldingRegions(result, expectedLastLineRegions); + }); + }); + test("Can detect all of the foldable regions in a document with mismatched regions", async () => { const expectedMismatchedFoldingRegions = [ - { start: 2, end: 4, kind: 3 }, + { start: 2, end: 3, kind: 3 }, ]; // Integration test against the test fixture 'folding-mismatch.ps1' that contains @@ -99,8 +129,8 @@ suite("Features", () => { test("Does not return duplicate or overlapping regions", async () => { const expectedMismatchedFoldingRegions = [ - { start: 1, end: 2, kind: null }, - { start: 2, end: 4, kind: null }, + { start: 1, end: 1, kind: null }, + { start: 2, end: 3, kind: null }, ]; // Integration test against the test fixture 'folding-mismatch.ps1' that contains