fix: tags now work with hasktags (#375)

This commit is contained in:
Simon Hauser
2020-12-31 16:17:09 +01:00
committed by GitHub
parent 686d560fa5
commit a0b37473a9
3 changed files with 51 additions and 23 deletions

View File

@@ -224,12 +224,12 @@ end
files.tags = function(opts)
local ctags_file = opts.ctags_file or 'tags'
if not vim.loop.fs_open(vim.fn.expand(ctags_file), "r", 438) then
if not vim.loop.fs_open(vim.fn.expand(ctags_file, true), "r", 438) then
print('Tags file does not exists. Create one with ctags -R')
return
end
local fd = assert(vim.loop.fs_open(vim.fn.expand(ctags_file), "r", 438))
local fd = assert(vim.loop.fs_open(vim.fn.expand(ctags_file, true), "r", 438))
local stat = assert(vim.loop.fs_fstat(fd))
local data = assert(vim.loop.fs_read(fd, stat.size, 0))
assert(vim.loop.fs_close(fd))
@@ -249,14 +249,18 @@ files.tags = function(opts)
post = function()
local selection = actions.get_selected_entry()
local scode = string.gsub(selection.scode, '[$]$', '')
scode = string.gsub(scode, [[\\]], [[\]])
scode = string.gsub(scode, [[\/]], [[/]])
scode = string.gsub(scode, '[*]', [[\*]])
if selection.scode then
local scode = string.gsub(selection.scode, '[$]$', '')
scode = string.gsub(scode, [[\\]], [[\]])
scode = string.gsub(scode, [[\/]], [[/]])
scode = string.gsub(scode, '[*]', [[\*]])
vim.cmd('norm! gg')
vim.fn.search(scode)
vim.cmd('norm! zz')
vim.cmd('norm! gg')
vim.fn.search(scode)
vim.cmd('norm! zz')
else
vim.api.nvim_win_set_cursor(0, {selection.lnum, 0})
end
end,
}
return true

View File

@@ -834,7 +834,13 @@ function make_entry.gen_from_ctags(opts)
return nil
end
local tag, file, scode = string.match(line, '([^\t]+)\t([^\t]+)\t/^\t?(.*)/;"\t+.*')
local tag, file, scode, lnum
-- ctags gives us: 'tags\tfile\tsource'
tag, file, scode = string.match(line, '([^\t]+)\t([^\t]+)\t/^\t?(.*)/;"\t+.*')
if not tag then
-- hasktags gives us: 'tags\tfile\tlnum'
tag, file, lnum = string.match(line, '([^\t]+)\t([^\t]+)\t(%d+).*')
end
if opts.only_current_file and file ~= current_file then
return nil
@@ -851,7 +857,7 @@ function make_entry.gen_from_ctags(opts)
filename = file,
col = 1,
lnum = 1,
lnum = lnum and tonumber(lnum) or 1,
}
end
end

View File

@@ -222,11 +222,40 @@ end, {})
previewers.qflist = previewers.vimgrep
previewers.ctags = defaulter(function(_)
local determine_jump = function(entry)
if entry.scode then
return function(self)
local scode = string.gsub(entry.scode, '[$]$', '')
scode = string.gsub(scode, [[\\]], [[\]])
scode = string.gsub(scode, [[\/]], [[/]])
scode = string.gsub(scode, '[*]', [[\*]])
pcall(vim.fn.matchdelete, self.state.hl_id, self.state.winid)
vim.cmd "norm! gg"
vim.fn.search(scode, "W")
vim.cmd "norm! zz"
self.state.hl_id = vim.fn.matchadd('TelescopePreviewMatch', scode)
end
else
return function(self, bufnr)
if self.state.last_set_bufnr then
pcall(vim.api.nvim_buf_clear_namespace, self.state.last_set_bufnr, ns_previewer, 0, -1)
end
pcall(vim.api.nvim_buf_add_highlight, bufnr, ns_previewer, "TelescopePreviewMatch", entry.lnum - 1, 0, -1)
pcall(vim.api.nvim_win_set_cursor, self.state.winid, { entry.lnum, 0 })
self.state.last_set_bufnr = bufnr
end
end
end
return previewers.new_buffer_previewer {
teardown = function(self)
if self.state and self.state.hl_id then
pcall(vim.fn.matchdelete, self.state.hl_id, self.state.hl_win)
self.state.hl_id = nil
elseif self.state and self.state.last_set_bufnr and vim.api.nvim_buf_is_valid(self.state.last_set_bufnr) then
vim.api.nvim_buf_clear_namespace(self.state.last_set_bufnr, ns_previewer, 0, -1)
end
end,
@@ -236,22 +265,11 @@ previewers.ctags = defaulter(function(_)
define_preview = function(self, entry, status)
putils.with_preview_window(status, nil, function()
local scode = string.gsub(entry.scode, '[$]$', '')
scode = string.gsub(scode, [[\\]], [[\]])
scode = string.gsub(scode, [[\/]], [[/]])
scode = string.gsub(scode, '[*]', [[\*]])
conf.buffer_previewer_maker(entry.filename, self.state.bufnr, self.state.bufname, true, function(bufnr)
vim.api.nvim_buf_call(bufnr, function()
pcall(vim.fn.matchdelete, self.state.hl_id, self.state.winid)
vim.cmd "norm! gg"
vim.fn.search(scode, "W")
vim.cmd "norm! zz"
self.state.hl_id = vim.fn.matchadd('TelescopePreviewMatch', scode)
determine_jump(entry)(self, bufnr)
end)
end)
end)
end
}