Advertisement
roachsinai

.ycm_extra_conf.py

Nov 24th, 2021
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. import os
  2. import ycm_core
  3.  
  4. DIR_OF_THIS_SCRIPT = os.path.abspath( os.path.dirname( __file__ ) )
  5. SOURCE_EXTENSIONS = [ '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ]
  6.  
  7. # These are the compilation flags that will be used in case there's no
  8. # compilation database set (by default, one is not set).
  9. # CHANGE THIS LIST OF FLAGS. YES, THIS IS THE DROID YOU HAVE BEEN LOOKING FOR.
  10. flags = [ '-x', 'c', '-Wall', '-Wextra', '-Werror' ]
  11.  
  12.  
  13. # Set this to the absolute path to the folder (NOT the file!) containing the
  14. # compile_commands.json file to use that instead of 'flags'. See here for
  15. # more details: http://clang.llvm.org/docs/JSONCompilationDatabase.html
  16. #
  17. # You can get CMake to generate this file for you by adding:
  18. # set( CMAKE_EXPORT_COMPILE_COMMANDS 1 )
  19. # to your CMakeLists.txt file.
  20. #
  21. # Most projects will NOT need to set this to anything; you can just change the
  22. # 'flags' list of compilation flags. Notice that YCM itself uses that approach.
  23. compilation_database_folder = 'DIR_OF_THIS_SCRIPT'
  24.  
  25. if os.path.exists( compilation_database_folder ):
  26. database = ycm_core.CompilationDatabase( compilation_database_folder )
  27. else:
  28. database = None
  29.  
  30.  
  31. def IsHeaderFile( filename ):
  32. extension = os.path.splitext( filename )[ 1 ]
  33. return extension in [ '.h', '.hxx', '.hpp', '.hh' ]
  34.  
  35.  
  36. def FindCorrespondingSourceFile( filename ):
  37. if IsHeaderFile( filename ):
  38. basename = os.path.splitext( filename )[ 0 ]
  39. for extension in SOURCE_EXTENSIONS:
  40. replacement_file = basename + extension
  41. if os.path.exists( replacement_file ):
  42. return replacement_file
  43. return filename
  44.  
  45.  
  46. def Settings( **kwargs ):
  47. if kwargs[ 'language' ] == 'cfamily':
  48. # If the file is a header, try to find the corresponding source file and
  49. # retrieve its flags from the compilation database if using one. This is
  50. # necessary since compilation databases don't have entries for header files.
  51. # In addition, use this source file as the translation unit. This makes it
  52. # possible to jump from a declaration in the header file to its definition
  53. # in the corresponding source file.
  54. filename = FindCorrespondingSourceFile( kwargs[ 'filename' ] )
  55.  
  56. if not database:
  57. filetype = os.path.splitext( filename )[ 1 ]
  58. # 可以识别要补全的文件是C还是CPP文件
  59. filetype_change_table = {'.cpp': 'c++', '.c': 'c'}
  60. filetype = filetype_change_table[filetype]
  61. ft_idx = flags.index('-x')
  62. flags[ft_idx + 1] = filetype
  63. return {
  64. 'flags': flags,
  65. 'include_paths_relative_to_dir': DIR_OF_THIS_SCRIPT,
  66. 'override_filename': filename
  67. }
  68.  
  69. compilation_info = database.GetCompilationInfoForFile( filename )
  70. if not compilation_info.compiler_flags_:
  71. return {}
  72.  
  73. # Bear in mind that compilation_info.compiler_flags_ does NOT return a
  74. # python list, but a "list-like" StringVec object.
  75. final_flags = list( compilation_info.compiler_flags_ )
  76.  
  77. return {
  78. 'flags': final_flags,
  79. 'include_paths_relative_to_dir': compilation_info.compiler_working_dir_,
  80. 'override_filename': filename
  81. }
  82. elif kwargs[ 'language' ] == 'python':
  83. client_data = kwargs['client_data']
  84. return {
  85. 'interpreter_path': client_data['g:ycm_python_interpreter_path'],
  86. 'sys_path': client_data['g:ycm_python_sys_path']
  87. }
  88. elif kwargs["language"] == "vim":
  89. return {
  90. "ls": {
  91. "iskeyword": "@,48-57,_,192-255,-#",
  92. "vimruntime": "",
  93. "runtimepath": "",
  94. "diagnostic": {"enable": True},
  95. }
  96. }
  97. return {}
  98.  
  99. def GetStandardLibraryIndexInSysPath( sys_path ):
  100. for path in sys_path:
  101. if os.path.isfile( os.path.join( path, 'os.py' ) ):
  102. return sys_path.index( path )
  103. raise RuntimeError( 'Could not find standard library path in Python path.' )
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement