From e4eb3f99c30942cde865a7e5403b3345cab735a8 Mon Sep 17 00:00:00 2001
From: Ariya Hidayat <ariya@metabase.com>
Date: Mon, 14 Feb 2022 15:06:59 -0800
Subject: [PATCH] Custom expression editor: don't suggest a unique matching
 function (#20485)

---
 .../src/metabase/lib/expressions/suggest.js   | 25 +++++++++++++------
 .../lib/expressions/suggest.unit.spec.js      | 10 ++++++++
 2 files changed, 28 insertions(+), 7 deletions(-)

diff --git a/frontend/src/metabase/lib/expressions/suggest.js b/frontend/src/metabase/lib/expressions/suggest.js
index 2731b6a5cf8..8c90b18b027 100644
--- a/frontend/src/metabase/lib/expressions/suggest.js
+++ b/frontend/src/metabase/lib/expressions/suggest.js
@@ -151,11 +151,22 @@ export function suggest({
   suggestions = suggestions.filter(suggestion => suggestion.range);
 
   // deduplicate suggestions and sort by type then name
-  return {
-    suggestions: _.chain(suggestions)
-      .uniq(suggestion => suggestion.text)
-      .sortBy("text")
-      .sortBy("order")
-      .value(),
-  };
+  suggestions = _.chain(suggestions)
+    .uniq(suggestion => suggestion.text)
+    .sortBy("text")
+    .sortBy("order")
+    .value();
+
+  // the only suggested function equals the prefix match?
+  if (suggestions.length === 1 && matchPrefix) {
+    const { icon } = suggestions[0];
+    if (icon === "function") {
+      const helpText = getHelpText(getMBQLName(matchPrefix));
+      if (helpText) {
+        return { helpText };
+      }
+    }
+  }
+
+  return { suggestions };
 }
diff --git a/frontend/test/metabase/lib/expressions/suggest.unit.spec.js b/frontend/test/metabase/lib/expressions/suggest.unit.spec.js
index 9d185160cff..e1e1e11ed0b 100644
--- a/frontend/test/metabase/lib/expressions/suggest.unit.spec.js
+++ b/frontend/test/metabase/lib/expressions/suggest.unit.spec.js
@@ -161,6 +161,16 @@ describe("metabase/lib/expression/suggest", () => {
         expect(args).toHaveLength(3);
       });
 
+      it("should provide help text for the unique match", () => {
+        const { structure, args } = helpText({
+          source: "lower", // doesn't need to be "lower(" since it's a unique match
+          query: ORDERS.query(),
+          startRule: "expression",
+        });
+        expect(structure).toEqual("lower(text)");
+        expect(args).toHaveLength(1);
+      });
+
       it("should provide help text after first argument if there's only one argument", () => {
         expect(
           helpText({
-- 
GitLab