Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 523
caiuspb Avatar asked Dec 26 '22 04:12

caiuspb


1 Answers

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
like image 125
Hai Feng Kao Avatar answered Dec 28 '22 18:12

Hai Feng Kao