Vim YouCompleteMe plugin recursively adds header directory

I am trying to set up the "YouCompleteMe" vim plugin. My C ++ project consists of many header files that are distributed throughout the directory tree. To add header directories, I have to add them to ".ycm_extra_conf.py".

Excerpts:

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

      

But something like this doesn't work:

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

      

Is there a way to tell YCM to recursively search for header files?

Thank.

+3


source to share


2 answers


I added new syntax -ISUB

to include all subdirectories.

eg. "-ISUB./Pods/Headers/Public"



complete .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

      

+5


source


I had the same problem, so I created a function that does this. add the following to your ".ycm_extra_conf.py" right after the flags list:

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 )

      



where "flagsRec" is the list of dirs (regular expressions) you want to traverse and add to "flags"

+3


source







All Articles