Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(popup): always use current window when position == "cursor" #338

Merged
merged 4 commits into from
Mar 12, 2024

Conversation

pysan3
Copy link
Contributor

@pysan3 pysan3 commented Feb 18, 2024

Fixes folke/noice.nvim#636.

There are many ways to fix this issue, but I think my approach is the cleanest.

Other options:

  • Overwrite internal.position.win to nil just like win_config.win here. (win_config.win will be nil, but internal.position.win is still unchanged and remembers the old value.)
    win_config.win = internal.position.relative == "win" and internal.position.win or nil
  • Pass win_config instead of internal.position to mod.get_container_info here.
    internal.container_info = mod.get_container_info(internal.position)

Basically we need to pass a table with win = nil to get_window_size() that is called inside get_container_info, or deal with invalid table.win and ignore that value inside that function.

Note

It might be better to relative ~= "win" and 0 or position.win?

Basically the values are relative = "editor"|"win"|"cursor", and "editor" case is dealt with above, so they mean the exact same thing for now, but may not be in the future (when new position value is added).

Copy link

codecov bot commented Feb 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.04%. Comparing base (fc59553) to head (42cfb30).

❗ Current head 42cfb30 differs from pull request most recent head c0e07ad. Consider uploading reports for the commit c0e07ad to get more accurate results

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #338   +/-   ##
=======================================
  Coverage   99.04%   99.04%           
=======================================
  Files          20       20           
  Lines        2733     2735    +2     
=======================================
+ Hits         2707     2709    +2     
  Misses         26       26           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@pysan3
Copy link
Contributor Author

pysan3 commented Feb 25, 2024

@MunifTanjim Any updates?

@MunifTanjim
Copy link
Owner

MunifTanjim commented Feb 28, 2024

What would happen if I open a popup with relative=cursor, then move the cursor to a different window and call popup:update_layout? 🤔

The container should still be the initial window where the popup was opened, right?

@pysan3
Copy link
Contributor Author

pysan3 commented Feb 28, 2024

Oh, I thought position = "cursor" should follow the cursor. So when update_layout, the popup position is remapped to the position / window of which the cursor is positioned right now.


This is how to reproduce the problem I was referring to.

And this PR fixes it by making popup always open from the current focused window.

  1. new popup (relative = "cursor", position = { row = 1, col = 1 })
  2. close it
  3. :vs
  4. :e different file
  5. reopen the popup
  6. <C-w>w (go back to the previous window)
  7. :q (close this window)
  8. (now there's only the new window opened by :vs)
  9. reopen the popup
  10. ERROR ...o/.local/share/nvim/lazy/nui.nvim/lua/nui/utils/init.lua:29: Invalid window id: 1000

When relative = "win" I agree that the popup should stay on the window where it was initially spawned from even after :update_layout() (and do not follow the nvim_get_current_win()).

@pysan3
Copy link
Contributor Author

pysan3 commented Mar 8, 2024

Any updates @MunifTanjim ?

@@ -89,7 +89,7 @@ function mod.get_container_info(position)

return {
relative = position.bufpos and "buf" or relative,
size = utils.get_window_size(position.win),
size = utils.get_window_size(relative == "cursor" and 0 or position.win),
Copy link
Owner

Choose a reason for hiding this comment

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

This will just fix the size. But reference to that invalid window id will remain internally.

Can we try this instead?

diff --git a/lua/nui/layout/utils.lua b/lua/nui/layout/utils.lua
index 6f251b5..66efcc3 100644
--- a/lua/nui/layout/utils.lua
+++ b/lua/nui/layout/utils.lua
@@ -137,7 +137,9 @@ function mod.update_layout_config(component_internal, config)
   if options.relative then
     internal.layout.relative = options.relative
 
-    local fallback_winid = internal.position and internal.position.win or vim.api.nvim_get_current_win()
+    local fallback_winid = internal.position and internal.position.win
+      or internal.layout.relative.type == "cursor" and 0
+      or vim.api.nvim_get_current_win()
     internal.position =
       vim.tbl_extend("force", internal.position or {}, mod.parse_relative(internal.layout.relative, fallback_winid))
 

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fair enough. I'll use that instead.

What do you think about this?

Note

It might be better to relative ~= "win" and 0 or position.win?

Basically the values are relative = "editor"|"win"|"cursor", and "editor" case is dealt with above, so they mean the exact same thing for now, but may not be in the future (when new position value is added).

Copy link
Owner

Choose a reason for hiding this comment

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

nui.nvim also accepts this:

Relative to the buffer position:
```lua
relative = {
type = "buf",
-- zero-indexed
position = {
row = 5,
col = 5,
},
},
```

Which internally translates to

win = current_window
bufpos = position_relative_to_buffer_on_current_window

The bufpos is only valid for a specific buffer, so win needs to be fixed to a window that contains that buffer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh so it is relative = "editor"|"win"|"cursor"|"buf"?

Then I'll use relative == "cursor" and 0.

Copy link
Contributor Author

@pysan3 pysan3 Mar 12, 2024

Choose a reason for hiding this comment

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

@MunifTanjim Fixed!

Please squash merge this PR. The commit history is kinda messy.

@MunifTanjim MunifTanjim force-pushed the fix-popup-cursor-win branch from 42cfb30 to c0e07ad Compare March 12, 2024 06:15
@MunifTanjim MunifTanjim merged commit 3dc46d7 into MunifTanjim:main Mar 12, 2024
4 checks passed
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.

bug: Repeated display “nui.nvim: Invalid window id” error
2 participants