Skip to content

Commit

Permalink
fixup! feat: add command to go to component(s) from external template
Browse files Browse the repository at this point in the history
  • Loading branch information
atscott authored and Keen Yee Liau committed Feb 26, 2021
1 parent d1ca20a commit 4f55cb1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 39 deletions.
12 changes: 3 additions & 9 deletions client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ interface GetTcbResponse {
selections: vscode.Range[];
}

type GetComponentsForOpenExternalTemplateResponse = Array<{uri: vscode.Uri; range: vscode.Range;}>;

export class AngularLanguageClient implements vscode.Disposable {
private client: lsp.LanguageClient|null = null;
private readonly disposables: vscode.Disposable[] = [];
Expand Down Expand Up @@ -134,7 +132,7 @@ export class AngularLanguageClient implements vscode.Disposable {
}

async getComponentsForOpenExternalTemplate(textEditor: vscode.TextEditor):
Promise<GetComponentsForOpenExternalTemplateResponse|undefined> {
Promise<vscode.Location[]|undefined> {
if (this.client === null) {
return undefined;
}
Expand All @@ -148,12 +146,8 @@ export class AngularLanguageClient implements vscode.Disposable {
}

const p2cConverter = this.client.protocol2CodeConverter;
return response.map(v => {
return {
range: p2cConverter.asRange(v.range),
uri: p2cConverter.asUri(v.uri),
};
});
return response.map(
v => new vscode.Location(p2cConverter.asUri(v.uri), p2cConverter.asRange(v.range)));
}

dispose() {
Expand Down
27 changes: 9 additions & 18 deletions client/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,27 +127,18 @@ function goToComponentWithTemplateFile(ngClient: AngularLanguageClient): Command
id: 'angular.goToComponentWithTemplateFile',
isTextEditorCommand: true,
async execute(textEditor: vscode.TextEditor) {
const componentLocations = await ngClient.getComponentsForOpenExternalTemplate(textEditor);
if (componentLocations === undefined) {
const locations = await ngClient.getComponentsForOpenExternalTemplate(textEditor);
if (locations === undefined) {
return;
}

const locations: vscode.Location[] =
componentLocations.map(location => new vscode.Location(location.uri, location.range));
// If there is more than one component that references the template, show them all. Otherwise
// go to the component immediately.
if (locations.length > 1) {
vscode.commands.executeCommand(
'editor.action.showReferences',
textEditor.document.uri,
new vscode.Position(
textEditor.selection.start.line, textEditor.selection.start.character),
locations,
);
} else {
const document = await vscode.workspace.openTextDocument(locations[0].uri);
await vscode.window.showTextDocument(document, {selection: locations[0].range});
}
vscode.commands.executeCommand(
'editor.action.goToLocations',
textEditor.document.uri,
textEditor.selection.active,
locations,
'peek', /** what to do when there are multiple results */
);
},
};
}
Expand Down
5 changes: 1 addition & 4 deletions common/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,13 @@
import * as lsp from 'vscode-languageserver-protocol';

export const GetComponentsWithTemplateFile = new lsp.RequestType<
GetComponentsWithTemplateFileParams, GetComponentsWithTemplateFileResponse,
GetComponentsWithTemplateFileParams, lsp.Location[],
/* error */ void>('angular/getComponentsWithTemplateFile');

export interface GetComponentsWithTemplateFileParams {
textDocument: lsp.TextDocumentIdentifier;
}

/** An array of locations that represent component declarations. */
export type GetComponentsWithTemplateFileResponse = Array<{uri: lsp.DocumentUri, range: lsp.Range}>;

export interface GetTcbParams {
textDocument: lsp.TextDocumentIdentifier;
position: lsp.Position;
Expand Down
17 changes: 9 additions & 8 deletions server/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
* found in the LICENSE file at https://angular.io/license
*/

import {GetComponentLocationsForTemplateResponse, NgLanguageService} from '@angular/language-service';
import {NgLanguageService} from '@angular/language-service';
import * as ts from 'typescript/lib/tsserverlibrary';
import * as lsp from 'vscode-languageserver/node';

import {ServerOptions} from '../common/initialize';
import {ProjectLanguageService, ProjectLoadingFinish, ProjectLoadingStart, SuggestIvyLanguageService, SuggestStrictMode} from '../common/notifications';
import {NgccProgressToken, NgccProgressType} from '../common/progress';
import {GetComponentsWithTemplateFile, GetComponentsWithTemplateFileResponse, GetTcbParams, GetTcbRequest, GetTcbResponse} from '../common/requests';
import {GetComponentsWithTemplateFile, GetTcbParams, GetTcbRequest, GetTcbResponse} from '../common/requests';

import {readNgCompletionData, tsCompletionEntryToLspCompletionItem} from './completion';
import {tsDiagnosticToLspDiagnostic} from './diagnostic';
Expand Down Expand Up @@ -161,20 +161,21 @@ export class Session {
};
}

private onGetComponentsWithTemplateFile(params: any): GetComponentsWithTemplateFileResponse
|undefined {
private onGetComponentsWithTemplateFile(params: any): lsp.Location[]|undefined {
const lsInfo = this.getLSAndScriptInfo(params.textDocument);
if (lsInfo === undefined) {
return undefined;
}
const {languageService, scriptInfo} = lsInfo;
const documentSpans = languageService.getComponentLocationsForTemplate(scriptInfo.fileName);
const results: GetComponentsWithTemplateFileResponse = [];
const results: lsp.Location[] = [];
for (const documentSpan of documentSpans) {
const scriptInfo = this.projectService.getScriptInfo(documentSpan.fileName);
const range =
scriptInfo ? tsTextSpanToLspRange(scriptInfo, documentSpan.textSpan) : EMPTY_RANGE;
results.push({uri: filePathToUri(documentSpan.fileName), range});
if (scriptInfo === undefined) {
continue;
}
const range = tsTextSpanToLspRange(scriptInfo, documentSpan.textSpan);
results.push(lsp.Location.create(filePathToUri(documentSpan.fileName), range));
}
return results;
}
Expand Down

0 comments on commit 4f55cb1

Please sign in to comment.