mirror of
https://github.com/nvim-treesitter/nvim-treesitter
synced 2024-09-16 14:34:05 +02:00
chore: cleanup main file, move statusline in module
This commit is contained in:
parent
91ee9e7376
commit
8b748a7570
3 changed files with 54 additions and 50 deletions
|
@ -1,5 +1,5 @@
|
||||||
function! nvim_treesitter#statusline(...) abort
|
function! nvim_treesitter#statusline(...) abort
|
||||||
return luaeval("require'nvim-treesitter'.statusline(_A)", get(a:, 1, {}))
|
return luaeval("require'nvim-treesitter.statusline'.statusline(_A)", get(a:, 1, {}))
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! nvim_treesitter#foldexpr() abort
|
function! nvim_treesitter#foldexpr() abort
|
||||||
|
|
|
@ -2,8 +2,7 @@ local install = require "nvim-treesitter.install"
|
||||||
local utils = require "nvim-treesitter.utils"
|
local utils = require "nvim-treesitter.utils"
|
||||||
local info = require "nvim-treesitter.info"
|
local info = require "nvim-treesitter.info"
|
||||||
local configs = require "nvim-treesitter.configs"
|
local configs = require "nvim-treesitter.configs"
|
||||||
local parsers = require "nvim-treesitter.parsers"
|
local statusline = require "nvim-treesitter.statusline"
|
||||||
local ts_utils = require "nvim-treesitter.ts_utils"
|
|
||||||
|
|
||||||
-- Registers all query predicates
|
-- Registers all query predicates
|
||||||
require "nvim-treesitter.query_predicates"
|
require "nvim-treesitter.query_predicates"
|
||||||
|
@ -17,52 +16,7 @@ function M.setup()
|
||||||
configs.init()
|
configs.init()
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.define_modules(...)
|
M.define_modules = configs.define_modules
|
||||||
configs.define_modules(...)
|
M.statusline = statusline.statusline
|
||||||
end
|
|
||||||
|
|
||||||
-- 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
|
return M
|
||||||
|
|
50
lua/nvim-treesitter/statusline.lua
Normal file
50
lua/nvim-treesitter/statusline.lua
Normal file
|
@ -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
|
Loading…
Reference in a new issue