aboutsummaryrefslogtreecommitdiffstats
path: root/lua/nvim-treesitter.lua
blob: cd1b685097fcfeade54a1ee8e34ac415eed1d76a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
--TODO(theHamsta): remove once stabilized!
if not pcall(require,"vim.treesitter.query") then
  error("nvim-treesitter requires a more recent Neovim nightly version!")
end

local install = require'nvim-treesitter.install'
local utils = require'nvim-treesitter.utils'
local ts_utils = require'nvim-treesitter.ts_utils'
local info = require'nvim-treesitter.info'
local configs = require'nvim-treesitter.configs'
local parsers = require'nvim-treesitter.parsers'


-- Registers all query predicates
require"nvim-treesitter.query_predicates"

local M = {}

function M.setup()
  utils.setup_commands('install', install.commands)
  utils.setup_commands('info', info.commands)
  utils.setup_commands('configs', configs.commands)
  configs.init()
end

function M.define_modules(...)
  configs.define_modules(...)
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 line = transform_fn(vim.trim(ts_utils.get_node_text(node)[1] or ''))
  -- 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*[%[%(%{]*%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 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