-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
617 lines (565 loc) · 15.8 KB
/
init.lua
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
---
--- Global
---
-- Set the mapleader and maplocalleader to space
vim.g.mapleader = " "
vim.g.maplocalleader = " "
-- Enable Nerd Font support
vim.g.have_nerd_font = true
-- Set various options using vim.opt
vim.opt.number = true
vim.opt.mouse = "a"
vim.opt.showmode = false
vim.opt.clipboard = "unnamedplus"
vim.opt.breakindent = true
vim.opt.undofile = true
vim.opt.ignorecase = true
vim.opt.smartcase = true
vim.opt.cursorline = true
vim.opt.hlsearch = true
vim.opt.tabstop = 4
vim.opt.shiftwidth = 4
vim.opt.expandtab = true
vim.opt.confirm = true
local local_keymap_options = { noremap = true, silent = true }
local is_cheatsheet_open = false
function move_right_if_in_tree()
-- Check if the current buffer name contains "NvimTree"
if string.find(vim.api.nvim_buf_get_name(0), "NvimTree") then
-- If "NvimTree" is in the buffer name, move focus to the right window
vim.api.nvim_command("wincmd l")
end
end
-- Define the function to handle ESC key mapping
function esc_keymap()
-- Check if cheatsheet is open, if so close it
if is_cheatsheet_open == true then
close_cheatsheet()
return
end
-- Check if the current buffer is a fugitiveblame buffer, if so close it with :q
if string.find(vim.api.nvim_buf_get_name(0), "fugitiveblame") then
vim.api.nvim_command("q")
return
end
-- Clear the search highlight
vim.api.nvim_exec("nohlsearch", true)
end
-- Set key mappings for saving the buffer in normal mode and insert mode
vim.keymap.set("n", "<Esc>", esc_keymap, local_keymap_options)
vim.keymap.set("n", "<C-s>", ":w<CR>", { desc = "Save buffer" })
vim.keymap.set("i", "<C-s>", "<ESC>:w<CR>a", { desc = "Save buffer" })
--- ######################################################################
--- Lazy
--- ######################################################################
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
vim.fn.system({
"git",
"clone",
"--filter=blob:none",
"https://github.com/folke/lazy.nvim.git",
"--branch=stable",
lazypath,
})
end
vim.opt.rtp:prepend(lazypath)
local plugins = {
{
"nvim-telescope/telescope.nvim",
tag = "0.1.5",
dependencies = { "nvim-lua/plenary.nvim" },
},
{
"nvim-tree/nvim-tree.lua",
version = "*",
lazy = false,
dependencies = {
"nvim-tree/nvim-web-devicons",
},
},
{
"nvim-treesitter/nvim-treesitter",
build = ":TSUpdate",
},
{
"nvim-lualine/lualine.nvim",
dependencies = { "nvim-tree/nvim-web-devicons" },
},
{
"williamboman/mason.nvim",
"williamboman/mason-lspconfig.nvim",
"neovim/nvim-lspconfig",
},
{ "nvim-telescope/telescope-ui-select.nvim" },
{ "romgrk/barbar.nvim" },
{ "Shatur/neovim-ayu" },
{ "hrsh7th/nvim-cmp" },
{ "L3MON4D3/LuaSnip" },
{ "saadparwaiz1/cmp_luasnip" },
{ "rafamadriz/friendly-snippets" },
{ "hrsh7th/cmp-nvim-lsp" },
{ "hrsh7th/cmp-path" },
{
"kdheepak/lazygit.nvim",
lazy = false,
cmd = {
"LazyGit",
"LazyGitConfig",
"LazyGitCurrentFile",
"LazyGitFilter",
"LazyGitFilterCurrentFile",
},
dependencies = {
"nvim-lua/plenary.nvim",
},
},
{ "voldikss/vim-floaterm" },
{
"crnvl96/lazydocker.nvim",
event = "VeryLazy",
dependencies = {
"MunifTanjim/nui.nvim",
},
},
{
"folke/noice.nvim",
event = "VeryLazy",
dependencies = {
"MunifTanjim/nui.nvim",
"rcarriga/nvim-notify",
},
},
{
"folke/todo-comments.nvim",
dependencies = { "nvim-lua/plenary.nvim" },
},
{ "tpope/vim-fugitive" },
{
"goolord/alpha-nvim",
config = function()
require("alpha").setup(require("alpha.themes.dashboard").config)
end,
},
{ "stevearc/conform.nvim" },
{ "zapling/mason-conform.nvim" },
{
"dpayne/CodeGPT.nvim",
dependencies = {
"nvim-lua/plenary.nvim",
"MunifTanjim/nui.nvim",
},
},
{
"nvim-telescope/telescope-fzf-native.nvim",
build = "make",
},
}
local opts = {}
require("lazy").setup(plugins, opts)
---
--- ayu
---
require("ayu").setup({
overrides = {
Normal = { bg = "None" },
ColorColumn = { bg = "None" },
SignColumn = { bg = "None" },
Folded = { bg = "None" },
FoldColumn = { bg = "None" },
CursorLine = { bg = "None" },
CursorColumn = { bg = "None" },
WhichKeyFloat = { bg = "None" },
VertSplit = { bg = "None" },
Comment = {
fg = "#10FF00",
italic = true,
},
Statement = {
fg = "orange",
},
Exception = {
fg = "orange",
},
Repeat = {
fg = "orange",
},
Operator = {
fg = "purple",
},
},
})
vim.cmd("colorscheme ayu-dark")
---
--- Telescope
---
local builtin = require("telescope.builtin")
vim.keymap.set("n", "<leader>fa", builtin.find_files, {})
vim.keymap.set("n", "<leader>fw", builtin.live_grep, {})
vim.keymap.set("n", "<leader>fb", builtin.buffers, {})
vim.keymap.set("n", "<leader>fz", builtin.current_buffer_fuzzy_find, {})
vim.keymap.set("n", "<leader>fh", builtin.help_tags, {})
require("telescope").setup({
defaults = {
path_display = { truncate = 1 },
},
extensions = {
["ui-select"] = {
require("telescope.themes").get_dropdown({}),
},
},
})
require("telescope").load_extension("ui-select")
require("telescope").load_extension("fzf")
vim.keymap.set("n", "<leader>fr", ":Telescope oldfiles<CR>", {})
---
--- nvim-tree
---
require("nvim-tree").setup({
view = {
width = "30%",
},
update_focused_file = {
enable = true,
},
})
-- Define a function named toggle_nvim_tree
function toggle_nvim_tree()
-- Get the name of the current buffer
local current_buffer_name = vim.api.nvim_buf_get_name(0)
-- Check if the current buffer name contains "NvimTree"
if string.find(current_buffer_name, "NvimTree") then
-- If "NvimTree" is in the buffer name, move focus to the right window
vim.api.nvim_command("wincmd l")
else
-- If "NvimTree" is not in the buffer name, focus on the NvimTree window
vim.api.nvim_command("NvimTreeFocus")
end
end
vim.keymap.set("n", "<C-n>", ":NvimTreeToggle<CR>", local_keymap_options)
vim.keymap.set("n", "<leader>e", toggle_nvim_tree, local_keymap_options)
vim.keymap.set("n", "<leader>tk", ":NvimTreeCollapse<CR>", local_keymap_options)
---
-- Treesitter
--
local config = require("nvim-treesitter.configs")
config.setup({
ensure_installed = { "python", "lua", "bash", "c", "html", "lua", "markdown", "vim", "vimdoc", "javascript", "css" },
highlight = { enable = true },
indent = { enable = true },
})
---
--- lualine
---
require("lualine").setup({
sections = {
lualine_c = {
"lsp_progress",
},
},
})
--
-- Mason, mason-lspconfig, lspconfig
--
require("mason").setup()
require("mason-lspconfig").setup({
ensure_installed = { "lua_ls", "pyright" },
})
local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities = vim.tbl_deep_extend("force", capabilities, require("cmp_nvim_lsp").default_capabilities())
require("lspconfig").lua_ls.setup({ capabilities = capabilities })
require("lspconfig").pyright.setup({ capabilities = capabilities })
vim.api.nvim_set_keymap("n", "gd", '<cmd>lua require("telescope.builtin").lsp_definitions()<CR>', local_keymap_options)
vim.api.nvim_set_keymap("n", "gr", '<cmd>lua require("telescope.builtin").lsp_references()<CR>', local_keymap_options)
vim.api.nvim_set_keymap(
"n",
"<leader>lz",
'<cmd>lua require("telescope.builtin").lsp_document_symbols()<CR>',
local_keymap_options
)
vim.api.nvim_set_keymap(
"n",
"<leader>la",
'<cmd>lua require("telescope.builtin").lsp_dynamic_workspace_symbols()<CR>',
local_keymap_options
)
vim.keymap.set("n", "<leader>K", vim.lsp.buf.hover, {})
vim.keymap.set("n", "<leader>ra", vim.lsp.buf.rename, {})
vim.keymap.set("n", "<leader>wa", vim.lsp.buf.add_workspace_folder, {})
vim.keymap.set("n", "<leader>wr", vim.lsp.buf.remove_workspace_folder, {})
vim.keymap.set("n", "<leader>ca", vim.lsp.buf.code_action, {})
vim.keymap.set("n", "<leader>wl", function()
print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
end, {})
---
--- barbar
---
vim.g.barbar_auto_setup = false
require("barbar").setup({
animation = false,
sidebar_filetypes = {
NvimTree = true,
},
})
-- Function to confirm and handle buffer changes before closing the buffer.
local function confirm_buffer_changes()
-- Check if the current buffer has been modified
if vim.api.nvim_buf_get_option(0, "modified") then
-- Define the options for the user prompt
local options = { "&Yes", "&No", "&Cancel" }
local message = "Save file?"
-- Show a confirmation dialog to the user
local choice = vim.fn.confirm(message, table.concat(options, "\n"), 0)
-- Handle user choice
if choice == 1 then
vim.cmd("w") -- Save the buffer
elseif choice == 3 then
return -- Cancel buffer closing
end
end
vim.cmd("BufferClose!") -- Close the buffer
end
vim.keymap.set("n", "<leader>x", confirm_buffer_changes)
vim.api.nvim_set_keymap("n", "<C-Right>", ":BufferNext<CR>", local_keymap_options)
vim.api.nvim_set_keymap("n", "<C-Left>", ":BufferPrevious<CR>", local_keymap_options)
---
--- nvim-cmp
---
local cmp = require("cmp")
require("luasnip.loaders.from_vscode").lazy_load()
cmp.setup({
snippet = {
expand = function(args)
require("luasnip").lsp_expand(args.body)
end,
},
window = {
completion = cmp.config.window.bordered(),
documentation = cmp.config.window.bordered(),
},
mapping = cmp.mapping.preset.insert({
["<C-b>"] = cmp.mapping.scroll_docs(-4),
["<C-f>"] = cmp.mapping.scroll_docs(4),
["<C-Space>"] = cmp.mapping.complete(),
["<C-e>"] = cmp.mapping.abort(),
["<CR>"] = cmp.mapping.confirm({ select = true }),
}),
sources = cmp.config.sources({
{ name = "nvim_lsp" },
{ name = "path" },
}, {
{ name = "buffer" },
}),
})
---
--- lazygit
---
require("lazy").setup({})
vim.keymap.set("n", "<leader>gg", ":LazyGit<CR>", local_keymap_options)
---
--- floaterm
---
vim.keymap.set("n", "<A-i>", ":FloatermToggle<CR>", local_keymap_options)
---
--- lazydocker
---
require("lazydocker").setup({})
function open_lazydocker()
move_right_if_in_tree()
vim.cmd("LazyDocker")
end
vim.keymap.set("n", "<leader>dk", open_lazydocker, local_keymap_options)
---
--- noice, notify
---
require("notify").setup({
background_colour = "#000000",
render = "wrapped-compact",
stages = "static",
top_down = true,
})
require("noice").setup({
views = {
cmdline_popup = {
position = {
row = "40%",
col = "50%",
},
},
popupmenu = {
relative = "editor",
position = {
row = "10%",
col = "50%",
},
size = {
width = 60,
height = 10,
},
border = {
style = "rounded",
padding = { 0, 1 },
},
win_options = {
winhighlight = { Normal = "Normal", FloatBorder = "DiagnosticInfo" },
},
},
},
lsp = {
override = {
["vim.lsp.util.convert_input_to_markdown_lines"] = true,
["vim.lsp.util.stylize_markdown"] = true,
["cmp.entry.get_documentation"] = true,
},
},
presets = {
command_palette = true,
},
})
---
--- todo-comments
---
require("todo-comments").setup({
highlight = {
pattern = [[.*<(KEYWORDS)\s*]],
},
search = {
pattern = [[\b(KEYWORDS)]],
},
})
---
--- fugitive
---
-- Function to toggle Git blame view in Neovim
function toggle_git_blame()
-- Check if current buffer is already in fugitiveblame view
if string.find(vim.api.nvim_buf_get_name(0), "fugitiveblame") then
-- Close the buffer if already in fugitiveblame view
vim.api.nvim_command("q")
else
move_right_if_in_tree()
-- Open Git blame view if not already in fugitiveblame view
vim.api.nvim_command("Git blame")
end
end
vim.keymap.set("n", "<leader>gb", toggle_git_blame, local_keymap_options)
---
--- alpha.nvim
---
local dashboard = require("alpha.themes.dashboard")
dashboard.section.header.val = {
"⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀",
"⠀⠀⢀⣀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⡀⠀⠀",
"⠀⢰⣿⣿⣿⣆⢀⣶⣶⣄⠀⢀⣤⡀⠀⠀⢀⣤⡀⠀⣠⣶⣶⡀⣰⣿⣿⣿⡆⠀",
"⠀⢸⣿⣿⣿⡟⠀⣿⣿⡿⠀⢸⣿⣿⠆⠰⣿⣿⡇⠀⢿⣿⣿⠀⢻⣿⣿⣿⡇⠀",
"⠀⢸⣿⣿⣿⡇⠀⠘⠛⠀⠀⠈⠿⠋⠀⠀⠙⠿⠁⠀⠀⠛⠃⠀⢸⣿⣿⣿⡇⠀",
"⠀⠘⣿⣿⣿⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⣿⣿⣿⠃⠀",
"⠀⠀⢻⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀ ⠀ ⠀⠀⠀⠀⠀⠀⣿⣿⡟⠀⠀",
"⠀⠀⠈⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⠁⠀⠀",
"⠀⠀⠀⠸⣿⡄⠀⠀⢀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡀⠀⠀⢠⣿⠇⠀⠀⠀",
"⠀⠀⠀⠀⢻⡇⠀⢀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⡀⠀⢸⡟⠀⠀⠀⠀",
"⠀⠀⠀⠀⠀⠃⠀⢸⣿⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢰⣿⡇⠀⠘⠀⠀⠀⠀⠀",
"⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿⡄⢀⣄⠀⠀⠀⠀⣠⡀⢠⣿⣿⡇⠀⠀⠀⠀⠀⠀⠀",
"⠀⠀⠀⠀⠀⠀⠀⠘⠿⠟⠁⢸⣿⣇⠀⠀⣼⣿⣷⠈⠻⠿⠃⠀⠀⠀⠀⠀⠀⠀",
"⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠉⠁⠀⠀⠈⠉⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀",
"⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀",
}
dashboard.section.buttons.val = {
dashboard.button("<ctrl>n", "🌿 > open tree", toggle_nvim_tree),
dashboard.button("<space>fa", "🔎 > find file", builtin.find_files),
dashboard.button("fr", "🗃️ > recent", ":Telescope oldfiles<CR>"),
dashboard.button("<space>th", "📜 > cheatsheet", show_cheatsheet),
dashboard.button("q", "❌ > quit nvim", ":qa<CR>"),
}
---
--- cheatsheet
---
local cheatsheet_window = nil
function show_cheatsheet()
move_right_if_in_tree()
local Popup = require("nui.popup")
local event = require("nui.utils.autocmd").event
cheatsheet_window = Popup({
enter = true,
focusable = false,
border = {
style = "rounded",
},
position = "50%",
size = {
width = "50%",
height = "70%",
},
})
-- mount/open the component
cheatsheet_window:mount()
is_cheatsheet_open = true
-- unmount component when cursor leaves buffer
cheatsheet_window:on(event.BufLeave, function()
cheatsheet_window:unmount()
end)
local cheatsheet_path = vim.fn.stdpath("config") .. "/cheatsheet.md"
local file_contents = vim.fn.readfile(cheatsheet_path)
-- set content
vim.api.nvim_buf_set_lines(cheatsheet_window.bufnr, 0, 1, false, file_contents)
return cheatsheet_window
end
-- Function to close the cheatsheet window
function close_cheatsheet()
cheatsheet_window:unmount() -- Unmount the cheatsheet window
is_cheatsheet_open = false -- Update the flag to indicate cheatsheet is closed
end
vim.keymap.set("n", "<leader>th", show_cheatsheet, {})
---
--- conform
---
local conform = require("conform")
conform.setup({
formatters_by_ft = {
lua = { "stylua" },
python = { "black" },
json = { "prettier" },
html = { "prettier" },
javascript = { "prettier" },
},
})
require("mason-conform").setup({})
-- Function to format a buffer
function format_buffer()
move_right_if_in_tree()
-- Call the 'conform.format' function with specific options
conform.format({
lsp_failback = true,
async = true,
timeout_ms = 1000,
})
end
vim.keymap.set("n", "<leader>fo", format_buffer, local_keymap_options)
---
--- codegpt
---
vim.g["codegpt_commands_defaults"] = {
["chat"] = {
model = "gpt-4",
system_message_template = "You are a general assistant to a software developer.",
user_message_template = "{{command_args}}",
callback_type = "code_popup",
},
["code_edit"] = {
model = "gpt-4",
user_message_template = "I have the following {{language}} code: ```{{filetype}}\n{{text_selection}}```\n{{command_args}}. {{language_instructions}} Only return the code snippet and nothing else.",
callback_type = "code_popup",
},
}
function codegpt_question()
move_right_if_in_tree()
local question = vim.fn.input("Ask GPT: ")
vim.api.nvim_command("Chat chat " .. question)
end
function codegpt_instruct_edit()
move_right_if_in_tree()
local instruction = vim.fn.input("Instruct GPT: ")
vim.api.nvim_command("Chat code_edit " .. instruction)
end
vim.keymap.set("n", "<leader>tq", codegpt_question, local_keymap_options)
vim.keymap.set("v", "<leader>ti", codegpt_instruct_edit, local_keymap_options)