forked from evandroforks/SublimePackageDefault
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename.py
70 lines (54 loc) · 1.85 KB
/
rename.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import sublime_plugin
import sublime
import os
class FileNameInputHandler(sublime_plugin.TextInputHandler):
def __init__(self, view):
self.view = view
self.file_name = os.path.basename(self.view.file_name() or self.view.name())
def name(self):
return "new_name"
def placeholder(self):
return "New File Name"
def initial_text(self):
return self.file_name
def initial_selection(self):
name = os.path.splitext(self.file_name)[0]
return [(0, len(name))]
def validate(self, name):
if self.view.file_name() is None:
return True
else:
return len(name) > 0
class RenameFileCommand(sublime_plugin.WindowCommand):
def run(self, new_name):
view = self.window.active_view()
old = view.file_name()
if old is None:
# Unsaved file
view.set_name(new_name)
else:
branch, leaf = os.path.split(old)
new = os.path.join(branch, new_name)
if new == old:
return
try:
if os.path.isfile(new) and not self.is_case_change(old, new):
raise OSError("File already exists")
os.rename(old, new)
if view:
view.retarget(new)
except OSError as e:
sublime.status_message("Unable to rename: " + str(e))
except:
sublime.status_message("Unable to rename")
def input(self, args):
if "new_name" not in args:
return FileNameInputHandler(self.window.active_view())
else:
return None
def is_case_change(self, old, new):
if old.lower() != new.lower():
return False
if os.stat(old).st_ino != os.stat(new).st_ino:
return False
return True