Merge pull request #973 from microsoft/dbaeumer/dominant-stoat-yellow

Convert the sample to use pull diagnostics
This commit is contained in:
Dirk Bäumer
2024-03-01 15:54:26 +01:00
committed by GitHub
5 changed files with 425 additions and 273 deletions

View File

@ -13,7 +13,7 @@
},
"devDependencies": {
"@types/vscode": "^1.75.1",
"@vscode/test-electron": "^2.3.8"
"@vscode/test-electron": "^2.3.9"
},
"engines": {
"vscode": "^1.75.0"
@ -35,9 +35,9 @@
"dev": true
},
"node_modules/@vscode/test-electron": {
"version": "2.3.8",
"resolved": "https://registry.npmjs.org/@vscode/test-electron/-/test-electron-2.3.8.tgz",
"integrity": "sha512-b4aZZsBKtMGdDljAsOPObnAi7+VWIaYl3ylCz1jTs+oV6BZ4TNHcVNC3xUn0azPeszBmwSBDQYfFESIaUQnrOg==",
"version": "2.3.9",
"resolved": "https://registry.npmjs.org/@vscode/test-electron/-/test-electron-2.3.9.tgz",
"integrity": "sha512-z3eiChaCQXMqBnk2aHHSEkobmC2VRalFQN0ApOAtydL172zXGxTwGrRtviT5HnUB+Q+G3vtEYFtuQkYqBzYgMA==",
"dev": true,
"dependencies": {
"http-proxy-agent": "^4.0.1",
@ -314,9 +314,9 @@
"dev": true
},
"@vscode/test-electron": {
"version": "2.3.8",
"resolved": "https://registry.npmjs.org/@vscode/test-electron/-/test-electron-2.3.8.tgz",
"integrity": "sha512-b4aZZsBKtMGdDljAsOPObnAi7+VWIaYl3ylCz1jTs+oV6BZ4TNHcVNC3xUn0azPeszBmwSBDQYfFESIaUQnrOg==",
"version": "2.3.9",
"resolved": "https://registry.npmjs.org/@vscode/test-electron/-/test-electron-2.3.9.tgz",
"integrity": "sha512-z3eiChaCQXMqBnk2aHHSEkobmC2VRalFQN0ApOAtydL172zXGxTwGrRtviT5HnUB+Q+G3vtEYFtuQkYqBzYgMA==",
"dev": true,
"requires": {
"http-proxy-agent": "^4.0.1",

View File

@ -17,6 +17,6 @@
},
"devDependencies": {
"@types/vscode": "^1.75.1",
"@vscode/test-electron": "^2.3.8"
"@vscode/test-electron": "^2.3.9"
}
}

File diff suppressed because it is too large Load Diff

View File

@ -56,10 +56,10 @@
"devDependencies": {
"@types/mocha": "^10.0.6",
"@types/node": "^18.14.6",
"@typescript-eslint/eslint-plugin": "^6.14.0",
"@typescript-eslint/parser": "^6.14.0",
"eslint": "^8.56.0",
"mocha": "^10.2.0",
"@typescript-eslint/eslint-plugin": "^7.1.0",
"@typescript-eslint/parser": "^7.1.0",
"eslint": "^8.57.0",
"mocha": "^10.3.0",
"typescript": "^5.3.3"
}
}

View File

@ -14,7 +14,9 @@ import {
CompletionItemKind,
TextDocumentPositionParams,
TextDocumentSyncKind,
InitializeResult
InitializeResult,
DocumentDiagnosticReportKind,
type DocumentDiagnosticReport
} from 'vscode-languageserver/node';
import {
@ -55,6 +57,10 @@ connection.onInitialize((params: InitializeParams) => {
// Tell the client that this server supports code completion.
completionProvider: {
resolveProvider: true
},
diagnosticProvider: {
interFileDependencies: false,
workspaceDiagnostics: false
}
}
};
@ -103,9 +109,10 @@ connection.onDidChangeConfiguration(change => {
(change.settings.languageServerExample || defaultSettings)
);
}
// Revalidate all open text documents
documents.all().forEach(validateTextDocument);
// Refresh the diagnostics since the `maxNumberOfProblems` could have changed.
// We could optimize things here and re-fetch the setting first can compare it
// to the existing setting, but this is out of scope for this example.
connection.languages.diagnostics.refresh();
});
function getDocumentSettings(resource: string): Thenable<ExampleSettings> {
@ -128,13 +135,31 @@ documents.onDidClose(e => {
documentSettings.delete(e.document.uri);
});
connection.languages.diagnostics.on(async (params) => {
const document = documents.get(params.textDocument.uri);
if (document !== undefined) {
return {
kind: DocumentDiagnosticReportKind.Full,
items: await validateTextDocument(document)
} satisfies DocumentDiagnosticReport;
} else {
// We don't know the document. We can either try to read it from disk
// or we don't report problems for it.
return {
kind: DocumentDiagnosticReportKind.Full,
items: []
} satisfies DocumentDiagnosticReport;
}
});
// The content of a text document has changed. This event is emitted
// when the text document first opened or when its content has changed.
documents.onDidChangeContent(change => {
validateTextDocument(change.document);
});
async function validateTextDocument(textDocument: TextDocument): Promise<void> {
async function validateTextDocument(textDocument: TextDocument): Promise<Diagnostic[]> {
// In this simple example we get the settings for every validate run.
const settings = await getDocumentSettings(textDocument.uri);
@ -176,9 +201,7 @@ async function validateTextDocument(textDocument: TextDocument): Promise<void> {
}
diagnostics.push(diagnostic);
}
// Send the computed diagnostics to VSCode.
connection.sendDiagnostics({ uri: textDocument.uri, diagnostics });
return diagnostics;
}
connection.onDidChangeWatchedFiles(_change => {