/tree-sitter-angular

Tree Sitter Grammar for Angular

Primary LanguagePythonMIT LicenseMIT

✨ Tree Sitter Grammar for Angular ✨

GitHub Workflow Status (with event) Angular Tree-sitter CLI version

Specification

This parser is a complete rewrite of steelsojka's tree-sitter-angular. This parser extends tree-sitter-html because the new Control Flow syntax is not valid HTML code.

Supported Features

  • Structural Directives
  • Property binding
  • Event binding
  • String interpolation
  • If-statements (v17)
  • For-statements (v17)
  • Switch-statements (v17)
  • Defer-statements (v17)

Filetype

By default Angular's template files are marked as HTML. In order for this parser to work, it has to be marked as angular. Currently Neovim does not do that yet, so to automatically set the filetype for Angular components, put:

autocmd BufRead,BufEnter *.component.html set filetype=angular

in ~/.config/nvim/ftdetect/angular.vim.

Alternatively, you can use :set filetype=angular on a given buffer.

Plugins

As this parser works on angular filetypes, it will cause other plugins to possibly not work correctly. Below are some fixes for the plugins that I use which I had to modify.

VonHeikemen/lsp-zero.nvim

Add this to your config:

    local config = require("lspconfig")
    local util = require("lspconfig.util")

    config.angularls.setup({
      root_dir = util.root_pattern("angular.json", "project.json"), -- This is for monorepo's
      filetypes = { "angular", "html", "typescript", "typescriptreact" },
    })

stevearc/conform.nvim

Add this to your config:

    conform.setup({
      formatters_by_ft = {
        angular = { "prettier" },
      }

      ...
    }

numToStr/Comment.nvim

Add this to your config:

  config = function()
    local comment = require("Comment")
    local ft = require("Comment.ft")

    local commentstr = "<!--%s-->"

    ft.set("angular", { commentstr, commentstr })

    comment.setup()
  end,

Issues

If you experience any issues, please feel free to open an issue with the code that's causing problems.