Skip to content

Commit

Permalink
fix(ui): fix suggestions and autocomplete for suggestions containing …
Browse files Browse the repository at this point in the history
…spaces
  • Loading branch information
paradoxuum committed Sep 21, 2024
1 parent f502843 commit dd918ea
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
15 changes: 9 additions & 6 deletions packages/ui/src/components/terminal/terminal-text-field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,24 +162,22 @@ export function TerminalTextField({
if (suggestion === undefined) return;

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

if (commandPath === undefined) {
const suggestionTitle = suggestion.title;
const textParts = terminalTextParts();
if (textParts.isEmpty()) return;

const pathParts = [...textParts];

let newText = currentText;
if (atNextPart) {
newText += suggestionTitle;
pathParts.push(suggestionTitle);
} else if (!textParts.isEmpty()) {
const lastPartSize = textParts[textParts.size() - 1];
newText =
newText.sub(0, newText.size() - lastPartSize.size()) +
suggestionTitle;
newText.sub(0, newText.size() - lastPart.size()) + suggestionTitle;
pathParts.remove(textParts.size() - 1);
pathParts.push(suggestionTitle);
}
Expand Down Expand Up @@ -224,7 +222,12 @@ export function TerminalTextField({

let newText = currentText;
const otherSuggestion = suggestion.others[0];
newText = newText.sub(0, newText.size() - (currentTextPart()?.size() ?? 0));

const trailingSpaces = newText.match("(%s+)$")[0] as string | undefined;
newText = newText.sub(
0,
newText.size() - (lastPart.size() ?? 0) - (trailingSpaces?.size() ?? 0),
);
newText += otherSuggestion.match("%s").isEmpty()
? otherSuggestion
: `"${otherSuggestion}"`;
Expand Down
15 changes: 14 additions & 1 deletion packages/ui/src/components/terminal/terminal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,20 @@ export function Terminal() {
terminalTextValid(false);
}

const textPart = !atNextPart ? parts[parts.size() - 1] : undefined;
const lastPart = parts[parts.size() - 1];
let textPart: string | undefined = undefined;
if (lastPart.sub(0, 1) === '"') {
textPart = lastPart.sub(2);
} else if (!atNextPart) {
textPart = lastPart;
}

if (textPart !== undefined) {
const trailingSpaces = text.match("(%s+)$")[0];
if (trailingSpaces !== undefined) {
textPart += trailingSpaces;
}
}

const argIndex =
path !== undefined
Expand Down

0 comments on commit dd918ea

Please sign in to comment.