Skip to content

Commit 2ef1e7e

Browse files
gunnarbeutnerlinusg
authored andcommitted
Meta: Add configuration file for YouCompleteMe
YouCompleteMe is a plugin for Vim which provides code-completion functionality. This change adds a configuration file which makes YouCompleteMe aware of which compile flags to use with clangd.
1 parent 83b3403 commit 2ef1e7e

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed

.ycm_extra_conf.py

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# Based on the example .ycm_extra_conf.py from YouCompleteMe, adapted
2+
# for SerenityOS.
3+
#
4+
# This file is NOT licensed under the GPLv3, which is the license for the rest
5+
# of YouCompleteMe.
6+
#
7+
# Here's the license text for this file:
8+
#
9+
# This is free and unencumbered software released into the public domain.
10+
#
11+
# Anyone is free to copy, modify, publish, use, compile, sell, or
12+
# distribute this software, either in source code form or as a compiled
13+
# binary, for any purpose, commercial or non-commercial, and by any
14+
# means.
15+
#
16+
# In jurisdictions that recognize copyright laws, the author or authors
17+
# of this software dedicate any and all copyright interest in the
18+
# software to the public domain. We make this dedication for the benefit
19+
# of the public at large and to the detriment of our heirs and
20+
# successors. We intend this dedication to be an overt act of
21+
# relinquishment in perpetuity of all present and future rights to this
22+
# software under copyright law.
23+
#
24+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27+
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
28+
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
29+
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
30+
# OTHER DEALINGS IN THE SOFTWARE.
31+
#
32+
# For more information, please refer to <http://unlicense.org/>
33+
34+
import os
35+
import subprocess
36+
import ycm_core
37+
38+
DIR_OF_THIS_SCRIPT = os.path.abspath(os.path.dirname(__file__))
39+
SOURCE_EXTENSIONS = ['.cpp', '.c']
40+
41+
gcc_path = None
42+
for serenity_arch in ['x86_64', 'i686', 'aarch64']:
43+
candidate_gcc_path = os.path.join(
44+
DIR_OF_THIS_SCRIPT, 'Toolchain',
45+
'Local', serenity_arch, 'bin', f'{serenity_arch}-pc-serenity-gcc'
46+
)
47+
if os.path.isfile(candidate_gcc_path):
48+
gcc_path = candidate_gcc_path
49+
break
50+
51+
serenity_flags = [
52+
'-D__serenity__',
53+
'-D__unix__'
54+
]
55+
56+
if gcc_path:
57+
gcc_output = subprocess.check_output(
58+
[gcc_path, '-E', '-Wp,-v', '-'],
59+
stdin=subprocess.DEVNULL, stderr=subprocess.STDOUT
60+
).rstrip().decode('utf8').split("\n")
61+
62+
for line in gcc_output:
63+
if not line.startswith(' '):
64+
continue
65+
include_path = line.lstrip()
66+
if '/../Build/' in include_path:
67+
continue
68+
serenity_flags.extend(('-isystem', include_path))
69+
70+
database = ycm_core.CompilationDatabase(f'Build/{serenity_arch}')
71+
72+
73+
def is_header_file(filename):
74+
extension = os.path.splitext(filename)[1]
75+
return extension in ['.h', '.hxx', '.hpp', '.hh']
76+
77+
78+
def find_corresponding_source_file(filename):
79+
if is_header_file(filename):
80+
basename = os.path.splitext(filename)[0]
81+
for extension in SOURCE_EXTENSIONS:
82+
replacement_file = basename + extension
83+
if os.path.exists(replacement_file):
84+
return replacement_file
85+
return filename
86+
87+
88+
def startswith_any(string, prefixes):
89+
for prefix in prefixes:
90+
if string.startswith(prefix):
91+
return True
92+
return False
93+
94+
95+
def Settings(**kwargs):
96+
if kwargs['language'] != 'cfamily':
97+
return {}
98+
# If the file is a header, try to find the corresponding source file and
99+
# retrieve its flags from the compilation database if using one. This is
100+
# necessary since compilation databases don't have entries for header files.
101+
# In addition, use this source file as the translation unit. This makes it
102+
# possible to jump from a declaration in the header file to its definition
103+
# in the corresponding source file.
104+
filename = find_corresponding_source_file(kwargs['filename'])
105+
106+
compilation_info = database.GetCompilationInfoForFile(filename)
107+
if not compilation_info.compiler_flags_:
108+
return {}
109+
110+
ignored_flags = [
111+
'--sysroot',
112+
'-fzero-call-used-regs=used-gpr',
113+
]
114+
115+
final_flags = [flag for flag in compilation_info.compiler_flags_ if not startswith_any(flag, ignored_flags)]
116+
final_flags.extend(serenity_flags)
117+
118+
with open('/tmp/x', 'w') as fp:
119+
import json
120+
json.dump(final_flags, fp)
121+
122+
return {
123+
'flags': final_flags,
124+
'include_paths_relative_to_dir': DIR_OF_THIS_SCRIPT,
125+
'override_filename': filename
126+
}

Documentation/VimConfiguration.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Vim Project Configuration
2+
3+
Vim can be configured to use the [YouCompleteMe](https://github.com/ycm-core/YouCompleteMe)
4+
plugin to provide code-completion.
5+
6+
Once you've installed the YouCompleteMe plugin you should whitelist the
7+
configuration file that is part of the SerenityOS repository. You can
8+
do this by adding the following option to your `.vimrc` file:
9+
10+
```vim
11+
let g:ycm_extra_conf_globlist = ['~/serenity/.ycm_extra_conf.py']
12+
```

0 commit comments

Comments
 (0)