Skip to content

feat(server): Add folding range support for Angular template syntax #1938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Input hashes for repository rule npm_translate_lock(name = "npm", pnpm_lock = "//:pnpm-lock.yaml").
# This file should be checked into version control along with the pnpm-lock.yaml file.
.npmrc=974837034
pnpm-lock.yaml=1223698519
yarn.lock=-2147056255
package.json=-381752920
pnpm-lock.yaml=-1547217849
yarn.lock=1378739518
package.json=733870343
pnpm-workspace.yaml=1711114604
2 changes: 1 addition & 1 deletion client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export class AngularLanguageClient implements vscode.Disposable {
provideFoldingRanges: async (
document: vscode.TextDocument, context: vscode.FoldingContext,
token: vscode.CancellationToken, next) => {
if (!(await this.isInAngularProject(document)) || document.languageId !== 'typescript') {
if (!await this.isInAngularProject(document)) {
return null;
}
return next(document, context, token);
Expand Down
63 changes: 63 additions & 0 deletions integration/lsp/ivy_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,69 @@ export class AppComponent {
expect(response).toContain({startLine: 7, endLine: 8});
});

it('provides folding ranges for control flow', async () => {
openTextDocument(client, APP_COMPONENT, `
import {Component, EventEmitter, Input, Output} from '@angular/core';

@Component({
selector: 'my-app',
template: \`
@if (1) {
1
} @else {
2
}

@switch (name) {
@case ('') {
3
} @default {
4
}
}5

@defer {
6
} @placeholder {
7
} @error {
8
} @loading {
9
}

@for (item of items; track $index) {
10
} @empty {
11
}
\`,
})
export class AppComponent {
name = 'Angular';
items = [];
}`);
const response = await client.sendRequest(lsp.FoldingRangeRequest.type, {
textDocument: {
uri: APP_COMPONENT_URI,
},
}) as lsp.FoldingRange[];
expect(Array.isArray(response)).toBe(true);
// 2 folding ranges for the if/else, 3 for the switch, 4 for defer, 2 for repeater
expect(response.length).toEqual(11);
expect(response).toContain({startLine: 6, endLine: 7}); // if
expect(response).toContain({startLine: 8, endLine: 9}); // else
expect(response).toContain({startLine: 12, endLine: 17}); // switch
expect(response).toContain({startLine: 13, endLine: 14}); // case
expect(response).toContain({startLine: 15, endLine: 16}); // default
expect(response).toContain({startLine: 20, endLine: 21}); // defer
expect(response).toContain({startLine: 22, endLine: 23}); // placeholder
expect(response).toContain({startLine: 24, endLine: 25}); // error
expect(response).toContain({startLine: 26, endLine: 27}); // loading
expect(response).toContain({startLine: 30, endLine: 31}); // for
expect(response).toContain({startLine: 32, endLine: 33}); // empty
});

describe('signature help', () => {
it('should show signature help for an empty call', async () => {
client.sendNotification(lsp.DidOpenTextDocumentNotification.type, {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@
"test:legacy-syntaxes": "yarn compile:syntaxes-test && yarn build:syntaxes && jasmine dist/syntaxes/test/driver.js"
},
"dependencies": {
"@angular/language-service": "17.0.0-next.7",
"@angular/language-service": "17.0.0-next.8",
"typescript": "5.1.3",
"vscode-html-languageservice": "^4.2.5",
"vscode-jsonrpc": "6.0.0",
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"ngserver": "./bin/ngserver"
},
"dependencies": {
"@angular/language-service": "17.0.0-next.7",
"@angular/language-service": "17.0.0-next.8",
"vscode-html-languageservice": "^4.2.5",
"vscode-jsonrpc": "6.0.0",
"vscode-languageserver": "7.0.0",
Expand Down
24 changes: 17 additions & 7 deletions server/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -820,24 +820,34 @@ export class Session {
}
}

private onFoldingRanges(params: lsp.FoldingRangeParams) {
if (!params.textDocument.uri?.endsWith('ts')) {
return null;
}

private onFoldingRanges(params: lsp.FoldingRangeParams): lsp.FoldingRange[]|null {
const lsInfo = this.getLSAndScriptInfo(params.textDocument);
if (lsInfo === null) {
return null;
}
const {scriptInfo} = lsInfo;
const {scriptInfo, languageService} = lsInfo;
const angularOutliningSpans = languageService.getOutliningSpans(scriptInfo.fileName);
const angularFoldingRanges = angularOutliningSpans.map(outliningSpan => {
const range = tsTextSpanToLspRange(
scriptInfo, {start: outliningSpan.textSpan.start, length: outliningSpan.textSpan.length});
// We do not want to fold the line containing the closing of the block because then the
// closing character (and line) would get hidden in the folding range. We only want to fold
// the inside and leave the start/end lines visible.
const endLine = Math.max(range.end.line - 1, range.start.line);
return lsp.FoldingRange.create(range.start.line, endLine);
});

if (!params.textDocument.uri?.endsWith('ts')) {
return angularFoldingRanges;
}
const sf = this.getDefaultProjectForScriptInfo(scriptInfo)?.getSourceFile(scriptInfo.path);
if (sf === undefined) {
return null;
}
const virtualHtmlDocContents = getHTMLVirtualContent(sf);
const virtualHtmlDoc =
TextDocument.create(params.textDocument.uri.toString(), 'html', 0, virtualHtmlDocContents);
return htmlLS.getFoldingRanges(virtualHtmlDoc);
return [...htmlLS.getFoldingRanges(virtualHtmlDoc), ...angularFoldingRanges];
}

private onDefinition(params: lsp.TextDocumentPositionParams):
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@
uuid "^8.3.2"
yargs "^17.0.0"

"@angular/[email protected].7":
version "17.0.0-next.7"
resolved "https://registry.yarnpkg.com/@angular/language-service/-/language-service-17.0.0-next.7.tgz#795731f9a0bc7dd69c49c51b49a9e4af75c2cafc"
integrity sha512-eC0OyhKq9+mZ+uSrb2upmC1kXWbHajV199W/YvFTY0N5yI5dAdRziRfp1ntB8gK/nCmdtpKugP6As+yNAKwK6w==
"@angular/[email protected].8":
version "17.0.0-next.8"
resolved "https://registry.yarnpkg.com/@angular/language-service/-/language-service-17.0.0-next.8.tgz#1366bd68bcd3dfe4d315636b31b54bf06adf85e5"
integrity sha512-c/Dk6OwV1UQd6a51WzYhPlsbg+lLi/YjZsWtSyvp3Q5lQp5I1q2lZfXSPrzGgJ7S0Fhrge69wcno4UWvWkrS9g==

"@assemblyscript/loader@^0.10.1":
version "0.10.1"
Expand Down