From 005175d3f3e2f7b975260e5411f634406bc80a15 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Sat, 9 Mar 2013 20:30:10 -0800 Subject: [PATCH] Filetype whitelist option added. Also renaming the filetype blacklist option for the sake of symmetry. Fixes #178. --- README.md | 43 +++++++++++++++++++++++++++++++------- autoload/youcompleteme.vim | 13 ++++++++---- plugin/youcompleteme.vim | 20 ++++++++++++------ 3 files changed, 58 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index f707a213..9f5f4c0c 100644 --- a/README.md +++ b/README.md @@ -484,21 +484,48 @@ Default: `2` let g:ycm_min_num_of_chars_for_completion = 2 -### The `g:ycm_filetypes_to_completely_ignore` option +### The `g:ycm_filetype_whitelist` option + +This option controls for which Vim filetypes (see `:h filetype`) should YCM be +turned on. The option value should be a Vim dictionary with keys being filetype +strings (like `python`, `cpp` etc) and values being unimportant (the dictionary +is used like a hash set, meaning that only the keys matter). + +The `*` key is special and matches all filetypes. By default, the whitelist +contains only this `*` key. + +YCM also has a `g:ycm_filetype_blacklist` option that lists filetypes for which +YCM shouldn't be turned on. YCM will work only in filetypes that both the +whitelist and the blacklist allow (the blacklist "allows" a filetype by _not_ +having it as a key). + +For example, let's assume you want YCM to work in files with the `cpp` filetype. +The filetype should then be present in the whitelist either directly (`cpp` key +in the whitelist) or indirectly through the special `*` key. It should _not_ be +present in the blacklist. + +Filetypes that are blocked by the either of the lists will be completely ignored +by YCM, meaning that neither the identifier-based completion engine nor the +semantic engine will operate in them. + +You can get the filetype of the current file in Vim with `:set ft?`. + +Default: `{'*' : 1}` + + let g:ycm_filetype_whitelist = { '*': 1 } + +### The `g:ycm_filetype_blacklist` option This option controls for which Vim filetypes (see `:h filetype`) should YCM be turned off. The option value should be a Vim dictionary with keys being filetype strings (like `python`, `cpp` etc) and values being unimportant (the dictionary -is used like a hash set, meaning that only the keys matter). The listed -filetypes will be completely ignored by YCM, meaning that neither the -identifier-based completion engine nor the semantic engine will operate in files -of those filetypes. +is used like a hash set, meaning that only the keys matter). -You can get the filetype of the current file in Vim with `:set ft?`. +See the `g:ycm_filetype_whitelist` option for more details on how this works. -Default: `{notes: 1, markdown: 1, text: 1}` +Default: `{'notes': 1, 'markdown': 1, 'text': 1}` - let g:ycm_filetypes_to_completely_ignore = { + let g:ycm_filetype_blacklist = { \ 'notes' : 1, \ 'markdown' : 1, \ 'text' : 1, diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index 0349cfb5..1da6f6e9 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -136,10 +136,15 @@ endfunction function! s:AllowedToCompleteInCurrentFile() - " If the user set the current filetype as a filetype that YCM should ignore, - " then we don't do anything - return !empty( &filetype ) && - \ !get( g:ycm_filetypes_to_completely_ignore, &filetype, 0 ) + if empty( &filetype ) + return 0 + endif + + let whitelist_allows = has_key( g:ycm_filetype_whitelist, '*' ) || + \ has_key( g:ycm_filetype_whitelist, &filetype ) + let blacklist_allows = !has_key( g:ycm_filetype_blacklist, &filetype ) + + return whitelist_allows && blacklist_allows endfunction diff --git a/plugin/youcompleteme.vim b/plugin/youcompleteme.vim index 86d0a8a8..d35969fd 100644 --- a/plugin/youcompleteme.vim +++ b/plugin/youcompleteme.vim @@ -58,12 +58,20 @@ let g:loaded_youcompleteme = 1 let g:ycm_min_num_of_chars_for_completion = \ get( g:, 'ycm_min_num_of_chars_for_completion', 2 ) -let g:ycm_filetypes_to_completely_ignore = - \ get( g:, 'ycm_filetypes_to_completely_ignore', { - \ 'notes' : 1, - \ 'markdown' : 1, - \ 'text' : 1, - \} ) +let g:ycm_filetype_whitelist = + \ get( g:, 'ycm_filetype_whitelist', { + \ '*' : 1, + \ } ) + +" The fallback to g:ycm_filetypes_to_completely_ignore is here because of +" backwards compatibility with previous versions of YCM. +let g:ycm_filetype_blacklist = + \ get( g:, 'ycm_filetype_blacklist', + \ get( g:, 'ycm_filetypes_to_completely_ignore', { + \ 'notes' : 1, + \ 'markdown' : 1, + \ 'text' : 1, + \ } ) ) let g:ycm_filetype_specific_completion_to_disable = \ get( g:, 'ycm_filetype_specific_completion_to_disable', {} )