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

Calling get_buf_range_url through a user command not including ranges #62

Open
smarquez1 opened this issue Aug 30, 2022 · 3 comments
Open
Assignees
Labels
bug Something isn't working

Comments

@smarquez1
Copy link

smarquez1 commented Aug 30, 2022

Hi, I created a user command based on a user's comment to open the current selected lines on github.
The problem is that, in Visual mode, it will not add the current code range to the URL.
Am I missing to pass something to the new user command or maybe this is a bug?

local linker = require "gitlinker"
local actions = require "gitlinker.actions"

function _G.gitOpenInBrowser(t)
  local mode = t.range > 0 and "v" or "n"
  linker.get_buf_range_url(mode, { action_callback = actions.open_in_browser })
end

vim.api.nvim_create_user_command(
  "GitOpenInBrowser",
  gitOpenInBrowser,
  { desc = "Open file in github", range = true }
)
@smarquez1 smarquez1 added the bug Something isn't working label Aug 30, 2022
@GlebBeloded
Copy link

I am terrible at neovim and lua, but for me changing
these two lines to

    local pos1 = vim.api.nvim_buf_get_mark(0, "<")[1]
    local pos2 = vim.api.nvim_buf_get_mark(0, ">")[1]

seemed to solve the issue

@smarquez1
Copy link
Author

Hey @GlebBeloded what problem where you getting exactly?
I think that the original issue got somewhat solved, but it's still buggy.

Right now I use lazy.nvim to setup my plugins including keymaps. This is how I setup gitlinker:

function _G.gitOpenInBrowser(mode)
  local linker = require "gitlinker"
  local actions = require "gitlinker.actions"
  local callback = { action_callback = actions.open_in_browser }
  linker.get_buf_range_url(mode, callback)
end

return {
  "ruifm/gitlinker.nvim",
  config = function() require("gitlinker").setup() end,
  keys = {
    ...
    { "<leader>gB", function() _G.gitOpenInBrowser("n") end, },
    { "<leader>gB", function() _G.gitOpenInBrowser("v") end, mode = "v",
    },
  },
}

The problem I'm getting now is that while gitlinker opens up a browser with a code region highlighted, the range code is displaced by one line both at the beginning and the end.

Is that the issue that your comment fixes?

@GlebBeloded
Copy link

@smarquez1
My issue was that

local pos1 = vim.fn.getpos("v")[2]
local pos2 = vim.fn.getcurpos()[2]

returned the same number, because I called gitlinked code not from a binding, but from user-defined command.
I've done a little googling and this post suggested, that when you start typing a command, you leave visual mode.

So the edit I've shown you above gets last visual selection, even if you are in normal mode now.

My command looks like this:

vim.api.nvim_create_user_command("Gitlink", function(t)
	local mode = t.range > 0 and "v" or "n"

	require("gitlinker").get_buf_range_url(mode, { action_callback = require("gitlinker.actions").copy_to_clipboard })
end, { range = true })

The code range is not displaced also on my end, so maybe it will fix stuff for you too.

stevanmilic pushed a commit to stevanmilic/gitlinker.nvim that referenced this issue Nov 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants