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

set up keymaps when plugin is lazy loaded #73

Merged
merged 2 commits into from
Mar 24, 2023

Conversation

matthewsia98
Copy link
Contributor

Addresses #72

Alternatively, we can skip the checks on setup and just expose the set_default_keymaps functions and leave it to the user to call them if they are lazy loading the plugin. Something like

return {
    "kiyoon/jupynium.nvim",
    build = "pip3 install --user .",
    event = "BufWinEnter *.ju.py",
    config = function()
        local jupynium = require("jupynium")
        jupynium.setup({})
        jupynium.set_default_keymaps()
        require("jupynium.textobjects").set_default_keymaps()
    end
}

local filename = vim.fn.expand "%"
local buf_id = vim.api.nvim_get_current_buf()
for _, pattern in ipairs(options.opts.jupynium_file_pattern) do
if utils.string_wildcard_match(filename, pattern) then
Copy link
Owner

Choose a reason for hiding this comment

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

You can instead use list_wildcard_match and then remove the for loop?

Copy link
Owner

Choose a reason for hiding this comment

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

Can you also add a comment that this part is needed when you add lazy loading like event = "BufWinEnter *.ju.*" in lazy.nvim?

@kiyoon
Copy link
Owner

kiyoon commented Mar 24, 2023

If you lazy load it this way, what happens to the vim commands? Does :JupyniumStartAndAttachToServer work, or with the same reason it won't be initialised?

Also, will auto attach to server work?

@kiyoon
Copy link
Owner

kiyoon commented Mar 24, 2023

If you lazy load it this way, what happens to the vim commands? Does :JupyniumStartAndAttachToServer work, or with the same reason it won't be initialised?

Also, will auto attach to server work?

I think the commands are loaded alright, but the auto starting server needs update as well. We can address this in a separate PR.

@thesimonho
Copy link
Contributor

this is a broader but related issue:

is this the best way to do buffer-local keybinds? because the current implementation, and in this PR, non-default/user-defined keymaps remain global, requiring the user to set up autocommands themselves

would it not make more sense to expose an on_attach and on_detach function in the config, which gets called by jupynium at the relevant time? that way the user can choose to pass their own keymap function (or the default keymap function) into on_attach and both just get handled the same way instead of needing different/extra function calls?

@kiyoon
Copy link
Owner

kiyoon commented Mar 24, 2023

Thanks for the idea, @sho-87!

I get your point, but for things as simple as keymapping, I prefer not to add too many configurations that will make it harder to understand the structure, maintain the plugin and deprecate things at some point.
For example, lazy.nvim plugin manager also allows you to add keybindings directly in there. Some people may prefer using that way instead.

I added the default keybindings not because I think that'd fit for everyone, but more because it'd make it easier to get started with the plugin and understand how things work. I still think that the best way to use the plugin is to turn off the default keybindings and add them on your own.

@kiyoon
Copy link
Owner

kiyoon commented Mar 24, 2023

@sho-87 Also, the current autocmd solution is not the best solution because it will remap the keybindings every time you enter *.ju.* files. It won't cause issues for the most of the time, but it can conflict with some plugins that are set later on. So unless we really improve the logic, there's no need to implement the hook because it's as simple as one autocmd anyway.

@kiyoon kiyoon merged commit 2e51026 into kiyoon:master Mar 24, 2023
@thesimonho
Copy link
Contributor

I think this merge broke custom keymaps on my end - can you check if this is also the case on yours? @kiyoon @matthewsia98

after connecting to a server, the default keymaps always show up even though my config has them turned off

reverting to the previous commit (93e4690) makes things work as expected

@kiyoon
Copy link
Owner

kiyoon commented Mar 24, 2023

Interesting, maybe the setup gets automatically called with the default configuration due to plugin/jupynium.vim.

I'll probably need to move this into after/

@matthewsia98
Copy link
Contributor Author

Hmmmm interesting indeed.
I added some print statements in the setup call like

function M.setup(opts)
    print("setup")
    print(vim.inspect(opts))
end

On opening a *.ju.* file it prints setup twice.
First with an empty table.
Second with my actual config.

@kiyoon
Copy link
Owner

kiyoon commented Mar 24, 2023

Does the fix work if you pull the recent version?

@matthewsia98
Copy link
Contributor Author

No..setup still gets called twice, first with {} and second with the actual user config

@thesimonho
Copy link
Contributor

yeah latest version still shows default binds

@kiyoon
Copy link
Owner

kiyoon commented Mar 24, 2023

Weird, it only gets called once for me

@kiyoon
Copy link
Owner

kiyoon commented Mar 24, 2023

Okay, I see that it gets called twice if the plugin is lazy-loaded. So even the after/ will be called first. Maybe I can remove the auto-calling setup completely, or figure out a different way to set default keymaps when lazy loading. (like mentioned on the top of this PR)

What do you think?

@matthewsia98
Copy link
Contributor Author

Can we not just add a !has("nvim") to the setup call in after/

@kiyoon
Copy link
Owner

kiyoon commented Mar 24, 2023

Can we not just add a !has("nvim") to the setup call in after/

This plugin is only compatible with neovim anyway. This is just there to make it possible to use without calling the setup function. Other plugins have this too. I thought lazy.nvim should call configuration before loading this file.

@kiyoon
Copy link
Owner

kiyoon commented Mar 24, 2023

I'll just remove the keymapping part for now. If you're lazy loading, you'll need to deal with the consequences. So you can do the following as you said:

return {
    "kiyoon/jupynium.nvim",
    build = "pip3 install --user .",
    event = "BufWinEnter *.ju.py",
    config = function()
        local jupynium = require("jupynium")
        jupynium.setup({})
        jupynium.set_default_keymaps()
        require("jupynium.textobjects").set_default_keymaps()
    end
}

@kiyoon
Copy link
Owner

kiyoon commented Mar 24, 2023

I just made another fix. @sho-87

@thesimonho
Copy link
Contributor

working now, thanks!

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