-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.py
48 lines (34 loc) · 1.42 KB
/
file.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import functools
import os
import sublime
from . import GitWindowCommand, git_root
class GitFileMove(GitWindowCommand):
def run(self, **args):
filename = self.relative_active_file_path()
branch, leaf = os.path.split(filename)
if not os.access(self.active_file_path(), os.W_OK):
sublime.error_message(leaf + " is read-only")
panel = self.get_window().show_input_panel(
"New path / name", filename, self.on_input, None, None
)
if branch:
# We want a trailing slash for selection purposes
branch = branch + os.path.sep
# Now, select just the base part of the filename
name, ext = os.path.splitext(leaf)
panel.sel().clear()
panel.sel().add(sublime.Region(len(branch), len(branch) + len(name)))
def on_input(self, newpath):
newpath = str(newpath) # avoiding unicode
if not newpath.strip():
return self.panel("No input received")
working_dir = git_root(self.get_working_dir())
newpath = os.path.join(working_dir, newpath)
command = ["git", "mv", "--", self.active_file_path(), newpath]
self.run_command(
command, functools.partial(self.on_done, newpath), working_dir=working_dir
)
def on_done(self, newpath, result):
if result.strip():
return self.panel(result)
self.active_view().retarget(newpath)