From 47d6d42a380017700a33c01728319cb0bece2ae3 Mon Sep 17 00:00:00 2001 From: Ben Vincent Date: Wed, 27 Mar 2024 19:44:21 +1100 Subject: [PATCH] vim updates --- .config/nvim/lua/func.lua | 7 ++++ .config/nvim/lua/func/findfile.lua | 37 +++++++++++++++++---- .config/nvim/lua/func/popterm.lua | 6 ++-- .config/nvim/lua/func/puppet.lua | 52 ++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 10 deletions(-) diff --git a/.config/nvim/lua/func.lua b/.config/nvim/lua/func.lua index 0d94e86..96b101f 100644 --- a/.config/nvim/lua/func.lua +++ b/.config/nvim/lua/func.lua @@ -32,3 +32,10 @@ vim.cmd [[ augroup END ]] +-- erb/eruby language keybindings +vim.cmd [[ + augroup lang_eruby + autocmd! + autocmd FileType eruby nnoremap s :call ToggleERBSyntax() + augroup END +]] diff --git a/.config/nvim/lua/func/findfile.lua b/.config/nvim/lua/func/findfile.lua index 506598e..083498f 100644 --- a/.config/nvim/lua/func/findfile.lua +++ b/.config/nvim/lua/func/findfile.lua @@ -1,19 +1,37 @@ vim.cmd([[ let g:previous_buffer = 0 -function! FindFile() +function! FindFile(searchType) " Store the buffer number of the current window let g:previous_buffer = bufnr('%') " Prompt user for search pattern let pattern = input('Enter search pattern: ') - let escaped_pattern = escape(pattern, '\/$.*[~') - "let cmd = "grep -Rl " . escaped_pattern . " *" - let cmd = "rg --column --no-heading --color=never -l " . shellescape(escaped_pattern) + let keywords = split(pattern) + " Construct a regex pattern for flexible matching in paths + 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) echo "No files found for pattern: " . pattern return @@ -41,6 +59,7 @@ function! FindFile() nnoremap v :call OpenFile('vsplit', g:previous_buffer) nnoremap h :call OpenFile('split', g:previous_buffer) nnoremap t :call OpenFile('tabedit', g:previous_buffer) + nnoremap q :close " Focus on the results window normal! G @@ -68,5 +87,9 @@ function! OpenFile(openType, bufnr) endif endfunction -nnoremap ff :call FindFile() +nnoremap ff :call FindFile("string") +nnoremap fs :call FindFile("string") +nnoremap fp :call FindFile("path") +nnoremap fa :call FindFile("all") + ]]) diff --git a/.config/nvim/lua/func/popterm.lua b/.config/nvim/lua/func/popterm.lua index e40239a..798af16 100644 --- a/.config/nvim/lua/func/popterm.lua +++ b/.config/nvim/lua/func/popterm.lua @@ -29,10 +29,10 @@ vim.cmd([[ endfunction " Define the :Popterm command to open the centered terminal - command! Popterm call OpenCenteredTerminal() - highlight Terminal guibg=#000000 guifg=none - nnoremap T :Popterm autocmd TermOpen * setlocal winhighlight=Normal:Terminal + highlight Terminal guibg=#000000 guifg=none + command! Popterm call OpenCenteredTerminal() + nnoremap T :Popterm endif ]]) diff --git a/.config/nvim/lua/func/puppet.lua b/.config/nvim/lua/func/puppet.lua index 34ef9a5..7753183 100644 --- a/.config/nvim/lua/func/puppet.lua +++ b/.config/nvim/lua/func/puppet.lua @@ -187,3 +187,55 @@ function! ApplyPuppetTemplate() endif 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 +]]) +