Skip to content

Commit 9b9c431

Browse files
committed
feat(language-service): add Document Symbols support for Angular templates
Add comprehensive Document Symbols support for Angular templates, enabling the Outline panel, breadcrumbs navigation, and Go to Symbol features. Features: - Block syntax: @if, @else, @for, @switch, @defer, @let with expressions - Structural directives: *ngIf, *ngFor, *ngSwitch, etc. - Variables: loop items, template references, context aliases - Semantic SymbolKinds: Struct for @if, Array for @for, Event for @defer Configuration: - angular.documentSymbols.enabled (default: true) - angular.documentSymbols.showImplicitForVariables (default: false)
1 parent b275206 commit 9b9c431

13 files changed

Lines changed: 1907 additions & 0 deletions

File tree

packages/language-service/api.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,52 @@ export interface ApplyRefactoringResult extends Omit<ts.RefactorEditInfo, 'notAp
8989
warningMessage?: string;
9090
}
9191

92+
/**
93+
* Angular-specific LSP SymbolKind values for template symbols.
94+
* These are used for symbols that don't have a direct TypeScript ScriptElementKind mapping.
95+
* Values match LSP SymbolKind enum: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#symbolKind
96+
*/
97+
export enum AngularSymbolKind {
98+
Namespace = 3,
99+
Array = 18,
100+
Object = 19,
101+
Struct = 23,
102+
Event = 24,
103+
}
104+
105+
/**
106+
* A document symbol representing an Angular template element.
107+
* This uses TypeScript's NavigationTree structure so it can be merged with TS symbols.
108+
*/
109+
export interface TemplateDocumentSymbol {
110+
/** Display name for the symbol */
111+
text: string;
112+
/** Kind of symbol (using TypeScript's ScriptElementKind for compatibility) */
113+
kind: ts.ScriptElementKind;
114+
/**
115+
* Optional LSP SymbolKind override for Angular-specific symbol types.
116+
* When set, this takes precedence over the default ScriptElementKind mapping.
117+
*/
118+
lspKind?: AngularSymbolKind;
119+
/** Span covering the entire symbol */
120+
spans: ts.TextSpan[];
121+
/** Span for just the name (used for selection) */
122+
nameSpan?: ts.TextSpan;
123+
/** Child symbols */
124+
childItems?: TemplateDocumentSymbol[];
125+
}
126+
127+
/**
128+
* Options for customizing document symbols behavior.
129+
*/
130+
export interface DocumentSymbolsOptions {
131+
/**
132+
* Show all implicit @for loop variables ($index, $count, $first, $last, $even, $odd).
133+
* When false (default), only explicitly aliased variables like `let i = $index` are shown.
134+
*/
135+
showImplicitForVariables?: boolean;
136+
}
137+
92138
/**
93139
* `NgLanguageService` describes an instance of an Angular language service,
94140
* whose API surface is a strict superset of TypeScript's language service.
@@ -102,6 +148,19 @@ export interface NgLanguageService extends ts.LanguageService {
102148
): GetTemplateLocationForComponentResponse;
103149
getTypescriptLanguageService(): ts.LanguageService;
104150

151+
/**
152+
* Gets document symbols for Angular templates, including control flow blocks,
153+
* elements, components, template references, and @let declarations.
154+
* Returns symbols in NavigationTree format for compatibility with TypeScript.
155+
*
156+
* @param fileName The file path to get template symbols for
157+
* @param options Optional configuration for document symbols behavior
158+
*/
159+
getTemplateDocumentSymbols(
160+
fileName: string,
161+
options?: DocumentSymbolsOptions,
162+
): TemplateDocumentSymbol[];
163+
105164
applyRefactoring(
106165
fileName: string,
107166
positionOrRange: number | ts.TextRange,

0 commit comments

Comments
 (0)