ClangComplete now sorts completions based on query

This commit is contained in:
Strahinja Val Markovic 2012-07-11 22:41:32 -07:00
parent 52b1b9f660
commit 27e1400558
5 changed files with 63 additions and 1 deletions

View File

@ -195,6 +195,7 @@ endfunction
function! s:ClangCompletion( query )
" TODO: don't trigger on a dot inside a string constant
py vim.command( 'let l:results = ' +
\ str( clangcomp.CandidatesForQuery( vim.eval( 'a:query' ) ) ) )

View File

@ -16,7 +16,10 @@
// along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
#include "ClangCompleter.h"
#include "Candidate.h"
#include "standard.h"
#include "CandidateRepository.h"
#include <clang-c/Index.h>
namespace YouCompleteMe
@ -88,6 +91,7 @@ std::vector< std::string > ToStringVector( CXCodeCompleteResults *results )
ClangCompleter::ClangCompleter()
: candidate_repository_( CandidateRepository::Instance() )
{
clang_index_ = clang_createIndex( 0, 0 );
}
@ -148,6 +152,7 @@ void ClangCompleter::UpdateTranslationUnit(
std::vector< std::string > ClangCompleter::CandidatesForLocationInFile(
const std::string &query,
const std::string &filename,
int line,
int column,
@ -176,6 +181,9 @@ std::vector< std::string > ClangCompleter::CandidatesForLocationInFile(
clang_defaultCodeCompleteOptions());
std::vector< std::string > completions = ToStringVector( results );
if ( !query.empty() )
completions = SortCandidatesForQuery( query, completions );
clang_disposeCodeCompleteResults( results );
return completions;
}
@ -237,4 +245,40 @@ CXTranslationUnit ClangCompleter::GetTranslationUnitForFile(
return unit;
}
std::vector< std::string > ClangCompleter::SortCandidatesForQuery(
const std::string &query,
const std::vector< std::string > &candidates )
{
Bitset query_bitset = LetterBitsetFromString( query );
std::vector< const Candidate* > repository_candidates =
candidate_repository_.GetCandidatesForStrings( candidates );
std::vector< Result > results;
// This loop needs to be a separate function
foreach ( const Candidate* candidate, repository_candidates )
{
if ( !candidate->MatchesQueryBitset( query_bitset ) )
continue;
Result result = candidate->QueryMatchResult( query );
if ( result.IsSubsequence() )
results.push_back( result );
}
std::sort( results.begin(), results.end() );
std::vector< std::string > sorted_candidates;
sorted_candidates.reserve( results.size() );
foreach ( const Result& result, results )
{
sorted_candidates.push_back( *result.Text() );
}
return sorted_candidates;
}
} // namespace YouCompleteMe

View File

@ -29,6 +29,8 @@ typedef struct CXTranslationUnitImpl *CXTranslationUnit;
namespace YouCompleteMe
{
class CandidateRepository;
struct UnsavedFile
{
UnsavedFile() : filename_( NULL ), contents_( NULL ), length_( 0 ) {}
@ -68,7 +70,9 @@ public:
void UpdateTranslationUnit( const std::string &filename,
const std::vector< UnsavedFile > &unsaved_files );
// TODO: rename this
std::vector< std::string > CandidatesForLocationInFile(
const std::string &query,
const std::string &filename,
int line,
int column,
@ -88,10 +92,20 @@ private:
const std::string &filename,
const std::vector< UnsavedFile > &unsaved_files );
std::vector< std::string > SortCandidatesForQuery(
const std::string &query,
const std::vector< std::string > &candidates );
/////////////////////////////
// PRIVATE MEMBER VARIABLES
/////////////////////////////
CXIndex clang_index_;
FlagsForFile flags_for_file_;
TranslationUnitForFilename filename_to_translation_unit_;
std::vector< std::string > global_flags_;
CandidateRepository &candidate_repository_;
};

View File

@ -127,6 +127,8 @@ std::vector< std::string > IdentifierCompleter::CandidatesForQueryAndType(
ResultsForQueryAndType( query, filetype, results );
std::vector< std::string > candidates;
candidates.reserve( results.size() );
foreach ( const Result& result, results )
{
candidates.push_back( *result.Text() );

View File

@ -119,7 +119,8 @@ class ClangCompleter( object ):
line, _ = vim.current.window.cursor
column = int( vim.eval( "s:completion_start_column" ) ) + 1
current_buffer = vim.current.buffer
results = self.completer.CandidatesForLocationInFile( current_buffer.name,
results = self.completer.CandidatesForLocationInFile( query,
current_buffer.name,
line,
column,
files )