summaryrefslogtreecommitdiff
path: root/dot-config/nvim/init.lua
blob: 3f7a2be04a7b1b9f218b6081e003f9813654f9ce (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
-- Settings
local set = vim.o

set.et = true
set.sw = 4
set.sts = -1
set.ts = 4
set.nu = true
set.signcolumn = "yes"
set.updatetime = 1000
set.ignorecase = true
set.smartcase = true
set.backup = false
set.undodir = vim.fn.stdpath("data") .. "/undodir"
set.undofile = true
set.splitright = true
set.splitbelow = true
set.breakindent = true
set.termguicolors = true
set.linebreak = true

vim.g.omni_sql_no_default_maps = 1337
-- vim.g.asmsyntax = "nasm"

vim.api.nvim_create_autocmd("TermOpen", {
    group = vim.api.nvim_create_augroup("TermNoNumbers", {}),
    command = "setlocal nonu nornu signcolumn=no"
})

vim.api.nvim_create_autocmd("TextYankPost", {
    group = vim.api.nvim_create_augroup("HighlightYank", {}),
    callback = function() vim.highlight.on_yank() end,
})

vim.filetype.add({
    filename = {
        ["compose.yaml"] = "yaml.docker-compose",
        ["docker-compose.yaml"] = "yaml.docker-compose",
    },
    extension = {
        templ = "templ",
        typ = "typst",
    }
})

vim.diagnostic.config({
    virtual_text = true,
    virtual_lines = { current_line = true },
})

-- Keymaps
for _, mod in ipairs({
    function(x) return x end,
    function(x) return "<c-w>" .. x end,
    function(x) return "<c-w><c-" .. x .. ">" end,
    function(x) return string.upper(x) end,
}) do
    vim.keymap.set("", mod("n"), mod("j"))
    vim.keymap.set("", mod("e"), mod("k"))
    vim.keymap.set("", mod("k"), mod("n"))
    vim.keymap.set("", mod("j"), mod("e"))
end

vim.g.mapleader = " "
vim.keymap.set("n", "gp", vim.cmd.bp)
vim.keymap.set("n", "gn", vim.cmd.bn)
vim.keymap.set("n", "<leader>h", function() set.hls = not set.hls end)
vim.keymap.set("t", "<c-l>", "<c-\\><c-n>")
vim.keymap.set("n", "<space>", "<nop>", { silent = true })

local function lsp_maps(buf)
    local telescope_builtin = require("telescope.builtin")
    local opts = { buffer = buf }

    vim.keymap.set("n", "gd", telescope_builtin.lsp_definitions, opts)
    vim.keymap.set("n", "gr", telescope_builtin.lsp_references, opts)
    vim.keymap.set("n", "gi", telescope_builtin.lsp_implementations, opts)
    vim.keymap.set("n", "gy", telescope_builtin.lsp_type_definitions, opts)
    vim.keymap.set("n", "<leader>d", telescope_builtin.diagnostics, opts)
    vim.keymap.set("n", "grn", vim.lsp.buf.rename, opts)
    vim.keymap.set("n", "[d", vim.diagnostic.goto_prev, opts)
    vim.keymap.set("n", "]d", vim.diagnostic.goto_next, opts)
    vim.keymap.set("n", "<leader>k", vim.diagnostic.open_float, opts)
    vim.keymap.set("n", "<leader>K", vim.diagnostic.setqflist, opts)
    vim.keymap.set("n", "<leader>e", vim.lsp.buf.hover, opts)
    vim.keymap.set("i", "<c-k>", vim.lsp.buf.signature_help, opts)
    vim.keymap.set("n", "<leader>.", vim.lsp.buf.code_action, opts)
    vim.keymap.set("n", "<leader>i", vim.lsp.buf.format, opts)
end

-- LSP
vim.api.nvim_create_autocmd("LspAttach", {
    group = vim.api.nvim_create_augroup("UserLspConfig", {}),
    callback = function(ev)
        local client = vim.lsp.get_client_by_id(ev.data.client_id)
        lsp_maps(ev.buf)

        if client.server_capabilities.documentHighlightProvider then
            local bg = require("catppuccin.palettes").get_palette().surface0
            vim.api.nvim_set_hl(0, "LspReferenceText", { bg = bg })
            vim.api.nvim_set_hl(0, "LspReferenceRead", { bg = bg })
            vim.api.nvim_set_hl(0, "LspReferenceWrite", { bg = bg })

            local group = vim.api.nvim_create_augroup("CursorHoldHighlightReferences", {})
            vim.api.nvim_create_autocmd("CursorHold", {
                group = group,
                buffer = ev.buf,
                callback = vim.lsp.buf.document_highlight,
            })
            vim.api.nvim_create_autocmd("CursorMoved", {
                group = group,
                buffer = ev.buf,
                callback = vim.lsp.buf.clear_references,
            })
        end
    end
})

local function setup_lsp()
    local lsp = require "lspconfig"
    local capabilities = require "cmp_nvim_lsp".default_capabilities()
    local function mkCap(obj)
        if not obj then
            obj = {}
        end
        if not obj.capabilities then
            obj.capabilities = capabilities
        end
        return obj
    end
    lsp.gopls.setup {
        capabilities = capabilities,
        settings = { gopls = {
            usePlaceholders = true,
            semanticTokens = true,
        } },
    }
    lsp.rust_analyzer.setup {
        capabilities = capabilities,
        settings = { ["rust-analyzer"] = {
            cargo = { allFeatures = true },
            check = { command = "clippy" },
        } },
    }
    lsp.hls.setup {
        capabilities = capabilities,
        settings = { haskell = {
            formattingProvider = "fourmolu",
        } },
    }
    lsp.lua_ls.setup {
        capabilities = capabilities,
        settings = { Lua = {
            diagnostics = { globals = {
                "vim",
            } },
            telemetry = {
                enable = false,
            },
        } }
    }
    lsp.templ.setup(mkCap())
    lsp.emmet_ls.setup(mkCap())
    lsp.ts_ls.setup(mkCap())
    lsp.dockerls.setup(mkCap())
    lsp.dockerls.setup(mkCap())
    lsp.docker_compose_language_service.setup(mkCap())
    lsp.terraformls.setup(mkCap())
    lsp.nixd.setup {
        capabilities = capabilities,
        settings = { nixd = {
            formatting = { command = { "nixfmt" } },
        } },
    }
    lsp.astro.setup(mkCap())
    lsp.elixirls.setup { capabilities = capabilities, cmd = { "elixir-ls" } }
    lsp.csharp_ls.setup(mkCap())
    lsp.zls.setup(mkCap())
    lsp.denols.setup(mkCap())
end

-- Plugins
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not (vim.uv or vim.loop).fs_stat(lazypath) then
    local lazyrepo = "https://github.com/folke/lazy.nvim.git"
    local out = vim.fn.system({
        "git", "clone", "--filter=blob:none", "--branch=stable", lazyrepo, lazypath,
    })
    if vim.v.shell_error ~= 0 then
        vim.api.nvim_echo({
            { "Failid to clone lazy.nvim:zn", "ErrorMsg" },
            { out, "WarningMsg" },
            { "\nPress the any key to exit..." },
        }, true, {})
        vim.fn.getchar()
        os.exit(1)
    end
end
vim.opt.rtp:prepend(lazypath)

require("lazy").setup({
    { "folke/lazy.nvim", tag = "stable" },
    {
        "catppuccin/nvim",
        config = function()
            require "catppuccin".setup {
                background = {
                    light = "latte",
                    dark = "mocha",
                },
            }
            vim.cmd.colorscheme("catppuccin")
        end,
        priority = 1000,
    },
    {
        "stevearc/oil.nvim",
        dependencies = { "nvim-tree/nvim-web-devicons" },
        config = true,
        lazy = false,
        keys = { { "-", vim.cmd.Oil } },
    },
    {
        "nvim-treesitter/nvim-treesitter",
        config = function()
            require("nvim-treesitter.configs").setup {
                ensure_installed = {
                    "vimdoc",
                    "rust", "go", "c", "zig",
                    "lua", "python", "haskell",
                    "fish", "bash",
                    "typescript", "svelte", "javascript", "css", "astro", "tsx", "prisma",
                    "nix",
                    "markdown", "typst",
                    "terraform", "hcl",
                    "sql",
                    "templ",
                    "html", "php",
                    "elixir", "heex",
                },
                indent = { enable = true },
                highlight = { enable = true },
            }
        end,
    },
    {
        "nvim-telescope/telescope.nvim",
        dependencies = { "nvim-lua/plenary.nvim" },
        keys = {
            { "<leader>f",       function() require "telescope.builtin".find_files() end },
            { "<leader>/",       function() require "telescope.builtin".live_grep() end },
            { "<leader>l",       function() require "telescope.builtin".resume() end },
            { "<leader><space>", function() require "telescope.builtin".buffers() end },
        },
        build = ":TSUpdate",
    },
    {
        "neovim/nvim-lspconfig",
        dependencies = { "hrsh7th/cmp-nvim-lsp" },
        config = setup_lsp,
    },
    {
        "hrsh7th/nvim-cmp",
        dependencies = {
            "hrsh7th/cmp-nvim-lsp",
            "saadparwaiz1/cmp_luasnip",
            "L3MON4D3/LuaSnip",
            "hrsh7th/cmp-omni",
        },
        config = function()
            local cmp = require "cmp"
            local luasnip = require "luasnip"
            cmp.setup {
                completion = { autocomplete = false },
                sources = cmp.config.sources {
                    { name = "nvim_lsp" },
                    { name = "luasnip" },
                    { name = "omni" },
                },
                snippet = {
                    expand = function(args) luasnip.lsp_expand(args.body) end,
                },
                mapping = {
                    ["<c-l>"] = cmp.mapping.confirm({ select = true }),
                    ["<c-e>"] = cmp.mapping.abort(),
                    ["<c-b>"] = cmp.mapping.scroll_docs(-4),
                    ["<c-f>"] = cmp.mapping.scroll_docs(4),
                    ["<c-n>"] = function(fallback)
                        if cmp.visible() then
                            cmp.select_next_item()
                        else
                            fallback()
                        end
                    end,
                    ["<c-p>"] = function(fallback)
                        if cmp.visible() then
                            cmp.select_prev_item()
                        else
                            fallback()
                        end
                    end,
                },
            }
        end,
        -- NOTE: If this is lazy, the `mapping`s from above don't
        -- work when completing for the first time
        lazy = false,
        keys = {
            { mode = "i",          "<c-l>", function() require "cmp".complete() end },
            { mode = { "i", "s" }, "<c-h>", function() require "luasnip".jump(-1) end },
            { mode = { "i", "s" }, "<c-e>", function() require "luasnip".jump(1) end },
        },
    },
    { "wsdjeg/vim-fetch" },
    {
        "ggandor/leap.nvim",
        keys = {
            { "s", function() require "leap".leap {} end },
            { "S", function() require "leap".leap { backward = true } end },
        },
    },
    { "numToStr/Comment.nvim",    config = true },
    { "willothy/flatten.nvim",    config = true, lazy = false },
    { "folke/todo-comments.nvim", config = true },
    {
        "akinsho/bufferline.nvim",
        after = "cattppuccin",
        dependencies = { "nvim-tree/nvim-web-devicons" },
        config = function()
            require("bufferline").setup {
                options = { diagnostics = "nvim_lsp" },
                highlights = require("catppuccin.groups.integrations.bufferline").get(),
            }
        end,
        lazy = false,
        keys = {
            { "<leader>a", function() require "bufferline".go_to(1) end },
            { "<leader>r", function() require "bufferline".go_to(2) end },
            { "<leader>s", function() require "bufferline".go_to(3) end },
            { "<leader>t", function() require "bufferline".go_to(4) end },
        },
    },
    { "kylechui/nvim-surround", event = "VeryLazy", config = true },
    {
        "folke/snacks.nvim",
        priority = 1000,
        lazy = false,
        opts = {
            input = { enabled = true },
            bigfile = { enabled = true },
            notifier = { enabled = true },
            indent = { enabled = true },
            terminal = {
                win = {
                    keys = {
                        gf = function()
                            local cfile = vim.fn.expand("<cfile>")
                            local line = vim.fn.getline(".")
                            local match = line:match(vim.pesc(cfile) .. "(:%d+:%d+)")
                            if match == nil then
                                match = line:match(vim.pesc(cfile) .. "(:%d+)")
                            end
                            if match == nil then
                                match = ""
                            end
                            local f = vim.fn.findfile(cfile, "**")
                            -- Go to previous window because `:e fname:lineno:colnu` works thanks to wsdjeg/vim-fetch but not when in the terminal window
                            vim.cmd.wincmd("p")
                            vim.schedule(function()
                                vim.cmd.e(f .. match)
                            end)
                        end,
                    },
                },
            },
        },
        keys = {
            { "<leader>q", function() Snacks.bufdelete() end },
            { "<leader>g", function() Snacks.lazygit() end },
            { "<leader>b", function()
                local t, created = Snacks.terminal.get()
                if created then
                    vim.schedule(function()
                        local autocmds = vim.api.nvim_get_autocmds({ event = "BufEnter", buffer = t.buf })
                        -- Snacks.notify(vim.pretty_print(autocmds))
                        vim.api.nvim_del_autocmd(
                            autocmds[1].id
                        )
                    end)
                else
                    t:show()
                    t:focus()
                    vim.cmd.startinsert()
                end
            end },
        },
    },
})

-- Other
vim.api.nvim_create_user_command("Djul", function(opts)
    local buf = vim.api.nvim_get_current_buf()
    local lines = vim.api.nvim_buf_get_lines(buf, opts.line1 - 1, opts.line2, false)
    for i, line in ipairs(lines) do
        line = string.gsub(line, "…", "...")
        line = string.gsub(line, "“", '"')
        line = string.gsub(line, "”", '"')
        line = string.gsub(line, "‘", "'")
        line = string.gsub(line, "’", "'")
        lines[i] = line
    end
    vim.api.nvim_buf_set_lines(buf, opts.line1 - 1, opts.line2, false, lines)
end, { range = "%" })

local busctl_buffer = ""
vim.system({ "busctl", "--user", "monitor", "org.freedesktop.portal.Desktop", "--json=short" }, {
    text = true,
    stdout = function(err, data)
        if err ~= nil then
            Snacks.notify(err)
            return
        end
        busctl_buffer = busctl_buffer .. data
        while true do
            local lf = busctl_buffer:find("\n")
            if lf == nil then break end
            local packet = vim.json.decode(busctl_buffer:sub(1, lf - 1))
            busctl_buffer = busctl_buffer:sub(lf + 1)

            if packet.interface ~= "org.freedesktop.portal.Settings" then goto continue end
            if packet.path ~= "/org/freedesktop/portal/desktop" then goto continue end
            if packet.member ~= "SettingChanged" then goto continue end
            if packet.payload.type ~= "ssv" then goto continue end

            if packet.payload.data[1] ~= "org.freedesktop.appearance" then goto continue end
            if packet.payload.data[2] ~= "color-scheme" then goto continue end
            if packet.payload.data[3].type ~= "u" then goto continue end
            local value = packet.payload.data[3].data
            vim.schedule(function() set.background = value == 1 and "dark" or "light" end)
            ::continue::
        end
    end,
    detach = true,
})