Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
211 changes: 209 additions & 2 deletions packages/cli/src/ui/hooks/useCommandCompletion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,12 @@ describe('useCommandCompletion', () => {
kind: CommandKind.BUILT_IN,
modelInvocable: false,
};
const fileCommand: SlashCommand = {
name: 'store-notes',
description: 'Store notes',
kind: CommandKind.FILE,
modelInvocable: true,
};

setupMocks({
slashSuggestions: [
Expand All @@ -529,7 +535,7 @@ describe('useCommandCompletion', () => {
useCommandCompletion(
useTextBufferForTest('please /store'),
testRootDir,
[skillCommand, builtInCommand],
[skillCommand, builtInCommand, fileCommand],
mockCommandContext,
false,
mockConfig,
Expand All @@ -544,11 +550,212 @@ describe('useCommandCompletion', () => {
expect.objectContaining({
enabled: true,
query: '/store',
slashCommands: [skillCommand],
slashCommands: [skillCommand, fileCommand],
}),
);
});

it.each([
{
input: '/review /sto',
expected: '/review /front-end-store-rules ',
},
{
input: '/review\n/sto',
expected: '/review\n/front-end-store-rules ',
},
])(
'should complete a repeated skill in $input',
async ({ input, expected }) => {
const firstSkill: SlashCommand = {
name: 'review',
description: 'Review changes',
kind: CommandKind.SKILL,
modelInvocable: true,
};
const secondSkill: SlashCommand = {
name: 'front-end-store-rules',
description: 'Store rules',
kind: CommandKind.SKILL,
modelInvocable: true,
};
const userOnlySkill: SlashCommand = {
name: 'store-locally',
description: 'Store locally',
kind: CommandKind.SKILL,
modelInvocable: false,
};
const fileCommand: SlashCommand = {
name: 'store-notes',
description: 'Store notes',
kind: CommandKind.FILE,
modelInvocable: true,
};

setupMocks({
slashSuggestions: [
{ label: 'front-end-store-rules', value: 'front-end-store-rules' },
],
slashCompletionRange: { completionStart: 1, completionEnd: 4 },
});

const { result } = renderHook(() => {
const textBuffer = useTextBufferForTest(input);
const completion = useCommandCompletion(
textBuffer,
testRootDir,
[firstSkill, secondSkill, userOnlySkill, fileCommand],
mockCommandContext,
false,
mockConfig,
);
return { ...completion, textBuffer };
});

await waitFor(() => {
expect(result.current.showSuggestions).toBe(true);
});

expect(useSlashCompletion).toHaveBeenLastCalledWith(
expect.objectContaining({
enabled: true,
query: '/sto',
slashCommands: [firstSkill, secondSkill, userOnlySkill],
}),
);

act(() => {
result.current.handleAutocomplete(0);
});

expect(result.current.textBuffer.text).toBe(expected);
},
);

it.each(['/stats /sto', '/unknown /sto', '/unknown\n/sto'])(
'should not treat an invalid stacked prefix as mid-input completion: %s',
async (input) => {
const skillCommand: SlashCommand = {
name: 'store-rules',
description: 'Store rules',
kind: CommandKind.SKILL,
modelInvocable: true,
};
const builtInCommand: SlashCommand = {
name: 'stats',
description: 'Show stats',
kind: CommandKind.BUILT_IN,
modelInvocable: false,
};

const { result } = renderHook(() =>
useCommandCompletion(
useTextBufferForTest(input),
testRootDir,
[skillCommand, builtInCommand],
mockCommandContext,
false,
mockConfig,
),
);

expect(result.current.midInputGhostText).toBeNull();

await waitFor(() => {
if (input.includes('\n')) {
expect(useSlashCompletion).toHaveBeenLastCalledWith(
expect.objectContaining({ enabled: false }),
);
} else {
expect(useSlashCompletion).toHaveBeenLastCalledWith(
expect.objectContaining({
enabled: true,
query: input,
slashCommands: [skillCommand, builtInCommand],
}),
);
}
});
},
);

it.each(['/tmp/foo.txt please /sto', '// note /sto'])(
'should treat non-command slash-led prefixes as regular mid-input completion: %s',
async (input) => {
const skillCommand: SlashCommand = {
name: 'store-rules',
description: 'Store rules',
kind: CommandKind.SKILL,
modelInvocable: true,
};

setupMocks({
slashSuggestions: [{ label: 'store-rules', value: 'store-rules' }],
});

const { result } = renderHook(() =>
useCommandCompletion(
useTextBufferForTest(input),
testRootDir,
[skillCommand],
mockCommandContext,
false,
mockConfig,
),
);

await waitFor(() => {
expect(result.current.showSuggestions).toBe(true);
});

expect(useSlashCompletion).toHaveBeenLastCalledWith(
expect.objectContaining({
enabled: true,
query: '/sto',
slashCommands: [skillCommand],
}),
);
},
);

it('should keep an indented first command in line-start completion', async () => {
const skillCommand: SlashCommand = {
name: 'front-end-store-rules',
description: 'Store rules',
kind: CommandKind.SKILL,
modelInvocable: true,
};
const builtInCommand: SlashCommand = {
name: 'stats',
description: 'Show stats',
kind: CommandKind.BUILT_IN,
modelInvocable: false,
};

renderHook(() =>
useCommandCompletion(
useTextBufferForTest(' /sto'),
testRootDir,
[skillCommand, builtInCommand],
mockCommandContext,
false,
mockConfig,
),
);

await waitFor(() => {
// This pins routing to the existing line-start path. The real slash
// completion may still decide whether an indented query has candidates.
expect(useSlashCompletion).toHaveBeenLastCalledWith(
expect.objectContaining({
enabled: true,
query: ' /sto',
slashCommands: [skillCommand, builtInCommand],
}),
);
});
});

it('should complete a file path when @ appears after a slash command', async () => {
setupMocks({
atSuggestions: [{ label: 'src/index.ts', value: 'src/index.ts' }],
Expand Down
97 changes: 67 additions & 30 deletions packages/cli/src/ui/hooks/useCommandCompletion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,24 @@ import {
} from './useSlashCompletion.js';
import type { Config } from '@qwen-code/qwen-code-core';
import { useCompletion } from './useCompletion.js';
import { parseSlashCommand } from '../../utils/commands.js';
import {
isStackedSkillCompletableCommand,
isValidStackedSkillPrefix,
parseSlashCommand,
} from '../../utils/commands.js';

export enum CompletionMode {
IDLE = 'IDLE',
AT = 'AT',
SLASH = 'SLASH',
}

enum SlashCompletionContext {
LINE_START = 'line-start',
MID_INPUT = 'mid-input',
STACKED_SKILL = 'stacked-skill',
}

function isExactMidInputModelInvocableCommand(
partialCommand: string,
slashCommands: readonly SlashCommand[],
Expand Down Expand Up @@ -97,7 +107,7 @@ export function useCommandCompletion(
query,
completionStart,
completionEnd,
isMidInputSlashCompletion,
slashCompletionContext,
} = useMemo(() => {
const currentLine = buffer.lines[cursorRow] || '';

Expand Down Expand Up @@ -137,59 +147,82 @@ export function useCommandCompletion(
query: partialPath,
completionStart: pathStart,
completionEnd: end,
isMidInputSlashCompletion: false,
slashCompletionContext: null,
};
}
}

if (cursorRow === 0 && isSlashCommand(currentLine.trim())) {
return {
completionMode: CompletionMode.SLASH,
query: currentLine,
completionStart: 0,
completionEnd: currentLine.length,
isMidInputSlashCompletion: false,
};
}

const cursorOffset = logicalPosToOffset(buffer.lines, cursorRow, cursorCol);
const midCmd = findMidInputSlashCommand(buffer.text, cursorOffset);
if (
midCmd &&
!isExactMidInputModelInvocableCommand(
midCmd.partialCommand,
slashCommands,
)
) {
if (midCmd) {
const lineStartOffset = logicalPosToOffset(buffer.lines, cursorRow, 0);
const startOnLine = midCmd.startPos - lineStartOffset;
if (startOnLine >= 0) {
const isInitialCommandOnFirstLine =
cursorRow === 0 &&
isSlashCommand(currentLine.trim()) &&
startOnLine >= 0 &&
codePoints.slice(0, startOnLine).join('').trim().length === 0;
const inputCodePoints = toCodePoints(buffer.text);
const prefix = inputCodePoints.slice(0, midCmd.startPos).join('');
const isStackedSkill =
!isInitialCommandOnFirstLine &&
isValidStackedSkillPrefix(prefix, slashCommands);
const isSlashLedInput = isSlashCommand(prefix.trimStart());
const isRegularMidInput =
!isInitialCommandOnFirstLine && !isSlashLedInput;
if (
startOnLine >= 0 &&
(isStackedSkill ||
(isRegularMidInput &&
!isExactMidInputModelInvocableCommand(
midCmd.partialCommand,
slashCommands,
)))
) {
return {
completionMode: CompletionMode.SLASH,
query: midCmd.token,
completionStart: startOnLine,
completionEnd: startOnLine + midCmd.token.length,
isMidInputSlashCompletion: true,
slashCompletionContext: isStackedSkill
? SlashCompletionContext.STACKED_SKILL
: SlashCompletionContext.MID_INPUT,
};
}
}

if (cursorRow === 0 && isSlashCommand(currentLine.trim())) {
return {
completionMode: CompletionMode.SLASH,
query: currentLine,
completionStart: 0,
completionEnd: currentLine.length,
slashCompletionContext: SlashCompletionContext.LINE_START,
};
}

return {
completionMode: CompletionMode.IDLE,
query: null,
completionStart: -1,
completionEnd: -1,
isMidInputSlashCompletion: false,
slashCompletionContext: null,
};
}, [cursorRow, cursorCol, buffer.lines, buffer.text, slashCommands]);

const slashCommandsForCompletion = useMemo(
() =>
isMidInputSlashCompletion
? slashCommands.filter(isMidInputCompletableCommand)
: slashCommands,
[isMidInputSlashCompletion, slashCommands],
);
const isMidInputSlashCompletion =
slashCompletionContext === SlashCompletionContext.MID_INPUT ||
slashCompletionContext === SlashCompletionContext.STACKED_SKILL;

const slashCommandsForCompletion = useMemo(() => {
if (slashCompletionContext === SlashCompletionContext.STACKED_SKILL) {
return slashCommands.filter(isStackedSkillCompletableCommand);
}
if (slashCompletionContext === SlashCompletionContext.MID_INPUT) {
return slashCommands.filter(isMidInputCompletableCommand);
}
return slashCommands;
}, [slashCompletionContext, slashCommands]);

const {
suggestions,
Expand Down Expand Up @@ -342,6 +375,10 @@ export function useCommandCompletion(
const midCmd = findMidInputSlashCommand(buffer.text, cursorOffset);
if (midCmd) {
if (isMidInputSlashCompletion) return null;
const prefix = toCodePoints(buffer.text)
.slice(0, midCmd.startPos)
.join('');
if (prefix.trimStart().startsWith('/')) return null;
const match = getBestSlashCommandMatch(
midCmd.partialCommand,
slashCommands,
Expand Down
Loading