summaryrefslogtreecommitdiff
path: root/.config/nvim/after/plugin/luasnip.lua
blob: 6d9d662dbe91a3a40da80fbeef5deb8ec6343585 (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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
local ls = require('luasnip')
local ts_utils = require('nvim-treesitter.ts_utils')
local ts_locals = require('nvim-treesitter.locals')

local c = ls.choice_node
local d = ls.dynamic_node
local f = ls.function_node
local i = ls.insert_node
local s = ls.s
local sn = ls.snippet_node
local t = ls.text_node
local fmt = require('luasnip.extras.fmt').fmt

ls.config.set_config {
	history = true,
	updateevents = 'TextChanged,TextChangedI',
}

ls.add_snippets('lua', {
	s('req', fmt([[local {} = require('{}')]], {
		f(function(name)
			local xs = vim.split(name[1][1], '.', { plain = true })
			return xs[#xs] or ''
		end, { 1 }),
		i(1),
	})),
})

local function is_ptr(str)
	return string.find(str, '*', 1, true) ~= nil
end

local function is_num(str)
	return vim.regex(
		'\\v^('
		.. 'u?int(8|16|32|64)?'
		.. '|byte'
		.. '|complex(64|128)'
		.. '|float(32|64)'
		.. '|rune'
		.. '|uintptr'
		.. ')$'
	):match_str(str) ~= nil
end

local transforms = {
	bool = 'false',
	string = '""',
	error = 'err',
	[is_num] = '0',
	[is_ptr] = 'nil',
}

local transform = function(text)
	local condition_matches = function(condition, ...)
		if type(condition) == 'string' then
			return condition == text
		else
			return condition(...)
		end
	end

	for condition, result in pairs(transforms) do
		if condition_matches(condition, text) then
			return t(result)
		end
	end

	return t(text .. '{}')
end

local handlers = {
	parameter_list = function(node)
		local result = {}

		local count = node:named_child_count()
		for idx = 0, count - 1 do
			local matching_node = node:named_child(idx)
			local type_node = matching_node:field('type')[1]
			table.insert(result, transform(
				vim.treesitter.get_node_text(type_node, 0)
			))
			if idx ~= count - 1 then
				table.insert(result, t({ ', ' }))
			end
		end

		return result
	end,

	type_identifier = function(node)
		local text = vim.treesitter.get_node_text(node, 0)
		return { transform(text) }
	end,
}

local function_node_types = {
	function_declaration = true,
	method_declaration = true,
	func_literal = true,
}

local function go_result_type()
	local cursor_node = ts_utils.get_node_at_cursor()
	if cursor_node == nil then
		print('Unable to find position')
		return t('')
	end
	local scope = ts_locals.get_scope_tree(cursor_node, 0)

	local function_node
	for _, v in ipairs(scope) do
		if function_node_types[v:type()] then
			function_node = v
			break
		end
	end

	if not function_node then
		print('Not inside of a function')
		return t('')
	end

	local query = vim.treesitter.query.parse('go', [[
		[
			(method_declaration result: (_) @id)
			(function_declaration result: (_) @id)
			(func_literal result: (_) @id)
		]
	]])
	for _, node in query:iter_captures(function_node, 0) do
		if handlers[node:type()] then
			return handlers[node:type()](node)
		end
	end
end

ls.add_snippets('go', {
	s(
		'f',
		fmt(
			[[
				func <><>() <>{
					<>
				}
			]],
			{
				c(1, {
					t(''),
					t('() '),
				}),
				i(2, 'name'),
				c(3, {
					t(''),
					t('() '),
				}),
				i(0),
			},
			{
				delimiters = '<>',
			}
		)
	),
	s(
		'ife',
		fmt(
			[[
				if err != nil {
					return <>
				}
			]],
			{
				d(1, function()
					return sn(nil, go_result_type())
				end),
			},
			{
				delimiters = '<>',
			}
		)
	),
})

vim.keymap.set({ 'i', 's' }, '<C-l>', function()
	if ls.expand_or_jumpable() then
		ls.expand_or_jump()
	end
end)
vim.keymap.set('i', '<C-h>', function()
	if ls.jumpable(-1) then
		ls.jump(-1)
	end
end)
vim.keymap.set('i', '<C-k>', function()
	if ls.choice_active() then
		ls.change_choice(-1)
	end
end)
vim.keymap.set('i', '<C-j>', function()
	if ls.choice_active() then
		ls.change_choice(1)
	end
end)