]> git.rmz.io Git - dotfiles.git/blob - vim/ycm_extra_conf.py
b4202ef2c8cd69ac15f977c4acd1581a01a0ae06
[dotfiles.git] / vim / ycm_extra_conf.py
1 from glob import glob
2 from os import path
3
4 import ycm_core
5
6
7 def findCompilationDatabaseFolder(dir='.'):
8 dirs = [path.dirname(f) for f in glob(dir + '/**/compile_commands.json',
9 recursive=True)]
10 return dirs
11
12
13 def generateQtFlags():
14 flags = ['-isystem', '/usr/include/qt/']
15 for p in glob('/usr/include/qt/*/'):
16 flags += ['-isystem', p]
17 return flags
18
19
20 def isHeader(filename):
21 ext = path.splitext(filename)[1]
22 return ext in ['.hpp', '.h']
23
24
25 def GetCompilationInfoForFile(database, filename):
26 if isHeader(filename):
27 basename = path.splitext(filename)[0]
28 for ext in ['.cpp', '.c']:
29 cpp_file = basename + ext
30 if path.exists(cpp_file):
31 compilation_info = database.GetCompilationInfoForFile(
32 cpp_file)
33 if compilation_info.compiler_flags_:
34 return compilation_info
35 return None
36 return database.GetCompilationInfoForFile(filename)
37
38
39 def FlagsForFile(filename, **kwargs):
40 client_data = kwargs['client_data']
41 cwd = client_data['getcwd()']
42
43 extra_flags = [
44 '-Wall',
45 '-Wextra',
46 # '-Wshadow',
47 # '-Werror',
48 # '-Wc++98-compat',
49 # '-Wno-long-long',
50 # '-Wno-variadic-macros',
51 # '-fexceptions',
52 # '-DNDEBUG',
53 ]
54
55 folders = findCompilationDatabaseFolder(cwd)
56 if not folders:
57 folder = None
58 else:
59 folder = folders[0]
60 if len(folders) > 1:
61 print("Multiple compilation databases found!")
62 print(folders)
63 print("Selecting first: %s" % (folder))
64
65 if folder:
66 database = ycm_core.CompilationDatabase(folder)
67 compilation_info = GetCompilationInfoForFile(database, filename)
68 if not compilation_info:
69 return None
70 flags = list(compilation_info.compiler_flags_)
71 else:
72 flags = [
73 '-std=c++14',
74 '-stdlib=libstdc++',
75 '-x', 'c++',
76 '-isystem', '/usr/include',
77 '-isystem', '/usr/local/include',
78 '-I', cwd,
79 '-I', cwd + '/include',
80 ]
81 flags += generateQtFlags()
82
83 return {
84 'flags': flags + extra_flags,
85 'do_cache': True
86 }