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

Provide callbacks as native Vim commands #29

Open
roginfarrer opened this issue Aug 16, 2021 · 6 comments
Open

Provide callbacks as native Vim commands #29

roginfarrer opened this issue Aug 16, 2021 · 6 comments
Assignees
Labels
enhancement New feature or request

Comments

@roginfarrer
Copy link

Describe the solution you'd like

I think this plugin would be a lot easier to use if it provided commands, just like vim-fugitive. Not only would it then be much more approachable to create your own keymappings, but it would make it easier to migrate from vim-fugitive (if the commands were the same).

If it were the same, I'd like to see:

  • :GBrowse: open the current file in the browser (in normal mode and visual mode)
  • :GBrowse!: copy the link to the clipboard (I think this comes from vim-rhubarb)
@roginfarrer roginfarrer added the enhancement New feature or request label Aug 16, 2021
@roginfarrer
Copy link
Author

I was able to recreate this behavior with the following in my config:

local function copyToClipboard(mode)
	require('gitlinker').get_buf_range_url(
		mode,
		{ action_callback = require('gitlinker.actions').copy_to_clipboard }
	)
end

local function openInBrowser(mode)
	require('gitlinker').get_buf_range_url(
		mode,
		{ action_callback = require('gitlinker.actions').open_in_browser }
	)
end

function _G.gbrowse(range, bang)
	local mode = range > 0 and 'v' or 'n'
	local hasBang = bang == '!'
	if hasBang then
		return copyToClipboard(mode)
	else
		return openInBrowser(mode)
	end
end

vim.cmd([[
	command! -nargs=0 -range -bang GBrowse call v:lua.gbrowse(<range>, '<bang>')
]])

u.nnoremap('<leader>gc', [[:GBrowse!<CR>]])
u.vnoremap('<leader>gc', [[:'<,'>GBrowse!<CR>]])
u.nnoremap('<leader>go', [[:GBrowse<CR>]])
u.vnoremap('<leader>go', [[:'<,'>GBrowse<CR>]])

@ruifm
Copy link
Owner

ruifm commented Aug 25, 2021

Hi @roginfarrer , great to hear that!

The decision to not provide native vim commands was deliberate. It goes in line with most new lua plugins and their philosophy. The same goes for the plugin not actually doing anything until a setup() like function is called.

I very much like that since it's transparent and forces the user to acknowledge what he is doing.

Yes, mapping to lua functions his currently very cumbersome but I hope that will improve in the near future with neovim's core rapid development.

I would happily accept a PR with your suggestion above somewhere in the README.

I would even be open to providing those commands if and only if the user calls require'gitlinker'.setup(). Those commands should not be called :GBrowse or anything resembling a fugitive command so that it does not create any chaos.

Thanking for your issue and sorry for the late reply. I'm on holidays until the end of the month.

@roginfarrer
Copy link
Author

No worries about the late reply, enjoy your holiday!

The decision to not provide native vim commands was deliberate. It goes in line with most new lua plugins and their philosophy.

While it may be common, I don't entirely agree that it's a standard nor user-friendly. There's a good conversation about this on reddit. I think some popular counter-examples would be Telescope (which uses :Telescope [arg] commands, Neogit (which uses :Neogit), and Trouble.nvim (which uses :Trouble).

I would even be open to providing those commands if and only if the user calls require'gitlinker'.setup()

Would this prevent users from using packer.nvim's ability to lazy-load using the cmd condition? If you're not familiar, this allows the user to configure certain commands related to a plugin, that when called, will require the specified plugin (so it doesn't initialize on neovim starting)

@roginfarrer
Copy link
Author

I should add, I respect your decision since it's your plugin! Just interested in continuing the conversation.

@ajitid
Copy link

ajitid commented Feb 10, 2022

Unrelated but, @roginfarrer does the code you pasted works for a line range if I manually type GBrowse in the command (instead of invoking using keymap)? I tried, it didn't work.

To reproduce:

  1. Select few lines
  2. Do :GBrowse (which would result in :'<,'>GBrowse as we did a visual selection)
  3. Notice that link that has been created has its end line same as the start line

@roginfarrer
Copy link
Author

Actually I think there's a bug with the range option in the plugin. I suggest opening a new issue for it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants