summaryrefslogtreecommitdiff
path: root/lua
diff options
context:
space:
mode:
authorBrian Ryall <bryall@users.noreply.github.com>2020-09-06 22:48:36 -0400
committerGitHub <noreply@github.com>2020-09-06 22:48:36 -0400
commitb065423013c35112f00154b575900482738ec7ef (patch)
treeee6e83b40b6e67d95b71fcd8c96cb5ad3868daf4 /lua
parent0185d9b6b0fb2b00317ddec870cc6d2ac923a4ad (diff)
added treesitter builtin (#31)
* added treesitter builtim * fixed treesitter pr comments * fix the buffer previewer to keep lnum visable
Diffstat (limited to 'lua')
-rw-r--r--lua/telescope/builtin.lua56
-rw-r--r--lua/telescope/make_entry.lua25
-rw-r--r--lua/telescope/previewers.lua2
3 files changed, 83 insertions, 0 deletions
diff --git a/lua/telescope/builtin.lua b/lua/telescope/builtin.lua
index 8cf6906..4eae327 100644
--- a/lua/telescope/builtin.lua
+++ b/lua/telescope/builtin.lua
@@ -370,4 +370,60 @@ builtin.buffers = function(opts)
}):find()
end
+local function prepare_match(entry, kind)
+ local entries = {}
+
+ if entry.node then
+ entry["kind"] = kind
+ table.insert(entries, entry)
+ else
+ for name, item in pairs(entry) do
+ vim.list_extend(entries, prepare_match(item, name))
+ end
+ end
+
+ return entries
+end
+
+builtin.treesitter = function(opts)
+ opts = opts or {}
+
+ local has_nvim_treesitter, nvim_treesitter = pcall(require, 'nvim-treesitter')
+ if not has_nvim_treesitter then
+ print('You need to install nvim-treesitter')
+ return
+ end
+
+ local parsers = require('nvim-treesitter.parsers')
+ if not parsers.has_parser() then
+ print('No parser for the current buffer')
+ return
+ end
+
+ local ts_locals = require('nvim-treesitter.locals')
+ local bufnr = opts.bufnr or vim.api.nvim_get_current_buf()
+
+ local results = {}
+ for _, definitions in ipairs(ts_locals.get_definitions(bufnr)) do
+ local entries = prepare_match(definitions)
+ for _, entry in ipairs(entries) do
+ table.insert(results, entry)
+ end
+ end
+
+ if vim.tbl_isempty(results) then
+ return
+ end
+
+ pickers.new(opts, {
+ prompt = 'Treesitter Symbols',
+ finder = finders.new_table {
+ results = results,
+ entry_maker = make_entry.gen_from_treesitter(opts)
+ },
+ previewer = previewers.vim_buffer.new(opts),
+ sorter = sorters.get_norcalli_sorter(),
+ }):find()
+end
+
return builtin
diff --git a/lua/telescope/make_entry.lua b/lua/telescope/make_entry.lua
index 7c35699..a25758e 100644
--- a/lua/telescope/make_entry.lua
+++ b/lua/telescope/make_entry.lua
@@ -191,4 +191,29 @@ function make_entry.gen_from_buffer(opts)
end
end
+function make_entry.gen_from_treesitter(opts)
+ opts = opts or {}
+ return function(entry)
+ local ts_utils = require('nvim-treesitter.ts_utils')
+ local start_row, start_col, end_row, end_col = ts_utils.get_node_range(entry.node)
+ local node_text = ts_utils.get_node_text(entry.node)[1]
+ local bufnr = vim.api.nvim_get_current_buf()
+ return {
+ valid = true,
+
+ value = entry.node,
+ ordinal = entry.kind .. " " .. node_text,
+ display = entry.kind .. " " .. node_text,
+
+ filename = vim.api.nvim_buf_get_name(bufnr),
+ -- need to add one since the previewer substacts one
+ lnum = start_row + 1,
+ col = start_col,
+ text = node_text,
+ start = start_row,
+ finish = end_row
+ }
+ end
+end
+
return make_entry
diff --git a/lua/telescope/previewers.lua b/lua/telescope/previewers.lua
index 2978e03..493a2f6 100644
--- a/lua/telescope/previewers.lua
+++ b/lua/telescope/previewers.lua
@@ -123,6 +123,8 @@ previewers.vim_buffer = defaulter(function(_)
if entry.lnum then
vim.api.nvim_buf_add_highlight(bufnr, previewer_ns, "Visual", entry.lnum - 1, 0, -1)
+ vim.api.nvim_win_set_option(status.preview_win, 'scrolloff', 10)
+ vim.api.nvim_win_set_cursor(status.preview_win, {entry.lnum, 0})
-- print("LNUM:", entry.lnum)
end
end,