fix(app): complete mentions at cursor (#37941)
Co-authored-by: Brendan Allan <14191578+Brendonovich@users.noreply.github.com>
This commit is contained in:
co-authored by
Brendan Allan
parent
47fc6f2991
commit
4872c48c23
@@ -26,6 +26,20 @@ describe("prompt input v2 interaction machine", () => {
|
|||||||
expect(closed.state.popover).toEqual({ type: "closed" })
|
expect(closed.state.popover).toEqual({ type: "closed" })
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test("opens context completion at the cursor", () => {
|
||||||
|
const value = "alpha @sr omega"
|
||||||
|
const input = persisted(value)
|
||||||
|
input.cursor = 9
|
||||||
|
|
||||||
|
const result = transitionPromptInputV2(
|
||||||
|
createPromptInputV2InteractionState(),
|
||||||
|
{ type: "input.changed", value, persist: false },
|
||||||
|
input,
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(result.state.popover).toEqual({ type: "context", query: "sr" })
|
||||||
|
})
|
||||||
|
|
||||||
test("enters shell mode from an initial exclamation mark", () => {
|
test("enters shell mode from an initial exclamation mark", () => {
|
||||||
const result = transitionPromptInputV2(
|
const result = transitionPromptInputV2(
|
||||||
createPromptInputV2InteractionState(),
|
createPromptInputV2InteractionState(),
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ export function transitionPromptInputV2(
|
|||||||
event: PromptInputV2InteractionEvent,
|
event: PromptInputV2InteractionEvent,
|
||||||
persisted: PromptInputV2PersistedState,
|
persisted: PromptInputV2PersistedState,
|
||||||
): PromptInputV2Transition {
|
): PromptInputV2Transition {
|
||||||
if (event.type === "input.changed") return inputChanged(state, event.value, event.persist !== false)
|
if (event.type === "input.changed") return inputChanged(state, event.value, event.persist !== false, persisted.cursor)
|
||||||
if (event.type === "commands.open") return openCommands(state, persisted)
|
if (event.type === "commands.open") return openCommands(state, persisted)
|
||||||
if (event.type === "context.open") return openContext(state, persisted)
|
if (event.type === "context.open") return openContext(state, persisted)
|
||||||
if (event.type === "popover.query") return queryChanged(state, event.value)
|
if (event.type === "popover.query") return queryChanged(state, event.value)
|
||||||
@@ -81,14 +81,19 @@ export function transitionPromptInputV2(
|
|||||||
return changed({ ...state, focus: "external" })
|
return changed({ ...state, focus: "external" })
|
||||||
}
|
}
|
||||||
|
|
||||||
function inputChanged(state: PromptInputV2InteractionState, value: string, persist: boolean): PromptInputV2Transition {
|
function inputChanged(
|
||||||
|
state: PromptInputV2InteractionState,
|
||||||
|
value: string,
|
||||||
|
persist: boolean,
|
||||||
|
cursor: number | undefined,
|
||||||
|
): PromptInputV2Transition {
|
||||||
const setText: PromptInputV2InteractionCommand[] = persist ? [{ type: "draft.setText", value }] : []
|
const setText: PromptInputV2InteractionCommand[] = persist ? [{ type: "draft.setText", value }] : []
|
||||||
if (state.mode === "normal" && value === "!") {
|
if (state.mode === "normal" && value === "!") {
|
||||||
return changed({ ...state, mode: "shell", popover: { type: "closed" }, focus: "editor" }, [
|
return changed({ ...state, mode: "shell", popover: { type: "closed" }, focus: "editor" }, [
|
||||||
{ type: "draft.setText", value: "" },
|
{ type: "draft.setText", value: "" },
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
const context = value.match(/(?:^|\s)@([^\s@]*)$/)
|
const context = value.slice(0, cursor ?? value.length).match(/(?:^|\s)@([^\s@]*)$/)
|
||||||
if (context) {
|
if (context) {
|
||||||
const query = context[1] ?? ""
|
const query = context[1] ?? ""
|
||||||
return changed({ ...state, popover: { type: "context", query }, focus: "editor" }, [
|
return changed({ ...state, popover: { type: "context", query }, focus: "editor" }, [
|
||||||
|
|||||||
Reference in New Issue
Block a user