Skip to content

Commit 8553115

Browse files
authored
fix(server): send diagnostic range to the Angular language service when fixing code errors (#1747)
This will make the fix error code simple, the range the fix error code get is always the start and end of the diagnostic. For example, `<span>{{title}}</span>`, the `title` is the missing member of component. When the range the user selects is from the start of `<span>` to the end of `</span>`, the fix error code needs to check the start of the diagnostic.
1 parent f88f15f commit 8553115

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

integration/lsp/ivy_spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,33 @@ describe('code fixes', () => {
700700
expect(codeActions).toContain(jasmine.objectContaining(expectedCodeActionInTemplate));
701701
});
702702

703+
it('should fix error when the range the user selects is larger than the diagnostic', async () => {
704+
const template = `<span>{{titl}}</span>`;
705+
openTextDocument(client, FOO_TEMPLATE, template);
706+
const languageServiceEnabled = await waitForNgcc(client);
707+
expect(languageServiceEnabled).toBeTrue();
708+
const diags = await getDiagnosticsForFile(client, FOO_TEMPLATE);
709+
const codeActions = await client.sendRequest(lsp.CodeActionRequest.type, {
710+
textDocument: {
711+
uri: FOO_TEMPLATE_URI,
712+
},
713+
range:
714+
lsp.Range.create(lsp.Position.create(0, 0), lsp.Position.create(0, template.length - 1)),
715+
context: lsp.CodeActionContext.create(diags),
716+
}) as lsp.CodeAction[];
717+
const expectedCodeActionInTemplate = {
718+
'edit': {
719+
'changes': {
720+
[FOO_TEMPLATE_URI]: [{
721+
'newText': 'title',
722+
'range': {'start': {'line': 0, 'character': 8}, 'end': {'line': 0, 'character': 12}}
723+
}]
724+
}
725+
}
726+
};
727+
expect(codeActions).toContain(jasmine.objectContaining(expectedCodeActionInTemplate));
728+
});
729+
703730
describe('should work', () => {
704731
beforeEach(async () => {
705732
openTextDocument(client, FOO_COMPONENT, `

server/src/session.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,20 @@ export class Session {
210210
if (!lsInfo) {
211211
return null;
212212
}
213-
const start = lspPositionToTsPosition(lsInfo.scriptInfo, params.range.start);
214-
const end = lspPositionToTsPosition(lsInfo.scriptInfo, params.range.end);
215-
const errorCodes = params.context.diagnostics.map(diag => diag.code)
216-
.filter((code): code is number => typeof code === 'number');
217213

218-
const codeActions = lsInfo.languageService.getCodeFixesAtPosition(
219-
filePath, start, end, errorCodes, defaultFormatOptions, defaultPreferences);
214+
const codeActions: ts.CodeFixAction[] = [];
215+
for (const diagnostic of params.context.diagnostics) {
216+
const errorCode = diagnostic.code;
217+
if (typeof errorCode !== 'number') {
218+
continue;
219+
}
220+
const start = lspPositionToTsPosition(lsInfo.scriptInfo, diagnostic.range.start);
221+
const end = lspPositionToTsPosition(lsInfo.scriptInfo, diagnostic.range.end);
222+
const codeActionsForDiagnostic = lsInfo.languageService.getCodeFixesAtPosition(
223+
filePath, start, end, [errorCode], defaultFormatOptions, defaultPreferences);
224+
codeActions.push(...codeActionsForDiagnostic);
225+
}
226+
220227
const individualCodeFixes = codeActions.map<lsp.CodeAction>(codeAction => {
221228
return {
222229
title: codeAction.description,

0 commit comments

Comments
 (0)