Vim插件YouCompleteMe递归添加头目录

时间:2014-09-17 09:21:13

标签: vim plugins syntax-highlighting

我正在尝试配置vim插件" YouCompleteMe"。我的C ++项目由许多头文件组成,这些头文件遍布目录树。为了添加头目录,我必须将它们添加到" .ycm_extra_conf.py"。

摘录:

'-I',
'./src/base/utils',
'-I',
'./src/base/modules',   

但是这样的事情不起作用:

'-I',
'./src/base/*',

有没有办法告诉YCM递归搜索头文件?

谢谢。

2 个答案:

答案 0 :(得分:5)

我添加了一个新语法-ISUB以包含所有子目录。

e.g。 "-ISUB./Pods/Headers/Public"

full .ycm_extra_conf.py here

import os
import ycm_core

flags = [
#custom definition, include subfolders
'-ISUB./Pods/Headers/Public',
'-I./Pod/Classes',
]

def Subdirectories(directory):
  res = []
  for path, subdirs, files in os.walk(directory):
    for name in subdirs:
      item = os.path.join(path, name)
      res.append(item)
  return res

def IncludeFlagsOfSubdirectory( flags, working_directory ):
  if not working_directory:
    return list( flags )
  new_flags = []
  make_next_include_subdir = False
  path_flags = [ '-ISUB']
  for flag in flags:
    # include the directory of flag as well
    new_flag = [flag.replace('-ISUB', '-I')]

    if make_next_include_subdir:
      make_next_include_subdir = False
      for subdir in Subdirectories(os.path.join(working_directory, flag)):
        new_flag.append('-I')
        new_flag.append(subdir)

    for path_flag in path_flags:
      if flag == path_flag:
        make_next_include_subdir = True
        break

      if flag.startswith( path_flag ):
        path = flag[ len( path_flag ): ]
        for subdir in Subdirectories(os.path.join(working_directory, path)):
            new_flag.append('-I' + subdir)
        break

    new_flags =new_flags + new_flag
  return new_flags

答案 1 :(得分:3)

我遇到了同样的问题所以我创建了一个功能。 在标志列表后面的“.ycm_extra_conf.py”中添加以下内容:

import glob
flagsRec=['/opt/e17/include/*'] 

def AddDirsRecursively( flagsRec ):                

  global flags                                                                                                         
  new_flags = []                                                                        
  for flag in flagsRec:                                                                                                                                
    for d in glob.glob(flag) :
      if os.path.isdir(d):
            new_flags.append('-I')
            new_flags.append(d)                                                               


  flags += new_flags                                                                                                                                


AddDirsRecursively( flagsRec )

其中“flagsRec”是您想要遍历并添加到“flags”的dirs(正则表达式)列表