vim updates

This commit is contained in:
Ben Vincent 2024-03-27 19:44:21 +11:00
parent dcfff8dbbf
commit 47d6d42a38
4 changed files with 92 additions and 10 deletions

View File

@ -32,3 +32,10 @@ vim.cmd [[
augroup END augroup END
]] ]]
-- erb/eruby language keybindings
vim.cmd [[
augroup lang_eruby
autocmd!
autocmd FileType eruby nnoremap <buffer> <Leader>s :call ToggleERBSyntax()<CR>
augroup END
]]

View File

@ -1,19 +1,37 @@
vim.cmd([[ vim.cmd([[
let g:previous_buffer = 0 let g:previous_buffer = 0
function! FindFile() function! FindFile(searchType)
" Store the buffer number of the current window " Store the buffer number of the current window
let g:previous_buffer = bufnr('%') let g:previous_buffer = bufnr('%')
" Prompt user for search pattern " Prompt user for search pattern
let pattern = input('Enter search pattern: ') let pattern = input('Enter search pattern: ')
let escaped_pattern = escape(pattern, '\/$.*[~') let keywords = split(pattern)
"let cmd = "grep -Rl " . escaped_pattern . " *" " Construct a regex pattern for flexible matching in paths
let cmd = "rg --column --no-heading --color=never -l " . shellescape(escaped_pattern) let regex_pattern = join(map(keywords, 'escape(v:val, "\\/$.*[~")'), '.*')
" Initialize an empty list for results
let results = []
" Find in file content
if a:searchType ==# 'string' || a:searchType ==# 'all'
let cmd_content = "rg --column --no-heading --color=never -l " . shellescape(pattern)
let results_content = systemlist(cmd_content)
let results = results + results_content
endif
" Find in file path with the constructed regex pattern
if a:searchType ==# 'path' || a:searchType ==# 'all'
" Use the regex pattern for searching in paths
let cmd_path = "rg --files | grep -P '" . regex_pattern . "'"
let results_path = systemlist(cmd_path)
let results = results + results_path
endif
" Remove duplicates and sort the results
let results = sort(uniq(results))
" Get the search results as a list
let results = systemlist(cmd)
if empty(results) if empty(results)
echo "No files found for pattern: " . pattern echo "No files found for pattern: " . pattern
return return
@ -41,6 +59,7 @@ function! FindFile()
nnoremap <silent> <buffer> v :call OpenFile('vsplit', g:previous_buffer)<CR> nnoremap <silent> <buffer> v :call OpenFile('vsplit', g:previous_buffer)<CR>
nnoremap <silent> <buffer> h :call OpenFile('split', g:previous_buffer)<CR> nnoremap <silent> <buffer> h :call OpenFile('split', g:previous_buffer)<CR>
nnoremap <silent> <buffer> t :call OpenFile('tabedit', g:previous_buffer)<CR> nnoremap <silent> <buffer> t :call OpenFile('tabedit', g:previous_buffer)<CR>
nnoremap <silent> <buffer> q :close<CR>
" Focus on the results window " Focus on the results window
normal! G normal! G
@ -68,5 +87,9 @@ function! OpenFile(openType, bufnr)
endif endif
endfunction endfunction
nnoremap <Leader>ff :call FindFile()<CR> nnoremap <Leader>ff :call FindFile("string")<CR>
nnoremap <Leader>fs :call FindFile("string")<CR>
nnoremap <Leader>fp :call FindFile("path")<CR>
nnoremap <Leader>fa :call FindFile("all")<CR>
]]) ]])

View File

@ -29,10 +29,10 @@ vim.cmd([[
endfunction endfunction
" Define the :Popterm command to open the centered terminal " Define the :Popterm command to open the centered terminal
command! Popterm call OpenCenteredTerminal()
highlight Terminal guibg=#000000 guifg=none
nnoremap T :Popterm<CR>
autocmd TermOpen * setlocal winhighlight=Normal:Terminal autocmd TermOpen * setlocal winhighlight=Normal:Terminal
highlight Terminal guibg=#000000 guifg=none
command! Popterm call OpenCenteredTerminal()
nnoremap T :Popterm<CR>
endif endif
]]) ]])

View File

@ -187,3 +187,55 @@ function! ApplyPuppetTemplate()
endif endif
endfunction endfunction
]]) ]])
vim.cmd([[
function! ToggleERBSyntax()
if !exists('b:original_syntax')
" If b:original_syntax is not set, default to eruby and store the current syntax
let b:original_syntax = &filetype
setlocal syntax=eruby
else
" If b:original_syntax is set, toggle the syntax based on the current setting
if &syntax == 'eruby'
" Extract the base file type from the file name
let l:filename = expand('%:t')
let l:base_filetype = matchstr(l:filename, '\v%(\.\w+)?\.erb$')
let l:base_filetype = substitute(l:base_filetype, '\.erb$', '', '')
let l:base_filetype = substitute(l:base_filetype, '^.', '', '')
" Determine the syntax based on the base file type
if l:base_filetype == 'sh'
setlocal syntax=sh
elseif l:base_filetype == 'py'
setlocal syntax=python
elseif l:base_filetype == 'pl'
setlocal syntax=perl
elseif l:base_filetype == 'yml' || l:base_filetype == 'yaml'
setlocal syntax=yaml
else
" Attempt to guess the syntax from the shebang if unknown
let l:firstline = getline(1)
if l:firstline =~# '^#!.*\/bash'
setlocal syntax=sh
elseif l:firstline =~# '^#!.*\/zsh'
setlocal syntax=sh
elseif l:firstline =~# '^#!.*\/python'
setlocal syntax=python
elseif l:firstline =~# '^#!.*\/perl'
setlocal syntax=perl
else
" Fallback to eruby syntax if no known type is found
echo "Could not swap syntax highlighter; falling back to eruby."
endif
endif
else
" If the current syntax is not eruby, revert to the original syntax
setlocal syntax=eruby
echo "Switched back to eruby syntax."
endif
" Reset b:original_syntax to indicate the syntax has been toggled back
unlet b:original_syntax
endif
endfunction
]])