fix(ui): prevent over-eager slash subcommand completion (#20136)

This commit is contained in:
Keith Guerin
2026-03-24 20:11:09 -07:00
committed by GitHub
parent 73526416cf
commit 46aa3fd193
4 changed files with 169 additions and 157 deletions
@@ -1,6 +1,6 @@
/**
* @license
* Copyright 2025 Google LLC
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
@@ -20,7 +20,7 @@ import {
useCommandCompletion,
CompletionMode,
} from './useCommandCompletion.js';
import type { CommandContext } from '../commands/types.js';
import type { CommandContext, SlashCommand } from '../commands/types.js';
import type { Config } from '@google/gemini-cli-core';
import { useTextBuffer } from '../components/shared/text-buffer.js';
import type { Suggestion } from '../components/SuggestionsDisplay.js';
@@ -72,7 +72,11 @@ const setupMocks = ({
shellSuggestions = [],
isLoading = false,
isPerfectMatch = false,
slashCompletionRange = { completionStart: 0, completionEnd: 0 },
slashCompletionRange = {
completionStart: 0,
completionEnd: 0,
getCommandFromSuggestion: () => undefined,
},
shellCompletionRange = {
completionStart: 0,
completionEnd: 0,
@@ -85,7 +89,13 @@ const setupMocks = ({
shellSuggestions?: Suggestion[];
isLoading?: boolean;
isPerfectMatch?: boolean;
slashCompletionRange?: { completionStart: number; completionEnd: number };
slashCompletionRange?: {
completionStart: number;
completionEnd: number;
getCommandFromSuggestion: (
suggestion: Suggestion,
) => SlashCommand | undefined;
};
shellCompletionRange?: {
completionStart: number;
completionEnd: number;
@@ -471,10 +481,15 @@ describe('useCommandCompletion', () => {
});
describe('handleAutocomplete', () => {
it('should complete a partial command', async () => {
it('should complete a partial command and NOT add a space if it has an action', async () => {
setupMocks({
slashSuggestions: [{ label: 'memory', value: 'memory' }],
slashCompletionRange: { completionStart: 1, completionEnd: 4 },
slashCompletionRange: {
completionStart: 1,
completionEnd: 4,
getCommandFromSuggestion: () =>
({ action: vi.fn() }) as unknown as SlashCommand,
},
});
const { result } = await renderCommandCompletionHook('/mem');
@@ -487,12 +502,40 @@ describe('useCommandCompletion', () => {
result.current.handleAutocomplete(0);
});
expect(result.current.textBuffer.text).toBe('/memory ');
expect(result.current.textBuffer.text).toBe('/memory');
});
it('should complete a partial command and ADD a space if it has NO action (e.g. just a parent)', async () => {
setupMocks({
slashSuggestions: [{ label: 'chat', value: 'chat' }],
slashCompletionRange: {
completionStart: 1,
completionEnd: 5,
getCommandFromSuggestion: () => ({}) as unknown as SlashCommand, // No action
},
});
const { result } = await renderCommandCompletionHook('/chat');
await waitFor(() => {
expect(result.current.suggestions.length).toBe(1);
});
act(() => {
result.current.handleAutocomplete(0);
});
expect(result.current.textBuffer.text).toBe('/chat ');
});
it('should complete a file path', async () => {
setupMocks({
atSuggestions: [{ label: 'src/file1.txt', value: 'src/file1.txt' }],
slashCompletionRange: {
completionStart: 0,
completionEnd: 0,
getCommandFromSuggestion: () => undefined,
},
});
const { result } = await renderCommandCompletionHook('@src/fi');
@@ -517,7 +560,11 @@ describe('useCommandCompletion', () => {
insertValue: 'resume list',
},
],
slashCompletionRange: { completionStart: 1, completionEnd: 5 },
slashCompletionRange: {
completionStart: 1,
completionEnd: 5,
getCommandFromSuggestion: () => undefined,
},
});
const { result } = await renderCommandCompletionHook('/resu');
@@ -539,6 +586,11 @@ describe('useCommandCompletion', () => {
setupMocks({
atSuggestions: [{ label: 'src/file1.txt', value: 'src/file1.txt' }],
slashCompletionRange: {
completionStart: 0,
completionEnd: 0,
getCommandFromSuggestion: () => undefined,
},
});
const { result } = await renderCommandCompletionHook(text, cursorOffset);
@@ -559,6 +611,11 @@ describe('useCommandCompletion', () => {
it('should complete a directory path ending with / without a trailing space', async () => {
setupMocks({
atSuggestions: [{ label: 'src/components/', value: 'src/components/' }],
slashCompletionRange: {
completionStart: 0,
completionEnd: 0,
getCommandFromSuggestion: () => undefined,
},
});
const { result } = await renderCommandCompletionHook('@src/comp');
@@ -579,6 +636,11 @@ describe('useCommandCompletion', () => {
atSuggestions: [
{ label: 'src\\components\\', value: 'src\\components\\' },
],
slashCompletionRange: {
completionStart: 0,
completionEnd: 0,
getCommandFromSuggestion: () => undefined,
},
});
const { result } = await renderCommandCompletionHook('@src\\comp');
@@ -594,6 +656,33 @@ describe('useCommandCompletion', () => {
expect(result.current.textBuffer.text).toBe('@src\\components\\');
});
it('should ADD a space for AT completion even if name matches a command with an action', async () => {
// Setup a mock where getCommandFromSuggestion WOULD return a command with an action
// if it were in SLASH mode.
setupMocks({
atSuggestions: [{ label: 'memory', value: 'memory' }],
slashCompletionRange: {
completionStart: 0,
completionEnd: 0,
getCommandFromSuggestion: () =>
({ action: vi.fn() }) as unknown as SlashCommand,
},
});
const { result } = await renderCommandCompletionHook('@mem');
await waitFor(() => {
expect(result.current.suggestions.length).toBe(1);
});
act(() => {
result.current.handleAutocomplete(0);
});
// Should have a space because it's AT mode, not SLASH mode
expect(result.current.textBuffer.text).toBe('@memory ');
});
it('should show ghost text for a single shell completion', async () => {
const text = 'l';
setupMocks({
@@ -905,6 +994,11 @@ describe('useCommandCompletion', () => {
it('should complete file path and add trailing space', async () => {
setupMocks({
atSuggestions: [{ label: 'src/file.txt', value: 'src/file.txt' }],
slashCompletionRange: {
completionStart: 0,
completionEnd: 0,
getCommandFromSuggestion: () => undefined,
},
});
const { result } = await renderCommandCompletionHook('/cmd @src/fi');