Skip to content
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

Adding format selection for VSCode plugin #933

Closed
wants to merge 11 commits into from
6 changes: 6 additions & 0 deletions Src/CSharpier.Playground/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 28 additions & 17 deletions Src/CSharpier.VSCode/src/FormattingService.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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);
};
}
}
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.