Skip to content

editor: Adding support of PHP and fix combined injection#1970

Merged
huacnlee merged 7 commits intolongbridge:mainfrom
PRRPCHT:tree-sitter-php
Feb 4, 2026
Merged

editor: Adding support of PHP and fix combined injection#1970
huacnlee merged 7 commits intolongbridge:mainfrom
PRRPCHT:tree-sitter-php

Conversation

@PRRPCHT
Copy link
Copy Markdown
Contributor

@PRRPCHT PRRPCHT commented Jan 23, 2026

Description

Adds PHP syntax highlighting to the code editor component using tree-sitter-php.

Screenshot

Before:
before

After:
after

How to Test

  1. Run cargo run -p gpui-component-story --example editor
  2. Open a PHP file
  3. The syntax of the file is now highlighted

Checklist

  • [X ] I have read the CONTRIBUTING document and followed the guidelines.
  • [ X] Reviewed the changes in this PR and confirmed AI generated code (If any) is accurate.
  • [ X] Passed cargo run for story tests related to the changes.
  • [ N/A] Tested macOS, Windows and Linux platforms performance (if the change is platform-specific)

@PRRPCHT PRRPCHT changed the title Adding support of PHP in the code editor via tree-sitter-php input: Adding support of PHP in the code editor via tree-sitter-php Jan 23, 2026
@huacnlee
Copy link
Copy Markdown
Member

image

Some tag highlight are not correct.

@PRRPCHT
Copy link
Copy Markdown
Contributor Author

PRRPCHT commented Jan 24, 2026

Thanks for your feedback and the test file!

Indeed after investigations there's a case where the closing html tags are not highlighted. It looks like there's no trivial solution, I'll look more into this as the solution may be somewhere in tree-sitter-php.

A short term solution could be to remove the HTML support, as per default.

@PRRPCHT
Copy link
Copy Markdown
Contributor Author

PRRPCHT commented Feb 2, 2026

It was a tricky one but we made it!

Problem

Tree-sitter-php splits HTML content into separate text nodes around <?php ?> blocks. For example:

<title><?php echo $title; ?></title>

Produces three pieces: text("<title>") → PHP code → text("</title>"). Each text node was being parsed as an independent HTML fragment, so the HTML parser saw </title> in isolation — an orphan closing tag with no matching opener — and couldn't highlight it.

This is a well-known issue (tree-sitter-php#123, nvim-treesitter#2565).

Fix

In the context of the PHP highlighter:

  1. injections.scm: Added (#set! injection.combined) to the text node rule. This is the standard tree-sitter directive that tells the highlighter to merge all disjoint text node ranges before parsing them as HTML.
  2. highlighter.rs: Implemented support for injection.combined in the highlighter (which was already started but unfinished):
    • Before: the highlighter would take each HTML fragment individually (e.g. just </title>). The HTML parser, seeing an orphan closing tag with no context, couldn't make sense of it.
    • After: at startup, injection patterns marked combined are split into a dedicated query. At highlight time, this query scans the entire document and collects all text node byte ranges. These ranges are passed to the HTML parser via Parser::set_included_ranges. The parser then reads the original source but skips over the PHP gaps, seeing one continuous HTML document (<html>...<title>...[skip]...</title>...</html>). Opening and closing tags match correctly, and the resulting byte offsets map straight back to the original file.

This solution was inspired by commented code already present in highlighter.rs (and partly reused) and the implementation of PHP highlighting in Zed. It is not limited to PHP & HTML.

Screenshot

Screenshot 2026-02-01 at 23 41 57

@huacnlee huacnlee changed the title input: Adding support of PHP in the code editor via tree-sitter-php editor: Adding support of PHP in the code editor via tree-sitter-php Feb 2, 2026
@huacnlee huacnlee self-assigned this Feb 2, 2026
@huacnlee huacnlee changed the title editor: Adding support of PHP in the code editor via tree-sitter-php editor: Adding support of PHP and fix combined injection Feb 3, 2026
huacnlee
huacnlee previously approved these changes Feb 3, 2026
@huacnlee huacnlee enabled auto-merge (squash) February 3, 2026 02:36
@huacnlee
Copy link
Copy Markdown
Member

huacnlee commented Feb 3, 2026

Thank you.

@huacnlee huacnlee disabled auto-merge February 4, 2026 01:40
@huacnlee huacnlee merged commit 7f3af1a into longbridge:main Feb 4, 2026
2 of 3 checks passed
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.

2 participants