aboutsummaryrefslogtreecommitdiffstats
path: root/lua
diff options
context:
space:
mode:
authorKristijan Husak <kristijan.husak@saturized.com>2020-10-09 13:43:34 +0200
committerKiyan Yazdani <yazdani.kiyan@protonmail.com>2020-10-11 13:35:18 +0200
commit62df1143da1e7c1d02a883f5ead2479ec17729ed (patch)
tree904e4f082c856a8a844d4a7e3d0abf417356b3dc /lua
parent[docgen] Update README.md (diff)
downloadnvim-treesitter-62df1143da1e7c1d02a883f5ead2479ec17729ed.tar
nvim-treesitter-62df1143da1e7c1d02a883f5ead2479ec17729ed.tar.gz
nvim-treesitter-62df1143da1e7c1d02a883f5ead2479ec17729ed.tar.bz2
nvim-treesitter-62df1143da1e7c1d02a883f5ead2479ec17729ed.tar.lz
nvim-treesitter-62df1143da1e7c1d02a883f5ead2479ec17729ed.tar.xz
nvim-treesitter-62df1143da1e7c1d02a883f5ead2479ec17729ed.tar.zst
nvim-treesitter-62df1143da1e7c1d02a883f5ead2479ec17729ed.zip
Add implementation for improved statusline. Closes #545.
Diffstat (limited to 'lua')
-rw-r--r--lua/nvim-treesitter.lua52
1 files changed, 52 insertions, 0 deletions
diff --git a/lua/nvim-treesitter.lua b/lua/nvim-treesitter.lua
index f90e264db..b61164268 100644
--- a/lua/nvim-treesitter.lua
+++ b/lua/nvim-treesitter.lua
@@ -53,4 +53,56 @@ function M.statusline(indicator_size)
end
end
+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
+
return M