Skip to content

Allow extensions to override grammar query files #40532

@ck37

Description

@ck37

Problem

Zed extensions cannot override tree-sitter grammar query files, forcing grammar maintainers to choose between supporting Zed or other editors like Neovim, which contradicts tree-sitter's goal of unified grammar definitions.

Current Behavior

When an extension specifies a grammar via repository + rev in extension.toml:

  1. Zed clones the grammar repository
  2. Zed loads queries only from the cloned grammar's queries/ directory
  3. Zed completely ignores any query files the extension provides in languages/LANG/*.scm

This means extensions cannot provide editor-specific customizations, even though the extension API suggests they should be able to.

Expected Behavior

Extensions should be able to provide override query files that take precedence over the grammar's defaults.

For example, if an extension provides languages/quarto/highlights.scm, Zed should load that file instead of the grammar's queries/highlights.scm.

Precedence order should be:

  1. Extension-provided queries (languages/quarto/*.scm)
  2. Grammar-provided queries (queries/*.scm)
  3. Grammar-provided editor-specific queries (queries/zed/*.scm)

Real-World Impact

This limitation has concrete consequences:

  1. Grammar fragmentation: Grammar authors must create editor-specific forks or branches, defeating the purpose of tree-sitter as a unified parsing solution.

  2. Scope incompatibility: Zed themes use legacy scope names (@text.title, @emphasis.strong) while nvim-treesitter uses modern conventions (@markup.heading, @markup.bold). Without extension overrides, grammars must choose one editor to support well.

  3. Maintenance burden: Query files must be duplicated across grammar repositories and extension repositories, as noted in discussion ❓ Why does Zed encourage fragmentation of Tree Sitter queries between editors? #16860.

  4. Tree-sitter design contradiction: Tree-sitter documentation states "all files needed to highlight a given language are normally included in the same git repository as the Tree-sitter grammar," yet Zed's current architecture encourages the opposite.

Example Case

The zed-quarto-extension needs to use tree-sitter-quarto but cannot because:

  • tree-sitter-quarto uses nvim-treesitter scopes (@markup.*) in its default queries/highlights.scm
  • Zed themes only recognize legacy scopes (@text.*, @emphasis.strong)
  • The extension provides Zed-compatible queries in languages/quarto/highlights.scm
  • Zed ignores the extension's queries and loads the grammar's incompatible queries
  • Result: No syntax highlighting in Zed

Workaround: Fork the grammar or maintain a separate branch with Zed-specific queries, creating exactly the fragmentation tree-sitter was designed to prevent.

Precedent from Other Editors

  • Neovim: Extensions/plugins can override grammar queries using runtime paths
  • VSCode: Language extensions can provide custom TextMate grammars
  • Helix: Query override via configuration is supported

Related

Implementation Notes

One approach could be to check for query files in this order:

  1. Extension's languages/LANG/highlights.scm
  2. Grammar's queries/highlights.scm
  3. Fall back to grammar's queries/zed/highlights.scm if present

This would allow extensions to provide editor-specific customizations while still using upstream grammars, maintaining the tree-sitter philosophy of grammar reuse across editors.

Metadata

Metadata

Assignees

No one assigned

    Labels

    supportUser support (non-defect troubleshooting, documentation, etc)

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions