RenameFile renames file to new name
(newpath string)
| 144 | |
| 145 | // RenameFile renames file to new name |
| 146 | func (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 |
| 196 | func (fn *Node) newFiles(filename string, addToVCS bool) { //types:add |
nothing calls this directly
no test coverage detected