From 63c3e41f8336b9d402d5b64b09d2c49ef22097aa Mon Sep 17 00:00:00 2001 From: Rudy Peralta Date: Mon, 14 Aug 2023 22:41:02 -0700 Subject: [PATCH 1/6] Added a test for nested lambda. Add more later --- Src/CSharpier.Cli.Tests/CliTests.cs | 16 ++++++++++++++++ Src/CSharpier.Playground/package-lock.json | 6 ++++++ package-lock.json | 6 ++++++ 3 files changed, 28 insertions(+) create mode 100644 Src/CSharpier.Playground/package-lock.json create mode 100644 package-lock.json diff --git a/Src/CSharpier.Cli.Tests/CliTests.cs b/Src/CSharpier.Cli.Tests/CliTests.cs index f3cd7eee1..4505bb353 100644 --- a/Src/CSharpier.Cli.Tests/CliTests.cs +++ b/Src/CSharpier.Cli.Tests/CliTests.cs @@ -163,6 +163,22 @@ public async Task Should_Format_Unicode() result.ExitCode.Should().Be(0); } + [Test] + public async Task Should_Format_Nested_Lambda() + { + var unformattedContent = "var result = outerLambda(innerLambda => { return innerLambda(); });"; + + await this.WriteFileAsync("NestedLambda.cs", unformattedContent); + + var result = await new CsharpierProcess() + .WithArguments("NestedLambda.cs") + .ExecuteAsync(); + + var expectedContent = "var result = outerLambda(innerLambda => { return innerLambda(); });"; + + result.Output.Should().Be(expectedContent); + } + [Test] public async Task Should_Print_NotFound() { diff --git a/Src/CSharpier.Playground/package-lock.json b/Src/CSharpier.Playground/package-lock.json new file mode 100644 index 000000000..8075b1ff5 --- /dev/null +++ b/Src/CSharpier.Playground/package-lock.json @@ -0,0 +1,6 @@ +{ + "name": "CSharpier.Playground", + "lockfileVersion": 3, + "requires": true, + "packages": {} +} diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 000000000..0e9076a2a --- /dev/null +++ b/package-lock.json @@ -0,0 +1,6 @@ +{ + "name": "csharpier", + "lockfileVersion": 3, + "requires": true, + "packages": {} +} From 145ece88b3ab3c3527a30014fdbdb6154fec3b02 Mon Sep 17 00:00:00 2001 From: Rudy Peralta Date: Mon, 14 Aug 2023 22:46:04 -0700 Subject: [PATCH 2/6] Added a further test for deeply nested lambdas w/ curly brackets --- Src/CSharpier.Cli.Tests/CliTests.cs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/Src/CSharpier.Cli.Tests/CliTests.cs b/Src/CSharpier.Cli.Tests/CliTests.cs index 4505bb353..cb8783cab 100644 --- a/Src/CSharpier.Cli.Tests/CliTests.cs +++ b/Src/CSharpier.Cli.Tests/CliTests.cs @@ -179,6 +179,34 @@ public async Task Should_Format_Nested_Lambda() result.Output.Should().Be(expectedContent); } + [Test] + public async Task Should_Format_Further_Nested_Lambda() + { + var unformattedContent = "var result = outerLambda(innerLambda1 => { return innerLambda2(() => { return innerLambda3(() => { return innerLambda4(); }); }); });"; + + await this.WriteFileAsync("FurtherNestedLambda.cs", unformattedContent); + + var result = await new CsharpierProcess() + .WithArguments("FurtherNestedLambda.cs") + .ExecuteAsync(); + + var expectedContent = +@" +var result = outerLambda(innerLambda1 => +{ + return innerLambda2(() => + { + return innerLambda3(() => + { + return innerLambdsa4(); + }); + }); +}); +"; + + result.Output.Should().Be(expectedContent); + } + [Test] public async Task Should_Print_NotFound() { From 58b2b95e9ada2d716be6ff6112a18d90ecb51d67 Mon Sep 17 00:00:00 2001 From: Rudy Peralta Date: Wed, 16 Aug 2023 19:56:14 -0700 Subject: [PATCH 3/6] Registered range formatting for CSharpier --- Src/CSharpier.VSCode/src/FormattingService.ts | 29 ++++++++----------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/Src/CSharpier.VSCode/src/FormattingService.ts b/Src/CSharpier.VSCode/src/FormattingService.ts index 26307e0ab..88245c19b 100644 --- a/Src/CSharpier.VSCode/src/FormattingService.ts +++ b/Src/CSharpier.VSCode/src/FormattingService.ts @@ -1,5 +1,5 @@ import { performance } from "perf_hooks"; -import { languages, Range, TextDocument, TextEdit } from "vscode"; +import { languages, Range, TextDocument, TextEdit, window } from "vscode"; import { CSharpierProcessProvider } from "./CSharpierProcessProvider"; import { Logger } from "./Logger"; @@ -11,36 +11,31 @@ export class FormattingService { this.logger = logger; this.csharpierProcessProvider = csharpierProcessProvider; - languages.registerDocumentFormattingEditProvider("csharp", { - provideDocumentFormattingEdits: this.provideDocumentFormattingEdits, + languages.registerDocumentRangeFormattingEditProvider("csharp", { + provideDocumentRangeFormattingEdits: this.provideDocumentRangeFormattingEdits, }); } - private provideDocumentFormattingEdits = async (document: TextDocument) => { + private provideDocumentRangeFormattingEdits = async (document: TextDocument, range: Range) => { this.logger.info("Formatting started for " + document.fileName + "."); const startTime = performance.now(); - const text = document.getText(); + const text = document.getText(range); const newText = await this.format(text, document.fileName); const endTime = performance.now(); this.logger.info("Formatted in " + (endTime - startTime) + "ms"); if (!newText || newText === text) { - this.logger.debug( - "Skipping write because " + !newText - ? "result is empty" - : "current document equals result", - ); + const errorMessage = "Skipping write because " + !newText + ? "File is empty or selected text is an incomplete code region" + : "current document equals result"; + + console.warn("Error formatting document: " + errorMessage); return []; } - return [TextEdit.replace(FormattingService.fullDocumentRange(document), newText)]; + return [TextEdit.replace(range, newText)]; }; - private static fullDocumentRange(document: TextDocument): Range { - const lastLineId = document.lineCount - 1; - return new Range(0, 0, lastLineId, document.lineAt(lastLineId).text.length); - } - private format = async (content: string, filePath: string) => { return this.csharpierProcessProvider.getProcessFor(filePath).formatFile(content, filePath); }; -} +} \ No newline at end of file From e65f35adf9cf6c9bda3a3bff08fa1d189e8c3a48 Mon Sep 17 00:00:00 2001 From: Rudy Peralta Date: Wed, 16 Aug 2023 22:31:09 -0700 Subject: [PATCH 4/6] Added in peristent formatting for format selection. Has the issue of over indenting a line if only part of it is selected --- Src/CSharpier.VSCode/src/FormattingService.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Src/CSharpier.VSCode/src/FormattingService.ts b/Src/CSharpier.VSCode/src/FormattingService.ts index 88245c19b..f38509dae 100644 --- a/Src/CSharpier.VSCode/src/FormattingService.ts +++ b/Src/CSharpier.VSCode/src/FormattingService.ts @@ -1,5 +1,5 @@ import { performance } from "perf_hooks"; -import { languages, Range, TextDocument, TextEdit, window } from "vscode"; +import { languages, Range, TextDocument, TextEdit, TextEditor, window } from "vscode"; import { CSharpierProcessProvider } from "./CSharpierProcessProvider"; import { Logger } from "./Logger"; @@ -19,8 +19,15 @@ export class FormattingService { private provideDocumentRangeFormattingEdits = async (document: TextDocument, range: Range) => { this.logger.info("Formatting started for " + document.fileName + "."); const startTime = performance.now(); + + const editor = window.activeTextEditor; + const nonEmptyLine = editor?.document.lineAt(range.start.line); + const indentation = nonEmptyLine?.text.match(/^\s*/)?.[0] ?? ""; + const text = document.getText(range); const newText = await this.format(text, document.fileName); + const formattedText = newText.replace(/^(?!$)/gm, indentation); + const endTime = performance.now(); this.logger.info("Formatted in " + (endTime - startTime) + "ms"); if (!newText || newText === text) { @@ -32,7 +39,7 @@ export class FormattingService { return []; } - return [TextEdit.replace(range, newText)]; + return [TextEdit.replace(range, formattedText)]; }; private format = async (content: string, filePath: string) => { From 309b0b0423c790253a250380fd2e8533089cd7f3 Mon Sep 17 00:00:00 2001 From: Rudy Peralta Date: Wed, 16 Aug 2023 23:00:46 -0700 Subject: [PATCH 5/6] Completed formatting and got rid of formatting bugs --- Src/CSharpier.VSCode/src/FormattingService.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Src/CSharpier.VSCode/src/FormattingService.ts b/Src/CSharpier.VSCode/src/FormattingService.ts index f38509dae..45de9fd37 100644 --- a/Src/CSharpier.VSCode/src/FormattingService.ts +++ b/Src/CSharpier.VSCode/src/FormattingService.ts @@ -22,9 +22,14 @@ export class FormattingService { const editor = window.activeTextEditor; const nonEmptyLine = editor?.document.lineAt(range.start.line); - const indentation = nonEmptyLine?.text.match(/^\s*/)?.[0] ?? ""; + if (!nonEmptyLine) { + return []; + } + const indentation = nonEmptyLine.text.match(/^\s*/)?.[0] ?? ""; + + const fullRange = new Range(nonEmptyLine.range.start, range.end); - const text = document.getText(range); + const text = document.getText(fullRange); const newText = await this.format(text, document.fileName); const formattedText = newText.replace(/^(?!$)/gm, indentation); @@ -39,7 +44,11 @@ export class FormattingService { return []; } - return [TextEdit.replace(range, formattedText)]; + if (formattedText === text) { + return []; + } + + return [TextEdit.replace(fullRange, formattedText)]; }; private format = async (content: string, filePath: string) => { From 634f0e7075c23e790091f2e32f8ecb0c0e540e79 Mon Sep 17 00:00:00 2001 From: Rudy Peralta Date: Wed, 16 Aug 2023 23:07:09 -0700 Subject: [PATCH 6/6] Removed old test that I added in --- Src/CSharpier.Cli.Tests/CliTests.cs | 44 ----------------------------- 1 file changed, 44 deletions(-) diff --git a/Src/CSharpier.Cli.Tests/CliTests.cs b/Src/CSharpier.Cli.Tests/CliTests.cs index cb8783cab..f3cd7eee1 100644 --- a/Src/CSharpier.Cli.Tests/CliTests.cs +++ b/Src/CSharpier.Cli.Tests/CliTests.cs @@ -163,50 +163,6 @@ public async Task Should_Format_Unicode() result.ExitCode.Should().Be(0); } - [Test] - public async Task Should_Format_Nested_Lambda() - { - var unformattedContent = "var result = outerLambda(innerLambda => { return innerLambda(); });"; - - await this.WriteFileAsync("NestedLambda.cs", unformattedContent); - - var result = await new CsharpierProcess() - .WithArguments("NestedLambda.cs") - .ExecuteAsync(); - - var expectedContent = "var result = outerLambda(innerLambda => { return innerLambda(); });"; - - result.Output.Should().Be(expectedContent); - } - - [Test] - public async Task Should_Format_Further_Nested_Lambda() - { - var unformattedContent = "var result = outerLambda(innerLambda1 => { return innerLambda2(() => { return innerLambda3(() => { return innerLambda4(); }); }); });"; - - await this.WriteFileAsync("FurtherNestedLambda.cs", unformattedContent); - - var result = await new CsharpierProcess() - .WithArguments("FurtherNestedLambda.cs") - .ExecuteAsync(); - - var expectedContent = -@" -var result = outerLambda(innerLambda1 => -{ - return innerLambda2(() => - { - return innerLambda3(() => - { - return innerLambdsa4(); - }); - }); -}); -"; - - result.Output.Should().Be(expectedContent); - } - [Test] public async Task Should_Print_NotFound() {