Skip to content

About

Angular support for the Zed editor, built with the Angular Language Server and Tree-sitter

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Angular Tools

Angular support for Zed, built on the official Angular Language Server. It works without settings in Angular projects, including nested ones in a monorepo.

  • Templates: Angular syntax highlighting, indentation, outline, and / to close an unfinished tag.
  • Language server: the project's own server or the one for its Angular version, with completion, diagnostics, hover, navigation, rename and inlay hints, in .html files and inline template: strings.
  • Selectors: rename, find references and go to definition across templates and pages such as index.html.
  • Formatting with Prettier's angular parser and the project's configuration.
  • Completion of plain HTML tags and attributes, Tailwind classes and, in inline templates, Emmet abbreviations.
  • Editing both tags of an element together, and the closing tag after >.

Installation

The extension is not in the Zed extension registry yet. Clone this repository, and in Zed run zed: install dev extension and select its folder. Node.js and the servers come through Zed and npm, so nothing else needs installing.

Templates

Files ending in .component.html or .ng.html open as Angular and get Angular syntax highlighting. Other templates, such as app.html, open as HTML: the language server works there too, but Zed picks a language by file name only. For Angular highlighting and formatting in them, map them in the project's .zed/settings.json:

{
  "file_types": { "Angular": ["**/src/**/*.html"] }
}

Renaming a tag in files of the Angular language also renames its other half, as Zed's linked_edits setting allows. The server does not answer this request, so the proxy does it with the HTML language service. In TypeScript files this stays with vtsls.

editor: rename on a plain tag renames both of its tags. The server would take the angle brackets as part of the name and then rename nothing, so the proxy answers this too.

Typing > after an opening tag adds its closing tag, except for void elements such as <input>, as Zed's use_on_type_format setting allows. In inline template: strings of Angular projects the HTML server described below does this, since vtsls does not. Over a WSL or SSH connection Zed leaves this edit out of the undo history, so ctrl-z does not remove the closing tag.

editor: wrap selections in tag works, and elements and control flow blocks count as text objects for Vim mode.

Typing / at the end of an unfinished opening tag closes it: <app-card becomes <app-card/>. The caret ends up after > in Zed builds that support the cursor_after_end bracket option and before it in others.

Language server

The extension runs the official @angular/language-server with Zed's Node.js. When the project has its own server in node_modules, that one runs with the project's TypeScript and @angular/language-service. Otherwise the extension installs the latest server of the project's Angular major version, read from the installed @angular/core or from package.json, and runs it with its own @angular/language-service and the project's TypeScript. Projects on Angular 10 or older, or with a version that names no number and nothing installed, get server 22. Nested projects, such as apps/web in a monorepo, count too. To find them the extension runs a small Node.js script, and when the granted_extension_capabilities setting forbids that, it looks only at the root of the workspace. With several Angular versions in one workspace, the server follows the newest. Each major version is kept, so projects on different Angular versions work side by side, and is brought up to date at most once a day.

Like the official VS Code client, the server starts for every HTML file, so external templates work without any settings. In projects without @angular/core the server turns itself off.

Settings of the official VS Code client work the same under lsp.angular-tools.settings, with the same defaults, for example:

{
  "lsp": {
    "angular-tools": {
      "settings": {
        "angular": {
          "forceStrictTemplates": true,
          "suppressAngularDiagnosticCodes": "2669",
          "inlayHints": { "variableTypes": { "forLoopVariableTypes": true } }
        }
      }
    }
  }
}

The suggest settings and those two options take effect after a restart of the server. The server watches files itself, not through Zed, because Zed sends file changes only to servers that ask for them, and this one leaves that to the VS Code client.

A server that crashes, for example out of memory, starts again with the open documents, up to five times. Requests it left unanswered fail.

The server also formats files of the Angular language with Prettier's angular parser. It uses the project's Prettier and configuration, including .prettierignore and .editorconfig, and falls back to the Prettier installed with the extension and Zed's indentation. Zed hands the formatting of this language to its language server, so it needs no formatter setting: editor: format works as is, and saving formats when format_on_save is on. Only .component.html and .ng.html files open as Angular by default; other templates, such as app.html, need the file_types mapping above. Without it they open as HTML and Zed formats them with its own Prettier, whose html parser leaves {{a}} and the contents of @if blocks as they are. Prettier's angular parser ignores ranges, so format_on_save set to modifications skips these files; modifications_if_available formats them whole.

In TypeScript files, Zed's TypeScript server vtsls does the work: in Angular projects the extension loads the @angular/language-service of the installed server for the project's Angular version into it as a tsserver plugin. Inline templates get completion, diagnostics and navigation, and renaming a component member also updates its templates. Find references on a member lists its uses in templates; the Angular plugin alone would point them into its generated .ngtypecheck.ts files, so the extension's plugin maps them back.

Emmet works in inline template: strings too. The Emmet extension does not cover TypeScript files, so the extension runs its own @olrtg/emmet-language-server for them in Angular projects, behind a proxy that answers only inside these strings. In .html files Emmet still comes from the Emmet extension.

Tailwind classes complete in files of the Angular language in projects that have tailwindcss in package.json. Zed's own Tailwind server covers HTML but not this language, so the extension runs @tailwindcss/language-server for it, with the settings of Zed's server from lsp.tailwindcss-language-server. Do not also add Zed's server to this language in language_servers, or completions appear twice. In Angular projects both servers, including the one in inline templates, also complete class names in bindings such as [class.flex] and unquoted keys in [ngClass]="{flex: a}".

Plain HTML tags and attributes, such as div and class, complete in files of the Angular language and in inline template: strings of Angular projects. The Angular Language Server offers only components and Angular bindings there, so the extension adds a small server built on vscode-html-languageservice and vscode-css-languageservice. It also describes them on hover, and completes and describes CSS in <style> and style="". It stays out of bindings such as [value] and of {{ }}, which remain with the Angular server. Zed colors these completions by the language of the file, so in .ts files they have no color.

Completion also starts right after {{. In templates the Angular Language Server answers it. In inline template: strings vtsls ignores {, so until the first character this server offers the members of the component, without inherited ones, and vtsls takes over from there.

The official language service does not rename selectors, so the extension adds its own small tsserver plugin: renaming app-card or [app-highlight] in a selector string or in a template updates the selector and every matching tag or attribute in all templates and other HTML files of the project, such as index.html. Files outside the project, including targets of symbolic links and of a templateUrl that points elsewhere, are never read or edited. In HTML templates a small proxy in front of the server answers these renames; in projects without @angular/core it only passes messages through.

Ctrl-click on a component tag or a directive attribute opens the selector that declares it, such as selector: 'app-card', in templates and in HTML pages that host a component, such as index.html. The server would open the class and does not know such pages, so the proxy and the plugin answer these requests.

Directives that a component does not import yet, such as [ngClass], complete in templates, and choosing one adds the import to the component and to its imports. The official service offers only directives already in the program, so a library that does not use @angular/common would not get them: for the program of each project with a tsconfig, the extension also loads the Angular libraries in package.json, those with @angular/core as a peer dependency. The service would name the import after an internal file of the package, so the extension replaces that with the package when the package exports the name.

The code action Go to component in an external template opens the component that uses it, the first one if there are several.

Find references on a selector name lists the selector string and every tag or attribute that uses it, again including pages such as index.html. A selector string is its own definition, like any declaration: Ctrl-hover underlines it and Ctrl-click shows these references.

Compatibility

Install either Angular Tools or the Angular extension (angular) from the Zed extension registry, not both. Both register a language named Angular and a grammar named angular, and Zed keeps only one of each, so highlighting and / closing depend on which extension loaded last. Both also start an Angular Language Server, so completions and diagnostics in templates would appear twice.

Development

In Zed, run zed: install dev extension and select this directory.

The tests check the queries against the grammar that the dev install checks out into grammars/angular, the selector rename module, the tsserver plugin and the proxy:

npm install
npm test

License

MIT

About

Angular support for the Zed editor, built with the Angular Language Server and Tree-sitter

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages