ycm_extra_conf.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # .ycm_extra_conf.py for nvim source code.
  2. import os
  3. import ycm_core
  4. def DirectoryOfThisScript():
  5. return os.path.dirname(os.path.abspath(__file__))
  6. def GetDatabase():
  7. compilation_database_folder = os.path.join(DirectoryOfThisScript(),
  8. 'build')
  9. if os.path.exists(compilation_database_folder):
  10. return ycm_core.CompilationDatabase(compilation_database_folder)
  11. return None
  12. def GetCompilationInfoForFile(filename):
  13. database = GetDatabase()
  14. if not database:
  15. return None
  16. return database.GetCompilationInfoForFile(filename)
  17. # It seems YCM does not resolve directories correctly. This function will
  18. # adjust paths in the compiler flags to be absolute
  19. def FixDirectories(args, compiler_working_dir):
  20. def adjust_path(path):
  21. return os.path.abspath(os.path.join(compiler_working_dir, path))
  22. adjust_next_arg = False
  23. new_args = []
  24. for arg in args:
  25. if adjust_next_arg:
  26. arg = adjust_path(arg)
  27. adjust_next_arg = False
  28. else:
  29. for dir_flag in ['-I', '-isystem', '-o', '-c']:
  30. if arg.startswith(dir_flag):
  31. if arg != dir_flag:
  32. # flag and path are concatenated in same arg
  33. path = arg[len(dir_flag):]
  34. new_path = adjust_path(path)
  35. arg = '{0}{1}'.format(dir_flag, new_path)
  36. else:
  37. # path is specified in next argument
  38. adjust_next_arg = True
  39. new_args.append(arg)
  40. return new_args
  41. def FlagsForFile(filename):
  42. compilation_info = GetCompilationInfoForFile(filename)
  43. if not compilation_info:
  44. return None
  45. # Add flags not needed for clang-the-binary,
  46. # but needed for libclang-the-library (YCM uses this last one).
  47. flags = FixDirectories((list(compilation_info.compiler_flags_)
  48. if compilation_info.compiler_flags_
  49. else []), compilation_info.compiler_working_dir_)
  50. extra_flags = ['-Wno-newline-eof']
  51. return {
  52. 'flags': flags + extra_flags,
  53. 'do_cache': True
  54. }