From 6b2c61a5cc59d61270266dbe399d5dc55cfad5b4 Mon Sep 17 00:00:00 2001 From: w0rp Date: Tue, 14 Nov 2017 19:55:28 +0000 Subject: [PATCH] Fix #1128 - Add g:ale_linters_explicit for only enabling linters explicitly --- autoload/ale/linter.vim | 27 ++++++++-- doc/ale.txt | 32 ++++++++--- plugin/ale.vim | 4 +- test/test_filetype_linter_defaults.vader | 68 ++++++++++++++++++++++++ test/test_linter_retrieval.vader | 3 +- 5 files changed, 119 insertions(+), 15 deletions(-) create mode 100644 test/test_filetype_linter_defaults.vader diff --git a/autoload/ale/linter.vim b/autoload/ale/linter.vim index c6667d95..e8f30fff 100644 --- a/autoload/ale/linter.vim +++ b/autoload/ale/linter.vim @@ -21,6 +21,8 @@ let s:default_ale_linter_aliases = { " " Only cargo is enabled for Rust by default. " rpmlint is disabled by default because it can result in code execution. +" +" NOTE: Update the g:ale_linters documentation when modifying this. let s:default_ale_linters = { \ 'csh': ['shell'], \ 'go': ['gofmt', 'golint', 'go vet'], @@ -308,11 +310,26 @@ function! s:GetLinterNames(original_filetype) abort return l:buffer_ale_linters endif - for l:dict in [l:buffer_ale_linters, g:ale_linters, s:default_ale_linters] - if has_key(l:dict, a:original_filetype) - return l:dict[a:original_filetype] - endif - endfor + " Try to get a buffer-local setting for the filetype + if has_key(l:buffer_ale_linters, a:original_filetype) + return l:buffer_ale_linters[a:original_filetype] + endif + + " Try to get a global setting for the filetype + if has_key(g:ale_linters, a:original_filetype) + return g:ale_linters[a:original_filetype] + endif + + " If the user has configured ALE to only enable linters explicitly, then + " don't enable any linters by default. + if g:ale_linters_explicit + return [] + endif + + " Try to get a default setting for the filetype + if has_key(s:default_ale_linters, a:original_filetype) + return s:default_ale_linters[a:original_filetype] + endif return 'all' endfunction diff --git a/doc/ale.txt b/doc/ale.txt index b653e5c4..cd6a3a0d 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -946,23 +946,25 @@ g:ale_linters *g:ale_linters* Type: |Dictionary| Default: `{}` - The |g:ale_linters| option sets a |Dictionary| mapping a filetype - to a |List| of linter programs to be run when checking particular filetypes. - Only the filetypes specified in the dictionary will be limited in terms - of which linters will be run. + The |g:ale_linters| option sets a |Dictionary| mapping a filetype to a + |List| of linter programs to be run when checking particular filetypes. This |Dictionary| will be merged with a default dictionary containing the following values: > { \ 'csh': ['shell'], + \ 'go': ['gofmt', 'golint', 'go vet'], + \ 'help': [], + \ 'python': ['flake8', 'mypy', 'pylint'], \ 'rust': ['cargo'], + \ 'spec': [], \ 'text': [], \ 'zsh': ['shell'], \} < This option can be used to enable only a particular set of linters for a - file. For example, you can enable only 'eslint' for JavaScript files: > + file. For example, you can enable only `eslint` for JavaScript files: > let g:ale_linters = {'javascript': ['eslint']} < @@ -971,14 +973,15 @@ g:ale_linters *g:ale_linters* let g:ale_linters = {'javascript': []} < - All linters available for a given filetype can be enabled by using the - string `'all'`: > + All linters will be run for unspecified filetypes. All available linters can + be enabled explicitly for a given filetype by passing the string `'all'`, + instead of a List. > let g:ale_linters = {'c': 'all'} < Linters can be configured in each buffer with buffer-local variables. ALE will first look for linters for filetypes in the `b:ale_linters` variable, - then `g:ale_linters`, and then a default Dictionary. + then `g:ale_linters`, and then the default Dictionary mentioned above. `b:ale_linters` can be set to a List, or the string `'all'`. When linters for two different filetypes share the same name, the first linter loaded @@ -994,6 +997,19 @@ g:ale_linters *g:ale_linters* " Explicitly enable all available linters for the filetype. let b:ale_linters = 'all' < + ALE can be configured to disable all linters unless otherwise specified with + `g:ale_enabled` or `b:ale_enabled` with the option |g:ale_linters_explicit|. + + +g:ale_linters_explicit *g:ale_linters_explicit* + + Type: |Number| + Default: `0` + + When set to `1`, only the linters from |g:ale_linters| and |b:ale_linters| + will be enabled. The default behavior for ALE is to enable as many linters + as possible, unless otherwise specified. + g:ale_loclist_msg_format *g:ale_loclist_msg_format* diff --git a/plugin/ale.vim b/plugin/ale.vim index 11df5204..31c3377e 100644 --- a/plugin/ale.vim +++ b/plugin/ale.vim @@ -68,7 +68,9 @@ let g:ale_filetype_blacklist = [ \] " This Dictionary configures which linters are enabled for which languages. -let g:ale_linters = get(g:, 'ale_linters', {}) +call ale#Set('linters', {}) +" This option can be changed to only enable explicitly selected linters. +call ale#Set('linters_explicit', 0) " This Dictionary configures which functions will be used for fixing problems. let g:ale_fixers = get(g:, 'ale_fixers', {}) diff --git a/test/test_filetype_linter_defaults.vader b/test/test_filetype_linter_defaults.vader new file mode 100644 index 00000000..ea4a05fb --- /dev/null +++ b/test/test_filetype_linter_defaults.vader @@ -0,0 +1,68 @@ +Before: + Save g:ale_linters + Save g:ale_linters_explicit + + let g:ale_linters_explicit = 0 + let g:ale_linters = {} + + function! GetLinterNames(filetype) abort + return map(ale#linter#Get(a:filetype), 'v:val.name') + endfunction + +After: + Restore + + call ale#linter#Reset() + +Execute(The defaults for the csh filetype should be correct): + AssertEqual ['shell'], GetLinterNames('csh') + + let g:ale_linters_explicit = 1 + + AssertEqual [], GetLinterNames('csh') + +Execute(The defaults for the go filetype should be correct): + AssertEqual ['gofmt', 'golint', 'go vet'], GetLinterNames('go') + + let g:ale_linters_explicit = 1 + + AssertEqual [], GetLinterNames('go') + +Execute(The defaults for the help filetype should be correct): + AssertEqual [], GetLinterNames('help') + +Execute(The defaults for the python filetype should be correct): + AssertEqual ['flake8', 'mypy', 'pylint'], GetLinterNames('python') + + let g:ale_linters_explicit = 1 + + AssertEqual [], GetLinterNames('python') + +Execute(The defaults for the rust filetype should be correct): + AssertEqual ['cargo'], GetLinterNames('rust') + + let g:ale_linters_explicit = 1 + + AssertEqual [], GetLinterNames('rust') + +Execute(The defaults for the spec filetype should be correct): + AssertEqual [], GetLinterNames('spec') + +Execute(The defaults for the text filetype should be correct): + AssertEqual [], GetLinterNames('text') + +Execute(The defaults for the zsh filetype should be correct): + AssertEqual ['shell'], GetLinterNames('zsh') + + let g:ale_linters_explicit = 1 + + AssertEqual [], GetLinterNames('zsh') + +Execute(The defaults for the verilog filetype should be correct): + " This filetype isn't configured with default, so we can test loading all + " available linters with this. + AssertEqual ['iverilog', 'verilator'], GetLinterNames('verilog') + + let g:ale_linters_explicit = 1 + + AssertEqual [], GetLinterNames('verilog') diff --git a/test/test_linter_retrieval.vader b/test/test_linter_retrieval.vader index 5d5b582d..5d1ee451 100644 --- a/test/test_linter_retrieval.vader +++ b/test/test_linter_retrieval.vader @@ -1,5 +1,6 @@ Before: - Save g:ale_linters, g:ale_linter_aliases + Save g:ale_linters + Save g:ale_linter_aliases let g:testlinter1 = {'name': 'testlinter1', 'executable': 'testlinter1', 'command': 'testlinter1', 'callback': 'testCB1', 'output_stream': 'stdout', 'read_buffer': 1, 'lint_file': 0, 'aliases': [], 'lsp': '', 'add_newline': 0} let g:testlinter2 = {'name': 'testlinter2', 'executable': 'testlinter2', 'command': 'testlinter2', 'callback': 'testCB2', 'output_stream': 'stdout', 'read_buffer': 0, 'lint_file': 1, 'aliases': [], 'lsp': '', 'add_newline': 0}