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 13, 2020
1 parent 2405736 commit a1651b9
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 4 deletions.
26 changes: 26 additions & 0 deletions lua/telescope/builtin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -923,4 +923,30 @@ builtin.marks = function(opts)
}):find()
end

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

if not vim.loop.fs_open('tags', "r", 438) then
print('Tags file does not exists. Create one with ctags -R')
return
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')

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

return builtin
70 changes: 69 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,72 @@ function make_entry.gen_from_vimoptions(opts)
end
end

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

local cwd = vim.fn.expand(opts.cwd or vim.fn.getcwd())
local current_file = path.normalize(vim.fn.expand('%'), cwd)

local display_items = {
{ width = 30 },
{ remaining = true },
}

if opts.show_line then
table.insert(display_items, 2, { width = 30 })
end

local displayer = entry_display.create {
separator = "",
items = display_items,
}

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 scode
if opts.show_line then
scode = entry.scode
end

return displayer {
filename,
entry.tag,
scode,
}
end

return function(line)
if line == '' or line:sub(1, 1) == '!' then
return nil
end

local tag, file, scode = string.match(line, '([^\t]+)\t([^\t]+)\t/^\t?(.*)/;"\t+.*')

if opts.only_current_file and file ~= current_file then
return nil
end

return {
valid = true,

ordinal = file .. ': ' .. tag .. ': ' .. scode,
display = make_display,
scode = scode,
tag = tag,

filename = file,

col = 1,
}
end
end

return make_entry
6 changes: 3 additions & 3 deletions lua/telescope/pickers/entry_display.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ end

local function truncate(str, len)
-- TODO: This doesn't handle multi byte chars...
if vim.fn.strdisplaywidth(str) > len - 1 then
str = str:sub(1, len)
if vim.fn.strdisplaywidth(str) > len then
str = str:sub(1, len - 1)
str = str .. ""
end
return str
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
28 changes: 28 additions & 0 deletions lua/telescope/previewers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,34 @@ previewers.vimgrep = defaulter(function(opts)
}
end, {})

previewers.ctags = defaulter(function(opts)
local maker = get_maker(opts)

return previewers.new_termopen_previewer {
get_command = function(entry, status)
local strip_endline = string.gsub(entry.scode, '[$]$', '')
local strip_doubleslash = string.gsub(strip_endline, [[\\]], [[\]])
local strip_c_comment = string.gsub(strip_doubleslash, [[\/]], [[/]])

local cmd = 'grep -Frn --exclude=tags --exclude-dir=".git" \'' .. strip_c_comment .. '\''
local output = vim.split(utils.get_os_command_output(cmd), ':')
entry.lnum = tonumber(output[2])

local win_id = status.preview_win
local height = vim.api.nvim_win_get_height(win_id)

local filename = entry.filename
local lnum = entry.lnum or 0

local context = math.floor(height / 2)
local start = math.max(0, lnum - context)
local finish = lnum + context

return maker(filename, lnum, start, finish)
end,
}
end, {})

previewers.qflist = defaulter(function(opts)
opts = opts or {}

Expand Down

0 comments on commit a1651b9

Please sign in to comment.