Skip to content

Commit 88f38c5

Browse files
committed
Update edit command, refactor file handling, and update edit prompt
1 parent 823bc75 commit 88f38c5

File tree

3 files changed

+18
-41
lines changed

3 files changed

+18
-41
lines changed

cmd/edit.go

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
2+
Copyright © 2023 Chandler <chandler@chand1012.dev>
33
*/
44
package cmd
55

@@ -20,13 +20,12 @@ import (
2020
// editCmd represents the edit command
2121
var editCmd = &cobra.Command{
2222
Use: "edit",
23-
Short: "A brief description of your command",
24-
Long: `A longer description that spans multiple lines and likely contains examples
25-
and usage of using your command. For example:
23+
Short: "Edit a file using AI",
24+
Long: `OttoDocs Edit allows you to use AI to help edit your code files.
25+
Provide a file name and a goal, and OttoDocs will return a generated version of the file.
26+
You can even specify the starting and ending lines for the edit, or choose to append the results to the file:
2627
27-
Cobra is a CLI library for Go that empowers applications.
28-
This application is a tool to generate the needed files
29-
to quickly create a Cobra application.`,
28+
Example: otto edit main.go --start 1 --end 10 --goal "Refactor the function"`,
3029
Aliases: []string{"e"},
3130
PreRun: func(cmd *cobra.Command, args []string) {
3231
if verbose {
@@ -47,25 +46,11 @@ to quickly create a Cobra application.`,
4746

4847
fileName := args[0]
4948

50-
// check if the file exists
51-
exists, err := utils.FileExists(fileName)
52-
if err != nil {
53-
log.Errorf("Error checking if file exists: %s", err)
54-
os.Exit(1)
55-
}
56-
57-
if !exists {
58-
// create a new empty file
59-
err = os.WriteFile(fileName, []byte(""), 0644)
60-
if err != nil {
61-
log.Errorf("Error creating new file: %s", err)
62-
os.Exit(1)
63-
}
64-
}
65-
6649
// load the file
6750
contents, err := utils.LoadFile(fileName)
68-
if err != nil {
51+
if os.IsNotExist(err) {
52+
contents = ""
53+
} else if err == nil {
6954
log.Errorf("Error loading file: %s", err)
7055
os.Exit(1)
7156
}
@@ -75,6 +60,7 @@ to quickly create a Cobra application.`,
7560
os.Exit(1)
7661
}
7762

63+
var editCode string
7864
if endLine != 0 {
7965
// get the lines to edit
8066
lines := strings.Split(contents, "\n")
@@ -83,7 +69,7 @@ to quickly create a Cobra application.`,
8369
os.Exit(1)
8470
}
8571

86-
contents = strings.Join(lines[startLine-1:endLine], "\n")
72+
editCode = strings.Join(lines[startLine-1:endLine], "\n")
8773
} else {
8874
endLine = len(strings.Split(contents, "\n"))
8975
}
@@ -98,7 +84,12 @@ to quickly create a Cobra application.`,
9884
}
9985
}
10086

101-
prompt := constants.EDIT_CODE_PROMPT + "Goal: " + chatPrompt + "\n\n" + strings.TrimRight(contents, " \n")
87+
var prompt string
88+
if editCode != "" {
89+
prompt = constants.EDIT_CODE_PROMPT + "\nEDIT: " + editCode + "\n\nGOAL: " + chatPrompt + "\n\nFILE: " + contents
90+
} else {
91+
prompt = constants.EDIT_CODE_PROMPT + "\nGOAL: " + chatPrompt + "\n\nFILE: " + contents
92+
}
10293

10394
stream, err := ai.SimpleStreamRequest(prompt, c)
10495
if err != nil {

pkg/constants/prompts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ var PR_BODY_PROMPT string = "You are a helpful assistant who writes pull request
4040

4141
var COMPRESS_DIFF_PROMPT string = "You are a helpful assistant who describes git diff changes. You will be given a Git diff and you should use it to create a description of the changes. The description should be no longer than 75 characters long and should describe the changes in the diff. Do not include the file names in the description."
4242

43-
var EDIT_CODE_PROMPT string = `You are a helpful assistant who edits code. You will be given a file what you are trying to accomplish in the edit and you should edit it to the best of your abilities. You should only output the code and should not output any other text.\n`
43+
var EDIT_CODE_PROMPT string = `You are a helpful assistant who edits code. You will be given a file what you are trying to accomplish in the edit and you should edit it to the best of your abilities. Only edit the code you are told. All code to edit will be preceded by "EDIT:". If "EDIT:" is omitted, assume you must edit the entire file. The goal of the edit will be preceded by "GOAL:". The entire file will be preceded by "FILE:". If there is not content after "FILE:", assume you are writing new code. Make sure to use the language specified in the task. The output code should be unformatted. Use no markdown.\n`

pkg/utils/misc.go

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)