Skip to content

fix(#2232): keep the completion menu inside the editor when it overflows - #2234

Open
anyingiit wants to merge 2 commits into
hrsh7th:mainfrom
anyingiit:fix-2232-menu-clamp
Open

anyingiit wants to merge 2 commits into
hrsh7th:mainfrom
anyingiit:fix-2232-menu-clamp

Conversation

@anyingiit

Copy link
Copy Markdown

Fixes #2232

Problem

custom_entries_view.open only moves the menu left when the cursor is in the right half of the editor:

if math.floor(vim.o.columns * 0.5) <= col + border_offset_col and vim.o.columns - col - border_offset_col <= width then

When the cursor is in the left half and the menu is still too wide (a long item, or a narrow terminal as in the issue), the column is left as requested. Neovim then clamps the float on screen by itself, but entries_win.style (and therefore view:info()) still holds the requested col/width. docs_view.open computes left_space/right_space from that info(), so it places the documentation window on what it thinks is free space, which is actually covered by the menu.

Change

Drop the "right half" part of the condition: whenever the menu does not fit to the right of its column, apply the existing clamp (shift left, and shrink if it is wider than the editor). Menus that end before the last column are not touched (the same one-column margin the right-half case already used), and the right-half case behaves as before.

With that, info() describes the geometry Neovim actually draws, and the docs view either finds the real free side or closes when there is no room (the "expected behavior" in the issue). This keeps the fix in the menu instead of reading back nvim_win_get_position, as suggested in the issue, and it can be tested headlessly.

Tests

New lua/cmp/view/custom_entries_view_spec.lua:

  • a menu wider than the editor, opened at column 0, must stay within vim.o.columns
  • a menu opened at column 20 that overflows the right edge must stay within vim.o.columns
  • a menu that fits is not moved

The first two fail without the change and pass with it. vusted ./lua passes (76 successes) with Neovim 0.11.4, luacheck lua reports 0 warnings, and stylua --check is clean on the changed files.

Motivation / disclosure: I had some spare AI-assistant quota (Claude Code) and am using it to try to help projects with open issues. The change was prepared with Claude Code and verified as listed above. If it doesn't fit, isn't up to your bar, or you'd simply rather not take it, please feel free to close it, no hard feelings at all 🙂

… overflows

The menu was only shifted left when the cursor was in the right half of
the editor. When the cursor was in the left half and the menu was too
wide, Neovim clamped the float itself, but the stored geometry still
held the requested column. The docs window is placed from that stored
geometry, so it ended up on top of the menu.

Clamp the column whenever the menu does not fit, so the stored geometry
matches what Neovim draws and the docs view can find the free space (or
close when there is none).
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-22T21:32:18.785470Z 9f6ac8b PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9f6ac8bbba

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

self.bottom_up = false
end
if math.floor(vim.o.columns * 0.5) <= col + border_offset_col and vim.o.columns - col - border_offset_col <= width then
if vim.o.columns - col - border_offset_col <= width then

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Use the offset-adjusted column for overflow checks

When window.completion.col_offset is negative and already moves a left-half menu on screen, this new branch decides overflow from the unoffset col and then applies the offset again in the later open call. With 80 columns, col = 20, width = 65, no border, and col_offset = -10, the code before this change opened at 10 (ending at 75), but this condition now clamps col to 14 and the final window opens at 4; thus a menu that fit is shifted an extra six columns and no longer has the requested alignment. Perform the fit/clamp against the final offset-adjusted column.

Useful? React with 👍 / 👎.

…fset

window.completion.col_offset was added after the fit check, so a negative
offset that already brought the menu on screen still triggered the clamp and
then moved it further left. Apply the offset first and check the fit at the
column the menu actually opens at.
@anyingiit

Copy link
Copy Markdown
Author

Good catch from the bot review: the fit check ran before window.completion.col_offset was applied, so with a negative offset the menu could be pushed further left than needed. The new commit applies the offset first and checks the fit at the column the menu actually opens at. In the example (80 columns, cursor at 20, 72-wide menu, col_offset = -10) it now opens at column 7 and ends at the last column instead of opening at 0. I added a spec for that case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Docs window overlaps the completion menu because positioning uses the requested float geometry instead of Neovim's clamped (rendered) one.

1 participant