Separate options for completion in string/comment

We want to turn on completion in strings by default, but not for completion in
comments.
This commit is contained in:
Strahinja Val Markovic 2013-04-24 23:15:13 -07:00
parent 663873255f
commit f4615c00c0
3 changed files with 58 additions and 11 deletions

View File

@ -663,14 +663,29 @@ Default: `1`
let g:ycm_allow_changing_updatetime = 1
### The `g:ycm_complete_in_comments_and_strings` option
### The `g:ycm_complete_in_comments` option
When this option is set to `1`, YCM will show the completion menu even when
typing inside strings and comments.
typing inside comments.
Default: `0`
let g:ycm_complete_in_comments_and_strings = 0
let g:ycm_complete_in_comments = 0
### The `g:ycm_complete_in_strings` option
When this option is set to `1`, YCM will show the completion menu even when
typing inside strings.
Note that this is turned on by default so that you can use the filename
completion inside strings. This is very useful for instance in C-family files
where typing `#include "` will trigger the start of filename completion. If you
turn off this option, you will turn off filename completion in such situations
as well.
Default: `1`
let g:ycm_complete_in_strings = 1
### The `g:ycm_collect_identifiers_from_comments_and_strings` option

View File

@ -73,6 +73,7 @@ function! youcompleteme#Enable()
call s:SetUpCpoptions()
call s:SetUpCompleteopt()
call s:SetUpKeyMappings()
call s:SetUpBackwardsCompatibility()
if g:ycm_register_as_syntastic_checker
call s:ForceSyntasticCFamilyChecker()
@ -141,6 +142,17 @@ function! s:SetUpKeyMappings()
endfunction
function! s:SetUpBackwardsCompatibility()
let complete_in_comments_and_strings =
\ get( g:, 'ycm_complete_in_comments_and_strings', 0 )
if complete_in_comments_and_strings
let g:ycm_complete_in_strings = 1
let g:ycm_complete_in_comments = 1
endif
endfunction
function! s:ForceSyntasticCFamilyChecker()
" Needed so that YCM is used as the syntastic checker
let g:syntastic_cpp_checkers = ['ycm']
@ -382,21 +394,38 @@ function! s:IdentifierFinishedOperations()
endfunction
" Returns 1 when inside comment and 2 when inside string
function! s:InsideCommentOrString()
if g:ycm_complete_in_comments_and_strings
return 0
endif
" Has to be col('.') -1 because col('.') doesn't exist at this point. We are
" in insert mode when this func is called.
let syntax_group = synIDattr( synIDtrans( synID( line( '.' ), col( '.' ) - 1, 1 ) ), 'name')
if stridx(syntax_group, 'Comment') > -1 || stridx(syntax_group, 'String') > -1
if stridx(syntax_group, 'Comment') > -1
return 1
endif
if stridx(syntax_group, 'String') > -1
return 2
endif
return 0
endfunction
function! s:InsideCommentOrStringAndShouldStop()
let retval = s:InsideCommentOrString()
let inside_comment = retval == 1
let inside_string = retval == 2
if inside_comment && g:ycm_complete_in_comments ||
\ inside_string && g:ycm_complete_in_strings
return 0
endif
return retval
endfunction
function! s:OnBlankLine()
return pyeval( 'not vim.current.line or vim.current.line.isspace()' )
endfunction
@ -407,7 +436,7 @@ function! s:InvokeCompletion()
return
endif
if s:InsideCommentOrString() || s:OnBlankLine()
if s:InsideCommentOrStringAndShouldStop() || s:OnBlankLine()
return
endif

View File

@ -88,8 +88,11 @@ let g:ycm_allow_changing_updatetime =
let g:ycm_add_preview_to_completeopt =
\ get( g:, 'ycm_add_preview_to_completeopt', 0 )
let g:ycm_complete_in_comments_and_strings =
\ get( g:, 'ycm_complete_in_comments_and_strings', 1 )
let g:ycm_complete_in_comments =
\ get( g:, 'ycm_complete_in_comments', 0 )
let g:ycm_complete_in_strings =
\ get( g:, 'ycm_complete_in_strings', 1 )
let g:ycm_collect_identifiers_from_comments_and_strings =
\ get( g:, 'ycm_collect_identifiers_from_comments_and_strings', 0 )