Skip to content

Open source AI coding agent. Designed for large projects and real world tasks.

Add editor selection and config improvements

Dane Schneider44fbda8fb523
diff --git a/app/cli/cmd/model_helpers.go b/app/cli/cmd/model_helpers.go
index 5ff95e0d..8cd6368f 100644
--- a/app/cli/cmd/model_helpers.go
+++ b/app/cli/cmd/model_helpers.go
@@ -5,6 +5,7 @@ import (
"os"
"plandex-cli/lib"
"plandex-cli/term"
+ shared "plandex-shared"
"github.com/fatih/color"
)
@@ -36,7 +37,7 @@ type maybePromptAndOpenModelsFileResult struct {
jsonData []byte
}
-func maybePromptAndOpenModelsFile(filePath, pathArg, cmd string) maybePromptAndOpenModelsFileResult {
+func maybePromptAndOpenModelsFile(filePath, pathArg, cmd string, defaultConfig *shared.PlanConfig, planConfig *shared.PlanConfig) maybePromptAndOpenModelsFileResult {
printManual := func() {
cmdPrefix := "\\"
@@ -48,7 +49,7 @@ func maybePromptAndOpenModelsFile(filePath, pathArg, cmd string) maybePromptAndO
Sprintf(" %s%s --save%s ", cmdPrefix, cmd, pathArg)))
}
- selectedEditor := lib.MaybePromptAndOpen(filePath)
+ selectedEditor := lib.MaybePromptAndOpen(filePath, defaultConfig, planConfig)
if selectedEditor {
fmt.Println("📝 Opened in editor")
diff --git a/app/cli/cmd/set_config.go b/app/cli/cmd/set_config.go
index 77517e53..4479a705 100644
--- a/app/cli/cmd/set_config.go
+++ b/app/cli/cmd/set_config.go
@@ -17,23 +17,23 @@ import (
)
func init() {
- RootCmd.AddCommand(setCmd)
- setCmd.AddCommand(defaultSetCmd)
+ RootCmd.AddCommand(setConfigCmd)
+ setConfigCmd.AddCommand(defaultSetConfigCmd)
RootCmd.AddCommand(setAutoCmd)
setAutoCmd.AddCommand(setAutoDefaultCmd)
}
-var setCmd = &cobra.Command{
+var setConfigCmd = &cobra.Command{
Use: "set-config [setting] [value]",
Short: "Update current plan config",
- Run: set,
+ Run: setConfig,
Args: cobra.MaximumNArgs(2),
}
-var defaultSetCmd = &cobra.Command{
+var defaultSetConfigCmd = &cobra.Command{
Use: "default [setting] [value]",
Short: "Update default plan config",
- Run: defaultSet,
+ Run: defaultSetConfig,
Args: cobra.MaximumNArgs(2),
}
@@ -52,14 +52,14 @@ var setAutoDefaultCmd = &cobra.Command{
}
func setAuto(cmd *cobra.Command, args []string) {
- set(cmd, append([]string{"auto-mode"}, args...))
+ setConfig(cmd, append([]string{"auto-mode"}, args...))
}
func setAutoDefault(cmd *cobra.Command, args []string) {
- defaultSet(cmd, append([]string{"auto-mode"}, args...))
+ defaultSetConfig(cmd, append([]string{"auto-mode"}, args...))
}
-func set(cmd *cobra.Command, args []string) {
+func setConfig(cmd *cobra.Command, args []string) {
auth.MustResolveAuthWithOrg()
lib.MustResolveProject()
@@ -120,7 +120,7 @@ func set(cmd *cobra.Command, args []string) {
term.PrintCmds("", "config", "config default", "set-config default")
}
-func defaultSet(cmd *cobra.Command, args []string) {
+func defaultSetConfig(cmd *cobra.Command, args []string) {
auth.MustResolveAuthWithOrg()
term.StartSpinner("")
@@ -271,6 +271,9 @@ func updateConfig(args []string, originalConfig *shared.PlanConfig) (string, *sh
} else if cfgSetting.ChoiceToKey != nil {
selection = cfgSetting.ChoiceToKey(selection)
}
+ } else if cfgSetting.EditorSetter != nil {
+ editor := lib.SelectEditor(false)
+ cfgSetting.EditorSetter(&config, editor.Name, editor.Cmd, editor.Args)
} else {
selection, err = term.GetRequiredUserStringInput(fmt.Sprintf("Set %s", cfgSetting.Name))
if err != nil {
diff --git a/app/cli/lib/editor.go b/app/cli/lib/editor.go
index 71984990..40c13d91 100644
--- a/app/cli/lib/editor.go
+++ b/app/cli/lib/editor.go
@@ -4,52 +4,152 @@ import (
"os"
"os/exec"
"path/filepath"
+ "plandex-cli/api"
"plandex-cli/term"
+ shared "plandex-shared"
"sort"
"strings"
)
-func MaybePromptAndOpen(path string) bool {
- editors := detectEditors()
- if len(editors) == 0 {
- // just exit if there are no editors available
+func MaybePromptAndOpen(path string, defaultConfig *shared.PlanConfig, planConfig *shared.PlanConfig) bool {
+ var cmd string
+ var args []string
+ var openManually bool
+
+ var checkConfig *shared.PlanConfig
+ if planConfig != nil {
+ checkConfig = planConfig
+ } else if defaultConfig != nil {
+ checkConfig = defaultConfig
+ } else {
+ term.OutputErrorAndExit("Missing config")
+ }
+
+ if checkConfig.EditorOpenManually {
return false
}
- opts := []string{}
- for _, c := range editors {
- opts = append(opts, "Open with "+c.name)
+
+ if checkConfig.EditorCommand == "" {
+ editorRes := SelectEditor(true)
+ cmd = editorRes.Cmd
+ args = editorRes.Args
+ openManually = editorRes.OpenManually
+
+ // update the default editor config
+ toUpdateDefault := *defaultConfig
+ toUpdateDefault.Editor = editorRes.Name
+ toUpdateDefault.EditorCommand = cmd
+ toUpdateDefault.EditorArgs = args
+ toUpdateDefault.EditorOpenManually = openManually
+
+ apiErr := api.Client.UpdateDefaultPlanConfig(shared.UpdateDefaultPlanConfigRequest{
+ Config: &toUpdateDefault,
+ })
+ if apiErr != nil {
+ term.OutputErrorAndExit("Error updating default config: %v", apiErr)
+ }
+
+ // also update the current plan config
+ if planConfig != nil {
+ toUpdate := *planConfig
+ toUpdate.Editor = editorRes.Name
+ toUpdate.EditorCommand = cmd
+ toUpdate.EditorArgs = args
+ toUpdate.EditorOpenManually = openManually
+ apiErr = api.Client.UpdatePlanConfig(CurrentPlanId, shared.UpdatePlanConfigRequest{
+ Config: &toUpdate,
+ })
+ if apiErr != nil {
+ term.OutputErrorAndExit("Error updating plan config: %v", apiErr)
+ }
+ }
+ } else {
+ cmd = checkConfig.EditorCommand
+ args = checkConfig.EditorArgs
}
- const openManually = "Open manually"
- opts = append(opts, openManually)
+ if openManually {
+ return false
+ }
- choice, err := term.SelectFromList("Open the file now?", opts)
+ err := exec.Command(cmd, append(args, path)...).Start()
if err != nil {
- term.OutputErrorAndExit("Error selecting editor: %v", err)
+ term.OutputErrorAndExit("Error opening template: %v", err)
}
- if choice == openManually {
- return false
+ return true
+}
+
+type SelectEditorResult struct {
+ Name string
+ Cmd string
+ Args []string
+ OpenManually bool
+}
+
+func SelectEditor(includeOpenManuallyOpt bool) SelectEditorResult {
+ editors := detectEditors()
+
+ opts := []string{}
+ for _, c := range editors {
+ opts = append(opts, c.name)
}
+ const otherOpt = "Other (custom command)"
+ opts = append(opts, otherOpt)
- var idx int
- for i, c := range opts {
- if c == choice {
- idx = i
- break
- }
+ const openManuallyOpt = "Open files manually"
+ if includeOpenManuallyOpt {
+ opts = append(opts, openManuallyOpt)
}
- if idx < len(editors) {
- sel := editors[idx]
- err = exec.Command(sel.cmd, append(sel.args, path)...).Start()
+ choice, err := term.SelectFromList("What's your preferred editor?", opts)
+ if err != nil {
+ term.OutputErrorAndExit("Error selecting editor: %v", err)
+ }
+
+ var name string
+ var cmd string
+ var args []string
+
+ if choice == otherOpt {
+ choice, err = term.GetRequiredUserStringInput("Enter the command to open the editor")
if err != nil {
- term.OutputErrorAndExit("Error opening template: %v", err)
+ term.OutputErrorAndExit("Error getting editor command: %v", err)
+ }
+ name = choice
+ parts := strings.Fields(choice)
+ if len(parts) == 0 {
+ term.OutputErrorAndExit("Invalid editor command: %s", choice)
+ }
+ cmd = parts[0]
+ if len(parts) > 1 {
+ args = parts[1:]
}
- return true
+ } else if choice == openManuallyOpt {
+ return SelectEditorResult{
+ Name: "Open manually",
+ Cmd: "",
+ Args: []string{},
+ OpenManually: true,
+ }
+ } else {
+ var candidate editorCandidate
+ for _, c := range editors {
+ if c.name == choice {
+ candidate = c
+ break
+ }
+ }
+ name = candidate.name
+ cmd = candidate.cmd
+ args = candidate.args
}
- return false
+ return SelectEditorResult{
+ Name: name,
+ Cmd: cmd,
+ Args: args,
+ }
}
type editorCandidate struct {