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()
        {

Review comment (refactor):
Extracted that as `offset_in_sub_ranges` since I need it in multiple places.

Code after:

    /// 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 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.