Skip to content

fix: color a code block of the documentation by the TextMate grammar of its own language - #2086

Merged
lagergren merged 1 commit into
redhat-developer:mainfrom
lagergren:fix/highlight-a-code-block-with-the-textmate-grammar-of-its-language
Nov 22, 2026
Merged

lagergren merged 1 commit into
redhat-developer:mainfrom
lagergren:fix/highlight-a-code-block-with-the-textmate-grammar-of-its-language

Conversation

@lagergren

@lagergren lagergren commented Nov 22, 2026 •

Copy link
Copy Markdown
Collaborator

Part of #557.

What

#557's case works on main: in the documentation of a Rust file, the // comments of a ```rust code block are gray, as in the editor. But when IntelliJ has no language for the language of a code block, LSP4IJ colors it with a TextMate grammar, and took the grammar of the file whose documentation it is first:

Code block Documentation of Before
```python a Rust file colored by the rules of Rust: its # comments aren't gray
```python a text file not colored
```rust a text file not colored
```python a Python file colored as Python

The language of the code block was used only when the file has no grammar, and only as a file extension, so python and rust, which aren't ones, found nothing unless a language server's mapping gave them an extension.

Change

TextMateHighlighterHelper.findLanguageDescriptor takes the grammar of the code block's language first:

  • the grammar of a file extension that a language server's mapping gives the language, as before;
  • or else that of the language's name as a file extension, as ts;
  • or else that of the file extension of the name, for the language identifiers of the LSP specification and names that code blocks often use, whose name isn't a file extension: rust for .rs, python for .py, typescript for .ts, shellscript and bash for .sh, and so on. The names are compared in lower case.

The grammar of the file comes after, for a code block without a language, as before, and for one whose language has no grammar, which it colored before too. Code blocks whose language IntelliJ has, as Java, are colored by its lexer, as before.

Test

  • MarkdownConverterWithPsiFileTest.testACodeBlockIsColoredByTheTextMateGrammarOfItsLanguage: a ```python code block renders the same in the documentation of a Rust file and of a text file as in that of a Python file, and a ```rust one the same in that of a text file as in that of a Rust file. It compares the renderings with each other, not with colors, which differ between IntelliJ versions. Without the change, the Python code block in the documentation of a Rust file differs.
  • testACodeBlockOfALanguageWithoutAGrammarIsColoredByTheGrammarOfTheFile: a code block of a language without a grammar renders as one without a language, by the grammar of the file, as before.
  • The documentation tests pass (33).

…of its own language

When IntelliJ has no language for the language of a code block, the
TextMate grammar of the file whose documentation it is came first. So
```python in the documentation of a Rust file was colored by the rules of
Rust, and its comments weren't gray. The language of the code block now
comes first, also when its name isn't a file extension, as rust for .rs
or python for .py, and the grammar of the file is used for a code block
whose language has none, as before.
@coderabbitai

coderabbitai Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • Bug Fixes
    • Fenced code blocks in Markdown documentation now use syntax highlighting for recognized language labels, even when the documentation file uses a different grammar.
    • Code blocks with unrecognized or missing language labels continue to use the documentation file’s grammar. This makes highlighting more consistent across documentation pages and code examples, including Python and Rust snippets.

The Markdown highlighter now selects a TextMate grammar using a code block’s language before falling back to the documentation file. Tests cover recognized, unknown, and unlabeled languages.

Changes: Markdown code-block highlighting

Layer / File(s) Summary
Language-based descriptor selection
src/main/java/com/redhat/devtools/lsp4ij/features/documentation/markdown/TextMateHighlighterHelper.java, src/test/java/com/redhat/devtools/lsp4ij/features/documentation/MarkdownConverterWithPsiFileTest.java
The helper maps language identifiers to extensions and tries a nonblank code-block language before falling back to the documentation filename. Tests check Python and Rust highlighting, plus fallback behavior for unknown and unlabeled languages.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: angelozerr

Merge Risk: Minimal · up to aed66

This changes how documentation code blocks are syntax-colored, so a block now uses the grammar for its own language before the documentation file's grammar. The impact is limited to highlighting, with a fallback for unknown languages. The tests cover the new behavior, so merge risk is minimal.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title states the main change: a documentation code block is now colored by the TextMate grammar of its own language instead of the documentation file's grammar. The changeset matches this, with Te…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description is related to the changeset. It explains that the TextMate grammar selection for fenced code blocks in documentation was wrong, describes the new selection order in TextMateHighlighter…
Full details: Title check

Explanation

The title states the main change: a documentation code block is now colored by the TextMate grammar of its own language instead of the documentation file's grammar. The changeset matches this, with TextMateHighlighterHelper now checking the code block's language first and tests covering the new selection order and fallback.

Full details: Description check

Explanation

The description is related to the changeset. It explains that the TextMate grammar selection for fenced code blocks in documentation was wrong, describes the new selection order in TextMateHighlighterHelper.findLanguageDescriptor, and lists the tests added in MarkdownConverterWithPsiFileTest. It is specific and matches the raw summary.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 292f6f89-dc95-464e-8640-f157a3ab5dc4
Commits

Reviewing files that changed from the base of the PR and between cff452a and aed6653.

Files selected for processing: 2

  • src/main/java/com/redhat/devtools/lsp4ij/features/documentation/markdown/TextMateHighlighterHelper.java
  • src/test/java/com/redhat/devtools/lsp4ij/features/documentation/MarkdownConverterWithPsiFileTest.java

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 0 remain after this review.

@codecov

codecov Bot commented Nov 22, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.48936% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 50.32%. Comparing base (cff452a) to head (aed6653).

Files with missing lines Patch % Lines
...umentation/markdown/TextMateHighlighterHelper.java 91.48% 1 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2086      +/-   ##
==========================================
+ Coverage   50.24%   50.32%   +0.07%     
==========================================
  Files         816      816              
  Lines       33699    33730      +31     
  Branches     6935     6934       -1     
==========================================
+ Hits        16931    16973      +42     
+ Misses      14127    14110      -17     
- Partials     2641     2647       +6     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lagergren lagergren added this to the 0.30.2 milestone Nov 22, 2026
@lagergren
lagergren merged commit a82a94e into redhat-developer:main Nov 22, 2026
10 checks passed
@lagergren lagergren added bug Something isn't working hover labels Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working hover

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant