From fed959a24b7d2a3033041b263139a59bbfff6bc3 Mon Sep 17 00:00:00 2001
From: Jan Travnicek <jan.travnicek@.fit.cvut.cz>
Date: Mon, 3 Jun 2019 11:13:25 +0200
Subject: [PATCH] tune ReadlinePromptCompletion to comply with clang-tidy

---
 aql2/src/prompt/ReadlinePromptCompletion.cpp | 22 ++++++++++++--------
 aql2/src/prompt/ReadlinePromptCompletion.h   |  9 ++++----
 2 files changed, 18 insertions(+), 13 deletions(-)

diff --git a/aql2/src/prompt/ReadlinePromptCompletion.cpp b/aql2/src/prompt/ReadlinePromptCompletion.cpp
index e1d2bd5c1b..d52c1dfedf 100644
--- a/aql2/src/prompt/ReadlinePromptCompletion.cpp
+++ b/aql2/src/prompt/ReadlinePromptCompletion.cpp
@@ -53,35 +53,39 @@ std::set < std::string > ReadlinePromptCompletion::filter_completions ( const st
 
 /* ========================================================================= */
 
+bool ReadlinePromptCompletion::masterCommandCompletionTest ( const ext::string & line, const int start, const std::string & expectation ) {
+	return line.starts_with ( expectation ) && ( unsigned ) start == expectation.length ( ) + 1;
+}
+
 ReadlinePromptCompletion::CompletionContext ReadlinePromptCompletion::context ( const char *text, const int start, const int end ) {
 	ext::string line ( rl_line_buffer );
 
 	if ( start == 0 )
 		return CompletionContext::COMMAND;
 
-	if ( line.starts_with ( "introspect" ) && start == 11 )
+	if ( masterCommandCompletionTest ( line, start, "introspect" ) )
 		return CompletionContext::COMMAND_INTROSPECT;
 
-	else if ( line.starts_with ( "set" ) && start == 4 )
+	else if ( masterCommandCompletionTest ( line, start, "set" ) )
 		return CompletionContext::SET;
 
-	else if ( line.starts_with ( "introspect overloads" ) && start == 21 )
+	else if ( masterCommandCompletionTest ( line, start, "introspect overloads" ) )
 		return CompletionContext::ALGORITHM;
 
-	else if ( line.starts_with ( "introspect variables" ) && start == 21 )
+	else if ( masterCommandCompletionTest ( line, start, "introspect variables" ) )
 		return CompletionContext::VARIABLE;
 
-	else if ( line.starts_with ( "introspect bindings" ) && start == 20 )
+	else if ( masterCommandCompletionTest ( line, start, "introspect bindings" ) )
 		return CompletionContext::BINDING;
 
-	else if ( line.starts_with ( "introspect algorithms" ) && start == 22 )
+	else if ( masterCommandCompletionTest ( line, start, "introspect algorithms" ) )
 		return CompletionContext::ALGORITHM_GROUP;
 
-	else if ( line.starts_with ( "introspect datatypes" ) && start == 21 )
+	else if ( masterCommandCompletionTest ( line, start, "introspect datatypes" ) )
 		return CompletionContext::DATATYPE_GROUP;
 
 	/* TODO
-	else if ( line.starts_with ( "introspect casts" ) && start == 17 )
+	else if ( masterCommandCompletionTest ( line, start, "introspect casts" ) )
 	*/
 
 	if ( end - start > 0 && text [ 0 ] == '$' )
@@ -98,7 +102,7 @@ ReadlinePromptCompletion::CompletionContext ReadlinePromptCompletion::context (
 	if ( p >= rl_line_buffer && ( *p == '|' || *p == '(' ) )
 		return CompletionContext::ALGORITHM;
 
-	if ( line.starts_with ( "execute" ) && start == 8 )
+	if ( masterCommandCompletionTest ( line, start, "execute" ) )
 		return CompletionContext::ALGORITHM;
 
 	/* undecided, fallback to filepath */
diff --git a/aql2/src/prompt/ReadlinePromptCompletion.h b/aql2/src/prompt/ReadlinePromptCompletion.h
index e7f65e9111..b9f0fb2aa5 100644
--- a/aql2/src/prompt/ReadlinePromptCompletion.h
+++ b/aql2/src/prompt/ReadlinePromptCompletion.h
@@ -25,7 +25,7 @@ class ReadlinePromptCompletion {
 	static std::set < std::string > fetchAlgorithmsFullyQualifiedName ( const char *text ) {
 		std::set < std::string > fullyQualifiedNames;
 
-		for ( const std::pair < std::string, std::vector < std::string > > & algo : abstraction::Registry::listAlgorithms ( ) ) {
+		for ( const ext::pair < std::string, ext::vector < std::string > > & algo : abstraction::Registry::listAlgorithms ( ) ) {
 			fullyQualifiedNames.insert ( algo.first );
 		}
 
@@ -35,7 +35,7 @@ class ReadlinePromptCompletion {
 	static std::set < std::string > fetchAlgorithmsLastSegmentName ( const char *text ) {
 		std::map < std::string, unsigned > collisions;
 
-		for ( const std::pair < std::string, std::vector < std::string > > & algo : abstraction::Registry::listAlgorithms ( ) ) {
+		for ( const ext::pair < std::string, ext::vector < std::string > > & algo : abstraction::Registry::listAlgorithms ( ) ) {
 			size_t pos = algo.first.find_last_of ( ':' );
 			if ( pos != std::string::npos )
 				collisions [ algo.first.substr ( pos + 1 ) ] += 1;
@@ -52,7 +52,7 @@ class ReadlinePromptCompletion {
 	static std::set < std::string > fetchAlgorithmGroups ( const char *text ) {
 		std::set < std::string > res;
 
-		for ( const std::pair < std::string, std::vector < std::string > > & algo : abstraction::Registry::listAlgorithms ( ) ) {
+		for ( const ext::pair < std::string, ext::vector < std::string > > & algo : abstraction::Registry::listAlgorithms ( ) ) {
 			std::set < std::string > groups = getGroups ( algo.first );
 			res.insert ( groups.begin ( ), groups.end ( ) );
 		}
@@ -123,9 +123,10 @@ public:
 	};
 
 	static char** readline_completion ( const char *text, int start, int end );
-	static CompletionContext context ( const char *text, const int start, const int end );
+	static CompletionContext context ( const char *text, int start, int end );
 
 private:
+	static bool masterCommandCompletionTest ( const ext::string & line, int start, const std::string & expectation );
 	static std::set < std::string > addPrefix ( const std::set < std::string > & collection, const std::string & prefix );
 	static std::set < std::string > getGroups ( const std::string & qualified_name );
 	static std::set < std::string > filter_completions ( const std::set < std::string > & choices, const char *text );
-- 
GitLab