-
Notifications
You must be signed in to change notification settings - Fork 3
/
cli_tools.lua
381 lines (366 loc) · 15.6 KB
/
cli_tools.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
local keymap = vim.api.nvim_set_keymap
local autocmd = vim.api.nvim_create_autocmd
local my_augroup = require('conf.builtin_extend').my_augroup
local bufmap = vim.api.nvim_buf_set_keymap
return {
-- an installer for a bunch of CLI tools (e.g. LSP, linters)
{
'williamboman/mason.nvim',
cmd = { 'Mason', 'MasonInstall', 'MasonUpdate', 'MasonUninstall' },
config = function()
require('mason').setup()
end,
},
{
'akinsho/toggleterm.nvim',
cmd = 'ToggleTerm',
config = function()
require('toggleterm').setup {
-- size can be a number or function which is passed the current terminal
size = function(term)
if term.direction == 'horizontal' then
return 15
elseif term.direction == 'vertical' then
return vim.o.columns * 0.30
end
end,
open_mapping = [[<Leader>ot]],
shade_terminals = false,
start_in_insert = false,
insert_mappings = false,
terminal_mappings = false,
persist_size = true,
direction = 'horizontal',
close_on_exit = true,
}
end,
init = function()
keymap('n', '<Leader>ot', '<cmd>exe v:count . "ToggleTerm"<CR>', { desc = 'Toggle display all terminals' })
keymap(
'n',
'<Leader>t!',
[[<cmd>execute v:count . "TermExec cmd='exit;'"<CR>]],
{ silent = true, desc = 'quit terminal' }
)
end,
},
{
'goerz/jupytext.vim',
event = { 'VeryLazy', 'LazyFile' },
lazy = vim.fn.argc(-1) == 0, -- load jupytext early when opening a file from the cmdline
init = function()
vim.g.jupytext_enabled = 1
-- the jupytext_fmt is not flexible enough to support fetch the format
-- dynamically for example if you want to use jupytext for both python and
-- R and given two different formats like py:percent and R:percent (or just
-- auto:percent). Since it is expected that one will use jupyter notebook
-- mainly for python I will just set the default format to py:percent.
-- TODO: write a PR to jupytext.vim to support multiple format.
vim.g.jupytext_fmt = 'py:percent'
end,
},
{
'milanglacier/yarepl.nvim',
event = 'VeryLazy',
config = function()
local yarepl = require 'yarepl'
local aider = require 'yarepl.extensions.aider'
yarepl.setup {
metas = { aider = aider.create_aider_meta(), python = false, R = false },
}
end,
init = function()
----- Set Aichat Keymap ------
keymap('n', '<Leader>cs', '<Plug>(REPLStart-aichat)', {
desc = 'Start an Aichat REPL',
})
keymap('n', '<Leader>cf', '<Plug>(REPLFocus-aichat)', {
desc = 'Focus on Aichat REPL',
})
keymap('n', '<Leader>ch', '<Plug>(REPLHide-aichat)', {
desc = 'Hide Aichat REPL',
})
keymap('v', '<Leader>cr', '<Plug>(REPLSendVisual-aichat)', {
desc = 'Send visual region to Aichat',
})
keymap('n', '<Leader>crr', '<Plug>(REPLSendLine-aichat)', {
desc = 'Send lines to Aichat',
})
keymap('n', '<Leader>cr', '<Plug>(REPLSendOperator-aichat)', {
desc = 'Send Operator to Aichat',
})
keymap('n', '<Leader>ce', '<Plug>(REPLExec-aichat)', {
desc = 'Execute command in aichat',
})
keymap('n', '<Leader>cq', '<Plug>(REPLClose-aichat)', {
desc = 'Quit Aichat',
})
keymap('n', '<Leader>cc', '<CMD>REPLCleanup<CR>', {
desc = 'Clear aichat REPLs.',
})
----- Set Aider Keymap ------
-- general keymap from yarepl
keymap('n', '<Leader>as', '<Plug>(REPLStart-aider)', {
desc = 'Start an aider REPL',
})
keymap('n', '<Leader>af', '<Plug>(REPLFocus-aider)', {
desc = 'Focus on aider REPL',
})
keymap('n', '<Leader>ah', '<Plug>(REPLHide-aider)', {
desc = 'Hide aider REPL',
})
keymap('v', '<Leader>ar', '<Plug>(REPLSendVisual-aider)', {
desc = 'Send visual region to aider',
})
keymap('n', '<Leader>arr', '<Plug>(REPLSendLine-aider)', {
desc = 'Send lines to aider',
})
keymap('n', '<Leader>ar', '<Plug>(REPLSendOperator-aider)', {
desc = 'Send Operator to aider',
})
-- special keymap from aider
keymap('n', '<Leader>ae', '<Plug>(AiderExec)', {
desc = 'Execute command in aider',
})
keymap('n', '<Leader>ay', '<Plug>(AiderSendYes)', {
desc = 'Send y to aider',
})
keymap('n', '<Leader>an', '<Plug>(AiderSendNo)', {
desc = 'Send n to aider',
})
keymap('n', '<Leader>aa', '<Plug>(AiderSendAbort)', {
desc = 'Send abort to aider',
})
keymap('n', '<Leader>aq', '<Plug>(AiderSendExit)', {
desc = 'Send exit to aider',
})
keymap('n', '<Leader>ag', '<cmd>AiderSetPrefix<cr>', {
desc = 'set aider prefix',
})
keymap('n', '<Leader>aG', '<cmd>AiderRemovePrefix<cr>', {
desc = 'remove aider prefix',
})
keymap('n', '<Leader>a<space>', '<cmd>checktime<cr>', {
desc = 'sync file changes by aider to nvim buffer',
})
----- Set Filetype Specific keymap -----
local ft_to_repl = {
r = 'radian',
R = 'radian',
rmd = 'radian',
quarto = 'radian',
markdown = 'radian',
python = 'ipython',
sh = 'bash',
}
autocmd('FileType', {
pattern = { 'quarto', 'markdown', 'rmd', 'python', 'sh', 'REPL', 'r' },
group = my_augroup,
desc = 'set up REPL keymap',
callback = function()
local repl = ft_to_repl[vim.bo.filetype]
repl = repl and ('-' .. repl) or ''
bufmap(0, 'n', '<LocalLeader>rs', string.format('<Plug>(REPLStart%s)', repl), {
desc = 'Start an REPL',
})
bufmap(0, 'n', '<LocalLeader>rf', '<Plug>(REPLFocus)', {
desc = 'Focus on REPL',
})
bufmap(0, 'n', '<LocalLeader>rv', '<CMD>FF repl_show<CR>', {
desc = 'View REPLs in telescope',
})
bufmap(0, 'n', '<LocalLeader>rh', '<Plug>(REPLHide)', {
desc = 'Hide REPL',
})
bufmap(0, 'v', '<LocalLeader>s', '<Plug>(REPLSendVisual)', {
desc = 'Send visual region to REPL',
})
bufmap(0, 'n', '<LocalLeader>ss', '<Plug>(REPLSendLine)', {
desc = 'Send line to REPL',
})
bufmap(0, 'n', '<LocalLeader>s', '<Plug>(REPLSendOperator)', {
desc = 'Send current line to REPL',
})
bufmap(0, 'n', '<LocalLeader>re', '<Plug>(REPLExec)', {
desc = 'Execute command in REPL',
})
bufmap(0, 'n', '<LocalLeader>rq', '<Plug>(REPLClose)', {
desc = 'Quit REPL',
})
bufmap(0, 'n', '<LocalLeader>rc', '<CMD>REPLCleanup<CR>', {
desc = 'Clear REPLs.',
})
bufmap(0, 'n', '<LocalLeader>rS', '<CMD>REPLSwap<CR>', {
desc = 'Swap REPLs.',
})
bufmap(0, 'n', '<LocalLeader>r?', '<Plug>(REPLStart)', {
desc = 'Start an REPL from available REPL metas',
})
bufmap(0, 'n', '<LocalLeader>ra', '<CMD>REPLAttachBufferToREPL<CR>', {
desc = 'Attach current buffer to a REPL',
})
bufmap(0, 'n', '<LocalLeader>rd', '<CMD>REPLDetachBufferToREPL<CR>', {
desc = 'Detach current buffer to any REPL',
})
local function send_a_code_chunk()
local leader = vim.g.mapleader
local localleader = vim.g.maplocalleader
-- NOTE: in an expr mapping, <Leader> and <LocalLeader>
-- cannot be translated. You must use their literal value
-- in the returned string.
if vim.bo.filetype == 'r' or vim.bo.filetype == 'python' then
return localleader .. 'si' .. leader .. 'c'
elseif
vim.bo.filetype == 'rmd'
or vim.bo.filetype == 'quarto'
or vim.bo.filetype == 'markdown'
then
return localleader .. 'sic'
end
end
bufmap(0, 'n', '<localleader>sc', '', {
desc = 'send a code chunk',
callback = send_a_code_chunk,
expr = true,
})
end,
})
end,
},
{
'lewis6991/gitsigns.nvim',
event = 'LazyFile',
init = function()
keymap('n', '<Leader>gp', '<cmd>Gitsigns preview_hunk<CR>', { noremap = true })
keymap('n', '<Leader>gB', '<cmd>Gitsigns blame<CR>', { noremap = true })
keymap('n', '<Leader>gb', '<cmd>Gitsigns blame_line<CR>', { noremap = true })
keymap('n', '<Leader>ga', '<cmd>Gitsigns<CR>', { noremap = true })
keymap('n', '<Leader>gr', '<cmd>Gitsigns reset_hunk<CR>', { noremap = true })
keymap('n', '<Leader>gs', '<cmd>Gitsigns stage_hunk<CR>', { noremap = true })
keymap('v', '<Leader>gs', ':<C-U>Gitsigns stage_hunk<CR>', { noremap = true })
keymap('n', '<Leader>gR', '<cmd>Gitsigns undo_stage_hunk<CR>', { noremap = true })
keymap('n', '<Leader>gq', '<cmd>Gitsigns setqflist<CR>', { noremap = true })
keymap('n', ']h', '', {
noremap = true,
desc = 'git next hunk',
callback = function()
if vim.wo.diff then
vim.cmd 'normal! ]c'
else
require('gitsigns').nav_hunk 'next'
end
end,
})
keymap('n', '[h', '', {
noremap = true,
desc = 'git prv hunk',
callback = function()
if vim.wo.diff then
vim.cmd 'normal! [c'
else
require('gitsigns').nav_hunk 'prev'
end
end,
})
-- text objects
keymap('v', 'ih', '<esc><cmd>Gitsigns select_hunk<CR>', { noremap = true })
keymap('o', 'ih', ':<C-U>Gitsigns select_hunk<CR>', { noremap = true })
end,
config = function()
require('gitsigns').setup { update_debounce = 200 }
end,
},
{
'sindrets/diffview.nvim',
-- I don't use this plugin very often. I've thought about removing it,
-- but it's still pretty useful, especially for showing side-by-side
-- diffs in two panels. So, I decided to keep the plugin and its
-- commands around, but I didn't set up any keymaps for it. This way,
-- it's there when I need it, but it doesn't take up space in my
-- regular key bindings.
cmd = { 'DiffviewOpen', 'DiffviewFileHistory' },
},
{
'MagicDuck/grug-far.nvim',
init = function()
local function grugfar(mode, wincmd, current_word, current_file)
return function()
local caller
if mode == 'n' then
caller = 'grug_far'
else
-- send control-u before calling the command
-- This is required for visual mode
vim.api.nvim_feedkeys('\27', 'nx', false)
caller = 'with_visual_selection'
end
require('grug-far')[caller] {
windowCreationCommand = wincmd,
prefills = {
search = (mode == 'n' and current_word) and vim.fn.expand '<cword>' or nil,
paths = current_file and vim.fn.expand '%' or nil,
},
}
end
end
for _, mode in ipairs { 'n', 'v' } do
keymap(mode, '<Leader>/', '', {
callback = grugfar(mode, 'tabnew', nil, nil),
desc = 'Search and Replace',
})
keymap(mode, '<Leader>ss', '', {
callback = grugfar(mode, 'split', true, nil),
desc = 'hsplit: S/R selected words',
})
keymap(mode, '<Leader>sv', '', {
callback = grugfar(mode, 'vsplit', true, nil),
desc = 'vsplit: S/R selected words',
})
keymap(mode, '<Leader>st', '', {
callback = grugfar(mode, 'tabnew', true, nil),
desc = 'tabnew: S/R of selected words',
})
keymap(mode, '<Leader>sf', '', {
callback = grugfar(mode, 'vsplit', true, true),
desc = 'vsplit: S/R selected words in current file',
})
end
end,
config = function()
require('grug-far').setup { startInInsertMode = false }
end,
},
{
'ludovicchabant/vim-gutentags',
init = function()
vim.g.gutentags_add_ctrlp_root_markers = 0
vim.g.gutentags_ctags_exclude = { '.*', '**/.*' }
vim.g.gutentags_generate_on_new = 0
vim.g.gutentags_generate_on_missing = 0
vim.g.gutentags_ctags_tagfile = '.tags'
vim.g.gutentags_ctags_extra_args = { [[--fields=*]] }
vim.filetype.add {
pattern = {
['%.tags'] = 'tags',
},
}
end,
event = 'VeryLazy',
},
{
'MeanderingProgrammer/render-markdown.nvim',
ft = { 'markdown', 'rmd', 'quarto' },
config = function()
require('render-markdown').setup {
file_types = { 'markdown', 'rmd', 'quarto' },
code = {
sign = false,
},
heading = {
sign = false,
icons = { ' ', ' ', ' ', ' ', ' ', ' ' },
},
}
end,
},
}