summaryrefslogtreecommitdiffstats
path: root/.config/nvim/after/ftplugin/lua.lua
blob: b7d29c137839619a3845daf185550aa79d6a8578 (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
local function format()
	local view = vim.fn.winsaveview()
	local buf_str = table.concat(vim.api.nvim_buf_get_lines(0, 0, -1, false), "\n") .. "\n"
	local tempname = vim.fn.tempname()
	local tempfile = assert(io.open(tempname, "w"), "Could not open temporary file")
	tempfile:write(buf_str)
	tempfile:close()
	local r_check = vim.system({ "stylua", "--check", tempname }):wait()
	if r_check.code == 0 then
		return
	elseif r_check.code ~= 1 then
		vim.notify("stylua failed (" .. r_check.code .. "):\n" .. r_check.stderr)
	end
	local r_format = vim.system({ "stylua", tempname }):wait()
	if r_format.code ~= 0 then
		vim.notify("stylua failed (" .. r_format.code .. "):\n" .. r_format.stderr)
	end
	local formatted_lines = {}
	for line in io.lines(tempname) do
		formatted_lines[#formatted_lines + 1] = line
	end
	os.remove(tempname)
	vim.api.nvim_buf_set_lines(0, 0, -1, false, formatted_lines)
	vim.fn.winrestview(view)
end

vim.opt_local.textwidth = vim.g.dotfiles.textwidth.lua -- gw wraps at this value
vim.opt_local.colorcolumn = "+1" -- Highlight one column after 'textwidth'
vim.opt_local.complete = {
	"o", -- 'omnifunc'
	"F", -- 'completefunc' (snippet source, see plugin/50-completion.lua)
}

vim.b.autoformat = true
vim.b.format_func = format

------------------------------------------------------------------------------------------------------------------------
-- Snippets
------------------------------------------------------------------------------------------------------------------------
-- Trigger word -> LSP snippet body. `$1` mirrors the `${1:X}` placeholder, so the variable is typed once.
-- Surfaced in the completion menu via the "F" source in 'complete' above (see plugin/50-completion.lua).
vim.b.snippets = {
	["debug_print"] = 'vim.print("${1:X} = " .. vim.inspect($1))',
}
vim.bo.completefunc = "v:lua.dotfiles_snippet_source"