diff --git a/python/ycm/diagnostic_filter.py b/python/ycm/diagnostic_filter.py index 24936dd2..67ac8c38 100644 --- a/python/ycm/diagnostic_filter.py +++ b/python/ycm/diagnostic_filter.py @@ -1,4 +1,4 @@ -# Copyright (C) 2013 Google Inc. +# Copyright (C) 2016 YouCompleteMe contributors # # This file is part of YouCompleteMe. # @@ -23,11 +23,7 @@ from future import standard_library standard_library.install_aliases() from builtins import * # noqa -from future.utils import itervalues, iteritems -from collections import defaultdict, namedtuple -from ycm import vimsupport import re -import vim class DiagnosticFilter( object ): @@ -35,14 +31,14 @@ class DiagnosticFilter( object ): self._filters = [] for filter_type in config.iterkeys(): - wrapper = lambda x: x + wrapper = _AsIs actual_filter_type = filter_type if filter_type[0] == '!': wrapper = _Not filter_type = filter_type[1:] compiler = FILTER_COMPILERS.get( filter_type ) - + if compiler is not None: for filter_config in _ListOf( config[ actual_filter_type ] ): fn = wrapper( compiler( filter_config ) ) @@ -56,7 +52,7 @@ class DiagnosticFilter( object ): if f( diagnostic ): return False - return True + return True @staticmethod @@ -64,18 +60,22 @@ class DiagnosticFilter( object ): base = dict( user_options.get( 'quiet_messages', {} ) ) for filetype in filetypes: - type_specific = user_options.get( filetype + '_quiet_messages', {} ) + type_specific = user_options.get( filetype + '_quiet_messages', {} ) base.update( type_specific ) return DiagnosticFilter( base ) def _ListOf( config_entry ): - if type( config_entry ) == type( [] ): + if isinstance( config_entry, list ): return config_entry return [ config_entry ] +def _AsIs( fn ): + return fn + + def _Not( fn ): def Inverted( diagnostic ): return not fn( diagnostic ) @@ -93,7 +93,7 @@ def _CompileRegex( raw_regex ): def _CompileLevel( level ): - # valid kinds are WARNING and ERROR; + # valid kinds are WARNING and ERROR; # expected input levels are `warnings` and `errors` # NB: we don't validate the input... expected_kind = level.upper()[:-1] diff --git a/python/ycm/diagnostic_interface.py b/python/ycm/diagnostic_interface.py index 089eac2d..e0e349b3 100644 --- a/python/ycm/diagnostic_interface.py +++ b/python/ycm/diagnostic_interface.py @@ -66,8 +66,11 @@ class DiagnosticInterface( object ): def UpdateWithNewDiagnostics( self, diags ): - diag_filter = DiagnosticFilter.from_filetype( self._user_options, vimsupport.CurrentFiletypes() ) - normalized_diags = [ _NormalizeDiagnostic( x ) for x in diags if diag_filter.Accept(x) ] + diag_filter = DiagnosticFilter.from_filetype( + self._user_options, + vimsupport.CurrentFiletypes() ) + normalized_diags = [ _NormalizeDiagnostic( x ) for x in diags + if diag_filter.Accept(x) ] self._buffer_number_to_line_to_diags = _ConvertDiagListToDict( normalized_diags ) diff --git a/python/ycm/tests/diagnostic_filter_tests.py b/python/ycm/tests/diagnostic_filter_tests.py index dc84bcf9..c3126940 100644 --- a/python/ycm/tests/diagnostic_filter_tests.py +++ b/python/ycm/tests/diagnostic_filter_tests.py @@ -1,4 +1,4 @@ -# Copyright (C) 2013 Google Inc. +# Copyright (C) 2016 YouCompleteMe contributors # # This file is part of YouCompleteMe. # @@ -26,20 +26,21 @@ from builtins import * # noqa from ycm.test_utils import MockVimModule MockVimModule() -import os from hamcrest import assert_that, equal_to from ycm.diagnostic_filter import DiagnosticFilter def _assert_accept_equals( filter, text_or_obj, expected ): - if type( text_or_obj ) is not type( {} ): + if not isinstance( text_or_obj, dict ): text_or_obj = { 'text': text_or_obj } assert_that( filter.Accept( text_or_obj ), equal_to( expected ) ) + def _assert_accepts( filter, text ): _assert_accept_equals( filter, text, True ) + def _assert_rejects( filter, text ): _assert_accept_equals( filter, text, False ) @@ -111,9 +112,9 @@ class Level_test(): opts = { 'quiet_messages' : { 'level': 'warnings' } } f = DiagnosticFilter.from_filetype( opts, [ 'java' ] ) - _assert_rejects( f, { 'text': 'This is an unimportant taco', + _assert_rejects( f, { 'text': 'This is an unimportant taco', 'kind': 'WARNING' } ) - _assert_accepts( f, { 'text': 'This taco will be shown', + _assert_accepts( f, { 'text': 'This taco will be shown', 'kind': 'ERROR' } ) @@ -121,7 +122,7 @@ class Level_test(): opts = { 'quiet_messages' : { 'level': 'errors' } } f = DiagnosticFilter.from_filetype( opts, [ 'java' ] ) - _assert_accepts( f, { 'text': 'This is an IMPORTANT taco', + _assert_accepts( f, { 'text': 'This is an IMPORTANT taco', 'kind': 'WARNING' } ) - _assert_rejects( f, { 'text': 'This taco will NOT be shown', + _assert_rejects( f, { 'text': 'This taco will NOT be shown', 'kind': 'ERROR' } )