aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/ui.lua
diff options
context:
space:
mode:
authorGregory Anders <8965202+gpanders@users.noreply.github.com>2024-08-31 19:56:20 -0500
committerGitHub <noreply@github.com>2024-08-31 19:56:20 -0500
commit9762c5e3406cab8152d8dd161c0178965d841676 (patch)
tree536eb99e00ca3e4f9153c6eabc5a8f81f7aaca02 /runtime/lua/vim/ui.lua
parent808d73b5dfcc3024d4ddf23f902009e65acc0972 (diff)
downloadrneovim-9762c5e3406cab8152d8dd161c0178965d841676.tar.gz
rneovim-9762c5e3406cab8152d8dd161c0178965d841676.tar.bz2
rneovim-9762c5e3406cab8152d8dd161c0178965d841676.zip
feat(ui): gx: use url extmark attribute and tree-sitter directive (#30192)
Use the "url" extmark attribute as well as the "url" tree-sitter metadata key to determine if the cursor is over something Nvim considers a URL.
Diffstat (limited to 'runtime/lua/vim/ui.lua')
-rw-r--r--runtime/lua/vim/ui.lua70
1 files changed, 52 insertions, 18 deletions
diff --git a/runtime/lua/vim/ui.lua b/runtime/lua/vim/ui.lua
index dccdbf32cc..b831a4f23e 100644
--- a/runtime/lua/vim/ui.lua
+++ b/runtime/lua/vim/ui.lua
@@ -167,29 +167,63 @@ function M.open(path)
return vim.system(cmd, opts), nil
end
---- Gets the URL at cursor, if any.
-function M._get_url()
- if vim.bo.filetype == 'markdown' then
- local range = vim.api.nvim_win_get_cursor(0)
- vim.treesitter.get_parser():parse(range)
- -- marking the node as `markdown_inline` is required. Setting it to `markdown` does not
- -- work.
- local current_node = vim.treesitter.get_node { lang = 'markdown_inline' }
- while current_node do
- local type = current_node:type()
- if type == 'inline_link' or type == 'image' then
- local child = assert(current_node:named_child(1))
- return vim.treesitter.get_node_text(child, 0)
+--- Returns all URLs at cursor, if any.
+--- @return string[]
+function M._get_urls()
+ local urls = {}
+
+ local bufnr = vim.api.nvim_get_current_buf()
+ local cursor = vim.api.nvim_win_get_cursor(0)
+ local row = cursor[1] - 1
+ local col = cursor[2]
+ local extmarks = vim.api.nvim_buf_get_extmarks(bufnr, -1, { row, col }, { row, col }, {
+ details = true,
+ type = 'highlight',
+ overlap = true,
+ })
+ for _, v in ipairs(extmarks) do
+ local details = v[4]
+ if details.url then
+ urls[#urls + 1] = details.url
+ end
+ end
+
+ local highlighter = vim.treesitter.highlighter.active[bufnr]
+ if highlighter then
+ local range = { row, col, row, col }
+ local ltree = highlighter.tree:language_for_range(range)
+ local lang = ltree:lang()
+ local query = vim.treesitter.query.get(lang, 'highlights')
+ if query then
+ local tree = ltree:tree_for_range(range)
+ for _, match, metadata in query:iter_matches(tree:root(), bufnr, row, row + 1, { all = true }) do
+ for id, nodes in pairs(match) do
+ for _, node in ipairs(nodes) do
+ if vim.treesitter.node_contains(node, range) then
+ local url = metadata[id] and metadata[id].url
+ if url and match[url] then
+ for _, n in ipairs(match[url]) do
+ urls[#urls + 1] = vim.treesitter.get_node_text(n, bufnr, metadata[url])
+ end
+ end
+ end
+ end
+ end
end
- current_node = current_node:parent()
end
end
- local url = vim._with({ go = { isfname = vim.o.isfname .. ',@-@' } }, function()
- return vim.fn.expand('<cfile>')
- end)
+ if #urls == 0 then
+ -- If all else fails, use the filename under the cursor
+ table.insert(
+ urls,
+ vim._with({ go = { isfname = vim.o.isfname .. ',@-@' } }, function()
+ return vim.fn.expand('<cfile>')
+ end)
+ )
+ end
- return url
+ return urls
end
return M