Skip to content

Commit

Permalink
WIP: Ability to ignore extra conf files
Browse files Browse the repository at this point in the history
  • Loading branch information
puremourning committed Apr 10, 2020
1 parent c86b061 commit 7e20d1e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 14 deletions.
11 changes: 9 additions & 2 deletions ycmd/completers/cpp/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
CLANG_RESOURCE_DIR )
from ycmd.responses import NoExtraConfDetected
ycm_core = ImportCore()
from ycmd.extra_conf_support import IgnoreExtraConf

# -include-pch and --sysroot= must be listed before -include and --sysroot
# respectively because the latter is a prefix of the former (and the algorithm
Expand Down Expand Up @@ -165,7 +166,10 @@ def _GetFlagsFromExtraConfOrDatabase( self, filename, client_data ):
# Load the flags from the extra conf file if one is found and is not global.
module = extra_conf_store.ModuleForSourceFile( filename )
if module and not extra_conf_store.IsGlobalExtraConfModule( module ):
return _CallExtraConfFlagsForFile( module, filename, client_data )
try:
return _CallExtraConfFlagsForFile( module, filename, client_data )
except IgnoreExtraConf:
pass

# Load the flags from the compilation database if any.
database = self.LoadCompilationDatabase( filename )
Expand All @@ -174,7 +178,10 @@ def _GetFlagsFromExtraConfOrDatabase( self, filename, client_data ):

# Load the flags from the global extra conf if set.
if module:
return _CallExtraConfFlagsForFile( module, filename, client_data )
try:
return _CallExtraConfFlagsForFile( module, filename, client_data )
except IgnoreExtraConf:
pass

# No compilation database and no extra conf found. Warn the user if not
# already warned.
Expand Down
13 changes: 9 additions & 4 deletions ycmd/completers/language_server/language_server_completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from ycmd.completers.completer import Completer, CompletionsCache
from ycmd.completers.completer_utils import GetFileContents, GetFileLines
from ycmd.utils import LOGGER
from ycmd.extra_conf_support import IgnoreExtraConf

from ycmd.completers.language_server import language_server_protocol as lsp

Expand Down Expand Up @@ -1416,10 +1417,14 @@ def DefaultSettings( self, request_data ):

def GetSettings( self, module, request_data ):
if hasattr( module, 'Settings' ):
settings = module.Settings(
language = self.Language(),
filename = request_data[ 'filepath' ],
client_data = request_data[ 'extra_conf_data' ] )
try:
settings = module.Settings(
language = self.Language(),
filename = request_data[ 'filepath' ],
client_data = request_data[ 'extra_conf_data' ] )
except IgnoreExtraConf:
settings = None

if settings is not None:
return settings

Expand Down
21 changes: 13 additions & 8 deletions ycmd/completers/python/python_completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# along with ycmd. If not, see <http://www.gnu.org/licenses/>.

from ycmd import extra_conf_store, responses
from ycmd.extra_conf_support import IgnoreExtraConf
from ycmd.completers.completer import Completer, SignatureHelpAvailalability
from ycmd.utils import ( CodepointOffsetToByteOffset,
ExpandVariablesInPath,
Expand Down Expand Up @@ -71,14 +72,18 @@ def _SettingsForRequest( self, request_data ):

def _GetSettings( self, module, filepath, client_data ):
# We don't warn the user if no extra conf file is found.
if module:
if hasattr( module, 'Settings' ):
settings = module.Settings( language = 'python',
filename = filepath,
client_data = client_data )
if settings is not None:
return settings
LOGGER.debug( 'No Settings function defined in %s', module.__file__ )
try:
if module:
if hasattr( module, 'Settings' ):
settings = module.Settings( language = 'python',
filename = filepath,
client_data = client_data )
if settings is not None:
return settings
LOGGER.debug( 'No Settings function defined in %s', module.__file__ )
except IgnoreExtraConf:
pass

return {
# NOTE: this option is only kept for backward compatibility. Setting the
# Python interpreter path through the extra conf file is preferred.
Expand Down
46 changes: 46 additions & 0 deletions ycmd/extra_conf_support.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Copyright (C) 2011-2019 ycmd contributors
#
# This file is part of ycmd.
#
# ycmd is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ycmd is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ycmd. If not, see <http://www.gnu.org/licenses/>.

from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
# Not installing aliases from python-future; it's unreliable and slow.
from builtins import * # noqa


class IgnoreExtraConf( Exception ):
"""Raise this exception from within a FlagsForFile or Settings function to
instruct ycmd to ignore this module for the current file.
For example, if you wish to delegate to ycmd's built-in compilation database
support, you can write:
from ycmd.extra_conf_support import IgnoreExtraConf
def Settings( **kwargs ):
if kwargs[ 'language' ] == 'c-family':
# Use compilation database
raise IgnoreExtraConf()
if kwargs[ 'language' ] == 'python':
...
This will then tell ycmd to use your compile_commands.json, or global extra
conf as if this local module doens't exist.
"""
pass

0 comments on commit 7e20d1e

Please sign in to comment.