Adding new candidates is now faster; + more

Also fixed a memory leak and started work on adding filepath-aware candidate
repos.
This commit is contained in:
Strahinja Val Markovic 2012-04-29 16:36:31 -07:00
parent 7627f87756
commit ced1d0ad5a
4 changed files with 56 additions and 12 deletions

View File

@ -17,33 +17,51 @@
#include "standard.h" #include "standard.h"
#include "Completer.h" #include "Completer.h"
#include "Utils.h"
namespace YouCompleteMe namespace YouCompleteMe
{ {
Completer::Completer( const Pylist &candidates ) Completer::Completer( const Pylist &candidates )
{ {
AddCandidatesToDatabase( candidates ); AddCandidatesToDatabase( candidates, "" );
} }
Completer::Completer( const Pylist &candidates, const std::string &filepath)
{
AddCandidatesToDatabase( candidates, filepath );
}
Completer::~Completer() Completer::~Completer()
{ {
foreach ( Candidate* candidate, candidates_ ) foreach ( const CandidateRepository::value_type &pair,
candidate_repository_ )
{ {
delete candidate; delete pair.second;
} }
} }
void Completer::AddCandidatesToDatabase(
const Pylist &candidates ) void Completer::AddCandidatesToDatabase( const Pylist &candidates,
const std::string &filepath )
{ {
std::string candidate_text;
for (int i = 0; i < boost::python::len( candidates ); ++i) for (int i = 0; i < boost::python::len( candidates ); ++i)
{ {
candidates_.insert( new Candidate( candidate_text = boost::python::extract< std::string >( candidates[ i ] );
boost::python::extract< std::string >( candidates[ i ] ) ) ); Candidate *&candidate = GetValueElseInsert( candidate_repository_,
candidate_text, NULL );
if ( !candidate )
{
candidate = new Candidate( candidate_text );
candidates_.insert( candidate );
}
} }
} }
void Completer::GetCandidatesForQuery( void Completer::GetCandidatesForQuery(
const std::string &query, Pylist &candidates ) const const std::string &query, Pylist &candidates ) const
{ {
@ -70,4 +88,5 @@ void Completer::GetCandidatesForQuery(
} }
} }
} // namespace YouCompleteMe } // namespace YouCompleteMe

View File

@ -22,6 +22,7 @@
#include <boost/utility.hpp> #include <boost/utility.hpp>
#include <boost/python.hpp> #include <boost/python.hpp>
#include <boost/unordered_map.hpp>
#include <set> #include <set>
#include <vector> #include <vector>
@ -31,6 +32,7 @@ namespace YouCompleteMe
{ {
typedef boost::python::list Pylist; typedef boost::python::list Pylist;
typedef boost::unordered_map< std::string, Candidate* > CandidateRepository;
// class Completer : boost::noncopyable // class Completer : boost::noncopyable
class Completer class Completer
@ -38,9 +40,11 @@ class Completer
public: public:
Completer() {} Completer() {}
Completer( const Pylist &candidates ); Completer( const Pylist &candidates );
Completer( const Pylist &candidates, const std::string &filepath );
~Completer(); ~Completer();
void AddCandidatesToDatabase( const Pylist &candidates ); void AddCandidatesToDatabase( const Pylist &candidates,
const std::string &filepath );
void GetCandidatesForQuery( void GetCandidatesForQuery(
const std::string &query, Pylist &candidates ) const; const std::string &query, Pylist &candidates ) const;
@ -54,6 +58,7 @@ private:
} }
}; };
CandidateRepository candidate_repository_;
std::set< Candidate*, CandidatePointerLess > candidates_; std::set< Candidate*, CandidatePointerLess > candidates_;
}; };

View File

@ -27,6 +27,22 @@ namespace YouCompleteMe
bool AlmostEqual( double a, double b ); bool AlmostEqual( double a, double b );
template <class Container, class Key>
typename Container::mapped_type &
GetValueElseInsert( Container &container,
Key const& key,
typename Container::mapped_type const& value )
{
return container.insert( typename Container::value_type( key, value ) )
.first->second;
}
template <class Container, class Key>
bool ContainsKey( Container &container, Key const& key)
{
return container.find( key ) != container.end();
}
} // namespace YouCompleteMe } // namespace YouCompleteMe
#endif /* end of include guard: UTILS_H_KEPMRPBH */ #endif /* end of include guard: UTILS_H_KEPMRPBH */

View File

@ -35,10 +35,11 @@ class CompletionSystem( object ):
def AddBufferIdentifiers( self ): def AddBufferIdentifiers( self ):
text = "\n".join( vim.current.buffer ) text = "\n".join( vim.current.buffer )
text = RemoveCppComments( text ) text = RemoveIdentFreeText( text )
idents = re.findall( self.pattern, text ) idents = re.findall( self.pattern, text )
self.completer.AddCandidatesToDatabase( idents ) filepath = vim.eval( "expand('%:p')" )
self.completer.AddCandidatesToDatabase( idents, filepath )
def CurrentColumn(): def CurrentColumn():
# vim's columns start at 1 while vim.current.line columns start at 0 # vim's columns start at 1 while vim.current.line columns start at 0
@ -74,7 +75,10 @@ def CurrentCursorText():
def SanitizeQuery( query ): def SanitizeQuery( query ):
return query.strip() return query.strip()
def RemoveCppComments( text ): def RemoveIdentFreeText( text ):
"""Removes commented-out code and code in quotes."""
# TODO: do we still need this sub-func?
def replacer( match ): def replacer( match ):
s = match.group( 0 ) s = match.group( 0 )
if s.startswith( '/' ): if s.startswith( '/' ):
@ -83,7 +87,7 @@ def RemoveCppComments( text ):
return s return s
pattern = re.compile( pattern = re.compile(
r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"', r'//.*?$|#.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"',
re.DOTALL | re.MULTILINE ) re.DOTALL | re.MULTILINE )
return re.sub( pattern, replacer, text ) return re.sub( pattern, replacer, text )