From 0bd8c1808606dbe73ea8a58824c73b6a6bc60d3e Mon Sep 17 00:00:00 2001 From: yage Date: Tue, 23 Aug 2022 13:43:52 -0600 Subject: [PATCH] more general command escaper than just tests --- vim/execute.vim | 80 +++++++++++++++++++++++++++++ vim/formatting.vim | 8 +-- vim/rc.vim | 10 ++-- vim/testing.vim | 125 --------------------------------------------- 4 files changed, 90 insertions(+), 133 deletions(-) create mode 100644 vim/execute.vim delete mode 100644 vim/testing.vim diff --git a/vim/execute.vim b/vim/execute.vim new file mode 100644 index 0000000..e0bd1c6 --- /dev/null +++ b/vim/execute.vim @@ -0,0 +1,80 @@ +let escapeTmuxSession = "vim-exec" +let escapeTmuxPaneID = g:escapeTmuxSession . ":0.0" + +let escapeCommandOutputs = ['tmux', 'split-pane-vertical', 'split-pane-horizontal'] + +function ExecuteCommand(args = '', output = '', flavor = 'shell') + let output = a:output + if output == '' + let output = GetPrefferredCommandOutput() + endif + + let command = GetCommandString(a:args, a:flavor, output) + + if output == 'tmux' + silent call EnsureTmuxSession() + call system("tmux send-keys -t ".g:escapeTmuxPaneID." '".command."' Enter") + silent call system("tmux display-popup -E 'tmux a -t ".g:escapeTmuxSession."' &") + elseif output == 'split-pane-horizontal' + execute "botright terminal " . command + elseif output == 'split-pane-vertical' + execute "botright vertical terminal " . command + else + execute "!" . command + endif +endfunction + +function GetCommandString(args, flavor, output) + let command = '' + let gitRoot = FindGitRoot() + + if a:flavor == 'shell' + let command = a:args + + elseif a:flavor == 'npm' + let command = 'npm test ' . a:args + if gitRoot != '' + let command = 'cd ' . gitRoot . '; ' . command + endif + + elseif a:flavor == 'django' + let command = './manage.py test --keepdb ' . a:args + if gitRoot != '' + let command = 'cd ' . gitRoot . '; ' . command + endif + endif + + if stridx(a:output, 'tmux') != -1 + let command = command.";" + \ . "echo \"-----------------------------\" | lolcat;" + \ . "echo \"(ENTER to close, C^c to stay)\";" + \ . "read = 800 + return output + endif + endfor + + return 'shell-escape' +endfunction + +function EnsureTmuxSession() + call system("tmux new -ds " . g:escapeTmuxSession . " -c $HOME >/dev/null 2>&1") +endfunction + +function FindGitRoot() + return finddir('.git/..', expand('%:p:h').';') +endfunction diff --git a/vim/formatting.vim b/vim/formatting.vim index d9745cf..2abdb81 100644 --- a/vim/formatting.vim +++ b/vim/formatting.vim @@ -14,6 +14,7 @@ function FormatFileType(indent, expandtab, foldmethod, foldlevel, spell) endfunction augroup forced_filetype_recognition + autocmd! autocmd BufRead,BufNewFile *.tmux setfiletype tmux autocmd BufRead,BufNewFile *.clisp setfiletype lisp autocmd BufRead,BufNewFile *.lsp setfiletype lisp @@ -64,7 +65,8 @@ let g:markdown_fenced_languages = ['javascript', 'json', 'python', 'bash', 'yaml " (t)ests " {{{ augroup file_specific_command_overrides - nnoremap ec :call SplitPaneTest('%:p', v:true) + autocmd! + nnoremap ec :call ExecuteCommand('%:p', 'split-pane-horizontal') nnoremap ei :echohl ErrorMsg echom 'ERROR: no interactive execute defined' echohl None nnoremap eb :echohl ErrorMsg echom 'ERROR: no build steps defined' echohl None nnoremap ef :echohl ErrorMsg echom 'ERROR: no auto-format steps defined' echohl None @@ -80,8 +82,8 @@ augroup file_specific_command_overrides autocmd FileType go nnoremap ef (go-imports) autocmd FileType go nnoremap gd (go-def-tab) - autocmd FileType python nnoremap ec :call SplitPaneTest('bpython %') - autocmd FileType python nnoremap ei :call SplitPaneTest('bpython -qi %') + autocmd FileType python nnoremap ec :call ExecuteCommand('bpython %:p', 'split-pane-vertical') + autocmd FileType python nnoremap ei :call ExecuteCommand('bpython -qi %:p', 'split-pane-vertical') augroup end " }}} diff --git a/vim/rc.vim b/vim/rc.vim index 064a7c9..796d397 100644 --- a/vim/rc.vim +++ b/vim/rc.vim @@ -5,7 +5,7 @@ if isdirectory(expand("$HOME/.vim/bundle/Vundle.vim")) endif source $WRYNVIMPATH/options.vim -source $WRYNVIMPATH/testing.vim +source $WRYNVIMPATH/execute.vim source $WRYNVIMPATH/formatting.vim source $WRYNVIMPATH/navigation.vim source $WRYNVIMPATH/color.vim @@ -49,10 +49,10 @@ nnoremap gb :Git blame " \r = open last REPL (p)ython (n)odejs (c)lisp nnoremap r q:?^echom 'quickrepl' -nnoremap rp q:oechom 'quickrepl' \| call SplitPaneTest('bpython') -nnoremap rn q:oechom 'quickrepl' \| call SplitPaneTest('node') -nnoremap rc q:oechom 'quickrepl' \| call SplitPaneTest('clisp', 1) -nnoremap rs q:oechom 'quickrepl' \| call SplitPaneTest('') +nnoremap rp q:oechom 'quickrepl' \| call ExecuteCommand('bpython', 'split-pane-vertical') +nnoremap rn q:oechom 'quickrepl' \| call ExecuteCommand('node', 'split-pane-vertical') +nnoremap rc q:oechom 'quickrepl' \| call ExecuteCommand('clisp', 'split-pane-horizontal') +nnoremap rs q:oechom 'quickrepl' \| call ExecuteCommand('zsh -l', 'split-pane-vertical') " \t = run last quicktest " t)ype new quicktest diff --git a/vim/testing.vim b/vim/testing.vim deleted file mode 100644 index 214c2ad..0000000 --- a/vim/testing.vim +++ /dev/null @@ -1,125 +0,0 @@ - -let tmuxTestSessionName = "test" -let defaultTmuxPaneId = g:tmuxTestSessionName . ":0.0" - -function InitializeTmuxTestSession() - call system("tmux new -ds " . g:tmuxTestSessionName . " -c $HOME >/dev/null 2>&1") -endfunction - - -" =================================================================== -" === test formats ================================================== -" =================================================================== - -function TmuxTest(shellCommand, paneId = g:defaultTmuxPaneId) - if a:paneId == g:defaultTmuxPaneId - silent call InitializeTmuxTestSession() - endif - - call system("tmux send-keys -t " . a:paneId . " 'clear; " . a:shellCommand . "' Enter") -endfunction - -function SplitPaneTest(shellCommand, horizontal = v:false) - if a:horizontal - execute "botright terminal " . a:shellCommand - else - execute "botright vertical terminal " . a:shellCommand - endif -endfunction - -function ShellEscapedTest(shellCommand) - execute "!" . a:shellCommand -endfunction - - -" =================================================================== -" === python-django ================================================= -" =================================================================== - -function DjangoTmuxTest(paneId = g:defaultTmuxPaneId) - let l:command = "cd " . getcwd() . "; " . GetDjangoTestCommand() - call TmuxTest(l:command, a:paneId) -endfunction - -function DjangoSplitTest(horizontal = v:false) - let l:command = GetDjangoTestCommand() - call SplitPaneTest(l:command, a:horizontal) -endfunction - -function DjangoTest() - let l:command = GetDjangoTestCommand() - call ShellEscapedTest(l:command) -endfunction - - -function GetDjangoTestCommand() - return GetDjangoManagePy() . " test --keepdb" -endfunction - -function GetDjangoManagePy() - return substitute(expand(getcwd()), "/code.*", "/code/manage.py", "") -endfunction - - -" =================================================================== -" === dotnet core =================================================== -" =================================================================== - -function DotnetTest(filter = '') - let l:command = - \ 'cd ' . GetDotnetProjectLocation() - \ . ';' . 'dotnet build -clp:ErrorsOnly' - \ . ';' . 'cd ' . GetDotnetProjectLocation(1) - \ . ';' . 'dotnet test -clp:ErrorsOnly' - if a:filter != '' - let l:command = l:command . ' --filter ' . a:filter - endif - call TmuxTest(l:command) -endfunction - -function DotnetBuild() - let l:command = - \ 'cd ' . GetDotnetProjectLocation() - \ . ';' . 'dotnet build -clp:ErrorsOnly' - - call TmuxTest(l:command) -endfunction - -function GetDotnetProjectLocation(test = v:false) - let l:projectRoot = substitute(expand(getcwd()), '/code.*', '/code', '') - let l:projectName = substitute(expand(getcwd()), l:projectRoot . '/\([^/]*\).*', '\1', '') - if a:test - let l:testPath = system('ls ' . l:projectRoot . '/**/*.csproj | grep Test | head -1') - else - if l:projectName != '' - let l:testPath = l:projectRoot . '/' . l:projectName - else - let l:testPath = system('ls ' . l:projectRoot . '/**/*.csproj | grep -v Test | head -1') - endif - endif - return substitute(l:testPath, '\(.*\)/.*.csproj.*', '\1', '') -endfunction - -" =================================================================== -" === npm =========================================================== -" =================================================================== - -function NpmTmuxTest(paneId = g:defaultTmuxPaneId) - let l:command = "cd " . getcwd() . "; " . GetNpmTestCommand() - call TmuxTest(l:command, a:paneId) -endfunction - -function NpmSplitTest(horizontal = v:false) - let l:command = GetNpmTestCommand() - call SplitPaneTest(l:command, a:horizontal) -endfunction - -function NpmTest() - let l:command = GetNpmTestCommand() - call ShellEscapedTest(l:command) -endfunction - - -function GetNpmTestCommand() - return "npm test" -endfunction