Created
October 14, 2013 23:22
-
-
Save zhaocai/6983945 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 9dfbbe18f2e84da649f28dc1a3b06935ac0e3b17 Mon Sep 17 00:00:00 2001 | |
From: Zhao Cai <[email protected]> | |
Date: Wed, 31 Jul 2013 16:43:01 -0400 | |
Subject: [PATCH] Add QueryPartialMatch to ycm_core | |
--- | |
cpp/ycm/PythonSupport.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ | |
cpp/ycm/PythonSupport.h | 11 +++++++++++ | |
cpp/ycm/ycm_core.cpp | 1 + | |
3 files changed, 55 insertions(+) | |
diff --git a/cpp/ycm/PythonSupport.cpp b/cpp/ycm/PythonSupport.cpp | |
index 635ae5e..560120f 100644 | |
--- a/cpp/ycm/PythonSupport.cpp | |
+++ b/cpp/ycm/PythonSupport.cpp | |
@@ -22,6 +22,8 @@ | |
#include "CandidateRepository.h" | |
#include <boost/algorithm/string.hpp> | |
#include <boost/algorithm/cxx11/any_of.hpp> | |
+#include <boost/regex.hpp> | |
+#include <iterator> | |
#include <vector> | |
using boost::algorithm::any_of; | |
@@ -102,4 +104,45 @@ boost::python::list FilterAndSortCandidates( | |
return filtered_candidates; | |
} | |
+int QueryPartialMatch( | |
+ const std::string ®ex, | |
+ const std::string &query, | |
+ const unsigned int valid_partial_match_size ) { | |
+ int match_result = 0; | |
+ unsigned int len; | |
+ | |
+ boost::regex expression( regex ); | |
+ | |
+ boost::sregex_iterator it( | |
+ query.begin(), query.end(), | |
+ expression, | |
+ boost::match_not_null | boost::match_default | boost::match_partial); | |
+ boost::sregex_iterator _; | |
+ | |
+ while( it != _ ) { | |
+ if( (*it)[0].matched == false ) { | |
+ // partial match | |
+ | |
+ // only want the rightmost match. | |
+ if ( (*it)[0].second == query.end() ) { | |
+ len = std::distance( (*it)[0].first, (*it)[0].second ); | |
+ if ( len >= valid_partial_match_size ) { | |
+ match_result = 1; | |
+ break; | |
+ } | |
+ } | |
+ } else { | |
+ // full match | |
+ if ( (*it)[0].second == query.end() ) { | |
+ match_result = 2; | |
+ break; | |
+ } | |
+ } | |
+ | |
+ ++it; | |
+ } | |
+ | |
+ return match_result; | |
+} | |
+ | |
} // namespace YouCompleteMe | |
diff --git a/cpp/ycm/PythonSupport.h b/cpp/ycm/PythonSupport.h | |
index 893389f..e7797ad 100644 | |
--- a/cpp/ycm/PythonSupport.h | |
+++ b/cpp/ycm/PythonSupport.h | |
@@ -31,6 +31,17 @@ boost::python::list FilterAndSortCandidates( | |
const std::string &candidate_property, | |
const std::string &query ); | |
+// Try partial match regex on query | |
+// | |
+// Return | |
+// - 0 : no match | |
+// - 1 : partial match | |
+// - 2 : full match | |
+int QueryPartialMatch( | |
+ const std::string ®ex, | |
+ const std::string &query, | |
+ const unsigned int valid_partial_match_size ); | |
+ | |
} // namespace YouCompleteMe | |
#endif /* end of include guard: PYTHONSUPPORT_H_KWGFEX0V */ | |
diff --git a/cpp/ycm/ycm_core.cpp b/cpp/ycm/ycm_core.cpp | |
index 4c1ba0d..13dfb54 100644 | |
--- a/cpp/ycm/ycm_core.cpp | |
+++ b/cpp/ycm/ycm_core.cpp | |
@@ -57,6 +57,7 @@ BOOST_PYTHON_MODULE(ycm_core) | |
def( "HasClangSupport", HasClangSupport ); | |
def( "FilterAndSortCandidates", FilterAndSortCandidates ); | |
+ def( "QueryPartialMatch", QueryPartialMatch ); | |
def( "YcmCoreVersion", YcmCoreVersion ); | |
class_< IdentifierCompleter, boost::noncopyable >( "IdentifierCompleter" ) | |
-- | |
1.7.11.1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment