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

feat: add open_cmd options #71

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,26 @@ that already exist in gitlinker! See [callbacks](#callbacks)

## Installation

Install it like any other vim plugin, just make sure
[plenary.nvim](https://github.com/nvim-lua/plenary.nvim) is also installed.
Install it like any other vim plugin.

- [packer.nvim](https://github.com/wbthomason/packer.nvim)

``` lua
use {
'ruifm/gitlinker.nvim',
requires = 'nvim-lua/plenary.nvim',
}
```

- [vim-plug](https://github.com/junegunn/vim-plug)

``` vim
Plug 'nvim-lua/plenary.nvim'
Plug 'ruifm/gitlinker.nvim'
```

### Requirements

- git
- neovim 0.5
- [plenary.nvim](https://github.com/nvim-lua/plenary.nvim)
- neovim 0.8

## Usage

Expand Down Expand Up @@ -126,6 +122,8 @@ require"gitlinker".setup({
action_callback = require"gitlinker.actions".copy_to_clipboard,
-- print the url after performing the action
print_url = true,
-- os-specific command to open url, default to Unix: `xdg-open`, Mac: `open`, Windows: `explorer`
open_cmd = '',
},
callbacks = {
["github.com"] = require"gitlinker.hosts".get_github_type_url,
Expand All @@ -140,7 +138,8 @@ require"gitlinker".setup({
["git.kernel.org"] = require"gitlinker.hosts".get_cgit_type_url,
["git.savannah.gnu.org"] = require"gitlinker.hosts".get_cgit_type_url
},
-- default mapping to call url generation with action_callback
-- default mapping to call url generation with action_callback
-- to disable the default mappings, set it to empty string
mappings = "<leader>gy"
})
```
Expand Down Expand Up @@ -239,7 +238,11 @@ default set to `require"gitlinker.actions".copy_to_clipboard` which copies to
generated url to your system clipboard.

An alternative callback `require"gitlinker.actions".open_in_browser` is provided
which opens the url in your preferred browser using `xdg-open` (linux only).
which opens the url in your preferred browser using os-specific command:

- Unix: `xdg-open`
- Mac: `open`
- Windows: `explorer`

You can define your own action callback.

Expand Down
12 changes: 6 additions & 6 deletions doc/gitlinker.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,19 @@ Supported git web hosts *gitlinker-supported*
==============================================================================
Installation *gitlinker-installation*

Install it like any other vim plugin, just make sure plenary.nvim is also
installed.
Install it like any other vim plugin.

* packer.nvim

>
use {
'ruifm/gitlinker.nvim',
requires = 'nvim-lua/plenary.nvim',
}
<

* vim-plug

>
Plug 'nvim-lua/plenary.nvim'
Plug 'ruifm/gitlinker.nvim'
<

Expand Down Expand Up @@ -124,8 +121,8 @@ Here’s all the options with their defaults:
action_callback = require"gitlinker.actions".copy_to_clipboard,
-- print the url after performing the action
print_url = true,
-- mapping to call url generation
mappings = "<leader>gy"
-- os-specific command to open url, default to Unix: `xdg-open`, Mac: `open`, Windows: `explorer`
open_cmd = '',
},
callbacks = {
["github.com"] = require"gitlinker.hosts".get_github_type_url,
Expand All @@ -140,6 +137,9 @@ Here’s all the options with their defaults:
["git.kernel.org"] = require"gitlinker.hosts"get_cgit_type_url,
["git.savannah.gnu.org"] = require"gitlinker.hosts"get_cgit_type_url
}
-- default mapping to call url generation with action_callback
-- to disable the default mappings, set it to empty string
mappings = "<leader>gy"
})
<

Expand Down
5 changes: 2 additions & 3 deletions lua/gitlinker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,14 @@ local function get_buf_range_url_data(mode, user_opts)
return nil
end

local buf_path = buffer.get_relative_path()
if
git.has_file_changed(buf_path, rev)
git.has_file_changed(buf_repo_path , rev)
and (mode == "v" or user_opts.add_current_line_on_normal_mode)
then
vim.notify(
string.format(
"Computed Line numbers are probably wrong because '%s' has changes",
buf_path
buf_repo_path
),
vim.log.levels.WARN
)
Expand Down
10 changes: 5 additions & 5 deletions lua/gitlinker/actions.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local M = {}

local api = vim.api
local job = require("plenary.job")
local fn = vim.fn

--- copies the url to clipboard
--
Expand All @@ -12,12 +12,12 @@ end

--- opens the url in your default browser
--
-- Uses xdg-open
-- @param url the url string
function M.open_in_browser(url)
local command = vim.loop.os_uname().sysname == "Darwin" and "open"
or "xdg-open"
job:new({ command = command, args = { url } }):start()
fn.jobstart(
{ require("gitlinker.opts").get().open_cmd, url },
{ detach = true }
)
end

return M
6 changes: 4 additions & 2 deletions lua/gitlinker/buffer.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
local M = {}

local api = vim.api
local path = require("plenary.path")

function M.get_relative_path(cwd)
return path:new(api.nvim_buf_get_name(0)):make_relative(cwd)
return vim.fn.fnamemodify(
vim.fs.normalize(api.nvim_buf_get_name(0)),
":s?" .. cwd .. "/" .. "??"
)
end

function M.get_curr_line()
Expand Down
29 changes: 14 additions & 15 deletions lua/gitlinker/git.lua
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
local M = {}

local job = require("plenary.job")
local path = require("plenary.path")
local fn = vim.fn

-- wrap the git command to do the right thing always
local function git(args, cwd)
local output
local p = job:new({
command = "git",
args = args,
-- output = fn.systemlist(vim.list_extend({ "git" }, args))
fn.jobstart(vim.list_extend({ "git" }, args), {
cwd = cwd or M.get_git_root(),
on_stdout = function(_, data, _)
output = data
end,
})
p:after_success(function(j)
output = j:result()
end)
p:sync()
vim.wait(200, function()
return output ~= nil
end, 20)
return output or {}
end

Expand Down Expand Up @@ -43,7 +42,8 @@ function M.is_file_in_rev(file, revspec)
end

function M.has_file_changed(file, rev)
if git({ "diff", rev, "--", file })[1] then
local output = git({ "diff", rev, "--", file })[1]
if output and output ~= "" then
return true
end
return false
Expand Down Expand Up @@ -229,7 +229,7 @@ end
function M.get_git_root()
return git(
{ "rev-parse", "--show-toplevel" },
tostring(path:new(vim.api.nvim_buf_get_name(0)):parent())
vim.fs.dirname(vim.api.nvim_buf_get_name(0))
)[1]
end

Expand All @@ -248,9 +248,8 @@ function M.get_branch_remote()
return nil
end

local remote_from_upstream_branch = upstream_branch:match(
"^(" .. allowed_chars .. ")%/"
)
local remote_from_upstream_branch =
upstream_branch:match("^(" .. allowed_chars .. ")%/")
if not remote_from_upstream_branch then
error(
string.format(
Expand Down
3 changes: 3 additions & 0 deletions lua/gitlinker/mappings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ end

function M.set(mappings)
mappings = mappings or "<leader>gy"
if mappings == '' then
return
end
set_keymap("n", mappings)
set_keymap("v", mappings, { silent = false })
end
Expand Down
8 changes: 8 additions & 0 deletions lua/gitlinker/opts.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
local M = {}

local os, open_cmd = jit.os, "xdg-open"
if os == "Darwin" then
open_cmd = "open"
elseif os == "Windows" then
open_cmd = "explorer"
end

local defaults = {
remote = "origin", -- force the use of a specific remote
add_current_line_on_normal_mode = true, -- if true adds the line nr in the url for normal mode
action_callback = require("gitlinker.actions").copy_to_clipboard, -- callback for what to do with the url
print_url = true, -- print the url after action
open_cmd = open_cmd -- os-specific command to open url
}

local opts
Expand Down
1 change: 0 additions & 1 deletion test/minimal_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ require("packer").startup(
use 'wbthomason/packer.nvim'
use {
'ruifm/gitlinker.nvim',
requires = 'nvim-lua/plenary.nvim',
}
end,
config = {package_root = '/tmp/nvim/site/pack'}
Expand Down