瀏覽代碼

Separate options for completion in string/comment

We want to turn on completion in strings by default, but not for completion in
comments.
Strahinja Val Markovic 12 年之前
父節點
當前提交
f4615c00c0
共有 3 個文件被更改,包括 58 次插入11 次删除
  1. 18 3
      README.md
  2. 35 6
      autoload/youcompleteme.vim
  3. 5 2
      plugin/youcompleteme.vim

+ 18 - 3
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
 

+ 35 - 6
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
 

+ 5 - 2
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 )