gecko-dev/.ycm_extra_conf.py
Emilio Cobos Álvarez f032c0c3c9 Bug 1646256 - Make rust-analyzer and YouCompleteMe work with mozilla-central. r=sylvestre
The default configuration recommended here[1], while being a good
generic default, it doesn't quite work for mozilla-central as that peeks
the closest ancestor directory with a `Cargo.toml`.

For example, when editing under `servo/components/style`, it'd try to
use that directory as a project root and fail like:

```
    0: Failed to run `cargo metadata --manifest-path /home/emilio/src/moz/gecko/servo/components/style/Cargo.toml`
    1: Error during execution of `cargo metadata`:     Updating crates.io index
       error: failed to get `servo_atoms` as a dependency of package `style v0.0.1 (/home/emilio/src/moz/gecko/servo/components/style)`
       Caused by:
         failed to load source for dependency `servo_atoms`
```

Instead, we want to override it using the `'project_directory'` setting
so that we hit this path[2] and it works out of the box.

[1]: https://rust-analyzer.github.io/manual.html#youcompleteme
[2]: 63db7ea379/ycmd/completers/language_server/language_server_completer.py (L1914-L1916)

Differential Revision: https://phabricator.services.mozilla.com/D79938
2020-06-17 17:53:56 +00:00

68 lines
2.0 KiB
Python

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import absolute_import, print_function
import json
import os
import shlex
import subprocess
import sys
old_bytecode = sys.dont_write_bytecode
sys.dont_write_bytecode = True
path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'mach'))
# If mach is not here, we're on the objdir go to the srcdir.
if not os.path.exists(path):
with open(os.path.join(os.path.dirname(__file__), 'mozinfo.json')) as info:
config = json.loads(info.read())
path = os.path.join(config['topsrcdir'], 'mach')
sys.dont_write_bytecode = old_bytecode
def _is_likely_cpp_header(filename):
if not filename.endswith('.h'):
return False
if filename.endswith('Inlines.h') or filename.endswith('-inl.h'):
return True
cpp_file = filename[:-1] + 'cpp'
return os.path.exists(cpp_file)
def Settings(**kwargs):
if kwargs[ 'language' ] == 'cfamily':
return FlagsForFile(kwargs['filename'])
# This is useful for generic language server protocols, like rust-analyzer,
# to discover the right project root instead of guessing based on where the
# closest Cargo.toml is.
return {
'project_directory': '.',
}
def FlagsForFile(filename):
output = subprocess.check_output([path, 'compileflags', filename])
output = output.decode('utf-8')
flag_list = shlex.split(output)
# This flag is added by Fennec for android build and causes ycmd to fail to parse the file.
# Removing this flag is a workaround until ycmd starts to handle this flag properly.
# https://github.com/Valloric/YouCompleteMe/issues/1490
final_flags = [x for x in flag_list if not x.startswith('-march=armv')]
if _is_likely_cpp_header(filename):
final_flags += ["-x", "c++"]
return {
'flags': final_flags,
'do_cache': True
}
if __name__ == '__main__':
print(FlagsForFile(sys.argv[1]))