-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
168 lines (136 loc) · 6.21 KB
/
init.lua
File metadata and controls
168 lines (136 loc) · 6.21 KB
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
-- LSP Setup
-- Config Reference: https://github.com/neovim/nvim-lspconfig/blob/master/doc/configs.md
vim.lsp.enable({
"luals",
-- "pyright",
"ruff",
"ty",
"rust-analyzer"
})
-- Vim Settings
vim.opt.number = true -- Show line numbers
vim.opt.relativenumber = true -- Show relative line numbers
vim.opt.expandtab = true -- Use spaces instead of tabs
vim.opt.tabstop = 2 -- Number of spaces that a <Tab> in the file counts for
vim.opt.shiftwidth = 2 -- Number of spaces to use for each step of (auto)indent
vim.opt.expandtab = true -- Use spaces instead of tabs
vim.opt.autoread = true -- Automatically reload files when changed on disk
vim.g.mapleader = " " -- Set leader key to space
vim.api.nvim_set_keymap('n', '<A-j>', ':m .+1<CR>==', { noremap = true }) -- Move line down with option-j
vim.api.nvim_set_keymap('n', '<A-k>', ':m .-2<CR>==', { noremap = true }) -- Move line up with option-k
vim.api.nvim_set_keymap('v', '<A-j>', ':m \'>+1<CR>gv=gv', { noremap = true })
vim.api.nvim_set_keymap('v', '<A-k>', ':m \'<-2<CR>gv=gv', { noremap = true })
vim.api.nvim_set_keymap('v', '<leader>y', '"+y', { noremap = true }) -- Copy to clipboard
-- Set spacing for specific file types
vim.api.nvim_create_autocmd({ 'FileType', 'BufRead', 'BufNewFile' }, {
pattern = '*.toml',
command = 'setlocal tabstop=4 shiftwidth=4 expandtab'
})
vim.api.nvim_create_autocmd({ 'FileType', 'BufRead', 'BufNewFile' }, {
pattern = '*.java',
command = 'setlocal tabstop=4 shiftwidth=4 expandtab'
})
-- Set up Copilot accept suggestion with Tab
vim.keymap.set("i", '<Tab>', function()
if require("copilot.suggestion").is_visible() then
require("copilot.suggestion").accept()
else
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<Tab>", true, false, true), "n", false)
end
end, {
silent = true,
})
vim.keymap.set("n", "<leader>h", "<cmd>cnext<CR>zz", { desc = "Forward qfixlist" })
vim.keymap.set("n", "<leader>;", "<cmd>cprev<CR>zz", { desc = "Backward qfixlist" })
-- Copy file path to clipboard
vim.keymap.set("n", "<C-g>", function()
local path = vim.fn.expand("%:p")
vim.fn.setreg("+", path)
print(path)
end, { desc = "Copy absolute file path to clipboard" })
vim.keymap.set("n", "<C-S-g>", function()
local name = vim.fn.expand("%:t")
vim.fn.setreg("+", name)
print(name)
end, { desc = "Copy filename to clipboard" })
vim.keymap.set("n", "<leader>ls", "<cmd>LspInfo<CR>", { desc = "Show LSP info" })
-- Oil sorting
vim.keymap.set("n", "<leader>Os", function()
require("oil").set_sort({ { "mtime", "desc" }, { "name", "asc" } })
end, { desc = "Sort Oil buffer by date (desc)" })
-- Detect macOS system appearance
local function is_dark_mode()
local handle = io.popen("defaults read -g AppleInterfaceStyle 2>/dev/null")
if handle then
local result = handle:read("*a")
handle:close()
return result:find("Dark") ~= nil
end
return false
end
vim.g.is_dark_mode = is_dark_mode()
vim.o.background = vim.g.is_dark_mode and "dark" or "light"
-- Initialize Lazy
require("lazy.lazy")
-- Telescope Keybinds
local keymap = vim.keymap -- for conciseness
vim.api.nvim_create_autocmd("LspAttach", {
group = vim.api.nvim_create_augroup("UserLspConfig", {}),
callback = function(ev)
-- Buffer local mappings.
-- See `:help vim.lsp.*` for documentation on any of the below functions
local opts = { buffer = ev.buf, silent = true }
-- set keybinds
opts.desc = "Show LSP references"
keymap.set("n", "<leader>gR", "<cmd>Telescope lsp_references<CR>", opts) -- show definition, references
opts.desc = "Go to declaration"
keymap.set("n", "<leader>gD", vim.lsp.buf.declaration, opts) -- go to declaration
opts.desc = "Show LSP definitions"
keymap.set("n", "<leader>gd", "<cmd>Telescope lsp_definitions<CR>", opts) -- show lsp definitions
opts.desc = "Show LSP implementations"
keymap.set("n", "<leader>gi", "<cmd>Telescope lsp_implementations<CR>", opts) -- show lsp implementations
opts.desc = "Show LSP type definitions"
keymap.set("n", "<leader>gt", "<cmd>Telescope lsp_type_definitions<CR>", opts) -- show lsp type definitions
opts.desc = "Grep string under cursor"
keymap.set("n", "<leader>fc", "<cmd>Telescope grep_string<CR>", opts) -- show lsp type definitions
opts.desc = "See available code actions"
keymap.set({ "n", "v" }, "<leader>ca", vim.lsp.buf.code_action, opts) -- see available code actions, in visual mode will apply to selection
opts.desc = "Smart rename"
keymap.set("n", "<leader>rn", vim.lsp.buf.rename, opts) -- smart rename
opts.desc = "Show buffer diagnostics"
keymap.set("n", "<leader>D", "<cmd>Telescope diagnostics bufnr=0<CR>", opts) -- show diagnostics for file
opts.desc = "Show line diagnostics"
keymap.set("n", "<leader>d", vim.diagnostic.open_float, opts) -- show diagnostics for line
opts.desc = "Go to previous diagnostic"
keymap.set("n", "[d", vim.diagnostic.goto_prev, opts) -- jump to previous diagnostic in buffer
opts.desc = "Go to next diagnostic"
keymap.set("n", "]d", vim.diagnostic.goto_next, opts) -- jump to next diagnostic in buffer
opts.desc = "Show documentation for what is under cursor"
keymap.set("n", "K", vim.lsp.buf.hover, opts) -- show documentation for what is under cursor
end,
})
-- Configure diagnostic view
vim.diagnostic.config({
virtual_text = {
source = true,
},
float = {
source = true,
},
virtual_lines = true,
})
-- Format on save
vim.api.nvim_create_autocmd("LspAttach", {
callback = function(args)
local client = vim.lsp.get_client_by_id(args.data.client_id)
if not client then return end
if client.supports_method('textDocument/formatting') then
vim.api.nvim_create_autocmd("BufWritePre", {
buffer = args.buf,
callback = function()
vim.lsp.buf.format({ bufnr = args.buf, id = client.id })
end,
})
end
end,
})