Compare commits

...

2 Commits

Author SHA1 Message Date
fcd5cfbd54 Extend :Rg to support visual select mode
Also move away from using the telescope builtin grep for word under
cursor mapping by extending the `:Rg` command to work in the following
circumstances:

* `:Rg text to search for`
* `:Rg` word under cursor
* `'<,'>Rg` visual selection
2024-05-25 11:03:06 +01:00
1996e631a3 Add :PreProcIfWrap command and C/C++ mappings
Quickly wrap a line or range in `#if 0`/`#endif` to quickly disable
compilation of sections of code.
2024-05-25 11:02:57 +01:00
5 changed files with 41 additions and 6 deletions

3
after/ftplugin/c.lua Normal file
View File

@ -0,0 +1,3 @@
local opts = { buffer = true, remap = false, silent = true }
vim.keymap.set('n', '0', ':PreProcIfWrap<CR>', opts)
vim.keymap.set('v', '0', ':PreProcIfWrap<CR>', opts)

View File

@ -1,2 +1,6 @@
vim.opt.commentstring = '//%s'
vim.opt.matchpairs:append('<:>')
local opts = { buffer = true, remap = false, silent = true }
vim.keymap.set('n', '0', ':PreProcIfWrap<CR>', opts)
vim.keymap.set('v', '0', ':PreProcIfWrap<CR>', opts)

View File

@ -36,7 +36,6 @@ return {
vim.keymap.set('n', '<leader>gF', builtin.find_files, opts)
vim.keymap.set('n', '<leader>gf', builtin.git_files, opts)
vim.keymap.set('n', '<leader>gg', builtin.live_grep, opts)
vim.keymap.set('n', '<leader>rg', builtin.grep_string, opts)
vim.keymap.set('n', '<leader>gb', builtin.buffers, opts)
vim.keymap.set('n', '<leader>gh', builtin.help_tags, opts)
vim.keymap.set('n', '<leader>bl', builtin.current_buffer_fuzzy_find, opts)

View File

@ -116,13 +116,38 @@ end, {
complete = 'file',
})
-- :Rg grep for the given string and fuzzy find the results, bang to enable
-- regex in given string
-- FIXME: Support visual select modes
-- :Rg grep for the given string, word under the cursor, or visual selection
-- then fuzzy find the results. Bang to enable regex in given string.
vim.api.nvim_create_user_command('Rg', function(opts)
local grep_opts = { search = opts.args }
print(vim.inspect(opts))
local search = opts.args
if opts.range == 2 then
local lines = vim.fn.getline(vim.fn.line("'<"), vim.fn.line("'>"))
local start_col = vim.fn.col("'<")
local end_col = vim.fn.col("'>")
if #lines == 1 then
lines[1] = string.sub(lines[1], start_col, end_col)
else
lines[1] = string.sub(lines[1], start_col)
lines[#lines] = string.sub(lines[#lines], 1, end_col)
end
search = table.concat(lines, "\n")
elseif #search == 0 then
search = vim.fn.expand('<cword>')
end
local grep_opts = { search = search}
if opts.bang then
grep_opts['use_regex'] = true
end
require('telescope.builtin').grep_string(grep_opts)
end, { bang = true, nargs = '*' })
end, { bang = true, range = true, nargs = '*' })
vim.api.nvim_create_user_command('PreProcIfWrap', function(opts)
local buffer = vim.api.nvim_get_current_buf()
vim.api.nvim_buf_set_lines(
buffer, opts.line2, opts.line2, true, { '#endif' })
vim.api.nvim_buf_set_lines(
buffer, opts.line1 - 1, opts.line1 - 1, true, { '#if 0' })
local window = vim.api.nvim_get_current_win()
vim.api.nvim_win_set_cursor(window, { opts.line1, 5 })
end, { range = true })

View File

@ -43,3 +43,7 @@ vim.keymap.set('t', '<C-w><C-g>', '<C-\\><C-n>:Gdb<CR>:startinsert<CR>', opts)
vim.keymap.set('t', '<C-w><C-e>', '<C-\\><C-n>:Program<CR>', opts)
vim.keymap.set('t', '<C-w><C-s>', '<C-\\><C-n>:Source<CR>', opts)
vim.keymap.set('t', '<C-w><C-a>', '<C-\\><C-n>:Asm<CR>', opts)
-- Mappings to grep for then fuzzy find the word under the cursor or visual selection
vim.keymap.set('n', '<leader>rg', ':Rg<CR>', opts)
vim.keymap.set('v', '<leader>rg', ":'<,'>Rg<CR>", opts)