Skip to content

Commit

Permalink
WIP: Add tags builtin
Browse files Browse the repository at this point in the history
  • Loading branch information
Conni2461 committed Nov 3, 2020
1 parent 051aefd commit faa79ff
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
47 changes: 47 additions & 0 deletions lua/telescope/builtin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -904,4 +904,51 @@ builtin.marks = function(opts)
}):find()
end

builtin.tags = function(opts)
-- TODO(conni2461): Handle opts like no filename, etc
opts = opts or {}

-- TODO(conni2461): Do we have a function like this already?! In plenary for example
-- If not put in utils or plenary
local function file_exists(file)
local f = io.open(file, "rb")
if f then f:close() end
return f ~= nil
end

if not file_exists('tags') then
local answer = vim.fn.input('Tags file does not exists. Do you want to create one? y/[n]')
if answer:lower() == 'y' then
os.execute('ctags -R --fields=+n')
else
return
end
end

-- TODO(conni2461): Currently has to be generated with --fields=n or --fields=+n to get the line numbers
-- Take a look at fzf, how they do it.
local results = {}
for line in io.lines('tags') do
if line:sub(1, 1) ~= '!'then
local tag, file, scode, lnum = string.match(line, '([^\t]+)\t([^\t]+)\t(.*)\t*\tline:(%d+)')
table.insert(results, {
tag = tag,
file = file,
line = scode,
lnum = tonumber(lnum)
})
end
end

pickers.new(opts,{
prompt = 'Tags',
finder = finders.new_table {
results = results,
entry_maker = make_entry.gen_from_ctags(opts),
},
previewer = previewers.vimgrep.new(opts),
sorter = conf.generic_sorter(opts),
}):find()
end

return builtin
34 changes: 33 additions & 1 deletion lua/telescope/make_entry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ do
end

local execute_keys = {
path = function(t)
path = function(t)
return t.cwd .. path.separator .. t.filename, false
end,

Expand Down Expand Up @@ -572,4 +572,36 @@ function make_entry.gen_from_vimoptions(opts)
end
end

function make_entry.gen_from_ctags(opts)
opts = opts or {}

local make_display = function(entry)
local display_filename
if opts.shorten_path then
display_filename = path.shorten(entry.filename)
else
display_filename = entry.filename
end

-- TODO(conni2461): Write make_entry and make table pretty with new table create
return display_filename .. ': ' .. entry.tag .. ': ' .. entry.line
end

return function(entry)
return {
valid = true,

ordinal = entry.file .. ': ' .. entry.tag .. ': ' .. entry.line,
display = make_display,
line = entry.line,
tag = entry.tag,

filename = entry.file,

lnum = entry.lnum or 1,
col = 1,
}
end
end

return make_entry

0 comments on commit faa79ff

Please sign in to comment.