Skip to content

Commit

Permalink
fix(ui): fix some remaining suggestion bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
paradoxuum committed Sep 28, 2024
1 parent 4873671 commit 8a9b162
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BaseRegistry } from "@rbxts/centurion";
import {
atNextPart,
commandArgIndex,
currentCommandPath,
currentTextPart,
Expand Down Expand Up @@ -65,7 +66,7 @@ export function getSuggestedText(
text: string,
suggestion?: Suggestion,
) {
if (suggestion === undefined) return text;
if (suggestion === undefined) return "";

let suggestionText: string;

Expand All @@ -75,11 +76,10 @@ export function getSuggestedText(
} else {
const command = currentCommandPath();
const argIndex = terminalArgIndex();
if (command === undefined || argIndex === undefined) return text;

const atNextPart = text.sub(-1) === " ";
if (command === undefined || argIndex === undefined) return "";

if (atNextPart && argIndex === commandArgIndex()) {
if (atNextPart() && argIndex === commandArgIndex()) {
suggestionText = suggestion.title;
} else if (!suggestion.others.isEmpty()) {
suggestionText = suggestion.others[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ export function TerminalTextField({
if (textBox === undefined) return;
textBox.Text = text;
textBox.CursorPosition = text.size() + 1;
suggestionText(text);
};

const traverseHistory = (up: boolean) => {
Expand Down
22 changes: 11 additions & 11 deletions packages/ui/src/components/terminal/terminal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useClient } from "../../hooks/use-client";
import { useHistory } from "../../hooks/use-history";
import { px } from "../../hooks/use-px";
import {
atNextPart,
commandArgIndex,
currentCommandPath,
currentSuggestion,
Expand Down Expand Up @@ -87,7 +88,7 @@ export function Terminal() {

// If the text ends in a space, we want to count that as having traversed
// to the next "part" of the text.
const atNextPart = text.sub(-1) === " ";
const endsInSpace = text.sub(-1) === " ";
const path = getValidPath(client.registry, parts);
const command =
path !== undefined ? client.registry.getCommand(path) : undefined;
Expand All @@ -102,16 +103,16 @@ export function Terminal() {
terminalTextValid(
noArgs ||
missing.isEmpty() ||
(atNextPart && missing.size() === 1),
(endsInSpace && missing.size() === 1),
);
} else {
currentCommandPath(undefined);
terminalTextValid(false);
}

let textPart = parts[parts.size() - 1] as string | undefined;
const quoted = textPart?.match(START_QUOTE_PATTERN) !== undefined;
if (atNextPart && !quoted) {
const atNext = atNextPart();
if (atNext) {
textPart = undefined;
} else if (textPart !== undefined) {
const spaces = text.match(TRAILING_SPACE_PATTERN)[0] ?? "";
Expand All @@ -120,19 +121,15 @@ export function Terminal() {

const argIndex =
path !== undefined
? parts.size() - path.size() - (atNextPart ? 0 : 1)
? parts.size() - path.size() - (atNext ? 0 : 1)
: -1;
terminalArgIndex(argIndex);

if (command === undefined || argIndex === -1) {
// Get command suggestions
const parentPath = !parts.isEmpty()
? new RegistryPath(
ArrayUtil.slice(
parts,
0,
parts.size() - (atNextPart ? 0 : 1),
),
ArrayUtil.slice(parts, 0, parts.size() - (atNext ? 0 : 1)),
)
: undefined;

Expand Down Expand Up @@ -244,7 +241,10 @@ export function Terminal() {
return;
}

const args = ArrayUtil.slice(terminalTextParts(), commandPath.size());
const args = ArrayUtil.slice(
splitString(terminalText(), " "),
commandPath.size(),
);
client.dispatcher.run(commandPath, args);
}}
/>
Expand Down
6 changes: 6 additions & 0 deletions packages/ui/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,11 @@ export const terminalText = atom("");
export const terminalTextParts = computed(() => {
return splitString(terminalText(), " ", true);
});
export const atNextPart = computed(() => {
const parts = terminalTextParts();
const textPart = parts[parts.size() - 1] as string | undefined;
const quoted = textPart?.match(`^(['"])`) !== undefined;
return terminalText().sub(-1) === " " && !quoted;
});
export const terminalTextValid = atom(false);
export const currentTextPart = atom<string | undefined>(undefined);
5 changes: 2 additions & 3 deletions test/src/server/commands/echo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ export class EchoCommand {
name: "text",
description: "The text to print",
type: CenturionType.String,
numArgs: "rest",
suggestions: ["Hello, World!", "Some other spaced text", "Test"],
},
],
aliases: ["print"],
})
run(_: CommandContext, text: string[]) {
print(text.join(" "));
run(_: CommandContext, text: string) {
print(text);
}
}

0 comments on commit 8a9b162

Please sign in to comment.