Unify enum and identityref completion

I could reuse it in the end!

Change-Id: I20abc8eb9305c4420f422c6d99ef3d4552ae336a
diff --git a/src/ast_handlers.cpp b/src/ast_handlers.cpp
index 8aa6470..7738085 100644
--- a/src/ast_handlers.cpp
+++ b/src/ast_handlers.cpp
@@ -25,3 +25,15 @@
     }
     return leafDataToString(value);
 }
+
+template<>
+decltype(ParserContext::m_suggestions) createSetSuggestions_class<yang::LeafDataTypes::Enum>::getSuggestions(const ParserContext& parserContext, const Schema& schema) const
+{
+    return schema.enumValues(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node);
+}
+
+template<>
+decltype(ParserContext::m_suggestions) createSetSuggestions_class<yang::LeafDataTypes::IdentityRef>::getSuggestions(const ParserContext& parserContext, const Schema& schema) const
+{
+    return schema.validIdentities(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node, Prefixes::WhenNeeded);
+}
diff --git a/src/ast_handlers.hpp b/src/ast_handlers.hpp
index 6ecfbc5..368d51e 100644
--- a/src/ast_handlers.hpp
+++ b/src/ast_handlers.hpp
@@ -14,6 +14,7 @@
 
 
 #include "ast_commands.hpp"
+#include "parser_context.hpp"
 #include "schema.hpp"
 #include "utils.hpp"
 namespace x3 = boost::spirit::x3;
@@ -679,35 +680,21 @@
     }
 };
 
-struct createEnumSuggestions_class {
+template<yang::LeafDataTypes TYPE>
+struct createSetSuggestions_class {
+    decltype(ParserContext::m_suggestions) getSuggestions(const ParserContext& ctx, const Schema& schema) const;
+
     template <typename T, typename Iterator, typename Context>
     void on_success(Iterator const& begin, Iterator const&, T&, Context const& context)
     {
         auto& parserContext = x3::get<parser_context_tag>(context);
         const Schema& schema = parserContext.m_schema;
 
-        // Only generate completions if the type is enum so that we don't
+        // Only generate completions if the type is correct so that we don't
         // overwrite some other completions.
-        if (schema.leafType(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node) == yang::LeafDataTypes::Enum) {
+        if (schema.leafType(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node) == TYPE) {
             parserContext.m_completionIterator = begin;
-            parserContext.m_suggestions = schema.enumValues(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node);
-        }
-    }
-};
-
-// FIXME: can I reuse createEnumSuggestions?
-struct createIdentitySuggestions_class {
-    template <typename T, typename Iterator, typename Context>
-    void on_success(Iterator const& begin, Iterator const&, T&, Context const& context)
-    {
-        auto& parserContext = x3::get<parser_context_tag>(context);
-        const Schema& schema = parserContext.m_schema;
-
-        // Only generate completions if the type is identityref so that we
-        // don't overwrite some other completions.
-        if (schema.leafType(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node) == yang::LeafDataTypes::IdentityRef) {
-            parserContext.m_completionIterator = begin;
-            parserContext.m_suggestions = schema.validIdentities(parserContext.m_tmpListKeyLeafPath.m_location, parserContext.m_tmpListKeyLeafPath.m_node, Prefixes::WhenNeeded);
+            parserContext.m_suggestions = getSuggestions(parserContext, schema);
         }
     }
 };
diff --git a/src/grammars.hpp b/src/grammars.hpp
index 11d1db0..dd4e16f 100644
--- a/src/grammars.hpp
+++ b/src/grammars.hpp
@@ -75,8 +75,8 @@
 x3::rule<suggestKeysEnd_class, x3::unused_type> const suggestKeysEnd = "suggestKeysEnd";
 x3::rule<createCommandSuggestions_class, x3::unused_type> const createCommandSuggestions = "createCommandSuggestions";
 x3::rule<completing_class, x3::unused_type> const completing = "completing";
-x3::rule<createEnumSuggestions_class, x3::unused_type> const createEnumSuggestions = "createEnumSuggestions";
-x3::rule<createIdentitySuggestions_class, x3::unused_type> const createIdentitySuggestions = "createIdentitySuggestions";
+x3::rule<createSetSuggestions_class<yang::LeafDataTypes::Enum>, x3::unused_type> const createEnumSuggestions = "createEnumSuggestions";
+x3::rule<createSetSuggestions_class<yang::LeafDataTypes::IdentityRef>, x3::unused_type> const createIdentitySuggestions = "createIdentitySuggestions";
 
 #if __clang__
 #pragma GCC diagnostic push