Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# LSPLINKS

> Support for [document links](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_documentLink) for neovim **0.9+**.
> Support for [document links](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_documentLink) for neovim **0.10+**.

### Usage

Expand Down
52 changes: 4 additions & 48 deletions lua/lsplinks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,6 @@ local function in_range(pos, range)
end
end

---@param name string
---@return boolean
local function lsp_has_capability(name)
local clients = nil
if vim.lsp.get_clients then
local bufnr = api.nvim_get_current_buf()
clients = vim.lsp.get_clients({ bufnr = bufnr })
else
clients = vim.lsp.buf_get_clients()
end
for _, client in ipairs(clients) do
if client.server_capabilities[name] then
return true
end
end
return false
end

local augroup = api.nvim_create_augroup("lsplinks", { clear = true })

Expand Down Expand Up @@ -108,18 +91,6 @@ function M.current()
return nil
end

--- Return the uri without the fragment
---
---@param uri string
---@return string
local function remove_uri_fragment(uri)
local fragment_index = uri:find("#")
if fragment_index ~= nil then
uri = uri:sub(1, fragment_index - 1)
end
return uri
end

--- Open the link under the cursor if one exists.
--- The return value indicates if a link was found.
---
Expand All @@ -131,26 +102,13 @@ function M.open(uri)
return false
end
if uri:find("^file:/") then
util.show_document({ uri = remove_uri_fragment(uri) }, "utf-8", { reuse_win = true, focus = true })
util.show_document({ uri = uri }, "utf-8", { reuse_win = true, focus = true })
local line_no, col_no = uri:match(".-#(%d+),(%d+)")
if line_no then
api.nvim_win_set_cursor(0, { tonumber(line_no), tonumber(col_no) - 1 })
end
else
if vim.ui.open then
vim.ui.open(uri)
else
-- for nvim earlier than 0.10
local opener
if vim.fn.has("macunix") == 1 then
opener = "open"
elseif vim.fn.has("linux") == 1 then
opener = "xdg-open"
elseif vim.fn.has("win64") == 1 or vim.fn.has("win32") == 1 then
opener = "start"
end
vim.fn.system(string.format("%s '%s' >/dev/null 2>&1", opener, uri))
end
vim.ui.open(uri)
end
return true
end
Expand All @@ -164,9 +122,6 @@ end

-- Refresh the links for the current buffer
function M.refresh()
if not lsp_has_capability("documentLinkProvider") then
return
end
local params = { textDocument = util.make_text_document_params() }
vim.lsp.buf_request(0, "textDocument/documentLink", params, function(err, result, ctx)
if err then
Expand All @@ -190,7 +145,8 @@ function M.refresh()
if options.highlight then
M.display()
end
end)
end, function() end
)
end

--- Get links for bufnr
Expand Down
Loading