3
\$\begingroup\$

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?

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Mar 24, 2014 at 20:32
\$\endgroup\$
1
  • \$\begingroup\$ In retrospect I think the whole approach is necessarily a hack. \$\endgroup\$ Commented Aug 10, 2015 at 13:45

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.