-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
271 lines (229 loc) · 8.54 KB
/
Copy pathinit.lua
File metadata and controls
271 lines (229 loc) · 8.54 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
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
-- SET USERNAME BELOW
local username = "nnadar" -- put purdue username between quotes
------------------------------------------------------------
-- Leader Key
------------------------------------------------------------
vim.g.mapleader = " "
------------------------------------------------------------
-- Basic UI
------------------------------------------------------------
vim.opt.number = true
vim.opt.relativenumber = true
vim.opt.cursorline = true
vim.opt.mouse = "a"
vim.opt.splitright = true
vim.opt.splitbelow = true
vim.opt.clipboard = "unnamedplus"
vim.opt.termguicolors = true
vim.opt.ignorecase = true
vim.opt.smartcase = true
vim.opt.expandtab = true
vim.opt.shiftwidth = 4
vim.opt.tabstop = 4
vim.opt.smartindent = true
vim.opt.scrolloff = 8
vim.opt.signcolumn = "yes"
vim.opt.wrap = false
-- Reduce the time Neovim waits for a mapped sequence to complete (300ms from 1000ms)
vim.opt.timeoutlen = 300
------------------------------------------------------------
-- Transparency
------------------------------------------------------------
vim.cmd [[
hi Normal guibg=NONE ctermbg=NONE
hi NormalNC guibg=NONE ctermbg=NONE
hi VertSplit guibg=NONE ctermbg=NONE
hi StatusLine guibg=NONE ctermbg=NONE
hi LineNr guibg=NONE ctermbg=NONE
hi CursorLine guibg=NONE ctermbg=NONE
]]
vim.o.winblend = 20
------------------------------------------------------------
-- Safety: Clear existing maps to ensure clean slate
------------------------------------------------------------
pcall(vim.keymap.del, 'n', '<leader>t')
pcall(vim.keymap.del, 'n', '<leader>e')
pcall(vim.keymap.del, 'n', '<leader>l')
pcall(vim.keymap.del, 't', '<leader>t')
pcall(vim.keymap.del, 't', '<leader>e')
pcall(vim.keymap.del, 't', '<leader>l')
------------------------------------------------------------
-- Keymaps
------------------------------------------------------------
local map = vim.keymap.set
local opts = { noremap = true, silent = true }
-- Save/Quit
map("n", "<leader>w", ":w<CR>", opts)
map("n", "<leader>q", ":q<CR>", opts)
-- Window navigation
map("n", "<C-h>", "<C-w>h", opts)
map("n", "<C-j>", "<C-w>j", opts)
map("n", "<C-k>", "<C-w>k", opts)
map("n", "<C-l>", "<C-w>l", opts)
-- Split management
map("n", "<leader>sv", ":vsplit<CR>", opts)
map("n", "<leader>sh", ":split<CR>", opts)
map("n", "<leader>sc", ":close<CR>", opts)
------------------------------------------------------------
-- File Explorer (netrw) — <leader>E (Uppercase)
------------------------------------------------------------
vim.g.netrw_banner = 0
vim.g.netrw_liststyle = 3
vim.g.netrw_browse_split = 4
vim.g.netrw_altv = 1
vim.g.netrw_winsize = 25
map("n", "<leader>E", function()
if vim.bo.filetype == "netrw" then
vim.cmd("bd")
else
vim.cmd("Lexplore 25")
end
end, opts)
------------------------------------------------------------
-- VS Code Style Copy / Paste
------------------------------------------------------------
map({'n','v'}, '<C-c>', '"+y', opts)
map('n', '<C-v>', '"+p', opts)
map('v', '<C-v>', '"+p', opts)
map('i', '<C-v>', '<C-r>+', opts)
map('i', '<C-c>', '<Esc>"+yyi', opts)
---
-- 🤖 AI Code Editor Integration ($\text{<leader>K}$)
---
-- Function to pipe code to the python script with an instruction
function _G.ai_edit_or_add_feature()
-- 1. Determine the mode for the filter command
local current_mode = vim.api.nvim_get_mode().mode
local cmd_prefix
if current_mode == 'v' or current_mode == 'V' or current_mode == 's' or current_mode == 'S' then
-- Visual/Select mode: command applies only to the selection range (:'<,'>!)
cmd_prefix = "'<,'>!"
print("AI: Editing selected code...")
else
-- Normal mode: command applies to the entire file (: %!) <-- FIXED
cmd_prefix = "%!"
print("AI: Adding feature/editing entire file...")
end
-- 2. Ask user for the instruction
vim.ui.input({ prompt = "AI Edit/Feature Instruction: " }, function(input)
-- *** Exit/Cancel Logic ***
-- If user presses <Esc> (input == nil) or presses Enter on empty prompt, cancel.
if not input or input == "" then
print("AI request cancelled. Press <Enter> to continue.")
return
end
-- Escape single quotes in the instruction for bash safety
local safe_input = input:gsub("'", "'\\''")
-- Construct the full command
local cmd = "bash -c 'source /homes/" .. username .. "/.config/nvim/ai/env/bin/activate && python3 /homes/ " .. username .. "/.config/nvim/ai/llm-editor.py \"" .. safe_input .. "\"'"
-- Execute the filter command
vim.cmd(cmd_prefix .. cmd)
end)
end
-- Map K (Shift+k) in both Normal and Visual Mode to trigger the edit/feature-add
map({"n", "v"}, "<leader>K", "<cmd>lua ai_edit_or_add_feature()<CR>", opts)
---
-- 🖥️ Terminal Management
---
------------------------------------------------------------
-- Terminal Management (two terminals: main + llm)
------------------------------------------------------------
local main_term_buf = nil
local llm_term_buf = nil
-- === Bottom Terminal (<leader>T) ===
function _G.toggle_main_term()
if main_term_buf and vim.api.nvim_buf_is_valid(main_term_buf) then
for _, win in ipairs(vim.api.nvim_list_wins()) do
if vim.api.nvim_win_get_buf(win) == main_term_buf then
vim.api.nvim_win_close(win, true)
return
end
end
vim.cmd("botright split")
vim.api.nvim_win_set_buf(0, main_term_buf)
vim.cmd("resize 15")
vim.cmd("startinsert")
return
end
vim.cmd("botright split term://$SHELL")
vim.cmd("resize 15")
main_term_buf = vim.api.nvim_get_current_buf()
vim.cmd("startinsert")
end
map("n", "<leader>T", "<cmd>lua toggle_main_term()<CR>", opts)
map("t", "<leader>T", "<C-\\><C-n><cmd>lua toggle_main_term()<CR>", opts)
-- === LLM Terminal (<leader>L) ===
function _G.toggle_llm_term()
if llm_term_buf and vim.api.nvim_buf_is_valid(llm_term_buf) then
for _, win in ipairs(vim.api.nvim_list_wins()) do
if vim.api.nvim_win_get_buf(win) == llm_term_buf then
vim.api.nvim_win_close(win, true)
return
end
end
vim.cmd("vsplit")
vim.api.nvim_win_set_buf(0, llm_term_buf)
vim.cmd("vertical resize 80")
vim.cmd("startinsert")
return
end
-- 1. Create the terminal buffer
local cmd = "bash -c 'source /homes/" .. username .. "/.config/nvim/ai/env/bin/activate && python3 /homes/" .. username .. "/.config/nvim/ai/llm-manager.py'"
vim.cmd("vsplit term://" .. cmd)
vim.cmd("vertical resize 80")
-- 2. Capture the new buffer ID
llm_term_buf = vim.api.nvim_get_current_buf()
-- 3. AutoCmd to close the window when the script finishes (FIX)
vim.api.nvim_create_autocmd("TermClose", {
buffer = llm_term_buf,
once = true,
callback = function()
pcall(vim.cmd, "bdelete!")
llm_term_buf = nil
end
})
vim.cmd("startinsert")
end
map("n", "<leader>L", "<cmd>lua toggle_llm_term()<CR>", opts)
map("t", "<leader>L", "<C-\\><C-n><cmd>lua toggle_llm_term()<CR>", opts)
---
-- ⚙️ Quality of Life
---
------------------------------------------------------------
-- Format Key (Shift + Alt + F)
------------------------------------------------------------
map("n", "<S-A-f>", function()
if vim.lsp and vim.lsp.buf and vim.lsp.buf.format then
vim.lsp.buf.format({ async = true })
else
vim.cmd("normal! gg=G")
end
end, opts)
------------------------------------------------------------
-- Buffer / Tab Management
------------------------------------------------------------
map("n", "<leader>bn", ":bnext<CR>", opts)
map("n", "<leader>bp", ":bprevious<CR>", opts)
map("n", "<leader>bd", ":bdelete<CR>", opts)
------------------------------------------------------------
-- Quality of Life
------------------------------------------------------------
vim.api.nvim_create_autocmd("TextYankPost", { callback = function()
vim.highlight.on_yank({ timeout = 200 })
end })
vim.api.nvim_create_autocmd("BufWritePre", {
pattern = "*",
command = [[%s/\s\+$//e]],
})
vim.api.nvim_create_autocmd("BufReadPost", {
callback = function()
local mark = vim.api.nvim_buf_get_mark(0, '"')
local lcount = vim.api.nvim_buf_line_count(0)
if mark[1] > 0 and mark[1] <= lcount then
pcall(vim.api.nvim_win_set_cursor, 0, mark)
end
end,
})
------------------------------------------------------------
-- End of Config
------------------------------------------------------------