diff --git a/cpp/Completer.cpp b/cpp/Completer.cpp index 73f20068..2ee50ff9 100644 --- a/cpp/Completer.cpp +++ b/cpp/Completer.cpp @@ -53,14 +53,14 @@ void ThreadMain( TaskStack &task_stack ) } // unnamed namespace -Completer::Completer( const Pylist &candidates ) +Completer::Completer( const std::vector< std::string > &candidates ) : threading_enabled_( false ) { AddCandidatesToDatabase( candidates, "", "" ); } -Completer::Completer( const Pylist &candidates, +Completer::Completer( const std::vector< std::string > &candidates, const std::string &filetype, const std::string &filepath ) : threading_enabled_( false ) @@ -91,18 +91,35 @@ void Completer::EnableThreading() void Completer::AddCandidatesToDatabase( const Pylist &new_candidates, const std::string &filetype, const std::string &filepath ) +{ + int num_candidates = len( new_candidates ); + std::vector< std::string > candidates; + candidates.reserve( num_candidates ); + + for (int i = 0; i < num_candidates; ++i) + { + candidates.push_back( extract< std::string >( new_candidates[ i ] ) ); + } + + AddCandidatesToDatabase( candidates, filetype, filepath ); +} + + +void Completer::AddCandidatesToDatabase( + const std::vector< std::string > &new_candidates, + const std::string &filetype, + const std::string &filepath ) { std::vector< Candidate *> &candidates = GetCandidateVector( filetype, filepath ); - int num_candidates = len( new_candidates ); + int num_candidates = new_candidates.size(); candidates.clear(); candidates.reserve( num_candidates ); - std::string candidate_text; for (int i = 0; i < num_candidates; ++i) { - candidate_text = extract< std::string >( new_candidates[ i ] ); + const std::string &candidate_text = new_candidates[ i ]; Candidate *&candidate = GetValueElseInsert( candidate_repository_, candidate_text, NULL ); if ( !candidate ) @@ -113,24 +130,26 @@ void Completer::AddCandidatesToDatabase( const Pylist &new_candidates, } -void Completer::CandidatesForQuery( const std::string &query, - Pylist &candidates ) const +std::vector< std::string > Completer::CandidatesForQuery( + const std::string &query ) const { - CandidatesForQueryAndType( query, "", candidates ); + return CandidatesForQueryAndType( query, "" ); } -void Completer::CandidatesForQueryAndType( const std::string &query, - const std::string &filetype, - Pylist &candidates ) const +std::vector< std::string > Completer::CandidatesForQueryAndType( + const std::string &query, + const std::string &filetype ) const { std::vector< Result > results; ResultsForQueryAndType( query, filetype, results ); + std::vector< std::string > candidates; foreach ( const Result& result, results ) { - candidates.append( *result.Text() ); + candidates.push_back( *result.Text() ); } + return candidates; } @@ -156,6 +175,7 @@ Future Completer::CandidatesForQueryAndTypeAsync( return Future( move( future ) ); } + AsyncResults Completer::ResultsForQueryAndType( const std::string &query, const std::string &filetype ) const @@ -165,6 +185,7 @@ AsyncResults Completer::ResultsForQueryAndType( return results; } + void Completer::ResultsForQueryAndType( const std::string &query, const std::string &filetype, std::vector< Result > &results ) const diff --git a/cpp/Completer.h b/cpp/Completer.h index 12e8cfe3..532673b2 100644 --- a/cpp/Completer.h +++ b/cpp/Completer.h @@ -56,25 +56,30 @@ class Completer : boost::noncopyable { public: Completer() {} - Completer( const Pylist &candidates ); - Completer( const Pylist &candidates, + Completer( const std::vector< std::string > &candidates ); + Completer( const std::vector< std::string > &candidates, const std::string &filetype, const std::string &filepath ); ~Completer(); void EnableThreading(); + void AddCandidatesToDatabase( + const std::vector< std::string > &new_candidates, + const std::string &filetype, + const std::string &filepath ); + void AddCandidatesToDatabase( const Pylist &new_candidates, const std::string &filetype, const std::string &filepath ); // Only provided for tests! - void CandidatesForQuery( const std::string &query, - Pylist &candidates ) const; + std::vector< std::string > CandidatesForQuery( + const std::string &query ) const; - void CandidatesForQueryAndType( const std::string &query, - const std::string &filetype, - Pylist &candidates ) const; + std::vector< std::string > CandidatesForQueryAndType( + const std::string &query, + const std::string &filetype ) const; Future CandidatesForQueryAndTypeAsync( const std::string &query, const std::string &filetype ) const; diff --git a/cpp/indexer.cpp b/cpp/indexer.cpp index 372bd9e9..707dfe20 100644 --- a/cpp/indexer.cpp +++ b/cpp/indexer.cpp @@ -30,9 +30,14 @@ BOOST_PYTHON_MODULE(indexer) .def( "ResultsReady", &Future::ResultsReady ) .def( "GetResults", &Future::GetResults ); + void (Completer::*actd) (const Pylist&, + const std::string&, + const std::string&) = + &Completer::AddCandidatesToDatabase; + class_< Completer, boost::noncopyable >( "Completer" ) .def( "EnableThreading", &Completer::EnableThreading ) - .def( "AddCandidatesToDatabase", &Completer::AddCandidatesToDatabase ) + .def( "AddCandidatesToDatabase", actd ) .def( "CandidatesForQueryAndTypeAsync", &Completer::CandidatesForQueryAndTypeAsync ); } diff --git a/cpp/tests/Completer_test.cpp b/cpp/tests/Completer_test.cpp index 06c9e1ac..cbf87f10 100644 --- a/cpp/tests/Completer_test.cpp +++ b/cpp/tests/Completer_test.cpp @@ -30,189 +30,134 @@ namespace YouCompleteMe namespace { -std::vector ToStringVector( const boost::python::list &pylist ) +std::vector< std::string > Candidates( const std::string &a, + const std::string &b = std::string(), + const std::string &c = std::string(), + const std::string &d = std::string(), + const std::string &e = std::string(), + const std::string &f = std::string(), + const std::string &g = std::string(), + const std::string &h = std::string(), + const std::string &i = std::string() ) { - std::vector values; - for (int i = 0; i < boost::python::len( pylist ); ++i) - { - values.push_back( - boost::python::extract< std::string >( pylist[ i ] ) ); - } - - return values; -} - -Pylist Candidates( const std::string &a, - const std::string &b = std::string(), - const std::string &c = std::string(), - const std::string &d = std::string(), - const std::string &e = std::string(), - const std::string &f = std::string(), - const std::string &g = std::string(), - const std::string &h = std::string(), - const std::string &i = std::string() ) -{ - Pylist candidates; - candidates.append( a ); + std::vector< std::string > candidates; + candidates.push_back( a ); if ( !b.empty() ) - candidates.append( b ); + candidates.push_back( b ); if ( !c.empty() ) - candidates.append( c ); + candidates.push_back( c ); if ( !d.empty() ) - candidates.append( d ); + candidates.push_back( d ); if ( !e.empty() ) - candidates.append( e ); + candidates.push_back( e ); if ( !f.empty() ) - candidates.append( f ); + candidates.push_back( f ); if ( !g.empty() ) - candidates.append( g ); + candidates.push_back( g ); if ( !h.empty() ) - candidates.append( h ); + candidates.push_back( h ); if ( !i.empty() ) - candidates.append( i ); + candidates.push_back( i ); return candidates; } } // unnamed namespace -class CompleterTest : public ::testing::Test + +TEST( CompleterTest, OneCandidate ) { - protected: - virtual void SetUp() - { - Py_Initialize(); - } -}; - - -TEST_F( CompleterTest, OneCandidate ) -{ - Pylist results; - Completer( Candidates( "foobar" ) ).CandidatesForQuery( "fbr", results ); - - EXPECT_THAT( ToStringVector( results ), ElementsAre( "foobar" ) ); + EXPECT_THAT( Completer( Candidates( + "foobar" ) ).CandidatesForQuery( "fbr" ), + ElementsAre( "foobar" ) ); } -TEST_F( CompleterTest, ManyCandidateSimple ) +TEST( CompleterTest, ManyCandidateSimple ) { - Pylist results; - Completer( Candidates( - "foobar", - "foobartest", - "Foobartest" ) ).CandidatesForQuery( "fbr", results ); - - EXPECT_THAT( ToStringVector( results ), + EXPECT_THAT( Completer( Candidates( + "foobar", + "foobartest", + "Foobartest" ) ).CandidatesForQuery( "fbr" ), WhenSorted( ElementsAre( "Foobartest", "foobar", "foobartest" ) ) ); } -TEST_F( CompleterTest, FirstCharSameAsQueryWins ) +TEST( CompleterTest, FirstCharSameAsQueryWins ) { - Pylist results; - Completer( Candidates( - "foobar", - "afoobar" ) ).CandidatesForQuery( "fbr", results ); - - EXPECT_THAT( ToStringVector( results ), + EXPECT_THAT( Completer( Candidates( + "foobar", + "afoobar" ) ).CandidatesForQuery( "fbr" ), ElementsAre( "foobar", "afoobar" ) ); } -TEST_F( CompleterTest, CompleteMatchForWordBoundaryCharsWins ) +TEST( CompleterTest, CompleteMatchForWordBoundaryCharsWins ) { - Pylist results; - Completer( Candidates( - "FooBarQux", - "FBaqux" ) ).CandidatesForQuery( "fbq", results ); - - EXPECT_THAT( ToStringVector( results ), + EXPECT_THAT( Completer( Candidates( + "FooBarQux", + "FBaqux" ) ).CandidatesForQuery( "fbq" ), ElementsAre( "FooBarQux", "FBaqux" ) ); - Pylist results2; - Completer( Candidates( - "CompleterTest", - "CompleteMatchForWordBoundaryCharsWins" - ) ).CandidatesForQuery( "ct", results2 ); - - EXPECT_THAT( ToStringVector( results2 ), + EXPECT_THAT( Completer( Candidates( + "CompleterTest", + "CompleteMatchForWordBoundaryCharsWins" ) ) + .CandidatesForQuery( "ct" ), ElementsAre( "CompleterTest", "CompleteMatchForWordBoundaryCharsWins" ) ); - Pylist results3; - Completer( Candidates( - "FooBar", - "FooBarRux" ) ).CandidatesForQuery( "fbr", results3 ); - - EXPECT_THAT( ToStringVector( results3 ), + EXPECT_THAT( Completer( Candidates( + "FooBar", + "FooBarRux" ) ).CandidatesForQuery( "fbr" ), ElementsAre( "FooBarRux", "FooBar" ) ); } -TEST_F( CompleterTest, RatioUtilizationTieBreak ) +TEST( CompleterTest, RatioUtilizationTieBreak ) { - Pylist results; - Completer( Candidates( - "FooBarQux", - "FooBarQuxZaa" ) ).CandidatesForQuery( "fbq", results ); - - EXPECT_THAT( ToStringVector( results ), + EXPECT_THAT( Completer( Candidates( + "FooBarQux", + "FooBarQuxZaa" ) ).CandidatesForQuery( "fbq" ), ElementsAre( "FooBarQux", "FooBarQuxZaa" ) ); - Pylist results2; - Completer( Candidates( - "FooBar", - "FooBarRux" ) ).CandidatesForQuery( "fba", results2 ); - - EXPECT_THAT( ToStringVector( results2 ), + EXPECT_THAT( Completer( Candidates( + "FooBar", + "FooBarRux" ) ).CandidatesForQuery( "fba" ), ElementsAre( "FooBar", "FooBarRux" ) ); } -TEST_F( CompleterTest, QueryPrefixOfCandidateWins ) +TEST( CompleterTest, QueryPrefixOfCandidateWins ) { - Pylist results; - Completer( Candidates( - "foobar", - "fbaroo" ) ).CandidatesForQuery( "foo", results ); - - EXPECT_THAT( ToStringVector( results ), + EXPECT_THAT( Completer( Candidates( + "foobar", + "fbaroo" ) ).CandidatesForQuery( "foo" ), ElementsAre( "foobar", "fbaroo" ) ); } -TEST_F( CompleterTest, ShorterCandidateWins ) +TEST( CompleterTest, ShorterCandidateWins ) { - Pylist results; - Completer( Candidates( - "FooBarQux", - "FaBarQux" ) ).CandidatesForQuery( "fbq", results ); - - EXPECT_THAT( ToStringVector( results ), + EXPECT_THAT( Completer( Candidates( + "FooBarQux", + "FaBarQux" ) ).CandidatesForQuery( "fbq" ), ElementsAre( "FaBarQux", "FooBarQux" ) ); - Pylist results2; - Completer( Candidates( - "CompleterT", - "CompleterTest" ) ).CandidatesForQuery( "co", results2 ); - - EXPECT_THAT( ToStringVector( results2 ), + EXPECT_THAT( Completer( Candidates( + "CompleterT", + "CompleterTest" ) ).CandidatesForQuery( "co" ), ElementsAre( "CompleterT", "CompleterTest" ) ); } -TEST_F( CompleterTest, SameLowercaseCandidateWins ) +TEST( CompleterTest, SameLowercaseCandidateWins ) { - Pylist results; - Completer( Candidates( - "foobar", - "Foobar" ) ).CandidatesForQuery( "foo", results ); - - EXPECT_THAT( ToStringVector( results ), + EXPECT_THAT( Completer( Candidates( + "foobar", + "Foobar" ) ).CandidatesForQuery( "foo" ), ElementsAre( "foobar", "Foobar" ) ); }