From 7682a1a49f7c3be58df18dfc18ad3ba65dd3be1f Mon Sep 17 00:00:00 2001 From: Thomas Vigouroux Date: Tue, 5 May 2020 15:04:01 +0200 Subject: feat: provide a statusline indicator It will show the current branch at the cursor going the tree as such. root->node->subnode->leaf If an argument is provided to `statusline`, then the tree will be truncated as follows : ..->subnode->subnode --- lua/nvim-treesitter.lua | 31 +++++++++++++++++++++++++++++++ lua/nvim-treesitter/parsers.lua | 3 ++- 2 files changed, 33 insertions(+), 1 deletion(-) (limited to 'lua') diff --git a/lua/nvim-treesitter.lua b/lua/nvim-treesitter.lua index e1c95ed31..90b5468a3 100644 --- a/lua/nvim-treesitter.lua +++ b/lua/nvim-treesitter.lua @@ -24,4 +24,35 @@ function M.setup(lang) end end +function M.statusline(indicator_size) + local indicator_size = indicator_size or 1000 + local expr = require"nvim-treesitter.utils".expression_at_point() + local current_node = + require'nvim-treesitter.node_movement'.current_node[api.nvim_get_current_buf()] + + local indicator = "" + while expr and (#indicator + #(expr:type()) + 5) < indicator_size do + + local prefix = "" + if expr:parent() then + prefix = "->" + end + + + if expr == current_node then + indicator = string.format("%s[%s]%s", prefix, expr:type(), indicator) + else + indicator = prefix .. expr:type() .. indicator + end + + expr = expr:parent() + end + + if expr then + return "..." .. indicator + else + return indicator + end +end + return M diff --git a/lua/nvim-treesitter/parsers.lua b/lua/nvim-treesitter/parsers.lua index e046ca45c..07f3e9d34 100644 --- a/lua/nvim-treesitter/parsers.lua +++ b/lua/nvim-treesitter/parsers.lua @@ -5,11 +5,12 @@ local M = {} function M.has_parser(lang) local lang = lang or api.nvim_buf_get_option(0, 'filetype') + if not lang or #lang == 0 then return false end return #api.nvim_get_runtime_file('parser/' .. lang .. '.*', false) > 0 end function M.get_parser(bufnr, lang) - if M.has_parser() then + if M.has_parser(lang) then local buf = bufnr or api.nvim_get_current_buf() local lang = lang or api.nvim_buf_get_option(buf, 'ft') if not M[buf] then -- cgit v1.2.3-70-g09d2