diff --git a/src/indexer.cc b/src/indexer.cc
index 051e4936..dcd5b815 100644
--- a/src/indexer.cc
+++ b/src/indexer.cc
@@ -516,7 +516,7 @@ void SetTypeName(IndexType* type,
   // type->def.detailed_name = param->PrettyPrintCursor(cursor.cx_cursor);
   type->def.detailed_name =
       param->ns.QualifiedName(container ? container : &parent, name);
-  auto idx = type->def.detailed_name.find(name);
+  auto idx = type->def.detailed_name.rfind(name);
   assert(idx != std::string::npos);
   type->def.short_name_offset = idx;
   type->def.short_name_size = strlen(name);
@@ -631,7 +631,8 @@ void SetVarDetail(IndexVar* var,
 #endif
   }
   // FIXME QualifiedName should return index
-  auto idx = def.detailed_name.find(short_name.begin(), 0, short_name.size());
+  auto idx = def.detailed_name.rfind(short_name.begin(), std::string::npos,
+                                     short_name.size());
   assert(idx != std::string::npos);
   def.short_name_offset = idx;
   def.short_name_size = short_name.size();
diff --git a/src/indexer.h b/src/indexer.h
index c556447e..1b41f12f 100644
--- a/src/indexer.h
+++ b/src/indexer.h
@@ -19,6 +19,7 @@
 #include <optional.h>
 #include <string_view.h>
 
+#include <ctype.h>
 #include <algorithm>
 #include <cassert>
 #include <cstdint>
@@ -375,6 +376,15 @@ struct VarDefDefinitionData {
     return std::string_view(detailed_name.c_str() + short_name_offset,
                             short_name_size);
   }
+  std::string DetailedName(bool qualified) const {
+    if (qualified)
+      return detailed_name;
+    int i = short_name_offset;
+    while (i && (isalnum(detailed_name[i - 1]) || detailed_name[i - 1] == '_' ||
+                 detailed_name[i - 1] == ':'))
+      i--;
+    return detailed_name.substr(0, i) + detailed_name.substr(short_name_offset);
+  }
 };
 
 template <typename TVisitor, typename Family>
diff --git a/src/messages/cquery_member_hierarchy.cc b/src/messages/cquery_member_hierarchy.cc
index 5e5d9c9c..56fb90e0 100644
--- a/src/messages/cquery_member_hierarchy.cc
+++ b/src/messages/cquery_member_hierarchy.cc
@@ -42,7 +42,7 @@ struct Out_CqueryMemberHierarchy
   struct Entry {
     QueryTypeId id;
     std::string_view name;
-    std::string_view fieldName;
+    std::string fieldName;
     lsLocation location;
     // For unexpanded nodes, this is an upper bound because some entities may be
     // undefined. If it is 0, there are no members.
@@ -116,9 +116,9 @@ void Expand(MessageHandler* m,
         Out_CqueryMemberHierarchy::Entry entry1;
         entry1.id = def1->type ? *def1->type : QueryTypeId();
         if (detailed_name)
-          entry1.fieldName = def1->detailed_name;
+          entry1.fieldName = def1->DetailedName(false);
         else
-          entry1.fieldName = def1->ShortName();
+          entry1.fieldName = std::string(def1->ShortName());
         Expand(m, &entry1, detailed_name, levels - 1);
         entry->children.push_back(std::move(entry1));
       });