Skip to content

Commit

Permalink
fix: fix autocomplete for args with spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
paradoxuum committed Sep 24, 2024
1 parent e8e137c commit ae9f726
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 30 deletions.
35 changes: 13 additions & 22 deletions packages/core/src/shared/util/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ const ESCAPE_PATTERN = `(\\*)['"]$`;
export function splitString(
text: string,
separator: string,
max = math.huge,
includeQuotes = false,
): string[] {
const resultText = encodeControlChars(text);
const t: string[] = [];
const results: string[] = [];

let buf: string | undefined;
let quoted: string | undefined;
Expand All @@ -65,40 +65,31 @@ export function splitString(
quoted === undefined &&
endQuote === undefined
) {
[buf, quoted] = [str, startQuote];
buf = str;
quoted = startQuote;
} else if (
buf !== undefined &&
endQuote === quoted &&
escaped.size() % 2 === 0
) {
[str, buf, quoted] = [`${buf}${separator}${str}`, undefined, undefined];
str = `${buf}${separator}${str}`;
buf = undefined;
quoted = undefined;
} else if (buf !== undefined) {
buf = `${buf}${separator}${str}`;
}

if (buf !== undefined) {
continue;
}

const result = decodeControlChars(
str.gsub(START_QUOTE_PATTERN, "")[0].gsub(END_QUOTE_PATTERN, "")[0],
);
if (t.size() > max) {
t[t.size() - 1] = result;
} else {
t.push(result);
if (buf !== undefined) continue;
if (!includeQuotes) {
str = str.gsub(START_QUOTE_PATTERN, "")[0].gsub(END_QUOTE_PATTERN, "")[0];
}
results.push(decodeControlChars(str));
}

if (buf !== undefined) {
if (t.size() > max) {
t[t.size() - 1] = decodeControlChars(buf);
} else {
t.push(decodeControlChars(buf));
}
results.push(decodeControlChars(buf));
}

return t;
return results;
}

/**
Expand Down
19 changes: 11 additions & 8 deletions packages/ui/src/components/terminal/terminal-text-field.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { splitString } from "@rbxts/centurion/out/shared/util/string";
import { subscribe } from "@rbxts/charm";
import { UserInputService } from "@rbxts/services";
import Vide, { cleanup, Derivable, effect, source } from "@rbxts/vide";
Expand Down Expand Up @@ -163,8 +164,8 @@ export function TerminalTextField({

const currentText = textBox.Text;
const textParts = terminalTextParts();
const lastPart = textParts[textParts.size() - 1];
const atNextPart = currentText.sub(-1) === " ";
const lastPart = !atNextPart ? textParts[textParts.size() - 1] : undefined;

if (commandPath === undefined) {
const suggestionTitle = suggestion.title;
Expand All @@ -175,7 +176,7 @@ export function TerminalTextField({
if (atNextPart) {
newText += suggestionTitle;
pathParts.push(suggestionTitle);
} else if (!textParts.isEmpty()) {
} else if (lastPart !== undefined) {
newText =
newText.sub(0, newText.size() - lastPart.size()) + suggestionTitle;
pathParts.remove(textParts.size() - 1);
Expand Down Expand Up @@ -221,13 +222,15 @@ export function TerminalTextField({
if (argIndex === undefined || commandArgs === undefined) return;

let newText = currentText;
const otherSuggestion = suggestion.others[0];

const trailingSpaces = newText.match("(%s+)$")[0] as string | undefined;
newText = newText.sub(
0,
newText.size() - (lastPart.size() ?? 0) - (trailingSpaces?.size() ?? 0),
);
const parts = splitString(newText, " ", true);
const currentPart = parts[parts.size() - 1];
const currentPartSize =
currentTextPart() !== undefined ? currentPart.size() : 0;

newText = newText.sub(0, newText.size() - currentPartSize);

const otherSuggestion = suggestion.others[0];
newText += otherSuggestion.match("%s").isEmpty()
? otherSuggestion
: `"${otherSuggestion}"`;
Expand Down

0 comments on commit ae9f726

Please sign in to comment.