summaryrefslogtreecommitdiff
path: root/.config/nvim/lua/plugins/luasnip.lua
blob: 39debb398028f838812a150d9e5a7246d2650ad7 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
return {
    'L3MON4D3/LuaSnip',
    version = 'v2.*', -- follow latest release.
    build = 'make install_jsregexp',
    opts = {},
    config = function(_, opts)
        require('luasnip').setup(opts)

        -- Load language specific snippets
        require('luasnip.loaders.from_vscode').load({ paths = './snippets' })

        -- load project specific snippets
        local project = vim.fn.fnamemodify(vim.fn.getcwd(), ':t')
        local project_snippets = {
            eko = 'eko.json',
            ['carbonight.nvim'] = 'carbonight.json',
        }
        local file = project_snippets[project]
        if file then
            require('luasnip.loaders.from_vscode').load_standalone({ path = './snippets/' .. file })
        end

        -- some shorthands...
        local ls = require('luasnip')
        local s = ls.snippet
        local sn = ls.snippet_node
        local isn = ls.indent_snippet_node
        local t = ls.text_node
        local i = ls.insert_node
        local f = ls.function_node
        local c = ls.choice_node
        local d = ls.dynamic_node
        local r = ls.restore_node
        local events = require('luasnip.util.events')
        local ai = require('luasnip.nodes.absolute_indexer')
        local opt = require('luasnip.nodes.optional_arg')
        local extras = require('luasnip.extras')
        local l = extras.lambda
        local rep = extras.rep
        local p = extras.partial
        local m = extras.match
        local n = extras.nonempty
        local dl = extras.dynamic_lambda
        local fmt = require('luasnip.extras.fmt').fmt
        local fmta = require('luasnip.extras.fmt').fmta
        local conds = require('luasnip.extras.expand_conditions')
        local postfix = require('luasnip.extras.postfix').postfix
        local ts_postfix = require('luasnip.extras.treesitter_postfix').treesitter_postfix
        local types = require('luasnip.util.types')
        local parse = require('luasnip.util.parser').parse_snippet
        local ms = ls.multi_snippet
        local k = require('luasnip.nodes.key_indexer').new_key
        local postfix_builtin = require('luasnip.extras.treesitter_postfix').builtin

        ls.add_snippets('all', {
            ts_postfix({
                matchTSNode = postfix_builtin.tsnode_matcher.find_topmost_types({
                    'identifier',
                    'method_invocation',
                    'field_access',
                    'type_identifier',
                    'class_literal',
                    'this',
                    'scoped_type_identifier',
                }),
                trig = '.unwrap',
            }, {
                f(function(_, parent)
                    -- Use POSTFIX_MATCH instead of LS_TSMATCH
                    local match = parent.snippet.env.LS_TSMATCH
                    -- vim.notify('postfix: "' .. vim.inspect(match) .. '"')

                    if not match then
                        return { 'unwrap()' }
                    end

                    -- If LuaSnip provides the match as a table, concat it
                    if type(match) == 'table' then
                        match = table.concat(match, '\n')
                    end

                    local wrapped = string.format('unwrap(%s)', match)
                    -- Split it back into a table of strings to safely handle any newlines
                    return vim.split(wrapped, '\n', { plain = true })
                end),
            }),

            -- ts_postfix({
            --     trig = 'unwrap',
            --     matchTSNode = {
            --         query = [[
            --               (field_access
            --                 object: (
            --                     field_access
            --                     method_invocation
            --                     identifier
            --                 ) @my_object
            --               ) @my_field
            --         ]],
            --         query_lang = 'java',
            --         select = 'longest',
            --     },
            -- }, {
            --     f(function(_, parent)
            --         -- Use POSTFIX_MATCH instead of LS_TSMATCH
            --         local match = parent.snippet.env.POSTFIX_MATCH
            --         vim.notify('postfix: "' .. vim.inspect(match) .. '"')
            --
            --         if not match then
            --             return { 'unwrap()' }
            --         end
            --
            --         -- If LuaSnip provides the match as a table, concat it
            --         if type(match) == 'table' then
            --             match = table.concat(match, '\n')
            --         end
            --
            --         -- Format the string
            --         local wrapped = string.format('unwrap(%s)', match)
            --
            --         -- Split it back into a table of strings to safely handle any newlines
            --         return vim.split(wrapped, '\n', { plain = true })
            --     end),
            -- }, { condition = require('luasnip.util.util').yes }),
        })
        -- postfix({ trig = 'unwrap', match_pattern = [[[%w%.%_%-%"%']+$]] }, {
        --     f(function(_, parent)
        --         local match = parent.snippet.env.POSTFIX_MATCH
        --         if match == nil then
        --             return 'bunger'
        --         end
        --         -- match = match:sub(1, -2)
        --         return 'unwrap(' .. match .. ')'
        --     end, {}),
        -- }, { condition = require('luasnip.util.util').yes }),

        -- postfix({ trig = '.unwrap', snippetType = 'snippet' }, {
        --     f(function(_, parent)
        --         local match = parent.snippet.env.POSTFIX_MATCH
        --         if match == nil then
        --             return 'bunger'
        --         end
        --         return '[' .. match .. ']'
        --     end, {}),
        -- }),
    end,
}