From fa1084cf8f00369a2ee8096b7eb0b5994cd68f73 Mon Sep 17 00:00:00 2001 From: Martin Grenfell Date: Mon, 6 Feb 2012 17:46:08 +0000 Subject: [PATCH] DRY up the code that loads checkers when multiple exist Javascript and json have multiple syntax checkers that can be loaded. Previously the logic to determine which checker to load was basically copied and pasted in both. The `go` checker will soon have more than one option too so remove the duplication by sticking the common code in the core. --- plugin/syntastic.vim | 35 ++++++++++++++++++++++++++++++++++ syntax_checkers/javascript.vim | 20 +------------------ syntax_checkers/json.vim | 20 +------------------ 3 files changed, 37 insertions(+), 38 deletions(-) diff --git a/plugin/syntastic.vim b/plugin/syntastic.vim index ccfbb796..fe12959e 100644 --- a/plugin/syntastic.vim +++ b/plugin/syntastic.vim @@ -413,6 +413,12 @@ function! s:EchoCurrentError() endif endfunction +"load the chosen checker for the current filetype - useful for filetypes like +"javascript that have more than one syntax checker +function! s:LoadChecker(checker) + exec "runtime syntax_checkers/" . &ft . "/" . a:checker . ".vim" +endfunction + "return a string representing the state of buffer according to "g:syntastic_stl_format " @@ -559,4 +565,33 @@ function! SyntasticAddToErrors(errors, options) return a:errors endfunction +"take a list of syntax checkers for the current filetype and load the right +"one based on the global settings and checker executable availabity +" +"a:checkers should be a list of syntax checker names. These names are assumed +"to be the names of the vim syntax checker files that should be sourced, as +"well as the names of the actual syntax checker executables. The checkers +"should be listed in order of default preference. +" +"if a option called 'g:syntastic_[filetype]_checker' exists then attempt to +"load the checker that it points to +function! SyntasticLoadChecker(checkers) + let opt_name = "g:syntastic_" . &ft . "_checker" + + if exists(opt_name) + let opt_val = {opt_name} + if index(a:checkers, opt_val) != -1 && executable(opt_val) + call s:LoadChecker(opt_val) + else + echoerr &ft . " syntax not supported or not installed." + endif + else + for checker in a:checkers + if executable(checker) + return s:LoadChecker(checker) + endif + endfor + endif +endfunction + " vim: set et sts=4 sw=4: diff --git a/syntax_checkers/javascript.vim b/syntax_checkers/javascript.vim index 0f1bd291..026c7371 100644 --- a/syntax_checkers/javascript.vim +++ b/syntax_checkers/javascript.vim @@ -20,22 +20,4 @@ endif let loaded_javascript_syntax_checker = 1 let s:supported_checkers = ["gjslint", "jslint", "jsl", "jshint"] - -function! s:load_checker(checker) - exec "runtime syntax_checkers/javascript/" . a:checker . ".vim" -endfunction - -if exists("g:syntastic_javascript_checker") - if index(s:supported_checkers, g:syntastic_javascript_checker) != -1 && executable(g:syntastic_javascript_checker) - call s:load_checker(g:syntastic_javascript_checker) - else - echoerr "Javascript syntax not supported or not installed." - endif -else - for checker in s:supported_checkers - if executable(checker) - call s:load_checker(checker) - break - endif - endfor -endif +call SyntasticLoadChecker(s:supported_checkers) diff --git a/syntax_checkers/json.vim b/syntax_checkers/json.vim index c528b1e7..2d2652db 100644 --- a/syntax_checkers/json.vim +++ b/syntax_checkers/json.vim @@ -20,22 +20,4 @@ endif let loaded_json_syntax_checker = 1 let s:supported_checkers = ["jsonlint", "jsonval"] - -function! s:load_checker(checker) - exec "runtime syntax_checkers/json/" . a:checker . ".vim" -endfunction - -if exists("g:syntastic_json_checker") - if index(s:supported_checkers, g:syntastic_json_checker) != -1 && executable(g:syntastic_json_checker) - call s:load_checker(g:syntastic_json_checker) - else - echoerr "JSON syntax not supported or not installed." - endif -else - for checker in s:supported_checkers - if executable(checker) - call s:load_checker(checker) - break - endif - endfor -endif +call SyntasticLoadChecker(s:supported_checkers)