From 1495cbec5aa9b8ee581d222b6988b30d3a71e213 Mon Sep 17 00:00:00 2001 From: Nolan Prochnau Date: Sun, 15 Nov 2020 10:54:33 -0500 Subject: [PATCH 1/5] Have plugin tags previewed Still doesn't update for multiple files, but the first file in the taglist is shown, so that's a start? --- lua/telescope/previewers.lua | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lua/telescope/previewers.lua b/lua/telescope/previewers.lua index 47b6e0d..9d0229d 100644 --- a/lua/telescope/previewers.lua +++ b/lua/telescope/previewers.lua @@ -423,8 +423,14 @@ previewers.help = defaulter(function(_) local tagfile = vim.fn.expand("$VIMRUNTIME") .. '/doc/tags' local old_tags = vim.o.tags - vim.o.tags = tagfile - local taglist = vim.fn.taglist('^' .. escaped .. '$', tagfile) + vim.o.tags = '' + for _,file in pairs(vim.fn.findfile('doc/tags', vim.o.runtimepath, -1)) do + vim.o.tags = vim.o.tags .. file .. ',' + end + + vim.o.tags = vim.o.tags:sub(1,-2) -- Remove trailing comma + print(vim.inspect(vim.o.tags)) + local taglist = vim.fn.taglist('^' .. escaped .. '$') vim.o.tags = old_tags if vim.tbl_isempty(taglist) then From 8eecc9c9dc5861023acbd6bc85be625cd5da1c60 Mon Sep 17 00:00:00 2001 From: Nolan Prochnau Date: Sun, 15 Nov 2020 13:26:30 -0500 Subject: [PATCH 2/5] Manually search through tags Probably slower, but the builtin `taglist()` only looks at the first file in 'tags', not ideal for this application. This can probably be cleaned up a little. --- lua/telescope/previewers.lua | 47 ++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/lua/telescope/previewers.lua b/lua/telescope/previewers.lua index 9d0229d..c43892d 100644 --- a/lua/telescope/previewers.lua +++ b/lua/telescope/previewers.lua @@ -418,31 +418,47 @@ previewers.help = defaulter(function(_) preview_fn = function(self, entry, status) with_preview_window(status, nil, function() local special_chars = ":~^.?/%[%]%*" + local delim = string.char(9) local escaped = vim.fn.escape(entry.value, special_chars) - local tagfile = vim.fn.expand("$VIMRUNTIME") .. '/doc/tags' - local old_tags = vim.o.tags - vim.o.tags = '' + local tags = {} + local tag_entry for _,file in pairs(vim.fn.findfile('doc/tags', vim.o.runtimepath, -1)) do - vim.o.tags = vim.o.tags .. file .. ',' + local f = assert(io.open(file, "rb")) + for line in f:lines() do + tag_entry = {} + for match in (line..delim):gmatch("(.-)" .. delim) do + if vim.tbl_isempty(tag_entry) then + tag_entry.name = match + elseif not tag_entry.filename then + tag_entry.filename = match + else + tag_entry.cmd = match + end + end + + table.insert(tags, tag_entry) + end + f:close() end - vim.o.tags = vim.o.tags:sub(1,-2) -- Remove trailing comma - print(vim.inspect(vim.o.tags)) - local taglist = vim.fn.taglist('^' .. escaped .. '$') - vim.o.tags = old_tags - - if vim.tbl_isempty(taglist) then - taglist = vim.fn.taglist(escaped, tagfile) + local search_tags = function(pattern) + local results = {} + for _, tag in pairs(tags) do + if vim.fn.match(tag.name, pattern) ~= -1 then + table.insert(results, tag) + end + end + return results end - if vim.tbl_isempty(taglist) then - return - end + local taglist = search_tags('^' .. escaped .. '$') + if taglist == {} then taglist = search_tags(escaped) end local best_entry = taglist[1] - local new_bufnr = vim.fn.bufnr(best_entry.filename, true) + local new_bufnr = vim.fn.bufnr(vim.fn.findfile('doc/'..best_entry.filename, vim.o.runtimepath), true) + print(vim.inspect(new_bufnr)) vim.api.nvim_buf_set_option(new_bufnr, 'filetype', 'help') vim.api.nvim_win_set_buf(status.preview_win, new_bufnr) @@ -451,6 +467,7 @@ previewers.help = defaulter(function(_) -- remove leading '/' search_query = search_query:sub(2) + print(search_query) -- Set the query to "very nomagic". -- This should make it work quite nicely given tags. From 30aa3904ce0fea28837c25a43c37070a8fcefeb9 Mon Sep 17 00:00:00 2001 From: Nolan Prochnau Date: Sun, 15 Nov 2020 13:39:46 -0500 Subject: [PATCH 3/5] Refactor to make logic less intense --- lua/telescope/previewers.lua | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/lua/telescope/previewers.lua b/lua/telescope/previewers.lua index c43892d..463f9a6 100644 --- a/lua/telescope/previewers.lua +++ b/lua/telescope/previewers.lua @@ -421,24 +421,27 @@ previewers.help = defaulter(function(_) local delim = string.char(9) local escaped = vim.fn.escape(entry.value, special_chars) - local tags = {} - local tag_entry - for _,file in pairs(vim.fn.findfile('doc/tags', vim.o.runtimepath, -1)) do + + local find_rtp_file = function(path, count) + return vim.fn.findfile(path, vim.o.runtimepath, count) + end + + local matches = {} + for _,file in pairs(find_rtp_file('doc/tags', -1)) do local f = assert(io.open(file, "rb")) for line in f:lines() do - tag_entry = {} + matches = {} + for match in (line..delim):gmatch("(.-)" .. delim) do - if vim.tbl_isempty(tag_entry) then - tag_entry.name = match - elseif not tag_entry.filename then - tag_entry.filename = match - else - tag_entry.cmd = match - end + table.insert(matches, match) end - table.insert(tags, tag_entry) + table.insert(tags, { + name = matches[1], + filename = matches[2], + cmd = matches[3] + }) end f:close() end @@ -457,7 +460,7 @@ previewers.help = defaulter(function(_) if taglist == {} then taglist = search_tags(escaped) end local best_entry = taglist[1] - local new_bufnr = vim.fn.bufnr(vim.fn.findfile('doc/'..best_entry.filename, vim.o.runtimepath), true) + local new_bufnr = vim.fn.bufnr(find_rtp_file('doc/' .. best_entry.filename), true) print(vim.inspect(new_bufnr)) vim.api.nvim_buf_set_option(new_bufnr, 'filetype', 'help') From 50a87e502ce2739aab67966acda0f04a5f49bb6e Mon Sep 17 00:00:00 2001 From: Nolan Prochnau Date: Sun, 15 Nov 2020 14:33:22 -0500 Subject: [PATCH 4/5] Delete debug output --- lua/telescope/previewers.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/lua/telescope/previewers.lua b/lua/telescope/previewers.lua index 463f9a6..05efa66 100644 --- a/lua/telescope/previewers.lua +++ b/lua/telescope/previewers.lua @@ -461,7 +461,6 @@ previewers.help = defaulter(function(_) local best_entry = taglist[1] local new_bufnr = vim.fn.bufnr(find_rtp_file('doc/' .. best_entry.filename), true) - print(vim.inspect(new_bufnr)) vim.api.nvim_buf_set_option(new_bufnr, 'filetype', 'help') vim.api.nvim_win_set_buf(status.preview_win, new_bufnr) From f9f83004b6c729888f73a05c97727431bc306562 Mon Sep 17 00:00:00 2001 From: Nolan Prochnau Date: Mon, 16 Nov 2020 10:37:08 -0500 Subject: [PATCH 5/5] Delete more debug things --- lua/telescope/previewers.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/lua/telescope/previewers.lua b/lua/telescope/previewers.lua index 05efa66..f9d8e5b 100644 --- a/lua/telescope/previewers.lua +++ b/lua/telescope/previewers.lua @@ -469,7 +469,6 @@ previewers.help = defaulter(function(_) -- remove leading '/' search_query = search_query:sub(2) - print(search_query) -- Set the query to "very nomagic". -- This should make it work quite nicely given tags.