2020-09-01 23:39:06 +02:00
|
|
|
--TODO(theHamsta): remove once stabilized!
|
|
|
|
|
if not pcall(require,"vim.treesitter.query") then
|
|
|
|
|
error("nvim-treesitter requires a more recent Neovim nightly version!")
|
|
|
|
|
end
|
|
|
|
|
|
2020-04-19 14:43:55 +02:00
|
|
|
local install = require'nvim-treesitter.install'
|
2020-04-22 11:13:05 +02:00
|
|
|
local utils = require'nvim-treesitter.utils'
|
2020-06-19 13:45:33 +02:00
|
|
|
local ts_utils = require'nvim-treesitter.ts_utils'
|
2020-04-22 11:13:05 +02:00
|
|
|
local info = require'nvim-treesitter.info'
|
|
|
|
|
local configs = require'nvim-treesitter.configs'
|
2020-06-20 12:21:42 +02:00
|
|
|
local parsers = require'nvim-treesitter.parsers'
|
2020-04-18 19:32:51 +02:00
|
|
|
|
2020-09-01 23:39:06 +02:00
|
|
|
|
2020-08-15 09:24:24 -05:00
|
|
|
-- Registers all query predicates
|
|
|
|
|
require"nvim-treesitter.query_predicates"
|
|
|
|
|
|
2020-04-18 19:32:51 +02:00
|
|
|
local M = {}
|
|
|
|
|
|
2020-05-08 11:22:59 +02:00
|
|
|
function M.setup()
|
2020-04-22 11:13:05 +02:00
|
|
|
utils.setup_commands('install', install.commands)
|
|
|
|
|
utils.setup_commands('info', info.commands)
|
|
|
|
|
utils.setup_commands('configs', configs.commands)
|
2020-07-02 10:26:53 -05:00
|
|
|
configs.init()
|
|
|
|
|
end
|
2020-04-18 19:32:51 +02:00
|
|
|
|
2020-07-02 10:26:53 -05:00
|
|
|
function M.define_modules(...)
|
|
|
|
|
configs.define_modules(...)
|
2020-04-21 23:40:23 +02:00
|
|
|
end
|
2020-04-19 14:43:55 +02:00
|
|
|
|
2020-05-05 15:04:01 +02:00
|
|
|
function M.statusline(indicator_size)
|
2020-06-21 19:50:24 +02:00
|
|
|
if not parsers.has_parser() then return end
|
2020-05-08 11:22:59 +02:00
|
|
|
local indicator_size = indicator_size or 100
|
2020-05-05 15:04:01 +02:00
|
|
|
|
2020-06-19 13:45:33 +02:00
|
|
|
local current_node = ts_utils.get_node_at_cursor()
|
2020-05-08 11:22:59 +02:00
|
|
|
if not current_node then return "" end
|
2020-05-05 15:04:01 +02:00
|
|
|
|
2020-05-08 11:22:59 +02:00
|
|
|
local expr = current_node:parent()
|
|
|
|
|
local prefix = ""
|
|
|
|
|
if expr then
|
|
|
|
|
prefix = "->"
|
|
|
|
|
end
|
2020-05-05 15:04:01 +02:00
|
|
|
|
2020-05-08 11:22:59 +02:00
|
|
|
local indicator = current_node:type()
|
|
|
|
|
while expr and (#indicator + #(expr:type()) + 5) < indicator_size do
|
|
|
|
|
indicator = expr:type() .. prefix .. indicator
|
2020-05-05 15:04:01 +02:00
|
|
|
expr = expr:parent()
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if expr then
|
|
|
|
|
return "..." .. indicator
|
|
|
|
|
else
|
|
|
|
|
return indicator
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-10-09 13:43:34 +02:00
|
|
|
local get_line_for_node = function(node, type_patterns, transform_fn)
|
|
|
|
|
local node_type = node:type()
|
|
|
|
|
local is_valid = false
|
|
|
|
|
for _, rgx in ipairs(type_patterns) do
|
|
|
|
|
if node_type:find(rgx) then
|
|
|
|
|
is_valid = true
|
|
|
|
|
break
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
if not is_valid then return '' end
|
|
|
|
|
local range = {node:range()}
|
|
|
|
|
local line = transform_fn(vim.trim(vim.fn.getline(range[1] + 1)))
|
|
|
|
|
-- Escape % to avoid statusline to evaluate content as expression
|
|
|
|
|
return line:gsub('%%', '%%%%')
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Trim spaces and opening brackets from end
|
|
|
|
|
local transform_line = function(line)
|
|
|
|
|
return line:gsub('[%[%(%{]*%s*$', '')
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function M.named_statusline(opts)
|
|
|
|
|
if not parsers.has_parser() then return end
|
|
|
|
|
local options = opts or {}
|
|
|
|
|
local indicator_size = options.indicator_size or 100
|
|
|
|
|
local type_patterns = options.type_patterns or {'class', 'function', 'method'}
|
|
|
|
|
local transform_fn = options.transform_fn or transform_line
|
|
|
|
|
local separator = options.separator or ' -> '
|
|
|
|
|
|
|
|
|
|
local current_node = ts_utils.get_node_at_cursor()
|
|
|
|
|
if not current_node then return "" end
|
|
|
|
|
|
|
|
|
|
local lines = {}
|
|
|
|
|
local expr = current_node
|
|
|
|
|
|
|
|
|
|
while expr do
|
|
|
|
|
local line = get_line_for_node(expr, type_patterns, transform_fn)
|
|
|
|
|
if line ~= '' and not vim.tbl_contains(lines, line) then
|
|
|
|
|
table.insert(lines, 1, line)
|
|
|
|
|
end
|
|
|
|
|
expr = expr:parent()
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local text = table.concat(lines, separator)
|
|
|
|
|
local text_len = #text
|
|
|
|
|
if text_len > indicator_size then
|
|
|
|
|
return '...'..text:sub(text_len - indicator_size, text_len)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return text
|
|
|
|
|
end
|
|
|
|
|
|
2020-04-18 19:32:51 +02:00
|
|
|
return M
|