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/Src/CSharpier.VSCode/src/FormattingService.ts b/Src/CSharpier.VSCode/src/FormattingService.ts index 26307e0ab..45de9fd37 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, TextEditor, window } from "vscode"; import { CSharpierProcessProvider } from "./CSharpierProcessProvider"; import { Logger } from "./Logger"; @@ -11,36 +11,47 @@ 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 editor = window.activeTextEditor; + const nonEmptyLine = editor?.document.lineAt(range.start.line); + if (!nonEmptyLine) { + return []; + } + const indentation = nonEmptyLine.text.match(/^\s*/)?.[0] ?? ""; + + const fullRange = new Range(nonEmptyLine.range.start, range.end); + + const text = document.getText(fullRange); 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) { - 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)]; - }; + if (formattedText === text) { + return []; + } - private static fullDocumentRange(document: TextDocument): Range { - const lastLineId = document.lineCount - 1; - return new Range(0, 0, lastLineId, document.lineAt(lastLineId).text.length); - } + return [TextEdit.replace(fullRange, formattedText)]; + }; private format = async (content: string, filePath: string) => { return this.csharpierProcessProvider.getProcessFor(filePath).formatFile(content, filePath); }; -} +} \ No newline at end of file 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": {} +}