Skip to content

Commit ceffeba

Browse files
sethtroisivachan-shetty
authored andcommitted
Improve slashCommand autoCompletion logic (google-gemini#2776)
1 parent 742d05c commit ceffeba

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

packages/cli/src/ui/components/InputPrompt.tsx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export const InputPrompt: React.FC<InputPromptProps> = ({
113113
return;
114114
}
115115
const query = buffer.text;
116-
const selectedSuggestion = completionSuggestions[indexToUse];
116+
const suggestion = completionSuggestions[indexToUse].value;
117117

118118
if (query.trimStart().startsWith('/')) {
119119
const parts = query.trimStart().substring(1).split(' ');
@@ -122,11 +122,16 @@ export const InputPrompt: React.FC<InputPromptProps> = ({
122122
const base = query.substring(0, slashIndex + 1);
123123

124124
const command = slashCommands.find((cmd) => cmd.name === commandName);
125-
if (command && command.completion) {
126-
const newValue = `${base}${commandName} ${selectedSuggestion.value}`;
127-
buffer.setText(newValue);
125+
// Make sure completion isn't the original command when command.completigion hasn't happened yet.
126+
if (command && command.completion && suggestion !== commandName) {
127+
const newValue = `${base}${commandName} ${suggestion}`;
128+
if (newValue === query) {
129+
handleSubmitAndClear(newValue);
130+
} else {
131+
buffer.setText(newValue);
132+
}
128133
} else {
129-
const newValue = base + selectedSuggestion.value;
134+
const newValue = base + suggestion;
130135
buffer.setText(newValue);
131136
handleSubmitAndClear(newValue);
132137
}
@@ -142,7 +147,7 @@ export const InputPrompt: React.FC<InputPromptProps> = ({
142147
buffer.replaceRangeByOffset(
143148
autoCompleteStartIndex,
144149
buffer.text.length,
145-
selectedSuggestion.value,
150+
suggestion,
146151
);
147152
}
148153
resetCompletionState();

packages/cli/src/ui/hooks/useCompletion.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ export function useCompletion(
135135
(cmd) => cmd.name === commandName || cmd.altName === commandName,
136136
);
137137

138-
if (command && command.completion) {
138+
// Continue to show command help until user types past command name.
139+
if (command && command.completion && parts.length > 1) {
139140
const fetchAndSetSuggestions = async () => {
140141
setIsLoadingSuggestions(true);
141142
if (command.completion) {

0 commit comments

Comments
 (0)