Repository: zed-industries/zed
File: crates/language/src/buffer.rs (Rust)

Code before:
    /// Returns the primary [`Language`] assigned to this [`Buffer`].
    pub fn language(&self) -> Option<&Arc<Language>> {
        self.language.as_ref()
    }

    /// Returns the [`Language`] at the given location.
    pub fn language_at<D: ToOffset>(&self, position: D) -> Option<Arc<Language>> {
        let offset = position.to_offset(self);
        let mut is_first = true;
        let start_anchor = self.anchor_before(offset);
        let end_anchor = self.anchor_after(offset);
        self.syntax_map
            .lock()
            .layers_for_range(offset..offset, &self.text, false)
            .filter(|layer| {
                if is_first {
                    is_first = false;
                    return true;
                }

                layer
                    .included_sub_ranges
                    .map(|sub_ranges| {
                        sub_ranges.iter().any(|sub_range| {
                            let is_before_start = sub_range.end.cmp(&start_anchor, self).is_lt();
                            let is_after_end = sub_range.start.cmp(&end_anchor, self).is_gt();
                            !is_before_start && !is_after_end
                        })
                    })
                    .unwrap_or(true)
            })
            .last()
            .map(|info| info.language.clone())
            .or_else(|| self.language.clone())
    }

    /// Returns each [`Language`] for the active syntax layers at the given location.
    pub fn languages_at<D: ToOffset>(&self, position: D) -> Vec<Arc<Language>> {
        let offset = position.to_offset(self);
        let mut languages: Vec<Arc<Language>> = self
            .syntax_map
            .lock()
            .layers_for_range(offset..offset, &self.text, false)
            .map(|info| info.language.clone())
            .collect();

        if languages.is_empty()
            && let Some(buffer_language) = self.language()
        {
            languages.push(buffer_language.clone());
        }

Review comment (bug):
HTML+ERB is quite tricky language since it:

- Defines itself as `HTML+ERB` language
- Injects HTML
- Injects Ruby

Let's take the following example:

```erb
<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.

Code after:
    pub fn language_at<D: ToOffset>(&self, position: D) -> Option<Arc<Language>> {
        let offset = position.to_offset(self);
        let text: &TextBufferSnapshot = &self.text;
        self.syntax_map
            .lock()
            .layers_for_range(offset..offset, text, false)
            .filter(|layer| {
                layer
                    .included_sub_ranges
                    .is_none_or(|ranges| offset_in_sub_ranges(ranges, offset, text))
            })
            .last()
            .map(|info| info.language.clone())
            .or_else(|| self.language.clone())
    }

    /// Returns each [`Language`] for the active syntax layers at the given location.
    pub fn languages_at<D: ToOffset>(&self, position: D) -> Vec<Arc<Language>> {
        let offset = position.to_offset(self);
        let text: &TextBufferSnapshot = &self.text;
        let mut languages: Vec<Arc<Language>> = self
            .syntax_map
            .lock()
            .layers_for_range(offset..offset, text, false)
            .filter(|layer| {
                // For combined injections, check if offset is within the actual sub-ranges.
                layer
                    .included_sub_ranges
                    .is_none_or(|ranges| offset_in_sub_ranges(ranges, offset, text))
            })
            .map(|info| info.language.clone())
            .collect();

        if languages.is_empty()
            && let Some(buffer_language) = self.language()
        {
            languages.push(buffer_language.clone());
        }

        languages
    }

    /// An integer version number that accounts for all updates besides
    /// the buffer's text itself (which is versioned via a version vector).
    pub fn non_text_state_update_count(&self) -> usize {
        self.non_text_state_update_count
    }

    /// Whether the buffer is being parsed in the background.
    #[cfg(any(test, feature = "test-support"))]
    pub fn is_parsing(&self) -> bool {