hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/.config/nvim/init.lua
blob: f04468233fabd47531175cd699f88aef102fd142 (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
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
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
local uv = vim.uv or vim.loop

if not uv.fs_stat(lazypath) then
    vim.fn.system({
        "git",
        "clone",
        "--filter=blob:none",
        "--branch=stable",
        "https://github.com/folke/lazy.nvim.git",
        lazypath,
    })
end
vim.opt.rtp:prepend(lazypath)

vim.g.mapleader = " "
vim.g.maplocalleader = " "

require("lazy").setup({
    "tpope/vim-sleuth",
    "lukas-reineke/indent-blankline.nvim",
    "andreasvc/vim-256noir",
    "windwp/nvim-autopairs",

    {
        "stevearc/oil.nvim",
        opts = {
            default_file_explorer = true,
            view_options = {
                show_hidden = true,
            },
        },
    },

    {
        "nvim-lualine/lualine.nvim",
        opts = {
            options = {
                globalstatus = true,
                component_separators = "",
                section_separators = "",
            },

            inactive_sections = {
                lualine_a = {},
                lualine_b = {},
                lualine_c = {},
                lualine_x = {},
                lualine_y = {},
                lualine_z = {},
            },
        },

        config = function(_, opts)
            local function fg(group, fallback)
                local ok, hl = pcall(vim.api.nvim_get_hl, 0, { name = group, link = false })
                if ok and hl.fg then
                    return string.format("#%06x", hl.fg)
                end
                return fallback
            end

            local text         = fg("Normal", "#b4b7ad")
            local accent       = fg("Statement", "#f0624b")
            local cool         = fg("Function", "#3d8e91") -- teal
            local warm         = fg("String", "#f7a21b") -- orange

            opts.sections      = {
                lualine_a = {},
                lualine_b = {},

                lualine_c = {
                    { "mode",     color = { fg = accent, bg = "NONE", gui = "bold" } },
                    { "filename", color = { fg = text } },
                },

                lualine_x = {
                    { "branch", color = { fg = cool } },
                    {
                        "diagnostics",
                        symbols = { error = "E:", warn = "W:", info = "I:", hint = "H:" },
                        -- no color override: lualine tints each severity from the theme
                    },
                },

                lualine_y = {},

                lualine_z = {
                    { "location", color = { fg = warm } },
                },
            }

            local none         = { fg = text, bg = "NONE" }
            local mirror       = { a = none, b = none, c = none }
            opts.options.theme = {
                normal   = mirror,
                insert   = mirror,
                visual   = mirror,
                replace  = mirror,
                command  = mirror,
                inactive = mirror,
            }

            require("lualine").setup(opts)

            vim.api.nvim_set_hl(0, "StatusLine", { bg = "NONE" })
            vim.api.nvim_set_hl(0, "StatusLineNC", { bg = "NONE" })
        end,
    },
    -- Telescope
    {
        "nvim-telescope/telescope.nvim",
        dependencies = { "nvim-lua/plenary.nvim" },

        config = function()
            require("telescope").setup({
                pickers = {
                    find_files = {
                        file_ignore_patterns = {
                            "^build/",
                            "^builddir/",
                            "^_build/",

                            "^ext/",
                            "^vendor/",
                        },
                    },
                },
            })
        end,
    },

    {
        "nvim-treesitter/nvim-treesitter",
        branch = "master",
        lazy = false,
        build = ":TSUpdate",
        config = function()
            require("nvim-treesitter.configs").setup({
                highlight = { enable = true },
                indent = { enable = false },
                ensure_installed = {
                    "lua", "vim", "c", "cpp", "markdown",
                    "python", "rust", "bash",
                },
            })
        end,
    },


    {
        "folke/snacks.nvim",
        priority = 1000,
        lazy = false,

        opts = {
            indent = {
                enabled = true,

                indent = {
                    enabled = false,
                },

                scope = {
                    enabled = false,
                },

                chunk = {
                    enabled = true,
                    hl = "Comment",

                    char = {
                        corner_top = "┌",
                        corner_bottom = "└",
                        horizontal = "─",
                        vertical = "│",
                        arrow = "→",
                    },
                },
            },
        },
    },

    -- Completion
    {
        "hrsh7th/nvim-cmp",
        dependencies = {
            "hrsh7th/cmp-nvim-lsp",
            "saadparwaiz1/cmp_luasnip",
            "L3MON4D3/LuaSnip",
            "rafamadriz/friendly-snippets",
        },

        config = function()
            local cmp = require("cmp")
            local luasnip = require("luasnip")

            require("luasnip.loaders.from_vscode").lazy_load()

            cmp.setup({
                -- No automatic completion popup.
                completion = {
                    autocomplete = false,
                },

                snippet = {
                    expand = function(args)
                        luasnip.lsp_expand(args.body)
                    end,
                },

                mapping = cmp.mapping.preset.insert({
                    -- Accept selected completion.
                    ["<CR>"] = cmp.mapping.confirm({
                        select = true,
                    }),

                    -- Manually open completion.
                    ["<M-Space>"] = cmp.mapping.complete(),

                    -- Navigate completion menu.
                    ["<C-n>"] = cmp.mapping.select_next_item(),
                    ["<C-p>"] = cmp.mapping.select_prev_item(),

                    -- Close completion menu.
                    ["<C-e>"] = cmp.mapping.abort(),
                }),

                sources = {
                    { name = "nvim_lsp" },
                    { name = "luasnip" },
                },
            })
        end,
    },
    -- LSP
    {
        "neovim/nvim-lspconfig",
        dependencies = {
            "williamboman/mason.nvim",
            "williamboman/mason-lspconfig.nvim",
            "WhoIsSethDaniel/mason-tool-installer.nvim",
            "hrsh7th/cmp-nvim-lsp",
            "j-hui/fidget.nvim",
        },
        config = function()
            require("mason").setup()
            require("fidget").setup()

            -- Per-server settings. Mason installs these; Neovim's native LSP
            -- (vim.lsp.config / vim.lsp.enable) configures and starts them.
            local servers = {
                lua_ls = {
                    settings = {
                        Lua = {
                            diagnostics = { globals = { "vim" } },
                            completion = { callSnippet = "Replace" },
                        },
                    },
                },
                rust_analyzer = {
                    settings = {
                        ["rust-analyzer"] = {
                            cargo = { buildScripts = { enable = true } },
                            procMacro = { enable = true },
                        },
                    },
                },
            }

            -- Advertise nvim-cmp completion capabilities to every server.
            vim.lsp.config("*", {
                capabilities = require("cmp_nvim_lsp").default_capabilities(),
            })

            -- Merge each server's settings on top of nvim-lspconfig's defaults.
            for name, cfg in pairs(servers) do
                vim.lsp.config(name, cfg)
            end

            -- Install the servers (mason-tool-installer), then auto-enable the
            -- installed ones via vim.lsp.enable (mason-lspconfig v2).
            require("mason-tool-installer").setup({
                ensure_installed = vim.tbl_keys(servers),
            })
            require("mason-lspconfig").setup({
                automatic_enable = true,
            })
        end,
    },

    -- Formatting
    {
        "stevearc/conform.nvim",
        opts = {
            notify_on_error = false,
            formatters_by_ft = {
                lua = { "stylua" },
                python = { "black" },
                -- rustfmt reads the edition from Cargo.toml (defaults to 2021)
                -- and respects rustfmt.toml, so no extra args are needed.
                rust = { "rustfmt" },
            },
        },
    },

    -- Git signs ASCII
    {
        "lewis6991/gitsigns.nvim",
        config = function()
            require("gitsigns").setup({
                signs = {
                    add = { text = "+" },
                    change = { text = "~" },
                    delete = { text = "-" },
                    topdelete = { text = "-" },
                    changedelete = { text = "~" },
                },
            })
        end,
    },

    -- Which-key
    {
        "folke/which-key.nvim",
        config = function()
            require("which-key").setup()
        end,
    },
})

-- ASCII diagnostics
vim.fn.sign_define("DiagnosticSignError", { text = "E", texthl = "DiagnosticError" })
vim.fn.sign_define("DiagnosticSignWarn", { text = "W", texthl = "DiagnosticWarn" })
vim.fn.sign_define("DiagnosticSignInfo", { text = "I", texthl = "DiagnosticInfo" })
vim.fn.sign_define("DiagnosticSignHint", { text = "H", texthl = "DiagnosticHint" })

-- Options
local opt = vim.opt
opt.number = true
opt.mouse = "a"
opt.splitright = true
opt.splitbelow = true
opt.autoindent = true
opt.smartindent = true
opt.expandtab = true
opt.shiftwidth = 4
opt.tabstop = 4
opt.ignorecase = true
opt.smartcase = true
opt.termguicolors = true
opt.cursorline = false
opt.linebreak = true
opt.scrolloff = 10
opt.clipboard = "unnamedplus"
opt.completeopt = { "menu", "menuone", "noinsert", "noselect" }

-- Typo forgiveness
vim.cmd([[
    cnoreabbrev W   w
    cnoreabbrev Q   q
    cnoreabbrev WQ  wq
    cnoreabbrev Wq  wq
    cnoreabbrev wQ  wq
    cnoreabbrev WQA wqa
    cnoreabbrev WQa wqa
    cnoreabbrev Wqa wqa
]])

-- Keymaps
local map = vim.keymap.set
map("n", "<Esc>", ":nohl<CR>")
map("n", "-", require("oil").open, { desc = "Open parent directory" })
map("n", "<C-e>", function()
    require("oil").open(vim.fn.getcwd())
end, { desc = "Browse working directory" })

local telescope = require("telescope.builtin")
map("n", "<leader>ff", telescope.find_files, { desc = "Find files" })
map("n", "<leader>fg", telescope.live_grep, { desc = "Find text" })
map("n", "<leader>fb", telescope.buffers, { desc = "Find buffers" })

map("n", "<leader>cf", function()
    require("conform").format({ async = true, lsp_format = "fallback" })
end, { desc = "Format buffer" })

vim.diagnostic.config({
    virtual_text = true,
    signs = true,
    underline = true,
    severity_sort = true,
})

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

vim.cmd.colorscheme("256_noir")

vim.cmd([[
    highlight Normal guibg=NONE ctermbg=NONE
    highlight NormalNC guibg=NONE ctermbg=NONE
    highlight SignColumn guibg=NONE ctermbg=NONE
    highlight EndOfBuffer guibg=NONE ctermbg=NONE
    highlight LineNr guibg=NONE ctermbg=NONE
    highlight CursorLineNr guibg=NONE ctermbg=NONE
    highlight CursorLine guibg=NONE ctermbg=NONE
    highlight StatusLine guibg=NONE ctermbg=NONE
    highlight StatusLineNC guibg=NONE ctermbg=NONE
]])

vim.api.nvim_set_hl(0, "Visual", { bg = "#222222" })
vim.api.nvim_set_hl(0, "Search", { bg = "#222222" })
vim.api.nvim_set_hl(0, "IncSearch", { bg = "#222222" })
vim.api.nvim_set_hl(0, "PMenu", { bg = "#222222" })
vim.api.nvim_set_hl(0, "PMenySelf", { bg = "#333333", bold=true })