configuration.nix/home/laptop/programs/neovim/nvim/init.lua

163 lines
4.2 KiB
Lua
Raw Normal View History

2023-07-15 17:27:18 +02:00
-- START GLOBAL CONFIG
2023-08-20 18:35:52 +02:00
vim.wo.number = true -- show numbers
vim.o.scrolloff = 15 -- scrll if n lines left
vim.o.hlsearch = false -- highlight search
2023-09-12 19:32:08 +02:00
vim.o.updatetime = 20
2023-08-21 13:11:34 +02:00
vim.o.autoread = true
2023-07-13 21:42:16 +02:00
2023-07-31 07:47:54 +02:00
vim.g.mapleader = " " -- leader space
2023-07-13 21:40:11 +02:00
vim.g.maplocalleader = " "
2023-07-14 06:41:00 +02:00
vim.keymap.set({ "n", "v" }, "<Space>", "<Nop>", { silent = true }) -- nop leader
2023-07-13 19:36:13 +02:00
local nmap = function(keys, cmd)
vim.keymap.set("n", keys, cmd, { noremap = true, silent = true })
end
local vmap = function(keys, cmd)
vim.keymap.set("v", keys, cmd, { noremap = true, silent = true })
end
local tmap = function(keys, cmd)
vim.keymap.set("t", keys, cmd, { noremap = true, silent = true })
end
2023-07-13 19:36:13 +02:00
2023-07-16 20:27:04 +02:00
nmap("<leader>/", require("Comment.api").toggle.linewise.current)
vmap("<leader>/", "<ESC><cmd>lua require('Comment.api').toggle.linewise(vim.fn.visualmode())<CR>")
2023-07-13 21:40:11 +02:00
nmap("<Tab>", "<cmd>BufferNext<cr>")
nmap("<S-Tab>", "<cmd>BufferPrevious<cr>")
nmap("<leader>x", "<cmd>BufferClose<cr>")
2023-07-20 17:07:12 +02:00
nmap("<leader>h", "<cmd>ToggleTerm<cr>")
tmap("<leader>h", "<cmd>ToggleTerm<cr>")
2023-07-16 18:46:24 +02:00
2023-07-15 11:53:42 +02:00
nmap("<leader>r", "<cmd>!%:p<cr>")
2023-07-13 21:40:11 +02:00
nmap("<leader>ff", require("telescope.builtin").find_files)
nmap("<leader>fw", require("telescope.builtin").live_grep)
2023-07-13 21:05:53 +02:00
2023-07-14 18:56:28 +02:00
nmap("<leader>e", vim.diagnostic.open_float)
2023-07-14 16:34:51 +02:00
2023-07-15 17:27:18 +02:00
-- END GLOBAL CONFIG
2023-09-12 19:32:08 +02:00
-- START LSP
2023-08-21 13:11:34 +02:00
2023-09-12 19:32:08 +02:00
local highlight_filetypes = { "*.ts", "*.tsx", "*.js", "*.jsx", "*.html", "*.lua" }
local prettier_filetypes = { "%.ts$", "%.tsx$", "%.js$", "%.jsx$", "%.html$", "%.json$", "%.css$" }
2023-08-21 13:11:34 +02:00
2023-09-12 19:32:08 +02:00
local function matches_filetypes(file_path, filetypes)
for _, pattern in ipairs(filetypes) do
if file_path:match(pattern) then
return true
end
end
return false
2023-08-21 13:11:34 +02:00
end
2023-09-12 19:32:08 +02:00
local function str_to_table(str)
local t = {}
for line in string.gmatch(str, "([^\n]+)") do
table.insert(t, line)
end
return t
end
local function prettier_format(file_path, content)
local prettier_command = string.format("prettier --stdin-filepath '%s' <<< '%s'", file_path, content)
local output = io.popen(prettier_command, "r")
local result = output:read("*a")
output:close()
return result
end
2023-07-13 21:05:53 +02:00
2023-07-13 21:40:11 +02:00
local cmp = require("cmp")
local lspconfig = require("lspconfig")
2023-07-15 11:53:42 +02:00
local servers = {
2023-07-15 17:27:18 +02:00
tsserver = {
settings = {
completions = {
completeFunctionCalls = true,
},
},
2023-07-15 17:27:18 +02:00
},
2023-07-15 11:53:42 +02:00
pylsp = {},
lua_ls = {},
rnix = {},
gopls = {},
tailwindcss = {},
2023-07-31 15:40:45 +02:00
prismals = {},
2023-09-10 11:30:47 +02:00
hls = {},
bashls = {},
2023-07-15 11:53:42 +02:00
}
2023-07-13 21:40:11 +02:00
local cmp_capabilities = require("cmp_nvim_lsp").default_capabilities()
2023-07-15 17:27:18 +02:00
local on_attach = function(client, bufnr)
2023-09-12 19:32:08 +02:00
local format_buffer = function()
local file_path = vim.api.nvim_buf_get_name(bufnr)
if (matches_filetypes(file_path, prettier_filetypes)) then
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
local content = table.concat(lines, "\n")
local result = prettier_format(file_path, content)
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, str_to_table(result))
else
vim.lsp.buf.format()
end
end
2023-07-14 18:56:28 +02:00
nmap("<leader>ca", vim.lsp.buf.code_action)
nmap("<leader>lr", vim.lsp.buf.rename)
nmap("gd", vim.lsp.buf.definition)
2023-08-27 13:15:33 +02:00
nmap("<leader>lf", function()
2023-09-12 19:32:08 +02:00
format_buffer()
2023-07-15 17:27:18 +02:00
end)
2023-07-13 21:40:11 +02:00
nmap("K", vim.lsp.buf.hover)
nmap("gr", require("telescope.builtin").lsp_references)
2023-08-20 17:49:32 +02:00
vim.api.nvim_create_autocmd("CursorHold", {
2023-08-21 13:11:34 +02:00
callback = function()
vim.lsp.buf.document_highlight()
end,
2023-09-12 19:32:08 +02:00
pattern = highlight_filetypes
2023-08-20 17:49:32 +02:00
})
vim.api.nvim_create_autocmd("CursorMoved", {
2023-08-21 13:11:34 +02:00
callback = function()
vim.lsp.buf.clear_references()
end,
2023-09-12 19:32:08 +02:00
pattern = highlight_filetypes
2023-08-20 17:49:32 +02:00
})
2023-07-13 21:40:11 +02:00
end
2023-07-13 21:05:53 +02:00
cmp.setup({
2023-07-13 21:40:11 +02:00
snippet = {
expand = function(args)
require("luasnip").lsp_expand(args.body)
end,
},
mapping = cmp.mapping.preset.insert({
["<C-Space>"] = cmp.mapping.complete(),
["<CR>"] = cmp.mapping.confirm({ select = true }),
}),
sources = cmp.config.sources({ { name = "nvim_lsp" }, { name = "luasnip" } }, { { name = "buffer" } }),
2023-07-13 21:05:53 +02:00
})
2023-07-15 11:53:42 +02:00
for server, cfg in pairs(servers) do
2023-07-13 21:40:11 +02:00
lspconfig[server].setup({
2023-07-15 19:25:16 +02:00
settings = cfg.settings,
2023-07-13 21:40:11 +02:00
capabilities = cmp_capabilities,
on_attach = on_attach,
})
2023-07-13 21:05:53 +02:00
end
-- END LSP
2023-07-31 07:47:54 +02:00
require 'nvim-treesitter.configs'.setup {
autotag = {
enable = true,
}
}
2023-07-16 20:27:04 +02:00
require("Comment").setup()
2023-07-16 08:19:05 +02:00
require("toggleterm").setup()
2023-07-13 21:05:53 +02:00
require("autoclose").setup()
require("gitsigns").setup()
2023-07-31 07:54:05 +02:00
require("nvim-surround").setup()
2023-07-13 19:36:13 +02:00
2023-07-14 06:41:00 +02:00
require("nightfox").setup({ options = { transparent = true } })
2023-07-14 18:56:28 +02:00
vim.cmd.colorscheme("carbonfox")