aboutsummaryrefslogtreecommitdiffstats
path: root/.config/nvim/init.lua
blob: cebdaf707b7d568eb0f0e6e7e8c235e2c00feaa7 (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
-- plugins
vim.cmd [[
	call plug#begin()
	" Plug 'github/copilot.vim'
	Plug 'milanglacier/minuet-ai.nvim'
	Plug 'hrsh7th/cmp-buffer'
	Plug 'hrsh7th/cmp-nvim-lsp'
	Plug 'hrsh7th/cmp-path'
	Plug 'hrsh7th/nvim-cmp'
	Plug 'https://github.com/adelarsq/vim-matchit'
	Plug 'https://github.com/preservim/nerdtree', { 'on': 'NERDTreeToggle' }
	Plug 'j-hui/fidget.nvim'
	Plug 'junegunn/fzf'
	Plug 'neovim/nvim-lspconfig'
	Plug 'nvim-lua/plenary.nvim'
	Plug 'nvim-telescope/telescope.nvim', { 'branch': '0.1.x' }
	Plug 'sbdchd/neoformat'
	Plug 'tmsvg/pear-tree'
	Plug 'tpope/vim-surround'
	Plug 'williamboman/mason-lspconfig.nvim'
	Plug 'williamboman/mason.nvim'
	Plug 'ThePrimeagen/vim-be-good'
	Plug 'WhoIsSethDaniel/mason-tool-installer.nvim'
	call plug#end()
]]

vim.g.NERDTreeShowHidden = 1
vim.g.pear_tree_ft_disabled = { "TelescopePrompt", "TelescopeResults" }

-- options
local opts = {
    autochdir = true,
    autoindent = true,
    autoread = true,
    backup = false,
    cursorline = true,
    expandtab = true,
    hlsearch = true,
    ignorecase = true,
    linebreak = true,
    listchars = { tab = ">-", trail = "~", extends = ">", precedes = "<", space = "." },
    mouse = "a",
    number = true,
    relativenumber = true,
    shiftwidth = 4,
    showmatch = true,
    signcolumn = "yes",
    smartcase = true,
    softtabstop = 4,
    splitbelow = true,
    splitright = true,
    tabstop = 4,
    termguicolors = true,
    ttimeout = true,
    ttimeoutlen = 1,
    ttyfast = true,
    undofile = true,
    updatetime = 300,
    writebackup = false,
}

for k, v in pairs(opts) do
    vim.opt[k] = v
end

-- spell and cursor shape
vim.opt.spelllang = { "en_us", "tl" }
vim.g.t_SI = "\27[6 q"
vim.g.t_EI = "\27[2 q"

-- terminal tweaks
if vim.env.TERM == "alacritty" then
    vim.opt.ttymouse = "sgr"
end

-- custom functions for keymaps
local term_buf = nil
local toggle_terminal
toggle_terminal = function()
    if term_buf and vim.fn.bufexists(term_buf) == 1 then
        local win = vim.fn.bufwinnr(term_buf)
        if win ~= -1 then
            vim.cmd(win .. "close")
            return
        end
        vim.cmd("belowright split")
        vim.cmd("resize " .. math.floor(vim.o.lines / 3))
        vim.cmd("buffer " .. term_buf)
        vim.cmd("startinsert")
        return
    end
    vim.cmd("belowright split")
    vim.cmd("resize " .. math.floor(vim.o.lines / 3))
    vim.cmd("terminal")
    vim.cmd("startinsert")
    term_buf = vim.api.nvim_get_current_buf()
end

-- custom keymaps
local keymap = vim.keymap.set

keymap("v", "<C-c>", '"+y', { noremap = true })
keymap("v", "<C-x>", '"+x', { noremap = true })
keymap({ "n", "v" }, "<C-p>", '"+p', { noremap = true })
keymap({ "n", "v" }, "<leader>p", '"+P', { noremap = true })
keymap("n", "<C-d>", "<C-d>zz", { noremap = true })
keymap("n", "<C-u>", "<C-u>zz", { noremap = true })
keymap("n", "n", "nzzzv", { noremap = true })
keymap("n", "N", "Nzzzv", { noremap = true })

keymap("n", "<S-A-b>h", ":NERDTreeToggle<CR>", { noremap = true })
keymap("n", "<S-A-b><S-A-h>", ":NERDTreeToggle<CR>", { noremap = true })
keymap("n", "<A-n>", ":NERDTreeToggle<CR>", { noremap = true })
keymap("n", "<S-A-n>", ":NERDTree<CR>", { noremap = true })
keymap("n", "<S-A-j>", "<C-W>w", { noremap = true })
keymap("n", "<S-A-k>", "<C-W>W", { noremap = true })
keymap({ "n", "t" }, "<S-A-b>j", function() toggle_terminal() end, { noremap = true })
keymap({ "n", "t" }, "<S-A-b><S-A-j>", function() toggle_terminal() end, { noremap = true })
keymap("n", "<leader>sc", ":set spell!<CR>", { noremap = true, silent = true })

keymap("n", "<A-j>", ":bnext<CR>", { noremap = true, silent = true })
keymap("n", "<A-k>", ":bprev<CR>", { noremap = true, silent = true })

keymap("n", "<A-q>", "ZQ", { noremap = true })
keymap("n", "<A-z>", "ZZ", { noremap = true })

keymap("n", "<A-,>", "<C-W>5<", { noremap = true })
keymap("n", "<A-.>", "<C-W>5>", { noremap = true })
keymap("n", "<A-->", "<C-W>5-", { noremap = true })
keymap("n", "<A-=>", "<C-W>5+", { noremap = true })

keymap("n", "<A-s>", ":vsplit<CR>", { noremap = true })
keymap("n", "<A-d>", ":split<CR>", { noremap = true })

keymap("n", "<A-t>", ":tabnew<CR>", { noremap = true })
keymap("n", "<A-[>", ":tabprev<CR>", { noremap = true })
keymap("n", "<A-]>", ":tabnext<CR>", { noremap = true })
keymap("n", "<A-;>", ":tabmove -<CR>", { noremap = true })
keymap("n", "<A-'>", ":tabmove +<CR>", { noremap = true })
keymap("n", "<leader>ft", ":set filetype=", { noremap = true })

-- copilot (disabled, using minuet-ai.nvim instead)
-- keymap("i", "<A-a>", 'copilot#Accept("<CR>")', { expr = true, silent = true, replace_keycodes = false })

-- ui and colors
vim.cmd("colorscheme kijish")

if vim.fn.has("gui_running") == 1 then
    vim.opt.t_Co = 256
    vim.opt.guifont = "Roboto Mono 11"
    vim.opt.guioptions:remove("m")
    vim.opt.guioptions:remove("T")
    vim.opt.guioptions:remove("r")
    vim.opt.guioptions:remove("L")
    vim.cmd("colorscheme tender")
end

-- autocmds
vim.api.nvim_create_autocmd("BufWritePre", {
    pattern = { "*.py", "*.f90", "*.f95", "*.for" },
    command = [[%s/\s\+$//e]],
})

local autoread_group = vim.api.nvim_create_augroup("autoread", { clear = true })
vim.api.nvim_create_autocmd({ "FocusGained", "BufEnter", "CursorHold", "CursorHoldI" }, {
    group = autoread_group,
    command = "if mode() !~ '\''[cCeEsS]'\'' | checktime | endif",
})

vim.cmd("filetype plugin on")
vim.cmd("filetype indent on")
vim.cmd("syntax on")
vim.cmd("highlight Cursorline cterm=bold ctermbg=black")
vim.cmd("highlight LineNr guifg=darkgray ctermfg=darkgray")
vim.cmd("highlight CursorLineNr guifg=cyan ctermfg=cyan")

vim.opt.fillchars = vim.opt.fillchars + {
    vert = "█",
    fold = "█",
    diff = "█",
    stl = "-",
    stlnc = "-",
}

vim.cmd([[
	hi VertSplit guifg=#1c1c1c
	hi User1 guifg=#999999 guibg=#1c1c1c
	hi User2 guifg=#72d5a3 guibg=#1c1c1c
	hi User3 guifg=#ec93d3 guibg=#1c1c1c
	hi User4 guifg=#ffffff guibg=#1c1c1c
	hi User5 guifg=#777777 guibg=#1c1c1c
]])

-- statusline
vim.o.statusline = table.concat({
    "%1* %n %*",
    "%3* %y %*",
    "%4* %<%f %*",
    "%2* %m %*",
    "%1* %= %5l %*",
    "%2* /%L %*",
    "%1* %4v %*",
    "%2* 0x%04B %*",
    "%5* %{&ff} %*",
})

-- telescope
local tele_ok, builtin = pcall(require, "telescope.builtin")
if tele_ok then
    keymap("n", "<leader>fg", function()
        builtin.live_grep({
            find_command = { "rg", "--ignore", "--hidden", "--files" },
            prompt_prefix = " search:  ",
        })
    end, {})

    keymap("n", "<leader>ff", function()
        builtin.find_files({
            find_command = { "rg", "--ignore", "--hidden", "--files" },
            prompt_prefix = " search:  ",
        })
    end, {})

    keymap("n", "<leader>fb", function()
        builtin.buffers({
            prompt_prefix = " search:  ",
        })
    end, {})

    keymap("n", "<leader>fh", function()
        builtin.help_tags({
            prompt_prefix = " search:  ",
        })
    end, {})
end

-- lsp
vim.diagnostic.config({
    update_in_insert = false,
    severity_sort = true,
    float = { border = "rounded", source = "if_many" },
    underline = { severity = { min = vim.diagnostic.severity.WARN } },
    virtual_text = true,
})

keymap("n", "[d", function() vim.diagnostic.jump({ count = -1 }) end, { desc = "diagnostic prev" })
keymap("n", "]d", function() vim.diagnostic.jump({ count = 1 }) end, { desc = "diagnostic next" })
keymap("n", "<leader>e", vim.diagnostic.open_float, { desc = "diagnostic float" })
keymap("n", "<C-k><C-i>", vim.diagnostic.open_float, { desc = "diagnostic float" })
keymap("n", "<leader>q", vim.diagnostic.setloclist, { desc = "diagnostic list" })
keymap("n", "<S-A-m>", vim.diagnostic.setloclist, { desc = "diagnostic list" })


vim.api.nvim_create_autocmd("LspAttach", {
    group = vim.api.nvim_create_augroup("user-lsp-attach", { clear = true }),
    callback = function(event)
        local map = function(keys, func, desc, mode)
            mode = mode or "n"
            vim.keymap.set(mode, keys, func, { buffer = event.buf, desc = "lsp: " .. desc })
        end

        map("grn", vim.lsp.buf.rename, "rename")
        map("gra", vim.lsp.buf.code_action, "code action", { "n", "x" })
        map("grD", vim.lsp.buf.declaration, "declaration")
        map("gri", vim.lsp.buf.implementation, "implementation")
        map("grr", vim.lsp.buf.references, "references")
        map("grd", vim.lsp.buf.definition, "definition")
        map("grt", vim.lsp.buf.type_definition, "type definition")
        map("K", vim.lsp.buf.hover, "hover")
        map("<leader>F", function() vim.lsp.buf.format({ async = true }) end, "format", { "n", "x" })

        local client = vim.lsp.get_client_by_id(event.data.client_id)
        if client and client:supports_method("textDocument/documentHighlight", event.buf) then
            local highlight_group = vim.api.nvim_create_augroup("user-lsp-highlight", { clear = false })
            vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI" }, {
                buffer = event.buf,
                group = highlight_group,
                callback = vim.lsp.buf.document_highlight,
            })
            vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, {
                buffer = event.buf,
                group = highlight_group,
                callback = vim.lsp.buf.clear_references,
            })
        end

        if client and client:supports_method("textDocument/inlayHint", event.buf) then
            map("<leader>th", function()
                vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled({ bufnr = event.buf }))
            end, "toggle inlay hints")
        end
    end,
})

local ok_fidget, fidget = pcall(require, "fidget")
if ok_fidget then
    fidget.setup({})
end

local ok_mason, mason = pcall(require, "mason")
local ok_mlsp, mason_lspconfig = pcall(require, "mason-lspconfig")
local ok_tools, mason_tool_installer = pcall(require, "mason-tool-installer")

-- put lsp servers here to ensure they are installed and configured
local servers = {
    bashls = {},
    clangd = {},
    dockerls = {},
    docker_compose_language_service = {},
    html = {},
    cssls = {},
    ts_ls = {},
    intelephense = {},
    pylsp = {},
    lua_ls = {
        settings = {
            Lua = {
                completion = { callSnippet = "Replace" },
                diagnostics = { globals = { "vim" } },
                workspace = {
                    checkThirdParty = false,
                    library = vim.api.nvim_get_runtime_file("", true),
                },
                telemetry = { enable = false },
            },
        },
    },
}

if ok_mason then
    mason.setup()
end

if ok_tools then
    mason_tool_installer.setup({ ensure_installed = vim.tbl_keys(servers) })
end

if ok_mlsp then
    mason_lspconfig.setup({
        ensure_installed = vim.tbl_keys(servers),
        automatic_installation = true,
    })
end

for server_name, server_config in pairs(servers) do
    vim.lsp.config(server_name, server_config)
    vim.lsp.enable(server_name)
end

-- completion
vim.opt.completeopt = { "menu", "menuone", "noinsert" }

local ok_cmp, cmp = pcall(require, "cmp")
if ok_cmp then
    cmp.setup({
        mapping = cmp.mapping.preset.insert({
            ["<Tab>"] = cmp.mapping(function(fallback)
                if cmp.visible() then
                    cmp.select_next_item()
                else
                    fallback()
                end
            end, { "i", "s" }),
            ["<S-Tab>"] = cmp.mapping(function(fallback)
                if cmp.visible() then
                    cmp.select_prev_item()
                else
                    fallback()
                end
            end, { "i", "s" }),
            ["<C-Space>"] = cmp.mapping.complete(),
            ["<C-e>"] = cmp.mapping.abort(),
            ["<CR>"] = cmp.mapping.confirm({ select = true }),
        }),
        sources = cmp.config.sources({
            { name = "nvim_lsp" },
        }, {
            { name = "buffer" },
            { name = "path" },
        }),
    })
end

-- minuet (ai completions via openrouter)
local _enc = vim.fn.expand("~/.config/minuet/key")
local _ukey = vim.fn.expand("~/.config/minuet/mojicrypt.ukey")

if vim.fn.filereadable(_enc) == 1 and vim.fn.filereadable(_ukey) == 1 then
    local ok_minuet, minuet = pcall(require, "minuet")
    if ok_minuet then
        minuet.setup({
            provider = "openai_compatible",
            virtualtext = {
                auto_trigger_ft = { "*" },
                keymap = {
                    accept = "<A-CR>",
                    accept_line = "<A-a>",
                    accept_n_lines = "<A-S-CR>",
                    next = "<A-]>",
                    prev = "<A-[>",
                    dismiss = "<A-BS>",
                },
            },
            request_timeout = 2.5,
            throttle = 1500,
            debounce = 600,
            provider_options = {
                openai_compatible = {
                    api_key = (function()
                        local cached
                        return function()
                            if not cached then
                                local result = vim.fn.system({
                                    "/home/kylert/.local/bin/mojicrypt", "decrypt",
                                    "-f", _enc,
                                    "-k", _ukey,
                                    "-o", "/dev/stdout",
                                })
                                if vim.v.shell_error == 0 then
                                    cached = result:gsub("%s+", "")
                                else
                                    cached = ""
                                end
                            end
                            return cached ~= "" and cached or nil
                        end
                    end)(),
                    end_point = "https://openrouter.ai/api/v1/chat/completions",
                    model = "gpt-oss-120b",
                    name = "Openrouter",
                    optional = {
                        max_tokens = 128,
                        top_p = 0.9,
                        provider = {
                            sort = "throughput",
                        },
                        reasoning_effort = "minimal",
                    },
                },
            },
        })
    end
end