From 116d78b42938ef0a8961483e261775786071ef89 Mon Sep 17 00:00:00 2001 From: Xavier Young <45989017+younger-1@users.noreply.github.com> Date: Sun, 16 Oct 2022 00:30:53 +0800 Subject: [PATCH 1/2] doc: remove plenary --- README.md | 8 ++------ doc/gitlinker.txt | 5 +---- test/minimal_init.lua | 1 - 3 files changed, 3 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 269d5718..bbdd6ed5 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/doc/gitlinker.txt b/doc/gitlinker.txt index 478133c1..507553d8 100644 --- a/doc/gitlinker.txt +++ b/doc/gitlinker.txt @@ -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' < diff --git a/test/minimal_init.lua b/test/minimal_init.lua index 3a5df3c4..fd7c3fdb 100644 --- a/test/minimal_init.lua +++ b/test/minimal_init.lua @@ -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'} From 292ca75521ebdc1a8b32e556218bbc0b6ecc42ba Mon Sep 17 00:00:00 2001 From: Xavier Young <45989017+younger-1@users.noreply.github.com> Date: Tue, 20 Dec 2022 23:41:58 +0800 Subject: [PATCH 2/2] feat: remove plenary --- README.md | 11 +++++++++-- doc/gitlinker.txt | 7 +++++-- lua/gitlinker.lua | 5 ++--- lua/gitlinker/actions.lua | 10 +++++----- lua/gitlinker/buffer.lua | 6 ++++-- lua/gitlinker/git.lua | 29 ++++++++++++++--------------- lua/gitlinker/mappings.lua | 3 +++ lua/gitlinker/opts.lua | 8 ++++++++ 8 files changed, 50 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index bbdd6ed5..b3120018 100644 --- a/README.md +++ b/README.md @@ -122,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, @@ -136,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 = "gy" }) ``` @@ -235,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. diff --git a/doc/gitlinker.txt b/doc/gitlinker.txt index 507553d8..2f869bf9 100644 --- a/doc/gitlinker.txt +++ b/doc/gitlinker.txt @@ -121,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 = "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, @@ -137,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 = "gy" }) < diff --git a/lua/gitlinker.lua b/lua/gitlinker.lua index c1b64fcb..8d7ed507 100644 --- a/lua/gitlinker.lua +++ b/lua/gitlinker.lua @@ -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 ) diff --git a/lua/gitlinker/actions.lua b/lua/gitlinker/actions.lua index e3e6c153..29eac47a 100644 --- a/lua/gitlinker/actions.lua +++ b/lua/gitlinker/actions.lua @@ -1,7 +1,7 @@ local M = {} local api = vim.api -local job = require("plenary.job") +local fn = vim.fn --- copies the url to clipboard -- @@ -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 diff --git a/lua/gitlinker/buffer.lua b/lua/gitlinker/buffer.lua index a2e04bb4..29bacf76 100644 --- a/lua/gitlinker/buffer.lua +++ b/lua/gitlinker/buffer.lua @@ -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() diff --git a/lua/gitlinker/git.lua b/lua/gitlinker/git.lua index 43522e41..27aad3ff 100644 --- a/lua/gitlinker/git.lua +++ b/lua/gitlinker/git.lua @@ -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 @@ -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 @@ -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 @@ -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( diff --git a/lua/gitlinker/mappings.lua b/lua/gitlinker/mappings.lua index 18b31e0a..d80f806f 100644 --- a/lua/gitlinker/mappings.lua +++ b/lua/gitlinker/mappings.lua @@ -18,6 +18,9 @@ end function M.set(mappings) mappings = mappings or "gy" + if mappings == '' then + return + end set_keymap("n", mappings) set_keymap("v", mappings, { silent = false }) end diff --git a/lua/gitlinker/opts.lua b/lua/gitlinker/opts.lua index ad58dd18..f98e83dd 100644 --- a/lua/gitlinker/opts.lua +++ b/lua/gitlinker/opts.lua @@ -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