Skip to content

Commit

Permalink
Filter and sort candidates when query is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
micbou committed Aug 25, 2017
1 parent 4480708 commit 1754b66
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 23 deletions.
2 changes: 1 addition & 1 deletion CORE_VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
28
29
5 changes: 3 additions & 2 deletions cpp/ycm/IdentifierDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void IdentifierDatabase::ResultsForQueryAndType(
std::lock_guard< std::mutex > locker( filetype_candidate_map_mutex_ );
it = filetype_candidate_map_.find( filetype );

if ( it == filetype_candidate_map_.end() || query.empty() )
if ( it == filetype_candidate_map_.end() )
return;
}
Bitset query_bitset = LetterBitsetFromString( query );
Expand All @@ -93,7 +93,8 @@ void IdentifierDatabase::ResultsForQueryAndType(
else
seen_candidates.insert( candidate );

if ( !candidate->MatchesQueryBitset( query_bitset ) )
if ( candidate->Text().empty() ||
!candidate->MatchesQueryBitset( query_bitset ) )
continue;

Result result = candidate->QueryMatchResult(
Expand Down
6 changes: 2 additions & 4 deletions cpp/ycm/PythonSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ boost::python::list FilterAndSortCandidates(
const std::string &query ) {
pylist filtered_candidates;

if ( query.empty() )
return candidates;

if ( !IsPrintable( query ) )
return boost::python::list();

Expand All @@ -84,7 +81,8 @@ boost::python::list FilterAndSortCandidates(
for ( int i = 0; i < num_candidates; ++i ) {
const Candidate *candidate = repository_candidates[ i ];

if ( !candidate->MatchesQueryBitset( query_bitset ) )
if ( candidate->Text().empty() ||
!candidate->MatchesQueryBitset( query_bitset ) )
continue;

Result result = candidate->QueryMatchResult( query,
Expand Down
15 changes: 11 additions & 4 deletions cpp/ycm/tests/IdentifierCompleter_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,19 @@ using ::testing::WhenSorted;
namespace YouCompleteMe {


// This differs from what we expect from the ClangCompleter. That one should
// return results for an empty query.
TEST( IdentifierCompleterTest, EmptyQueryNoResults ) {
TEST( IdentifierCompleterTest, SortOnEmptyQuery ) {
EXPECT_THAT( IdentifierCompleter(
StringVector(
"foobar" ) ).CandidatesForQuery( "" ),
"foo",
"bar" ) ).CandidatesForQuery( "" ),
ElementsAre( "bar",
"foo" ) );
}

TEST( IdentifierCompleterTest, IgnoreEmptyCandidate ) {
EXPECT_THAT( IdentifierCompleter(
StringVector(
"" ) ).CandidatesForQuery( "" ),
IsEmpty() );
}

Expand Down
5 changes: 1 addition & 4 deletions ycmd/completers/completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,7 @@ def ComputeCandidates( self, request_data ):
return []

candidates = self._GetCandidatesFromSubclass( request_data )
if request_data[ 'query' ]:
candidates = self.FilterAndSortCandidates( candidates,
request_data[ 'query' ] )
return candidates
return self.FilterAndSortCandidates( candidates, request_data[ 'query' ] )


def _GetCandidatesFromSubclass( self, request_data ):
Expand Down
8 changes: 5 additions & 3 deletions ycmd/tests/clang/get_completions_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,9 @@ def GetCompletions_FilenameCompleter_ClientDataGivenToExtraConf_test( app ):
)


@ExpectedFailure( 'Filtering and sorting does not support candidates with '
'non-ASCII characters.',
contains_string( "value for 'completions' no item matches" ) )
@SharedYcmd
def GetCompletions_UnicodeInLine_test( app ):
RunTest( app, {
Expand Down Expand Up @@ -536,9 +539,8 @@ def GetCompletions_UnicodeInLine_test( app ):
} )


@ExpectedFailure( 'Filtering and sorting does not work when the candidate '
'contains non-ASCII characters. This is due to the way '
'the filtering and sorting code works.',
@ExpectedFailure( 'Filtering and sorting does not support candidates with '
'non-ASCII characters.',
contains_string( "value for 'completions' no item matches" ) )
@SharedYcmd
def GetCompletions_UnicodeInLineFilter_test( app ):
Expand Down
12 changes: 12 additions & 0 deletions ycmd/tests/completer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ def FilterAndSortCandidates_ServerCompleter_test():
[ { 'insertion_text': 'password' } ] )


def FilterAndSortCandidates_SortOnEmptyQuery_test():
_FilterAndSortCandidates_Match( [ 'foo', 'bar' ],
'',
[ 'bar', 'foo' ] )


def FilterAndSortCandidates_IgnoreEmptyCandidate_test():
_FilterAndSortCandidates_Match( [ '' ],
'',
[] )


@ExpectedFailure( 'Filtering does not support unicode characters',
contains_string( '[]' ) )
def FilterAndSortCandidates_Unicode_test():
Expand Down
12 changes: 9 additions & 3 deletions ycmd/tests/cs/get_completions_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@
# Not installing aliases from python-future; it's unreliable and slow.
from builtins import * # noqa

from hamcrest import ( assert_that, calling, empty, greater_than, has_item,
has_items, has_entries, raises )
from hamcrest import ( assert_that, calling, contains_string, empty,
greater_than, has_item, has_items, has_entries, raises )
from nose.tools import eq_
from webtest import AppError

from ycmd.tests.cs import PathToTestFile, SharedYcmd, WrapOmniSharpServer
from ycmd.tests.test_utils import BuildRequest, CompletionEntryMatcher
from ycmd.tests.test_utils import ( BuildRequest, CompletionEntryMatcher,
ExpectedFailure )
from ycmd.utils import ReadFile


Expand All @@ -52,6 +53,11 @@ def GetCompletions_Basic_test( app ):
eq_( 12, response_data[ 'completion_start_column' ] )


@ExpectedFailure( 'Filtering and sorting does not support candidates with '
'non-ASCII characters.',
contains_string( "but: a sequence containing a dictionary "
"containing {'insertion_text': 'a_unicøde'} "
"was" ) )
@SharedYcmd
def GetCompletions_Unicode_test( app ):
filepath = PathToTestFile( 'testy', 'Unicode.cs' )
Expand Down
10 changes: 8 additions & 2 deletions ycmd/tests/go/get_completions_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
# Not installing aliases from python-future; it's unreliable and slow.
from builtins import * # noqa

from hamcrest import assert_that, has_item, has_items
from hamcrest import assert_that, contains_string, has_item, has_items

from ycmd.tests.go import PathToTestFile, SharedYcmd
from ycmd.tests.test_utils import BuildRequest, CompletionEntryMatcher
from ycmd.tests.test_utils import ( BuildRequest, CompletionEntryMatcher,
ExpectedFailure )
from ycmd.utils import ReadFile


Expand Down Expand Up @@ -64,6 +65,11 @@ def GetCompletions_Unicode_InLine_test( app ):
CompletionEntryMatcher( u'Sprintf' ) ) )


@ExpectedFailure( 'Filtering and sorting does not support candidates with '
'non-ASCII characters.',
contains_string( "but: a sequence containing a dictionary "
"containing {'insertion_text': 'Unicøde'} "
"was <[]>" ) )
@SharedYcmd
def GetCompletions_Unicode_Identifier_test( app ):
filepath = PathToTestFile( 'unicode.go' )
Expand Down

0 comments on commit 1754b66

Please sign in to comment.