From f4615c00c07545205a9a601dfb44815ab8079cd2 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Wed, 24 Apr 2013 23:15:13 -0700 Subject: [PATCH] Separate options for completion in string/comment We want to turn on completion in strings by default, but not for completion in comments. --- README.md | 21 ++++++++++++++++--- autoload/youcompleteme.vim | 41 ++++++++++++++++++++++++++++++++------ plugin/youcompleteme.vim | 7 +++++-- 3 files changed, 58 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index a0ed4ec3..2cab3edd 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index 2ffde9be..e2c869b3 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -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 diff --git a/plugin/youcompleteme.vim b/plugin/youcompleteme.vim index 206933f5..3b3b2e78 100644 --- a/plugin/youcompleteme.vim +++ b/plugin/youcompleteme.vim @@ -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 )