github.com/jmigpin/editor@v1.6.0/core/internalcmds/lsprotorename.go (about) 1 package internalcmds 2 3 import ( 4 "fmt" 5 6 "github.com/jmigpin/editor/core" 7 "github.com/jmigpin/editor/core/lsproto" 8 "github.com/jmigpin/editor/ui" 9 ) 10 11 func LSProtoRename(args0 *core.InternalCmdArgs) error { 12 erow := args0.ERow 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 := args0.Part.Args[1:] 23 if len(args) < 1 { 24 return fmt.Errorf("expecting at least 1 argument") 25 } 26 27 // new name argument "to" 28 to := args[len(args)-1].UnquotedString() 29 30 // before patching, check all affected files are not edited 31 prePatchFn := func(wecs []*lsproto.WorkspaceEditChange) error { 32 for _, wec := range wecs { 33 info, ok := args0.Ed.ERowInfo(wec.Filename) 34 if !ok { // erow not open 35 continue 36 } 37 if info.HasRowState(ui.RowStateEdited | ui.RowStateFsDiffer) { 38 return fmt.Errorf("row has edits, save first: %v", info.Name()) 39 } 40 } 41 return nil 42 } 43 44 // id offset to rename "from" 45 ta := erow.Row.TextArea 46 wecs, err := args0.Ed.LSProtoMan.TextDocumentRenameAndPatch(args0.Ctx, erow.Info.Name(), ta.RW(), ta.CursorIndex(), to, prePatchFn) 47 if err != nil { 48 return err 49 } 50 51 // reload filenames 52 args0.Ed.UI.RunOnUIGoRoutine(func() { 53 for _, wec := range wecs { 54 info, ok := args0.Ed.ERowInfo(wec.Filename) 55 if !ok { // erow not open 56 continue 57 } 58 if err := info.ReloadFile(); err != nil { 59 args0.Ed.Error(err) 60 } 61 } 62 }) 63 64 return nil 65 }