Skip to content

Commit

Permalink
feat(server): add related information to diagnostics (#1492)
Browse files Browse the repository at this point in the history
Show related information in the diagnostics generated for the language
service.
  • Loading branch information
danieltre23 authored Aug 26, 2021
1 parent 4fcbdb7 commit 04b215b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
5 changes: 5 additions & 0 deletions integration/lsp/ivy_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ describe('Angular Ivy language server', () => {
expect(diagnostics.length).toBe(1);
expect(diagnostics[0].message)
.toBe(`Property 'doesnotexist' does not exist on type 'FooComponent'.`);
expect(diagnostics[0].relatedInformation).toBeDefined();
expect(diagnostics[0].relatedInformation!.length).toBe(1);
expect(diagnostics[0].relatedInformation![0].message)
.toBe(`Error occurs in the template of component FooComponent.`);
expect(diagnostics[0].relatedInformation![0].location.uri).toBe(FOO_COMPONENT_URI);
});

it('should support request cancellation', async () => {
Expand Down
3 changes: 2 additions & 1 deletion server/src/diagnostic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import * as ts from 'typescript/lib/tsserverlibrary';
import * as lsp from 'vscode-languageserver';
import {tsTextSpanToLspRange} from './utils';
import {tsRelatedInformationToLspRelatedInformation, tsTextSpanToLspRange} from './utils';

/**
* Convert ts.DiagnosticCategory to lsp.DiagnosticSeverity
Expand Down Expand Up @@ -45,5 +45,6 @@ export function tsDiagnosticToLspDiagnostic(
tsDiagnosticCategoryToLspDiagnosticSeverity(tsDiag.category),
tsDiag.code,
tsDiag.source,
tsRelatedInformationToLspRelatedInformation(scriptInfo, tsDiag.relatedInformation),
);
}
29 changes: 29 additions & 0 deletions server/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,35 @@ export function lspRangeToTsPositions(
return [start, end];
}

/**
* Convert a ts.DiagnosticRelatedInformation array to a
* lsp.DiagnosticRelatedInformation array
* @param scriptInfo Used to determine the offsets.
* @param relatedInfo
*/
export function tsRelatedInformationToLspRelatedInformation(
scriptInfo: ts.server.ScriptInfo,
relatedInfo?: ts.DiagnosticRelatedInformation[]): lsp.DiagnosticRelatedInformation[]|undefined {
if (relatedInfo === undefined) return;
const lspRelatedInfo: lsp.DiagnosticRelatedInformation[] = [];
for (const info of relatedInfo) {
if (info.file === undefined || info.start === undefined || info.length === undefined) continue;
const textSpan: ts.TextSpan = {
start: info.start,
length: info.length,
};
const location = lsp.Location.create(
filePathToUri(info.file.fileName),
tsTextSpanToLspRange(scriptInfo, textSpan),
);
lspRelatedInfo.push(lsp.DiagnosticRelatedInformation.create(
location,
ts.flattenDiagnosticMessageText(info.messageText, '\n'),
));
}
return lspRelatedInfo;
}

export function isConfiguredProject(project: ts.server.Project):
project is ts.server.ConfiguredProject {
return project.projectKind === ts.server.ProjectKind.Configured;
Expand Down

0 comments on commit 04b215b

Please sign in to comment.