MCPcopy Create free account
hub / github.com/cogentcore/core / RenameFile

Method RenameFile

filetree/file.go:146–193  ·  view source on GitHub ↗

RenameFile renames file to new name

(newpath string)

Source from the content-addressed store, hash-verified

144
145// RenameFile renames file to new name
146func (fn *Node) RenameFile(newpath string) error { //types:add
147 if fn.isExternal() {
148 return nil
149 }
150 root := fn.FileRoot
151 var err error
152 fn.closeBuf() // invalid after this point
153 orgpath := fn.Filepath
154 newpath, err = fn.Info.Rename(newpath)
155 if len(newpath) == 0 || err != nil {
156 return err
157 }
158 if fn.IsDir() {
159 if fn.FileRoot.isDirOpen(orgpath) {
160 fn.FileRoot.setDirOpen(core.Filename(newpath))
161 }
162 }
163 repo, _ := fn.Repo()
164 stored := false
165 if fn.IsDir() && !fn.HasChildren() {
166 err = os.Rename(string(orgpath), newpath)
167 } else if repo != nil && fn.Info.VCS >= vcs.Stored {
168 stored = true
169 err = repo.Move(string(orgpath), newpath)
170 } else {
171 err = os.Rename(string(orgpath), newpath)
172 if err != nil && errors.Is(err, syscall.ENOENT) { // some kind of bogus error it seems?
173 err = nil
174 }
175 }
176 if err == nil {
177 err = fn.Info.InitFile(newpath)
178 }
179 if err == nil {
180 fn.Filepath = core.Filename(fn.Info.Path)
181 fn.SetName(fn.Info.Name)
182 fn.SetText(fn.Info.Name)
183 }
184 // todo: if you add orgpath here to git, then it will show the rename in status
185 if stored {
186 fn.AddToVCS()
187 }
188 if root != nil {
189 root.UpdatePath(string(orgpath))
190 root.UpdatePath(newpath)
191 }
192 return err
193}
194
195// newFiles makes a new file in selected directory
196func (fn *Node) newFiles(filename string, addToVCS bool) { //types:add

Callers

nothing calls this directly

Calls 15

isExternalMethod · 0.95
closeBufMethod · 0.95
IsDirMethod · 0.95
RepoMethod · 0.95
AddToVCSMethod · 0.95
FilenameTypeAlias · 0.92
isDirOpenMethod · 0.80
setDirOpenMethod · 0.80
InitFileMethod · 0.80
UpdatePathMethod · 0.80
MoveMethod · 0.65
IsMethod · 0.65

Tested by

no test coverage detected