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)!: moved="any", enter default, callback=fn #622

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

errael
Copy link

@errael errael commented Sep 19, 2024

(I'm looking at adding mousemoved='any', close='click', and popup_close() to popups; came across a few things to fix first.)

To reviewers
This is my first neovim related PR and my first lua code. Suggestions about style... welcome. Not sure how this repo handles reviews, so I'm tagging some names that have done some popup work (sorry for the noise). There's some TODO I scattered with questions, looking for feedback (would like to remove before merge). There's a probable bug in tbl.apply_defaults, noted in popup/utils.lua.

@l-kershaw @fdschmidt93 @AlejandroSuero @xactlyblue @jesseleite
@Conni2461 @tjdevries

  • Fix moved = 'any' option.
    Before this PR, the popup option "moved = 'any'," gets an exception because of changes made to vim.lsp.util.close_preview_autocmd:

    Those changes looks like a fix. The now private code from "...lsp.util" is copied in here and the the buffer numbers are added as needed for the new code.

  • Default the new option enter to false
    Changed the default to false, so the behavior matches vim with no options; which is to leave the cursor where it was and not change the focus to the popup. Is there a place to note breaking changes? Wonder the reason for "enter" option?

  • Fix callback
    I couldn't get it to work in a simple case. Converted to an autocmd. Comments on potential timing issues? Accessing buffer in callback does work. There's some preliminary work to support "popup_close(win_id, result)" and mouse stuff for a later PR.

  • Update POPUP.md

Tests

The options callback and enter are tested. The option moved only has a partial test which verifies the option can be used (it fails without this PR). #622 (comment) details the problems I'm having with getting the tests to work. Will revisit if/when I understand the trick that's needed.

Could also do ...

There's dict_default(). Would using tbl.apply_defaults make sense? Seems cleaner.
Should tbl.apply_defaults do a deepcopy of any table from defaults?

There was a comment around the callback setup.

Giving win_id is pointless here because it's closed right afterwards
but it might make more sense once hidden is implemented

I've removed the comment. The called back function may want to know the win_id; for example,
there could be a data structure associated with the win_id that can be freed.

PS: For the stuff I'm looking at adding, I could open a discussion or just do the PR and discuss there; recommendation?

@errael errael marked this pull request as draft September 19, 2024 19:39
@AlejandroSuero
Copy link
Contributor

@errael I am not too familiar with the codebase itself but I can solve some of the questions

To run test:

To make test:

To check code style:

  • Check the files you touched and make a similar style.
  • Check the linter passes when using make lint (same Makefile as before).

The next thing I am not so sure about, but I think this PR will "break" things in comparison with the master branch so it would be nice to use the conventional commits syntax to display it, i.e. fix(popup)!: .... Also commenting it in the PR itself often helps to see it before checking the code (at least for me).

@errael errael changed the title fix(popup): moved="any", enter default, callback=fn fix(popup)!: moved="any", enter default, callback=fn Sep 19, 2024
@errael
Copy link
Author

errael commented Sep 19, 2024

@AlejandroSuero Thanks so much; very useful. luacheck was yet another thing to install. The commit syntax you mean is the ! in the message; cool.

I was going to ask about running just popup tests. But got around to doing the following on the command line

nvim --headless --noplugin -u scripts/minimal.vim -c "PlenaryBustedDirectory 
    tests/plenary/popup_spec.lua {minimal_init = 'tests/minimal_init.vim', sequential = true}"

which seemed to work.

It's been so many years since I've messed with makefiles... Wanting to use the Makefile to run only popup_spec.lua, this seems to work

... "PlenaryBustedDirectory tests/plenary/$(TEST1) {mi...

and doing

TEST1=popup_spec.lua make test

Is there something else that makes sense? I'll include this change in the PR, can always take it out.

One test, out of all the tests, is failing. Is this something to file an issue about?

@AlejandroSuero
Copy link
Contributor

@errael to test single files you can change or create a target on the Makefile, the PlenaryBustedDirectory with PlenaryTestFile as indicated in the reference plenary.test_harness that I linked before.

I will stick to the testing the whole suite in case some other modules make use of popup. Also is what is used on CI. But this something that @Conni2461 and @tjdevries should know better to explain why there is no target for test-file passing the file as arguments.

Regarding the test failure, right now Im on my mobile so its a pain to check it tbh, but no ones resolves your question, tomorrow when I wake I see what could be the point of failure 😊

@errael
Copy link
Author

errael commented Sep 20, 2024

I've added some tests. I've run into a problem. In particular async(), from busted, is not found.

Here's the use case

  1. move the cursor with vim.fn.cursor(2, 1)
  2. let the cursor move events and autocommands propagate
  3. check that things happened as expected

The code can be seen in popup_spec.lua around line 195 in the PR after

    it("with moved", function()
      async()
/src/tools/neovim/plenary.nvim/tests/plenary/popup_spec.lua:196: attempt to call global 'async' (a nil value)

@errael
Copy link
Author

errael commented Sep 20, 2024

Regarding the test failure, right now Im on my mobile so its a pain to check it tbh, but no ones resolves your question, tomorrow when I wake I see what could be the point of failure 😊

What I'm seeing

Testing: 	/junk/repo/neovim/plenary.nvim/tests/plenary/scandir_spec.lua	
Success	||	scandir can list all files recursive with cwd	
...
Success	||	scandir ls works for cwd	
Success	||	scandir ls works for another directory	
Fail	||	scandir ls works with opts.hidden for cwd	
            .../repo/neovim/plenary.nvim/tests/plenary/scandir_spec.lua:246: Expected objects to be the same.
            Passed in:
            (boolean) false
            Expected:
            (boolean) true
            
            stack traceback:
            	.../repo/neovim/plenary.nvim/tests/plenary/scandir_spec.lua:246: in function <.../repo/neovim/plenary.nvim/tests/plenary/scandir_spec.lua:240>
            	
	
Success: 	21	
Failed : 	1	
Errors : 	0	

@errael
Copy link
Author

errael commented Sep 20, 2024

Make it ready for review.

Test callback option OK. Test enter option OK.

Test moved option has problems; it's commented out. Pretty sure the test is flawed. I'll address it with the next PR.

@errael errael marked this pull request as ready for review September 20, 2024 05:56
@AlejandroSuero
Copy link
Contributor

@errael The principal issue I see is that async() is not defined (it doesn't exists), so does timer and done.

In my case, scandir_spec.lua doesn't fail.

Here is the error that async throws in my end.

Fail || plenary.popup moved option without moved
     .../aome/personal/plenary.nvim/tests/plenary/popup_spec.lua:179: attempt to call global 'async' (a nil value)
            
     stack traceback:
       .../path/to/plenary.nvim/tests/plenary/popup_spec.lua:179: in function <.../path/to/plenary.nvim/tests/plenary/popup_spec.lua:178>
            
Fail || plenary.popup moved option with moved
     .../path/to/plenary.nvim/tests/plenary/popup_spec.lua:195: attempt to call global 'async' (a nil value)
            
      stack traceback:
        .../path/to/plenary.nvim/tests/plenary/popup_spec.lua:195: in function <.../path/to/plenary.nvim/tests/plenary/popup_spec.lua:194>

@AlejandroSuero
Copy link
Contributor

You can use either vim.defer_fn or vim.schedule_wrap or require "plenary.job" and make use of Job:new.

Some examples in async_testing_spec.

Also in tests/plenary/async/async_spec.lua you can see how to use "plenary.async".tests.add_to_env() to make the it async.

@AlejandroSuero
Copy link
Contributor

@errael I made the following changes, in case you want me to commit it to your branch.

-- at the top
require("plenary.async").tests.add_to_env() -- this lets you use the `a` variable to make it async
-- ...
  a.describe("moved option", function()
    local function populate()
      local wnr = vim.fn.winnr()
      local bnr = vim.fn.winbufnr(wnr)
      vim.fn.setbufline(bnr, 1, {"one", "two", "three", "four"})
      vim.fn.cursor(1, 1)
    end
    local callback_result
    local function callback(wid, result)
      callback_result = result
    end

    a.it("without moved", function()
      -- removed aync()
      callback_result = nil
      populate()
      local win_id = popup.create("hello there", {
        callback = callback,
      })
      -- move the cursor, should not do callback
      vim.fn.cursor(2, 1)
      local timer = vim.uv.new_timer() -- declared timer
      timer:start(10, 0, function()
        eq(nil, callback_result)
        vim.api.nvim_win_close(win_id, true)
      end)
      -- removed done()
    end)

    a.it("with moved", function()
      -- removed async()
      callback_result = nil
      populate()
      local win_id = popup.create("hello there", {
        moved = "any",
        callback = callback,
      })
      -- move the cursor, window closes and callback invoked
      vim.fn.cursor(2, 1)
      local timer = vim.uv.new_timer() -- declared timer
      timer:start(10, 0, function()
        eq(-1, callback_result)
        if -1 ~= callback_result then
          -- window wasn't closed
          vim.api.nvim_win_close(win_id, true)
        end
        -- removed done()
      end)
    end)
  end)

@errael
Copy link
Author

errael commented Sep 20, 2024

You can use either vim.defer_fn or vim.schedule_wrap or require "plenary.job" and make use of Job:new.

Cool. vim.defer_fn looks convenient. I'll play around with those.

Some examples in async_testing_spec.

Also in tests/plenary/async/async_spec.lua you can see how to use "plenary.async".tests.add_to_env() to make the it async.

Thanks for the notes on using async. I'll take a look at the references you provided; appreciated. So the busted async isn't available I guess. I'll have to think about why the done wouldn't be needed.

No need to do the commit, thanks for taking a look. So much new stuff ...

@errael
Copy link
Author

errael commented Sep 20, 2024

I'll update the OP, with status of the tests. But here's a detailed look at the moved = "any" tests that are commented out. There is one of the tests that runs; the test does nothing. But it does fail without this PR because of changes made a few years ago.

Source the following lua file. With the nvim_win_close the callback is invoked before exit. Without it, the callback is invoked immediately after exit. Before sourcing the cursor should not be on the 2nd character. If the cursor is on the 2nd character then no callback and the popup is visible when the script completes; at this point move the cursor and the popup exits.

The cursor move autocmd that triggers the callback does not happen until the script exits. I'm guessing its because the cursor isn't actually moved until it's in the top level main loop (if that's a viable concept) and the screen is drawn. Given this comment in vim.wait

Nvim still processes other events during this time.

I had hopes; but who knows what the "other events" encompasses. I tried various vim.def_fn and vim.wait things.

local Popup = require 'plenary.popup'

local msgs = {}
local function add_msg(msg)
    table.insert(msgs, os.date('!%T') .. ': ' .. msg)
end

local callback_result
local function callback(wid, result)
    add_msg('CALLBACK' .. tostring(result))
    vim.print(os.date('!%T') .. ': CALLBACK ' .. tostring(result))
    callback_result = result
end

add_msg('Start')

callback_result = nil
vim.fn.cursor(1, 1)

local main_wid = vim.fn.win_getid()
local popup_wid = Popup.create("hello there", {
    moved = "any",
    callback = callback,
})
vim.fn.feedkeys(' ', 'x')

add_msg('Exit ' .. tostring(callback_result))
vim.print(vim.inspect(msgs))

Output showing callback after exit

{ "22:55:15: Start", "22:55:15: Exit nil" }
22:55:15: CALLBACK -1

@errael
Copy link
Author

errael commented Sep 21, 2024

@Conni2461 @AlejandroSuero (and any cognizant lurkers) I think this is ready. There's questions, but it seems to work as advertised.

@errael
Copy link
Author

errael commented Sep 21, 2024

In my case, scandir_spec.lua doesn't fail.

@AlejandroSuero The "failure" I'm seeing on my system is expected because I'm using hg-git, not git, so

eq(true, contains_match(dirs, "%.git$"))

fails.

@AlejandroSuero
Copy link
Contributor

@errael I suspect that this is the expected behaviour right?

Screenshot 2024-09-21 at 14 48 38

And when moving the cursor, then it prints something like:

{ "22:55:15: Start", "22:55:15: Exit nil" }
22:55:15: CALLBACK -1

@@ -160,3 +303,4 @@ describe("plenary.popup", function()
end)
end)
end)
-- vim:sw=2 ts=2 et
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be removed cause it can mess up other peoples environments, better to keep it in your config.

Somewhere like path/to/your/config/after/ftplugin/lua.lua if you only want to set it for lua files.

I leave you an example of using this in my config for Golang

Copy link
Author

Choose a reason for hiding this comment

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

So the use of a modeline is not the style in plenary.nvim. It's nice to have stuff local instead of global. Oh well. I'll take it out

While I'm figuring out lua... I'll use

autocmd BufRead */plenary.nvim/*.lua set sw=2 ts=2 sts=2 noet

Copy link
Author

Choose a reason for hiding this comment

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

This should be removed cause it can mess up other peoples environments, better to keep it in your config.

BTW, there's set nomodeline or set modelines=0 to avoid modelines modifying your environment.

Copy link
Author

Choose a reason for hiding this comment

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

I just came across :help editorconfig

Think of it like 'modeline' for an entire (recursive) directory.

Adding such a file to the plenary.nvim repo might be useful.

@AlejandroSuero
Copy link
Contributor

Also I applied the formatter on the file and got this diff

Screenshot 2024-09-21 at 14 56 34

The project uses StyLua for the formatting rules and this is how its applied later on CI.

stylua:
name: stylua
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
- uses: JohnnyMorganz/stylua-action@v2
with:
token: ${{ secrets.GITHUB_TOKEN }}
version: latest
# CLI arguments
args: --color always --check .

You can either use the cli like stylua -f ./.stylua.toml --check . to check for formatting errors or stylua -f ./.stylua.toml . to fix them. Another way is using a plugin to attach formatters, I personally use none-ls the successor of null-ls RIP, but there are others out there.

@errael
Copy link
Author

errael commented Sep 21, 2024

I suspect that this is the expected behaviour right?
...
And when moving the cursor, then it prints something like:

{ "22:55:15: Start", "22:55:15: Exit nil" }
22:55:15: CALLBACK -1

I guess this is the expected behavior, no cursor move events until the screen is drawn with the cursor in a different location; but it makes it difficult to write tests. There is undoubtedly a way to do it, I don't know what it is; and it may not be worth the effort.

To contrast, notice with nvim_win_close before the script exits

 vim.fn.feedkeys(' ', 'x')
 
+vim.api.nvim_win_close(popup_wid, true)
+
 add_msg('Exit ' .. tostring(callback_result))
 vim.print(vim.inspect(msgs))

that the callback is invoked before exit, and so that can be checked. Note the Exit -1 instead of Exit nil.

14:03:30: CALLBACK -1                                                                     
{ "14:03:30: Start", "14:03:30: CALLBACK-1", "14:03:30: Exit -1" }                                  

@errael
Copy link
Author

errael commented Sep 21, 2024

Also I applied the formatter on the file and got this diff

Thanks, I'll checkout installing StyLua locally.

@errael errael force-pushed the PopupFix branch 2 times, most recently from 12574a1 to 4f0bbe2 Compare September 21, 2024 15:17
@errael
Copy link
Author

errael commented Sep 21, 2024

Updated after a review.

I guess there may be no one still working with plenary.vim familiar with and interested in popup compatibility for a technical review.

@errael
Copy link
Author

errael commented Sep 21, 2024

Update for StyLua of the runtime code in this PR.

@errael
Copy link
Author

errael commented Sep 27, 2024

Oops, forgot to remove the modeline. Done.

@Conni2461 @tjdevries This PR fixes issues in existing popup code; see OP.

Looking for some advisement on getting reviews and merge or reject.

Makefile Outdated
Comment on lines 5 to 7
test1:
nvim --headless --noplugin -u scripts/minimal.vim -c "PlenaryBustedDirectory tests/plenary/$(TEST1) {minimal_init = 'tests/minimal_init.vim', sequential = true}"

Copy link
Contributor

Choose a reason for hiding this comment

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

If you want to add this to the Makefile targets I think it should be it's separated PR. But if @tjdevries or @Conni2461 thinks it's okay to leave it in this one, then it won't be needed.

Copy link
Author

Choose a reason for hiding this comment

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

Removed the change. I'll maintain it locally.

Comment on lines 210 to 273
local function populate()
local wnr = vim.fn.winnr()
local bnr = vim.fn.winbufnr(wnr)
vim.fn.setbufline(bnr, 1, { "--one--", "--two--", "--three--", "--four--" })
vim.fn.cursor(1, 1)
end
]]

--[[
it("without moved", function()
--async()
callback_result = nil
populate()
local popup_wid = popup.create("hello there", {
callback = callback,
})
-- move the cursor, should not do callback
vim.fn.cursor(2, 1)
local timer = vim.uv.new_timer()
timer:start(10, 0, function()
eq(nil, callback_result)
vim.api.nvim_win_close(popup_wid, true)
--done()
end)
end)
]]

--[[
it("with moved", function()
--async()
callback_result = nil
populate()
local popup_wid = popup.create("hello there", {
moved = "any",
callback = callback,
})
-- move the cursor, window closes and callback invoked
vim.fn.cursor(2, 1)
local done = false
vim.defer_fn(function()
--eq(-1, callback_result)
--eq(0, callback_result)
done = true
end, 100)
vim.wait(100, function()
eq(0, callback_result)
return done
end, 60)
--if -1 ~= callback_result then
-- -- window wasn't closed
-- vim.api.nvim_win_close(popup_wid, true)
--end

--local timer = vim.uv.new_timer()
--timer:start(10, 0, function()
-- eq(-1, callback_result)
-- if -1 ~= callback_result then
-- -- window wasn't closed
-- vim.api.nvim_win_close(popup_wid, true)
-- end
-- --done()
--end)
end)
]]
Copy link
Contributor

Choose a reason for hiding this comment

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

If this won't be used in the end, remove it along with the require("plenary.async").tests.add_to_env() comment on line 2, but if you want to try and add the async testing functionality, you can head to async test folder and see how it's used there.

Copy link
Author

Choose a reason for hiding this comment

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

I took this out and stashed it. I'll reconsider adding it at a later time.

end

-- TODO: Wonder what this is about? Debug? Convenience to get bufnr?
Copy link
Contributor

Choose a reason for hiding this comment

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

I think what this does if I'm not mistaken @tjdevries @Conni2461 it's that checks for vim_options.finalize_callback to be (it could be null) and if does exists calls it, maybe there's a part of the code where it removes it or it's set to false, will look into that.

Copy link
Author

Choose a reason for hiding this comment

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

vim_options.finalize_callback

The question is about why it exists at all. It was added with #513.

@xactlyblue Could you clarify the purpose of #513? It doesn't seem to have anything to do with popups.

Copy link
Contributor

@AlejandroSuero AlejandroSuero Oct 9, 2024

Choose a reason for hiding this comment

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

Yeah why it exists, that idk, if @xactlyblue could clarify on that regard, would be nice. I tried to find where it was being used or defined and this is what I got.

I added this to log when it's called or not:

image

Results from make test:

Screenshot 2024-10-09 at 18 04 38

Video demo when using Telescope which creates a popup:

finalize_callback-demo.mp4

Note

As you can see it adds the not called log twice because I think Telescope calls it twice for each one it creates. I used Telescope 3 times, and got 6 not called logs.

Copy link
Contributor

Choose a reason for hiding this comment

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

Tried both in this PR and in master btw, in case it was related to something this PR was removing.

Copy link
Contributor

@xactlyblue xactlyblue Oct 11, 2024

Choose a reason for hiding this comment

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

if @xactlyblue could clarify on that regard

Hey.

I made that change a while ago to address a problem I was having where I needed to access some information about the buffer created for the popup (I forgot the details to be honest, but if you need them I could probably go through my dotfiles commit history to figure out what my specific use-case was at the time).

It just creates an easier way for the user to manually access the bufnr and win_id of the popup for whatever reason they might need it. There isn't any internal implementation or usage of it.

I probably should've added a comment explaining the changes to clear the purpose up, sorry for any confusion!

Copy link
Author

Choose a reason for hiding this comment

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

creates an easier way for the user to manually access the bufnr and win_id

Thanks, no problem. Debug convenience. Do you agree there's no reason to keep it in a final product?

Consider nvim_win_get_buf(win_id)

Copy link
Contributor

@xactlyblue xactlyblue Oct 11, 2024

Choose a reason for hiding this comment

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

Sorry, I should have clarified: this change was meant to be accessible by the user (for whatever reason they may need to access the bufnr and win_id of a created popup).

I'm not really familiar with the inner-workings of plenary or it's API, so I apologize if this change was unnecessary, but I couldn't find any existing method to easily access the bufnr of a created popup via a callback, so I figured the simplest solution would be to add an optional callback to allow the user to access the bufnr and win_id as soon as they're available.

If I recall correctly my use-case had something to do with zindex/overlap issues in a plugin that allowed you to supply popup arguments to plenary, which is why I made it a callback (I guess I figured it was more useful/elegant to add a general callback for plenary instead of modifying the plugin itself) the user can supply.

I looked through my NeoVim configuration and I'm not currently using it so I personally don't mind if it's removed but I'm not sure if anyone else is making use of it in their own configurations (though I'd guess the odds of that are fairly slim considering it's not really documented anywhere and has fairly specific use-cases).

Copy link
Author

Choose a reason for hiding this comment

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

I understood (I think). I didn't see a reason for it, other than convenience.

simple, existing method to access the bufnr of a created popup

I'm new to neovim, I don't know what was available when. Currently nvim_win_get_buf(win_id) or winbufnr() gets the buffer number from a window. May be other ways.

I'm not asking you to remove it, just wanted to make sure there wasn't something I missed.

@errael errael force-pushed the PopupFix branch 2 times, most recently from a2b6c91 to cc176c9 Compare October 9, 2024 15:56
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.

3 participants