aboutsummaryrefslogtreecommitdiffstats
path: root/lua/alpha/scratchs/draft.lua
blob: 8954fdcec58b86e51694e54de5625b51227eae1e (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
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
local pickers = require("telescope.pickers")
local finders = require("telescope.finders")
local conf = require("telescope.config").values
local action_state = require("telescope.actions.state")
local action_set = require("telescope.actions.set")
local get_node_text = vim.treesitter.get_node_text

local function getElements()
    vim.treesitter.set_query(
        "yaml",
        "GraphQL_endpoints",
        [[
    (document (block_node (block_mapping (block_mapping_pair
        value: (block_node (block_mapping (block_mapping_pair
            value: (block_node (block_mapping (block_mapping_pair
                value: (block_node (block_mapping (block_mapping_pair
                    key: (flow_node) @mutation
                    value: (block_node (block_mapping (block_mapping_pair
                         key: (flow_node) @resolve (#eq? @resolve "resolve")
                         value: (flow_node) @resolver
                     )))
                )))
            )))
        )))
    ))))
        ]]
    )

    local query = vim.treesitter.get_query("yaml", "GraphQL_endpoints")

    local elements = {}

    local parsers = require("nvim-treesitter.parsers")
    local parser = parsers.get_parser(0)
    ---@diagnostic disable-next-line: deprecated
    local tree = unpack(parser:parse())

    for id, node in query:iter_captures(tree:root(), 0) do
        if query.captures[id] == "mutation" then
            table.insert(elements, { get_node_text(node, 0), node:start() + 1 })
        end
    end

    return elements
end

function Queries(opts)
    opts = opts or {}
    pickers.new(opts, {
        promt_title = "Queries",
        finder = finders.new_table({
            results = getElements(),
            entry_maker = function(entry)
                return {
                    value = entry,
                    display = entry[1],
                    ordinal = entry[1],
                    path = vim.api.nvim_buf_get_name(0),
                    lnum = entry[2],
                }
            end,
        }),
        sorter = conf.generic_sorter(opts),
        previewer = conf.grep_previewer(opts),
        attach_mappings = function()
            action_set.select:enhance({
                post = function()
                    local selection = action_state.get_selected_entry()
                    vim.api.nvim_win_set_cursor(0, { selection.lnum, 0 })
                end,
            })

            return true
        end,
    }):find()
end