Skip to content

Commit 7e1d9f0

Browse files
authored
Shortcut for creating untitled prompt file (microsoft#252256)
* Shortcut for creating untitled prompt file * resolve comments
1 parent b227e84 commit 7e1d9f0

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

src/vs/workbench/contrib/chat/browser/promptSyntax/newPromptFileActions.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { isEqual } from '../../../../../base/common/resources.js';
77
import { URI } from '../../../../../base/common/uri.js';
88
import { getCodeEditor } from '../../../../../editor/browser/editorBrowser.js';
99
import { SnippetController2 } from '../../../../../editor/contrib/snippet/browser/snippetController2.js';
10-
import { localize } from '../../../../../nls.js';
10+
import { localize, localize2 } from '../../../../../nls.js';
1111
import { Action2, MenuId, registerAction2 } from '../../../../../platform/actions/common/actions.js';
1212
import { ICommandService } from '../../../../../platform/commands/common/commands.js';
1313
import { ContextKeyExpr } from '../../../../../platform/contextkey/common/contextkey.js';
@@ -168,8 +168,52 @@ class NewModeFileAction extends AbstractNewPromptFileAction {
168168
}
169169
}
170170

171+
class NewUntitledPromptFileAction extends Action2 {
172+
constructor() {
173+
super({
174+
id: 'workbench.command.new.untitled.prompt',
175+
title: localize2('commands.new.untitled.prompt.title', "New Untitled Prompt File"),
176+
f1: true,
177+
precondition: ContextKeyExpr.and(PromptsConfig.enabledCtx, ChatContextKeys.enabled),
178+
category: CHAT_CATEGORY,
179+
keybinding: {
180+
weight: KeybindingWeight.WorkbenchContrib
181+
},
182+
});
183+
}
184+
185+
public override async run(accessor: ServicesAccessor) {
186+
const editorService = accessor.get(IEditorService);
187+
const snippetService = accessor.get(ISnippetsService);
188+
189+
const languageId = getLanguageIdForPromptsType(PromptsType.prompt);
190+
191+
const input = await editorService.openEditor({
192+
resource: undefined,
193+
languageId,
194+
options: {
195+
pinned: true
196+
}
197+
});
198+
199+
const editor = getCodeEditor(editorService.activeTextEditorControl);
200+
if (editor && editor.hasModel()) {
201+
const snippets = await snippetService.getSnippets(languageId, { fileTemplateSnippets: true, noRecencySort: true, includeNoPrefixSnippets: true });
202+
if (snippets.length > 0) {
203+
SnippetController2.get(editor)?.apply([{
204+
range: editor.getModel().getFullModelRange(),
205+
template: snippets[0].body
206+
}]);
207+
}
208+
}
209+
210+
return input;
211+
}
212+
}
213+
171214
export function registerNewPromptFileActions(): void {
172215
registerAction2(NewPromptFileAction);
173216
registerAction2(NewInstructionsFileAction);
174217
registerAction2(NewModeFileAction);
218+
registerAction2(NewUntitledPromptFileAction);
175219
}

0 commit comments

Comments
 (0)