Skip to content

Commit

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

builtin.tags = function(opts)
opts = opts or {}

if not vim.loop.fs_open('tags', "r", 438) 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

local fd = assert(vim.loop.fs_open('tags', "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))
local lines = vim.split(data, '\n')

local results = {}
opts.file_width = 0
opts.tag_width = 0
for i = 2, #lines do
if lines[i]:sub(1, 1) ~= '!' and lines[i] ~= "" then
if not string.match(lines[i], '\tline:%d') then
print("You have to create tags with the line field. Run: ctags -R --field=+n")
return
end
local tag, file, scode, lnum = string.match(lines[i], '([^\t]+)\t([^\t]+)\t/^\t?(.*)/;"\t+.*\tline:(%d+)')
table.insert(results, {
tag = tag,
file = file,
line = scode,
lnum = tonumber(lnum)
})
if not opts.hide_filename then
if opts.file_width < #file then
opts.file_width = #file
end
end

if opts.tag_width < #tag then
opts.tag_width = #tag
end
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
52 changes: 51 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,54 @@ function make_entry.gen_from_vimoptions(opts)
end
end

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

local displayer = entry_display.create {
separator = "",
items = {
{ width = opts.file_width },
{ width = opts.tag_width },
{ remaining = true },
},
}

local make_display = function(entry)
local filename
if not opts.hide_filename then
if opts.shorten_path then
filename = path.shorten(entry.filename)
else
filename = entry.filename
end
end
local line
if not opts.hide_line then
line = entry.line
end

return displayer {
filename,
entry.tag,
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
4 changes: 2 additions & 2 deletions lua/telescope/pickers/entry_display.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ end

local function truncate(str, len)
-- TODO: This doesn't handle multi byte chars...
if vim.fn.strdisplaywidth(str) > len - 1 then
if vim.fn.strdisplaywidth(str) > len then
str = str:sub(1, len)
str = str .. ""
end
Expand All @@ -68,7 +68,7 @@ entry_display.create = function(configuration)

return function(self, picker)
local results = {}
for k, v in ipairs(self) do
for k, v in pairs(self) do
table.insert(results, generator[k](v, picker))
end

Expand Down

0 comments on commit 69ddba3

Please sign in to comment.