Repository: helix-editor/helix
File: helix-term/src/ui/popup.rs (Rust)

Code before:

    /// Toggles the Popup's scrollbar.
    /// Consider disabling the scrollbar in case the child
    /// already has its own.
    pub fn with_scrollbar(mut self, enable_scrollbar: bool) -> Self {
        self.has_scrollbar = enable_scrollbar;
        self
    }

    pub fn contents(&self) -> &T {
        &self.contents
    }

    pub fn contents_mut(&mut self) -> &mut T {
        &mut self.contents
    }

    pub fn area(&mut self, viewport: Rect, editor: &Editor) -> Rect {
        // trigger required_size so we recalculate if the child changed
        let (width, height) = self
            .required_size((viewport.width, viewport.height))
            .expect("Component needs required_size implemented in order to be embedded in a popup");

        let position = self
            .position
            .unwrap_or_else(|| editor.cursor().0.unwrap_or_default());

        // if there's a orientation preference, use that
        // if we're on the top part of the screen, do below
        // if we're on the bottom part, do above

        // -- make sure frame doesn't stick out of bounds
        let mut rel_x = position.col as u16;
        let mut rel_y = position.row as u16;
        if viewport.width <= rel_x + width {
            rel_x = rel_x.saturating_sub((rel_x + width).saturating_sub(viewport.width));
        }

        // TODO can_put_below should be useless since MIN_HEIGHT is 4 and there is always 4 lines
        // below the cursor (ask a maintainer and remove the code related to can_put_below).
        let can_put_below = viewport.height > rel_y + MIN_HEIGHT;
        let can_put_above = rel_y.checked_sub(MIN_HEIGHT).is_some();
        let final_pos = match self.position_bias {
            Open::Below => match can_put_below {
                true => Open::Below,
                false => Open::Above,
            },
            Open::Above => match can_put_above {
                true => Open::Above,
                false => Open::Below,
            },

Review comment (performance):
I think this should stay as `get_ir_insert` otherwise the popus move with the cursor. I also have some larger changes for this in #6417 IIRC since cursor calulation changes/is made more efficient there. 

I also wouldn't want to recompute the cursor position all the time (which is slow)

Code after:
    }

    /// Toggles the Popup's scrollbar.
    /// Consider disabling the scrollbar in case the child
    /// already has its own.
    pub fn with_scrollbar(mut self, enable_scrollbar: bool) -> Self {
        self.has_scrollbar = enable_scrollbar;
        self
    }

    pub fn contents(&self) -> &T {
        &self.contents
    }

    pub fn contents_mut(&mut self) -> &mut T {
        &mut self.contents
    }

    pub fn area(&mut self, viewport: Rect, editor: &Editor) -> Rect {
        let child_size = self
            .contents
            .required_size((viewport.width, viewport.height))
            .expect("Component needs required_size implemented in order to be embedded in a popup");

        self.area_internal(viewport, editor, child_size)
    }

    pub fn area_internal(
        &mut self,
        viewport: Rect,
        editor: &Editor,
        child_size: (u16, u16),
    ) -> Rect {
        let width = child_size.0.min(viewport.width);
        let height = child_size.1.min(viewport.height.saturating_sub(2)); // add some spacing in the viewport

        let position = self
            .position
            .get_or_insert_with(|| editor.cursor().0.unwrap_or_default());

        // if there's a orientation preference, use that
        // if we're on the top part of the screen, do below
        // if we're on the bottom part, do above

        // -- make sure frame doesn't stick out of bounds
        let mut rel_x = position.col as u16;
        let mut rel_y = position.row as u16;
        if viewport.width <= rel_x + width {
            rel_x = rel_x.saturating_sub((rel_x + width).saturating_sub(viewport.width));
        }