Преглед изворни кода

fix(app): support nested slash command autocomplete (#38097)

Daniel Polito пре 1 месец
родитељ
комит
b513fafe83

+ 13 - 0
packages/session-ui/src/v2/components/prompt-input/machine.test.ts

@@ -26,6 +26,19 @@ describe("prompt input v2 interaction machine", () => {
     expect(closed.state.popover).toEqual({ type: "closed" })
   })
 
+  test("completes nested slash command names", () => {
+    const open = transitionPromptInputV2(
+      createPromptInputV2InteractionState(),
+      { type: "input.changed", value: "/review/" },
+      persisted(),
+    )
+    const item = { ...command, label: "/review/nested" }
+    const selected = transitionPromptInputV2(open.state, { type: "popover.select", item }, persisted("/review/"))
+
+    expect(open.state.popover).toEqual({ type: "command-inline", query: "review/" })
+    expect(selected.commands).toContainEqual({ type: "draft.setText", value: "/review/nested " })
+  })
+
   test("opens context completion at the cursor", () => {
     const value = "alpha @sr omega"
     const input = persisted(value)

+ 2 - 2
packages/session-ui/src/v2/components/prompt-input/machine.ts

@@ -102,7 +102,7 @@ function inputChanged(
     ])
   }
 
-  const command = value.match(/^\/([^\s/]*)$/)
+  const command = value.match(/^\/(\S*)$/)
   if (command) {
     const query = command[1] ?? ""
     return changed({ ...state, popover: { type: "command-inline", query }, focus: "editor" }, [
@@ -240,7 +240,7 @@ function populated(persisted: PromptInputV2PersistedState) {
 }
 
 function replaceTrigger(value: string, trigger: "@" | "/", replacement: string) {
-  const index = value.lastIndexOf(trigger)
+  const index = trigger === "/" ? value.indexOf(trigger) : value.lastIndexOf(trigger)
   return index < 0 ? replacement : value.slice(0, index) + replacement
 }