From 7b031d312734c41ff6bdcf0a80dcec96d3db9253 Mon Sep 17 00:00:00 2001 From: Michael Campagnaro Date: Wed, 10 May 2023 14:56:43 -0400 Subject: [PATCH] More tweaks to the vimrc run function --- vimrc | 68 ++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 51 insertions(+), 17 deletions(-) diff --git a/vimrc b/vimrc index b2015e1..49722b4 100644 --- a/vimrc +++ b/vimrc @@ -1251,29 +1251,63 @@ function! Build(optimized=0, silent=0) endfunction function! RunProgram() - if filereadable("run") - exec "AsyncRun! -post=call\\ StopRunTask() ./run %" - else - let l:ext = tolower(expand('%:e')) - if l:ext == "jai" - if filereadable(expand('%:p:r') . '.exe') - exec "AsyncRun! " . expand('%:p:r') . ".exe" - else - if tolower(expand('%:h')) == "modules" - " This is likely a jai module inside a project. Check for an " exe one directory back. - let l:files = systemlist('ls ' . expand('%:p:h') . '/../*.exe 2>/dev/null') - else - let l:files = systemlist('ls ' . expand('%:p:h') . '/*.exe 2>/dev/null') - endif + let l:ran = 0 + let l:ext = tolower(expand('%:e')) + let l:path_to_use = "" + if l:ext == "jai" + " Maybe the current file has an exe, i.e. wasn't compiled with a build script. + if filereadable(expand('%:p:r') . '.exe') + let l:ran = 1 + exec "AsyncRun! " . expand('%:p:r') . ".exe" + elseif tolower(expand('%:h:t')) == "modules" || tolower(expand('%:h:h:t')) == "modules" + " This is likely a jai module inside a project. We will want to do the exe/run script checks in the parent project folder. + " The :h:h tests for a module inside a folder, e.g. modules/Basic/module.jai + echo "module" + if tolower(expand('%:h:t')) == "modules" + let l:path_to_use = "/.." + else + let l:path_to_use = "/../.." + endif + endif + elseif l:ext == "py" + let l:ran = 1 + exec "AsyncRun! python %" + endif + + if l:ran == 0 + " First check the current file's directory (and one directory back) + " for a run script, falling back to the current working directory (and + " one directory back) of the editor. + if filereadable(expand('%:h') . '/run') + echo "file here" + exec "AsyncRun! " . expand('%:h') . "/run" + elseif filereadable(expand('%:h') . '/../run') + " Handles editing a file in a code/ or src/ and there's a run script one directory back. + echo "file one back" + exec "AsyncRun! " . expand('%:h') . "/../run" + elseif filereadable("run") + echo "cwd here" + exec "AsyncRun! ./run" + elseif filereadable("../run") + " Handles editing a file in a code/ or src/ and there's a run script one directory back. + echo "cwd one back" + exec "AsyncRun! ../run" + else + " Final attempt is to run any exe that's found nearby. + " We start with an exe relative to the open file's path. + let l:files = systemlist('ls '.expand('%:h').''.l:path_to_use.'/*.exe 2>/dev/null') + if len(l:files) > 0 + exec "AsyncRun! " . l:files[0] + else + " Last attempt is any exe in the current working directory. + let l:files = systemlist('ls *.exe 2>/dev/null') if len(l:files) > 0 exec "AsyncRun! " . l:files[0] else - call PrintError("No exe found. Compile first?") + call PrintError("No exe or run script found!") endif endif - elseif l:ext == "py" - exec "AsyncRun! python %" endif endif endfunction