aboutsummaryrefslogtreecommitdiffstats
path: root/lua/alpha/utils.lua
blob: f1de1506e1293f22f95fcb9fe9bfdab8918c277e (plain)
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
local ts_utils = require("nvim-treesitter.ts_utils")

local M = {}

M.ucfirst = function(str)
    return (str:gsub("^%l", string.upper))
end

M.get_master_node = function()
    local node = ts_utils.get_node_at_cursor()
    if node == nil then
        error("No treesitter parser found.")
    end

    local start_row = node:start()

    local parent = node:parent()
    while (parent ~= nil and parent:start() == start_row) do
        node = parent
        parent = node:parent()
    end

    return node
end

M.get_root_node = function ()
    local node = ts_utils.get_node_at_cursor()
    if node == nil then
        error("No treesitter parser found.")
    end

    local parent = node:parent()
    while (parent ~= nil) do
        node = parent
        parent = node:parent()
    end

    return node
end

return M