caiuspb
caiuspb

Reputation: 974

Vim Plugin YouCompleteMe add header directory recursively

I am trying to configure the vim Plugin "YouCompleteMe". My C++ project consists of many header files, which are spread all over the directory tree. In order to add header directories I have to add them in the ".ycm_extra_conf.py".

Excerpt:

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

But something like this does not work:

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

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

Thank you.

Upvotes: 4

Views: 2486

Answers (2)

Hai Feng Kao
Hai Feng Kao

Reputation: 5298

I have added a new syntax -ISUB to include all sub-directories.

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

Upvotes: 5

avilog
avilog

Reputation: 51

I had the same problem so I created a function that do it. 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"

Upvotes: 5

Related Questions