language: Respect combined injection sub-ranges for language queries#48522
Merged
Veykril merged 1 commit intozed-industries:mainfrom Feb 13, 2026
Merged
Conversation
Follow-up to zed-industries#41111. PR zed-industries#41111 introduced combined injection handling, but cursor language queries still relied on layer range selection alone. For combined injections this can surface a language from the outer combined layer even when the cursor is outside that language's actual included sub-range. So, we just need to add sub-range filtering based on anchor-aware boundary checks. And apply it in `Buffer::language_at`, `Buffer::languages_at`, and `BufferSnapshot::language_scope_at` (all places that rely on the described behavior). I also added some additional test cases for the `HTML+ERB` lang to verify language resolution for HTML and Ruby positions.
vitallium
commented
Feb 5, 2026
Comment on lines
-1725
to
-1732
| is_first = false; | ||
| return true; | ||
| } | ||
|
|
||
| layer | ||
| .included_sub_ranges | ||
| .map(|sub_ranges| { | ||
| sub_ranges.iter().any(|sub_range| { |
Contributor
Author
There was a problem hiding this comment.
Extracted that as offset_in_sub_ranges since I need it in multiple places.
vitallium
commented
Feb 5, 2026
| .layers_for_range(offset..offset, &self.text, false) | ||
| .layers_for_range(offset..offset, text, false) | ||
| .filter(|layer| { | ||
| // For combined injections, check if offset is within the actual sub-ranges. |
Contributor
Author
There was a problem hiding this comment.
HTML+ERB is quite tricky language since it:
- Defines itself as
HTML+ERBlanguage - Injects HTML
- Injects Ruby
Let's take the following example:
<div>Hello</div>
<%= yield %>
<p>World</p>- HTML+ERB lang is active in
<%| - HTML lang is active in
<div>Hello</div>and<p>World</p>. - Ruby lang is active only inside
<%= yield %>.
So, when a file mixes languages, HTML+ERB for example, the injected language (Ruby or HTML) is only valid in specific spans, so we check those real sub-ranges at the cursor
position to avoid reporting the wrong lang and breaking language-specific actions.
vitallium
commented
Feb 5, 2026
| } | ||
|
|
||
| #[gpui::test] | ||
| fn test_languages_at_for_combined_injections(cx: &mut App) { |
Contributor
Author
There was a problem hiding this comment.
Copy&pasted both tests, I hope that's OK? :D
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #41111.
PR #41111 introduced combined injection handling, but cursor language queries still relied on layer range selection alone. For combined injections this can surface a language from the outer combined layer even when the cursor is outside that language's actual included sub-range. So, we just need to add sub-range filtering based on anchor-aware boundary checks in the same way we did in the previous PR. That means: apply it in
Buffer::language_at,Buffer::languages_at, andBufferSnapshot::language_scope_at. All places that rely on the described behavior.I also added some additional test cases for the
HTML+ERBlang to verify language resolution for HTML and Ruby positions.Thank you!
Closes #48358
Release Notes: