Skip to content

Commit 34bef58

Browse files
committed
feat: allow completion to be in a new file
1 parent f1463c2 commit 34bef58

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@
9595
"type": "string",
9696
"description": "Prompt to use for the action"
9797
},
98+
"target": {
99+
"type": "string",
100+
"enum": [
101+
"inplace",
102+
"newfile"
103+
],
104+
"default": "inplace",
105+
"description": "Where to put the result of the action"
106+
},
98107
"git": {
99108
"type": "object",
100109
"description": "Git options",

src/commands/run_action_command.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Selection, TextEditor } from 'vscode'
2-
import { ProgressLocation, Range, extensions, window } from 'vscode'
2+
import { Position, ProgressLocation, Range, Uri, extensions, window, workspace } from 'vscode'
33
import type { AI, Action, Prompt } from '../types/index.js'
44
import { ai as aiIndex } from '../ai/index.js'
55
import type { BaseAI } from '../ai/base_ai.js'
@@ -13,7 +13,6 @@ export class RunActionCommand extends BaseCommand {
1313
const configuration = await this.getAIConfiguration(action)
1414
const prompt = await this.getPrompt(action)
1515

16-
this.logger.log('Commit changes before action...')
1716
await this.commitWithAction(action, 'before')
1817

1918
const message = `Ask '${configuration.name}' to '${prompt.name}'...`
@@ -27,11 +26,14 @@ export class RunActionCommand extends BaseCommand {
2726
const ai = await this.createAI(configuration)
2827
const completion = await ai.ask(prompt.content, this.getActiveEditorText())
2928

30-
this.logger.log('Apply changes...')
31-
await this.applyChanges(completion)
29+
if (action.target === 'inplace') {
30+
await this.applyChanges(completion)
31+
}
32+
else if (action.target === 'newfile') {
33+
await this.showCompletion(completion)
34+
}
3235
})
3336

34-
this.logger.log('Commit changes after action...')
3537
await this.commitWithAction(action, 'after')
3638

3739
this.logger.log(
@@ -137,6 +139,8 @@ export class RunActionCommand extends BaseCommand {
137139
}
138140

139141
protected async applyChanges(content: string): Promise<void> {
142+
this.logger.log('Apply changes...')
143+
140144
this.getActiveEditor().edit((editBuilder) => {
141145
const range = this.hasActiveEditorSelection() ? this.getActiveEditorSelection() : new Range(0, 0, this.getActiveEditorText().length, 0)
142146

@@ -147,7 +151,16 @@ export class RunActionCommand extends BaseCommand {
147151
await this.saveActiveEditor()
148152
}
149153

154+
protected async showCompletion(content: string): Promise<void> {
155+
this.logger.log('Show completion...')
156+
157+
const editor = await workspace.openTextDocument({ content, language: 'plaintext' })
158+
await window.showTextDocument(editor)
159+
}
160+
150161
protected async commitWithAction(action: Action, when: 'before' | 'after'): Promise<void> {
162+
this.logger.log(`Commit changes ${when} action...`)
163+
151164
let commitMessage = when === 'before' ? action.git?.commitMessageBeforeAction : action.git?.commitMessageAfterAction
152165

153166
if (!commitMessage) {

src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export interface Action {
1616
name: string
1717
ai: AI['name']
1818
prompt: Prompt['name']
19+
target: 'inplace' | 'newfile'
1920
git?: {
2021
commitMessageBeforeAction?: '__ask__' | (string & {})
2122
commitMessageAfterAction?: '__ask__' | (string & {})

0 commit comments

Comments
 (0)