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

Move VS Code language service modules into their own folder #2055

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion vscode/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { TextDocument, Uri, Range, Location } from "vscode";
import { Utils } from "vscode-uri";
import { ILocation, IRange, IWorkspaceEdit } from "qsharp-lang";
import { ILocation, IRange, IWorkspaceEdit, VSDiagnostic } from "qsharp-lang";
import * as vscode from "vscode";

export const qsharpLanguageId = "qsharp";
Expand Down Expand Up @@ -58,3 +58,40 @@ export function toVscodeWorkspaceEdit(
}
return workspaceEdit;
}

export function toVsCodeDiagnostic(d: VSDiagnostic): vscode.Diagnostic {
let severity;
switch (d.severity) {
case "error":
severity = vscode.DiagnosticSeverity.Error;
break;
case "warning":
severity = vscode.DiagnosticSeverity.Warning;
break;
case "info":
severity = vscode.DiagnosticSeverity.Information;
break;
}
const vscodeDiagnostic = new vscode.Diagnostic(
toVscodeRange(d.range),
d.message,
severity,
);
if (d.uri && d.code) {
vscodeDiagnostic.code = {
value: d.code,
target: vscode.Uri.parse(d.uri),
};
} else if (d.code) {
vscodeDiagnostic.code = d.code;
}
if (d.related) {
vscodeDiagnostic.relatedInformation = d.related.map((r) => {
return new vscode.DiagnosticRelatedInformation(
toVscodeLocation(r.location),
r.message,
);
});
}
return vscodeDiagnostic;
}
104 changes: 12 additions & 92 deletions vscode/src/diagnostics.ts
Original file line number Diff line number Diff line change
@@ -1,100 +1,20 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import {
ILanguageService,
IQSharpError,
VSDiagnostic,
log,
qsharpLibraryUriScheme,
} from "qsharp-lang";
import { IQSharpError, log, qsharpLibraryUriScheme } from "qsharp-lang";
import * as vscode from "vscode";
import { toVscodeLocation, toVscodeRange, qsharpLanguageId } from "./common.js";
import { qsharpLanguageId, toVsCodeDiagnostic } from "./common.js";

export function startCheckingQSharp(
languageService: ILanguageService,
): vscode.Disposable[] {
return [
...startLanguageServiceDiagnostics(languageService),
...startQsharpJsonDiagnostics(),
...startCommandDiagnostics(),
];
}

function startLanguageServiceDiagnostics(
languageService: ILanguageService,
): vscode.Disposable[] {
const diagCollection =
vscode.languages.createDiagnosticCollection(qsharpLanguageId);

async function onDiagnostics(evt: {
detail: {
uri: string;
version: number;
diagnostics: VSDiagnostic[];
};
}) {
const diagnostics = evt.detail;
const uri = vscode.Uri.parse(diagnostics.uri);

if (uri.scheme === qsharpLibraryUriScheme) {
// Don't report diagnostics for library files.
return;
}

diagCollection.set(
uri,
diagnostics.diagnostics.map((d) => toVsCodeDiagnostic(d)),
);
}

languageService.addEventListener("diagnostics", onDiagnostics);

return [
{
dispose: () => {
languageService.removeEventListener("diagnostics", onDiagnostics);
},
},
diagCollection,
];
}

export function toVsCodeDiagnostic(d: VSDiagnostic): vscode.Diagnostic {
let severity;
switch (d.severity) {
case "error":
severity = vscode.DiagnosticSeverity.Error;
break;
case "warning":
severity = vscode.DiagnosticSeverity.Warning;
break;
case "info":
severity = vscode.DiagnosticSeverity.Information;
break;
}
const vscodeDiagnostic = new vscode.Diagnostic(
toVscodeRange(d.range),
d.message,
severity,
);
if (d.uri && d.code) {
vscodeDiagnostic.code = {
value: d.code,
target: vscode.Uri.parse(d.uri),
};
} else if (d.code) {
vscodeDiagnostic.code = d.code;
}
if (d.related) {
vscodeDiagnostic.relatedInformation = d.related.map((r) => {
return new vscode.DiagnosticRelatedInformation(
toVscodeLocation(r.location),
r.message,
);
});
}
return vscodeDiagnostic;
/**
* Initialize diagnostics for `qsharp.json` files and failures
* that get reported from various Q# commands.
*
* These are distinct from the errors reported by the Q# language
* service, (a.k.a. compiler errors that get reported as you type).
* Those are initialized in `language-service/diagnostics.js`
*/
export function startOtherQSharpDiagnostics(): vscode.Disposable[] {
return [...startQsharpJsonDiagnostics(), ...startCommandDiagnostics()];
}

//
Expand Down
Loading
Loading