diff options
| author | kiyan <yazdani.kiyan@protonmail.com> | 2022-07-18 12:29:02 +0200 |
|---|---|---|
| committer | Christian Clason <christian.clason@uni-due.de> | 2022-07-18 15:44:59 +0200 |
| commit | 8b748a7570b89822d47ac0ed0f694efda6523c7d (patch) | |
| tree | b18e487072db2ecf15cb5f88d04f7410d804bdfa /lua/nvim-treesitter/statusline.lua | |
| parent | 91ee9e73767a518fbc04562a84b590a18db9f68d (diff) | |
chore: cleanup main file, move statusline in module
Diffstat (limited to 'lua/nvim-treesitter/statusline.lua')
| -rw-r--r-- | lua/nvim-treesitter/statusline.lua | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/lua/nvim-treesitter/statusline.lua b/lua/nvim-treesitter/statusline.lua new file mode 100644 index 00000000..82be0653 --- /dev/null +++ b/lua/nvim-treesitter/statusline.lua @@ -0,0 +1,50 @@ +local parsers = require "nvim-treesitter.parsers" +local ts_utils = require "nvim-treesitter.ts_utils" + +local M = {} + +-- Trim spaces and opening brackets from end +local transform_line = function(line) + return line:gsub("%s*[%[%(%{]*%s*$", "") +end + +function M.statusline(opts) + if not parsers.has_parser() then + return + end + local options = opts or {} + -- if type(opts) == "number" then + -- options = { indicator_size = opts } + -- end + local bufnr = options.bufnr or 0 + 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 = ts_utils._get_line_for_node(expr, type_patterns, transform_fn, bufnr) + 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 + +return M |
