Skip to content
  • Rate limit · GitHub

    Access has been restricted

    You have triggered a rate limit.

    Please wait a few minutes before you try again;
    in some cases this may take up to an hour.

  • Notifications You must be signed in to change notification settings
  • Fork 109
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

Updating the VSCode extension to support csharpier 1.0.0 #1428

Merged
merged 10 commits into from
Jan 8, 2025
Prev Previous commit
Next Next commit
centralizing the supported languages. Tweaking diagnostics so they do…
…n't run as often
Rate limit · GitHub

Access has been restricted

You have triggered a rate limit.

Please wait a few minutes before you try again;
in some cases this may take up to an hour.

belav committed Jan 8, 2025
commit 5f9990693b11cc7fd6262433c139cc53671e860b
15 changes: 9 additions & 6 deletions Src/CSharpier.VSCode/src/CSharpierProcessProvider.ts
Original file line number Diff line number Diff line change
@@ -17,7 +17,6 @@ import { runFunctionsUntilResultFound } from "./RunFunctionsUntilResultFound";

export class CSharpierProcessProvider implements Disposable {
warnedForOldVersion = false;
logger: Logger;
customPathInstaller: CustomPathInstaller;
installerService: InstallerService;
warmingByDirectory: Record<string, boolean | undefined> = {};
@@ -28,9 +27,12 @@ export class CSharpierProcessProvider implements Disposable {
> = {};
disableCSharpierServer: boolean;

constructor(logger: Logger, extension: Extension<unknown>) {
this.logger = logger;
this.customPathInstaller = new CustomPathInstaller(logger);
constructor(
private readonly logger: Logger,
extension: Extension<unknown>,
private readonly supportedLanguageIds: string[],
) {
this.customPathInstaller = new CustomPathInstaller(this.logger);
this.installerService = new InstallerService(
this.logger,
this.killRunningProcesses,
@@ -42,10 +44,11 @@ export class CSharpierProcessProvider implements Disposable {
false;

window.onDidChangeActiveTextEditor((event: TextEditor | undefined) => {
if (event?.document?.languageId !== "csharp") {
let languageId = event?.document?.languageId;
if (!languageId || !this.supportedLanguageIds.includes(languageId)) {
return;
}
this.findAndWarmProcess(event.document.fileName);
this.findAndWarmProcess(event!.document.fileName);
});
}

26 changes: 18 additions & 8 deletions Src/CSharpier.VSCode/src/DiagnosticsService.ts
Original file line number Diff line number Diff line change
@@ -29,12 +29,12 @@ export class DiagnosticsService implements vscode.CodeActionProvider, vscode.Dis

constructor(
private readonly formatDocumentProvider: FormatDocumentProvider,
private readonly documentSelector: Array<vscode.DocumentFilter>,
private readonly supportedLanguageIds: string[],
private readonly logger: Logger,
) {
this.diagnosticCollection = vscode.languages.createDiagnosticCollection(DIAGNOSTICS_ID);
this.codeActionsProvider = vscode.languages.registerCodeActionsProvider(
this.documentSelector,
this.supportedLanguageIds,
this,
DiagnosticsService.metadata,
);
@@ -49,6 +49,8 @@ export class DiagnosticsService implements vscode.CodeActionProvider, vscode.Dis
this.codeActionsProvider.dispose();
}

private onChangeTimeout: NodeJS.Timeout | undefined;

private registerEditorEvents(): void {
const activeDocument = vscode.window.activeTextEditor?.document;
if (activeDocument) {
@@ -58,10 +60,14 @@ export class DiagnosticsService implements vscode.CodeActionProvider, vscode.Dis
const onDidChangeTextDocument = vscode.workspace.onDidChangeTextDocument(e => {
if (
e.contentChanges.length &&
vscode.window.activeTextEditor?.document === e.document
vscode.window.activeTextEditor?.document === e.document &&
this.supportedLanguageIds.includes(e.document.languageId)
) {
// when editing don't pop up any new diagnostics, but if someone cleans up one then allow that update
void this.runDiagnostics(e.document, true);
clearTimeout(this.onChangeTimeout);
this.onChangeTimeout = setTimeout(() => {
// when editing don't pop up any new diagnostics, but if someone cleans up one then allow that update
void this.runDiagnostics(e.document, true);
}, 100);
}
});

@@ -90,10 +96,15 @@ export class DiagnosticsService implements vscode.CodeActionProvider, vscode.Dis
onlyAllowLessDiagnostics = false,
): Promise<void> {
const shouldRunDiagnostics =
this.documentSelector.some(selector => selector.language === document.languageId) &&
!!vscode.workspace.getWorkspaceFolder(document.uri) &&
(workspace.getConfiguration("csharpier").get<boolean>("enableDiagnostics") ?? true);
if (!shouldRunDiagnostics) {

let currentDiagnostics = this.diagnosticCollection.get(document.uri);

if (
!shouldRunDiagnostics ||
(currentDiagnostics?.length === 0 && onlyAllowLessDiagnostics)
) {
this.diagnosticCollection.set(document.uri, []);
return;
}
@@ -110,7 +121,6 @@ export class DiagnosticsService implements vscode.CodeActionProvider, vscode.Dis
};
const diagnostics = this.getDiagnostics(document, diff);
if (onlyAllowLessDiagnostics) {
let currentDiagnostics = this.diagnosticCollection.get(document.uri);
let currentCount = !currentDiagnostics ? 0 : currentDiagnostics.length;
if (diagnostics.length >= currentCount) {
return;
21 changes: 11 additions & 10 deletions Src/CSharpier.VSCode/src/Extension.ts
Original file line number Diff line number Diff line change
@@ -19,6 +19,8 @@ export async function activate(context: ExtensionContext) {
await initPlugin(context);
}

export const supportedLanguageIds = ["csharp", "xml"];

const initPlugin = async (context: ExtensionContext) => {
const enableDebugLogs =
workspace.getConfiguration("csharpier").get<boolean>("enableDebugLogs") ?? false;
@@ -40,22 +42,21 @@ const initPlugin = async (context: ExtensionContext) => {

NullCSharpierProcess.create(logger);

const csharpierProcessProvider = new CSharpierProcessProvider(logger, context.extension);
const csharpierProcessProvider = new CSharpierProcessProvider(
logger,
context.extension,
supportedLanguageIds,
);
const formatDocumentProvider = new FormatDocumentProvider(logger, csharpierProcessProvider);
const diagnosticsDocumentSelector: DocumentFilter[] = [
{
language: "csharp",
scheme: "file",
},
];

const diagnosticsService = new DiagnosticsService(
formatDocumentProvider,
diagnosticsDocumentSelector,
supportedLanguageIds,
logger,
);
const fixAllCodeActionProvider = new FixAllCodeActionProvider(diagnosticsDocumentSelector);
const fixAllCodeActionProvider = new FixAllCodeActionProvider(supportedLanguageIds);

new FormattingService(formatDocumentProvider);
new FormattingService(formatDocumentProvider, supportedLanguageIds);
new FixAllCodeActionsCommand(context, formatDocumentProvider, logger);

context.subscriptions.push(
4 changes: 2 additions & 2 deletions Src/CSharpier.VSCode/src/FixAllCodeActionProvider.ts
Original file line number Diff line number Diff line change
@@ -13,9 +13,9 @@ export class FixAllCodeActionProvider implements vscode.CodeActionProvider, vsco

private readonly codeActionsProvider: vscode.Disposable;

constructor(private readonly documentSelector: vscode.DocumentSelector) {
constructor(private readonly supportedLanguageIds: string[]) {
this.codeActionsProvider = vscode.languages.registerCodeActionsProvider(
this.documentSelector,
this.supportedLanguageIds,
this,
FixAllCodeActionProvider.metadata,
);
22 changes: 12 additions & 10 deletions Src/CSharpier.VSCode/src/FormattingService.ts
Original file line number Diff line number Diff line change
@@ -12,17 +12,19 @@ import { FormatDocumentProvider } from "./FormatDocumentProvider";
import { Logger } from "./Logger";

export class FormattingService {
constructor(private readonly formatDocumentProvider: FormatDocumentProvider) {
languages.registerDocumentFormattingEditProvider("xml", {
provideDocumentFormattingEdits: this.provideDocumentFormattingEdits,
});
languages.registerDocumentFormattingEditProvider("csharp", {
provideDocumentFormattingEdits: this.provideDocumentFormattingEdits,
});
constructor(
private readonly formatDocumentProvider: FormatDocumentProvider,
supportedLanguageIds: string[],
) {
for (let languageId of supportedLanguageIds) {
languages.registerDocumentFormattingEditProvider(languageId, {
provideDocumentFormattingEdits: this.provideDocumentFormattingEdits,
});

languages.registerDocumentRangeFormattingEditProvider("csharp", {
provideDocumentRangeFormattingEdits: this.provideDocumentRangeFormattingEdits,
});
languages.registerDocumentRangeFormattingEditProvider(languageId, {
provideDocumentRangeFormattingEdits: this.provideDocumentRangeFormattingEdits,
});
}
}

private provideDocumentRangeFormattingEdits = async (
Rate limit · GitHub

Access has been restricted

You have triggered a rate limit.

Please wait a few minutes before you try again;
in some cases this may take up to an hour.