Skip to content

LSP support for HEEX ~H sigil contents & tree-sitter-heex - #75

Open
superhawk610 wants to merge 46 commits into
remoteoss:mainfrom
superhawk610:feat/treesitter-heex
Open

LSP support for HEEX ~H sigil contents & tree-sitter-heex#75
superhawk610 wants to merge 46 commits into
remoteoss:mainfrom
superhawk610:feat/treesitter-heex

Conversation

@superhawk610

@superhawk610 superhawk610 commented Jun 6, 2026

Copy link
Copy Markdown
Contributor

Resolves #74.

This PR extends the existing Elixir tokenizer to parse the contents of ~H sigils and resolve any function components and expressions contained within so that textDocument/definition works. This allows all common LSP functionality within ~H sigils, commonly found in Phoenix LiveView render functions and function components. It also extends the existing tree-sitter-elixir parser with tree-sitter-heex in nested sub-trees within sigils.

HEEX Parsing

TokenizeHEEX will only output a minimal subset of HEEX contents:

  • TokModule / TokIdent for function components
  • any Elixir tokens within <% .. %> / { .. } interpolations
  • TokHEEXOpenTag / TokHEEXCloseTag for HTML < and </ tags
  • TokEOL for end-of-line

All other HEEX contents are ignored during tokenization.

The parser has also been extended to read .foo as a function call to foo() when it occurs immediately after a TokHEEXOpenTag / TokHEEXCloseTag. Function components with a module prefix, e.g. <Foo.bar />, are already handled by the existing parser.

Tree-sitter Integration

The existing tree-sitter tree stored by the document cache is a single tree with a single language. It's been extended with new Tree / TreeNode types that support nested trees with different languages. A typical Elixir document will now be modeled with a 3-level Elixir->HEEX->Elixir tree:

t1 := &Tree{   /* def render(assigns) do\n~H"<div class={foo()} />"\nend */
  Root: nil,
  Trunk: &rootElixirTree,
  Language: LangElixir,
  Branches: {
    NodeId: t2 := &Tree{   /* <div class={foo()} /> */
      Root: t1.TrunkNode(),
      Trunk: &nestedHeexTree,
      Language: LangHeex,
      Branches: {
        NodeId: t3 := &Tree{   /* foo() */
          Root: t2.TrunkNode(),
          Trunk: &nestedElixirTree,
          Language: LangElixir,
          Branches: nil,
        },
      },
    },
  },
}

The TreeNode type is a light wrapper around *tree_sitter.Node that also tracks which Tree the node is a member of, facilitates traversal between nested trees, and wraps common methods like StartByte(), EndByte(), StartPosition(), EndPosition(), and Utf8Text() to correctly handle byte offsets from the root tree.


Note

Medium Risk
Touches core document parsing, caching, and all tree-sitter-driven LSP paths; scope is large but covered by new HEEX and tree tests and an index version bump.

Overview
Adds Phoenix HEEX support inside ~H sigils so LSP navigation (go-to-definition, references, hover, completion, rename, highlights) works on LiveView templates, not only plain Elixir.

The tokenizer now lexes ~H bodies via TokenizeHeex (components like <.foo />, <Foo.bar />, {...} / <% %> interpolations) and maps <.name after HEEX open tags to function calls in the reference indexer. ExpressionAtCursor gains HEEX-aware cursor context tests.

Tree-sitter is refactored from a single Elixir parse to a nested treesitter.Tree: Elixir trunk with HEEX branches on ~H quoted_content and Elixir branches on HEEX expression_value, using tree-sitter-heex. DocumentStore keeps per-language parsers and caches this composite tree; LSP handlers call methods on *treesitter.Tree (e.g. FindVariableOccurrences) so positions resolve across nested languages.

Index version is bumped to 13 for the parser/index changes.

Reviewed by Cursor Bugbot for commit 713f6f3. Bugbot is set up for automated code reviews on this repo. Configure here.

Comment thread internal/lsp/elixir.go Outdated
Comment thread internal/lsp/elixir.go Outdated
@JesseHerrick

Copy link
Copy Markdown
Member

Hey @superhawk610, nice work on this! However, I would suggest that we take a slightly different approach. I think that we should index HEEX (both inside sigils and heex files) as well using the tokenizer and parser. My reasoning:

  • We plan on getting rid of tree-sitter at some point. It adds quite a bit of complexity.
  • In this current approach, go to definition works but go to references wouldn't. We also gain other features by indexing these files.

I realize that this is more complex to build up front, but I think the end result would be well worth it.

Comment thread internal/lsp/elixir.go Outdated
Comment thread internal/lsp/elixir.go Outdated
Comment thread internal/treesitter/variables.go Outdated
@superhawk610

Copy link
Copy Markdown
Contributor Author

I'm working on indexing HEEX with the tokenizer/parser so it's incorporated with the existing process. I'm not confident I can write a full tokenizer for HEEX's grammar, so I'm going with a hybrid approach for now that still shells out to tree-sitter-heex. I'm hoping that once this first step is done, it will provide most of the plumbing and we'll just need a tokenizer for HEEX.

Comment thread internal/parser/tokenizer.go Outdated
Comment thread internal/treesitter/variables.go Outdated
Comment thread internal/treesitter/variables.go Outdated
Comment thread internal/treesitter/variables.go Outdated
Comment thread internal/lsp/server.go Outdated
Comment thread internal/parser/tokenizer.go Outdated
Comment thread internal/treesitter/variables.go Outdated
Comment thread internal/treesitter/variables.go Outdated

@superhawk610 superhawk610 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point go-to definition works, but find all references doesn't since the tree-sitter parsing in variables.go treats HEEX sigil contents as opaque. I've taken a pass at a possible approach through this using nested HEEX tree-sitter trees, but this has grown quite a bit in scope. I think a good next step would be to think this through a bit more thoroughly at a high level and plan the implementation more explicitly. What do you think?

Comment thread internal/parser/tokenizer.go
@JesseHerrick

Copy link
Copy Markdown
Member

I'm working on indexing HEEX with the tokenizer/parser so it's incorporated with the existing process. I'm not confident I can write a full tokenizer for HEEX's grammar, so I'm going with a hybrid approach for now that still shells out to tree-sitter-heex. I'm hoping that once this first step is done, it will provide most of the plumbing and we'll just need a tokenizer for HEEX.

I can take a stab at this part if you'd like - I can't promise a specific timeline though. Unfortunately, we can't ship something that shells out to tree-sitter during indexing. It's extremely important that we don't have performance regressions during indexing. There's too much overhead in calling out to tree-sitter. Remote's codebase has >57k files to parse, so we need indexing to be as fast as possible.

@superhawk610

superhawk610 commented Jun 7, 2026

Copy link
Copy Markdown
Contributor Author

No expectations at all on timeline! I've gotten this to handle what I need (go-to definition within HEEX sigils), and I don't mind just building from my fork if this ends up being too much work or too niche. Totally agree on avoiding performance regressions, Dexter's speed is its best selling point amongst my peers! 😎

Please feel free to contribute whatever you'd like to this branch, take over or use some/any of this PR, or close it out if it's not in the cards.

@JesseHerrick

Copy link
Copy Markdown
Member

I'll see if I can take a stab at it on this PR sometime this week and we can ship it together. In the meantime, feel free to keep shipping things to this branch and I'll add stuff when I can.

Comment thread internal/parser/tokenizer.go Outdated
Comment thread internal/lsp/documents.go Outdated
Comment thread internal/lsp/documents.go Outdated
Comment thread internal/lsp/server.go Outdated
Comment thread internal/treesitter/tree.go Outdated
@superhawk610

superhawk610 commented Jun 9, 2026

Copy link
Copy Markdown
Contributor Author

I started on a barebones HEEX tokenizer. The line between tokenizer and parser is a bit blurry and this may lean a bit far into parsing. My aim is to get a simple replacement for tree-sitter-heex that can tokenize at minimum: TokModule, TokIdent, TokDot, and recursively tokenize interpolated expressions. If this seems to be moving in the right direction, I may have some more time later in the week to continue.

Comment thread internal/parser/tokenizer.go
Comment thread internal/parser/tokenizer.go Outdated
Comment thread internal/parser/tokenizer.go Outdated
Comment thread internal/parser/tokenizer_test.go
Comment thread internal/treesitter/tree.go
Comment thread internal/parser/tokenizer.go
Comment thread internal/parser/tokenizer.go
Comment thread internal/parser/tokenizer.go Outdated
Comment thread internal/parser/tokenizer.go Outdated
Comment thread internal/parser/tokenizer_test.go
@superhawk610

superhawk610 commented Jun 10, 2026

Copy link
Copy Markdown
Contributor Author

OK, HEEX tokenizer is now on par with the tree-sitter-heex approach, though lacking some edge-case coverage.

  • scanInterpolation / scanUntil don't handle early occurrences of the terminator, e.g. <div class={"{}"}> will terminate at the first } rather than the second
  • HEEX special forms e.g. <%= for, <%= case, etc. aren't parsed correctly
  • malformed HTML can probably get the tokenizer stuck in an infinite loop added FuzzTokenizeHeex and caught a couple degenerate cases, ran for 10 minutes afterward without catching anything else
  • find all references still doesn't work, as the treesitter module needs to be updated to traverse the new heex nested sub-tree

Comment thread internal/parser/tokenizer.go
Comment thread internal/parser/tokenizer.go Outdated
Comment thread internal/treesitter/tree.go
Comment thread internal/treesitter/variables.go Outdated
Comment thread internal/treesitter/variables.go Outdated
Comment thread internal/treesitter/tree.go Outdated
Comment thread internal/lsp/server_test.go Outdated
Comment thread internal/treesitter/variables.go
@superhawk610 superhawk610 changed the title LSP textDocument/definition support for HEEX ~H sigil via tree-sitter-heex LSP support for HEEX ~H sigil contents & tree-sitter-heex Jun 11, 2026
Comment thread internal/parser/tokenizer.go
Comment thread internal/parser/tokenizer.go Outdated
@superhawk610
superhawk610 force-pushed the feat/treesitter-heex branch from 4c40ca7 to 429f6bd Compare June 11, 2026 19:14

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 429f6bd. Configure here.

Comment thread internal/treesitter/variables.go
@superhawk610

Copy link
Copy Markdown
Contributor Author

Alright @JesseHerrick updated the approach per your request. HEEX files and sigil contents are now fully indexed and the existing tree-sitter-elixir tree recursively parses nested HEEX/Elixir sub-trees. TokenizeHeex aims to parse as little of the HEEX contents as is necessary to find relevant Elixir function calls and interpolations.

Let me know what you think!

Comment thread internal/parser/tokenizer.go Outdated
Comment thread internal/parser/tokenizer.go Outdated
Comment thread internal/parser/tokenizer.go Outdated
Comment thread internal/parser/tokenizer.go
Comment thread internal/parser/tokenizer.go Outdated
Comment thread internal/parser/parser_tokenized.go
Comment thread internal/parser/parser_tokenized.go Outdated
Comment thread internal/treesitter/tree.go Outdated
Comment thread internal/treesitter/tree.go Outdated
Comment thread internal/treesitter/variables.go
Comment thread internal/treesitter/variables.go
Comment thread internal/treesitter/variables.go Outdated
@superhawk610

superhawk610 commented Jul 15, 2026

Copy link
Copy Markdown
Contributor Author

@JesseHerrick thanks for the thorough review! I'm working through addressing your feedback this week, I'll try to get what I can pushed up by this weekend.

As I've been using this locally over the past month or so, I've realized another small gap - string interpolation doesn't output any interpolated tokens, so go-to definition like this doesn't work:

defmodule MyModule do
  def run do
    "foo #{bar()} baz"
    #      ^^^^^
    #      go-to definition here should jump to `defp bar`
  end

  defp bar, do: ".."
end 

This isn't strictly related to the HEEX tokenization this PR does, but I was able to get it working with minor changes to scanInterpolation and the other work in this PR. It's a mostly transparent change, the only thing that's not immediately obvious is how to represent this in the output token stream. Here's my first pass:

 "foo #{bar()} baz"       {         bar          (            )             }
[    TokString       TokOpenBrace TokIdent TokOpenParen TokCloseParen TokCloseBrace]

This emits a single TokString that spans the entire length, including interpolations, followed by any tokens produced by interpolation. That's the same thing we're currently doing for TokSigil. All that's required is updating the expected output on a handful of tests, mainly in tokenizer_test.go.

If it's out of scope for this PR no worries, just thought I'd mention here since I've been enjoying having it locally.


EDIT: Alright, I've addressed most of the outstanding feedback and pushed up my work so far. It includes the above changes to string interpolation. I still need to address the comments about variable resolution in variables.go, hoping I'll get to that sometime early next week.

EDIT 2: Resolved 2/3 outstanding issues. Tracking whether a token is logically contained in another token required adding a Parent field to the Token struct, which is a straightforward but non-trivial change worth discussing. This also fixed a latent bug in TokenAtOffset caused by overlapping ranges emitted by container tokens (TokString, TokHeredoc, and TokSigil). Planning to knock out the last issue for scope resolution next, then this should be ready for another round of review.

EDIT 3: Alright, resolved the last issue around HEEX variable scoping. This was outside of my comfort zone so I got Claude's help, and also had it do a pass over everything we've implemented so far and it caught and resolved a few more issues.

Curly interpolation {..} is disabled within script/style tags and when
the phx-no-curly-interpolation attribute is present on a parent tag.

This also fixes a latent issue with `scanInterpolation` where terminator
sequences would always be offset, even if they weren't encountered.
- references to local functions are no longer emitted
- references contained in single-line `def` and module attributes are
  now emitted correctly
- function component refs now correctly emit only when injectors are
  present
- HEEX expressions now emit references properly
- fixed failing tests for tokenizing string interpolation
- `TokString` now properly comes before the interpolated tokens and
  contains the full Start:End range of the string
@superhawk610
superhawk610 force-pushed the feat/treesitter-heex branch from 713f6f3 to 679fcab Compare July 17, 2026 21:22
superhawk610 and others added 5 commits July 17, 2026 15:07
Tokens now store a Parent which indicates the offset of the token which
logically contains them, if they're part of an interpolation. This is
now taken into account by TokenAtOffset.
Model HEEX bindings (EEx loops, case/cond clause arms, :for/:let) as
byte ranges over the flat sibling directives they span, assign each
occurrence to its smallest active range, and filter rename, collision,
and autocomplete results to the cursor's range. Fixes conflation of
same-named bindings across HEEX directive boundaries.

- FindVariableOccurrences: post-pass range filter (shadowing-safe)
- NameExistsInScopeOf: collision reach matches rename reach
- FindVariablesInScope: prune out-of-scope loop/:let vars
- add TreeNode.RawChild/RawChildCount/NamedChild navigation primitives

Pure-Elixir behavior unchanged (no-op when no sub-trees in scope).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
visitTree only called onLeave for last-sibling nodes, so a
<script>/<style>/phx-no-curly-interpolation tag leaked its
interpolate=false state to following siblings, leaving their {expr}
unparsed. Fire onLeave once per node in post-order.

Also address review findings in the HEEX tokenizer: fix onNode
operator precedence, use startLine for empty sigils, bounds-guard
scanSigilCharacters, enforce tokenizeUntil opener/terminator prefix
invariant with a panic, and rename isStatementStart to couldBeginCall.
Add regression tests for sibling interpolation leakage and nested loops.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Add an ordered branchesByStart index alongside Tree.Branches so
collectHeexRangesInScope binary-searches overlapping sub-trees instead of
linearly scanning the whole map. Branches within a tree are disjoint,
non-nested siblings, so a sorted slice + sort.Search gives O(log n + k).
The map is retained for the O(1) node-ID lookups.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@JesseHerrick

Copy link
Copy Markdown
Member

Sorry for being slow here @superhawk610! This is a major change so will need to spend some time properly digging in here. I'll review when I can.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LSP textDocument/definition support for HEEX ~H sigils

2 participants