Skip to content

Commit 72df9f9

Browse files
authored
Merge pull request #51 from kieferrm/multi-location-diagnostics
Update sample with support for multi-location diagnostics
2 parents 78ecd9b + 227d7cb commit 72df9f9

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

lsp-sample/server/src/server.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@ let documents: TextDocuments = new TextDocuments();
2020
// for open, change and close text document events
2121
documents.listen(connection);
2222

23+
24+
let shouldSendDiagnosticRelatedInformation: boolean = false;
25+
2326
// After the server has started the client sends an initialize request. The server receives
2427
// in the passed params the rootPath of the workspace plus the client capabilities.
2528
connection.onInitialize((_params): InitializeResult => {
29+
shouldSendDiagnosticRelatedInformation = _params.capabilities && _params.capabilities.textDocument && _params.capabilities.textDocument.publishDiagnostics && _params.capabilities.textDocument.publishDiagnostics.relatedInformation;
2630
return {
2731
capabilities: {
2832
// Tell the client that the server works in FULL text document sync mode
@@ -72,15 +76,41 @@ function validateTextDocument(textDocument: TextDocument): void {
7276
let index = line.indexOf('typescript');
7377
if (index >= 0) {
7478
problems++;
75-
diagnostics.push({
79+
80+
let diagnosic: Diagnostic = {
7681
severity: DiagnosticSeverity.Warning,
7782
range: {
7883
start: { line: i, character: index },
7984
end: { line: i, character: index + 10 }
8085
},
8186
message: `${line.substr(index, 10)} should be spelled TypeScript`,
8287
source: 'ex'
83-
});
88+
};
89+
if (shouldSendDiagnosticRelatedInformation) {
90+
diagnosic.relatedInformation = [
91+
{
92+
location: {
93+
uri: textDocument.uri,
94+
range: {
95+
start: { line: i, character: index },
96+
end: { line: i, character: index + 10 }
97+
}
98+
},
99+
message: 'Spelling matters'
100+
},
101+
{
102+
location: {
103+
uri: textDocument.uri,
104+
range: {
105+
start: { line: i, character: index },
106+
end: { line: i, character: index + 10 }
107+
}
108+
},
109+
message: 'Particularly for names'
110+
}
111+
];
112+
}
113+
diagnostics.push(diagnosic);
84114
}
85115
}
86116
// Send the computed diagnostics to VSCode.

0 commit comments

Comments
 (0)