github.com/jmigpin/editor@v1.6.0/core/internalcmds/gorename.go (about) 1 package internalcmds 2 3 import ( 4 "fmt" 5 6 "github.com/jmigpin/editor/core" 7 "github.com/jmigpin/editor/ui" 8 ) 9 10 func GoRename(args0 *core.InternalCmdArgs) error { 11 erow := args0.ERow 12 part := args0.Part 13 14 if !erow.Info.IsFileButNotDir() { 15 return fmt.Errorf("not a file") 16 } 17 18 if erow.Row.HasState(ui.RowStateEdited | ui.RowStateFsDiffer) { 19 return fmt.Errorf("row has edits, save first") 20 } 21 22 args := part.Args[1:] 23 if len(args) < 1 { 24 return fmt.Errorf("expecting at least 1 argument") 25 } 26 27 // optional "-all" (only as first arg) for full rename (not an option on either gorename/gopls) 28 isF := false 29 if args[0].String() == "-all" { 30 isF = true 31 args = args[1:] 32 } 33 34 // new name argument "to" 35 to := args[len(args)-1].UnquotedString() 36 37 // allow other args 38 otherArgs := []string{} 39 for i := 0; i < len(args)-1; i++ { 40 otherArgs = append(otherArgs, args[i].UnquotedString()) 41 } 42 43 // id offset to rename "from" 44 offset := erow.Row.TextArea.CursorIndex() 45 46 // command 47 offsetStr := fmt.Sprintf("%v:#%v", erow.Info.Name(), offset) 48 cargs := []string{} 49 if isF { 50 cargs = []string{"gorename", "-offset", offsetStr, "-to", to} 51 cargs = append(cargs, otherArgs...) 52 } else { 53 cargs = append([]string{"gopls", "rename"}, append(otherArgs, []string{"-w", offsetStr, to}...)...) 54 } 55 56 // TODO: reload all changed files (check stdout) 57 58 reloadOnNoErr := func(err error) { 59 if err == nil { 60 erow.Ed.UI.RunOnUIGoRoutine(func() { 61 erow.Reload() 62 }) 63 } 64 } 65 66 core.ExternalCmdFromArgs(erow, cargs, reloadOnNoErr, nil) 67 68 return nil 69 }