As a part of my Tabv Vim plugin (which at this stage is nothing more than a pitiful rag-tag assortment of half-baked hacks), I have a function which attempts to guess the directory paths for main source and unit tests. The first working version of this function is (full source):
let g:tabv_grunt_file_path='Gruntfile.js'
function s:GuessPathsFromGruntfile()
execute "sview " . g:tabv_grunt_file_path
global/^\_s*['"].*\*\.spec\.js['"]\_s*[,\]]\_s*/y a
let l:matches = matchlist(getreg('a'), '[''"]\(.*\)/\*\.spec\.js[''"]')
if len(l:matches) > 1
let g:tabv_javascript_unittest_directory = l:matches[1]
endif
global/^\_s*['"].*\*\.js['"]\_s*[,\]]\_s*/y a
let l:matches = matchlist(getreg('a'), '[''"]\(.*\)/\*\.js[''"]')
if len(l:matches) > 1
let g:tabv_javascript_source_directory = l:matches[1]
endif
close
endfunction
Also relevant, from the same script, and above the function shown:
let g:tabv_javascript_source_directory="src"
let g:tabv_javascript_source_extension=".js"
let g:tabv_javascript_unittest_directory="unittests"
let g:tabv_javascript_unittest_extension=".spec.js"
Now, the algorithm needs some help, but that's not what I'm worried about at the moment. (I try to build out this plugin on an as-needed basis, rather than going for the gold, so to speak.) What I do not like is:
- The code duplication
- The fact that the first and second parts of the function are similar.
- The fact that I am not using the global variables for the file extensions in the regular expression.
- The fact that I am actually visually opening and closing the Gruntfile.
- The
execute
statement
What else do you see wrong here, and how could I do better? What can I do to correct the problems mentioned above?
-
\$\begingroup\$ In retrospect I think the whole approach is necessarily a hack. \$\endgroup\$Kazark– Kazark2015年08月10日 13:45:00 +00:00Commented Aug 10, 2015 at 13:45