Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Improves copy, cut and paste for when target exists.

  • Loading branch information...
commit b4a2363f7c137c1eac40a526174103f3eba2bd06 1 parent 2b17622
@titoBouzout authored
View
83 SideBar.py
@@ -346,10 +346,12 @@ def is_enabled(self, paths = []):
class SideBarCopyCommand(sublime_plugin.WindowCommand):
def run(self, paths = []):
+
s = sublime.load_settings("SideBarEnhancements/Clipboard.sublime-settings")
items = []
for item in SideBarSelection(paths).getSelectedItemsWithoutChildItems():
items.append(item.path())
+
s.set('cut', '')
s.set('copy', "\n".join(items))
if len(items) > 1 :
@@ -361,11 +363,14 @@ def is_enabled(self, paths = []):
return len(paths) > 0
class SideBarPasteCommand(sublime_plugin.WindowCommand):
- def run(self, paths = [], in_parent = 'False'):
+ def run(self, paths = [], in_parent = 'False', test = 'True', replace = 'False'):
s = sublime.load_settings("SideBarEnhancements/Clipboard.sublime-settings")
cut = s.get('cut', '')
copy = s.get('copy', '')
+
+ already_exists_paths = []
+
if SideBarSelection(paths).len() > 0:
if in_parent == 'False':
location = SideBarSelection(paths).getSelectedItems()[0].path()
@@ -382,34 +387,76 @@ def run(self, paths = [], in_parent = 'False'):
for path in cut:
path = SideBarItem(path, os.path.isdir(path))
new = os.path.join(location.path(), path.name())
- try:
- if not path.move(new):
- sublime.error_message("Unable to cut and paste, destination exists.")
- return
- except:
- sublime.error_message("Unable to move:\n\n"+path.path()+"\n\nto\n\n"+new)
- return
+ if test == 'True' and os.path.exists(new):
+ already_exists_paths.append(new)
+ elif test == 'False':
+ if os.path.exists(new) and replace == 'False':
+ pass
+ else:
+ #try:
+ if not path.move(new, replace == 'True'):
+ sublime.error_message("Unable to cut and paste, destination exists.")
+ return
+ #except:
+ # sublime.error_message("Unable to move:\n\n"+path.path()+"\n\nto\n\n"+new)
+ # return
if copy != '':
copy = copy.split("\n")
for path in copy:
path = SideBarItem(path, os.path.isdir(path))
new = os.path.join(location.path(), path.name())
- try:
- if not path.copy(new):
- sublime.error_message("Unable to copy and paste, destination exists.")
- return
- except:
- sublime.error_message("Unable to copy:\n\n"+path.path()+"\n\nto\n\n"+new)
- return
-
- cut = s.set('cut', '')
- SideBarProject().refresh();
+ if test == 'True' and os.path.exists(new):
+ already_exists_paths.append(new)
+ elif test == 'False':
+ if os.path.exists(new) and replace == 'False':
+ pass
+ else:
+ #try:
+ if not path.copy(new, replace == 'True'):
+ sublime.error_message("Unable to copy and paste, destination exists.")
+ return
+ #except:
+ # sublime.error_message("Unable to copy:\n\n"+path.path()+"\n\nto\n\n"+new)
+ # return
+
+ if test == 'True' and len(already_exists_paths):
+ self.confirm(paths, in_parent, already_exists_paths)
+ elif test == 'True' and not len(already_exists_paths):
+ self.run(paths, in_parent, 'False', 'False')
+ elif test == 'False':
+ cut = s.set('cut', '')
+ SideBarProject().refresh();
+
+ def confirm(self, paths, in_parent, data):
+ import functools
+ window = sublime.active_window()
+ window.show_input_panel("BUG!", '', '', None, None)
+ window.run_command('hide_panel');
+
+ yes = []
+ yes.append('Yes, Replace the following items:');
+ for item in data:
+ yes.append(SideBarItem(item, os.path.isdir(item)).pathWithoutProject())
+
+ no = []
+ no.append('No');
+ no.append('Continue copying without replacing');
+
+ window.show_quick_panel([yes, no], functools.partial(self.on_done, paths, in_parent))
+
+ def on_done(self, paths, in_parent, result):
+ if result != -1:
+ if result == 0:
+ self.run(paths, in_parent, 'False', 'True')
+ else:
+ self.run(paths, in_parent, 'False', 'False')
def is_enabled(self, paths = [], in_parent = False):
s = sublime.load_settings("SideBarEnhancements/Clipboard.sublime-settings")
return s.get('cut', '') + s.get('copy', '') != '' and len(SideBarSelection(paths).getSelectedDirectoriesOrDirnames()) == 1
+
class SideBarCopyNameCommand(sublime_plugin.WindowCommand):
def run(self, paths = []):
to_copy = []
View
90 SideBarItem.py
@@ -2,6 +2,7 @@
import sublime
import os
import re
+import shutil
from SideBarProject import SideBarProject
from Utils import Object
@@ -179,21 +180,83 @@ def create(self):
self.dirnameCreate()
self.write('')
- def move(self, location):
+ def copy(self, location, replace = False):
location = SideBarItem(location, os.path.isdir(location));
+ if location.exists() and replace == False:
+ return False
+ elif location.exists() and location.isFile():
+ os.remove(location.path())
+
+ location.dirnameCreate();
+ if self.isDirectory():
+ if location.exists():
+ self.copy_recursive(self.path(), location.path())
+ else:
+ shutil.copytree(self.path(), location.path())
+ else:
+ shutil.copy2(self.path(), location.path())
+ return True
+
+ def copy_recursive(self, _from, _to):
+
+ if os.path.isfile(_from) or os.path.islink(_from):
+ try:
+ os.makedirs(os.path.dirname(_to));
+ except:
+ pass
+ if os.path.exists(_to):
+ os.remove(_to)
+ shutil.copy2(_from, _to)
+ else:
+ try:
+ os.makedirs(_to);
+ except:
+ pass
+ for content in os.listdir(_from):
+ __from = os.path.join(_from, content)
+ __to = os.path.join(_to, content)
+ self.copy_recursive(__from, __to)
+
+ def move(self, location, replace = False):
+ location = SideBarItem(location, os.path.isdir(location));
+ if location.exists() and replace == False:
+ return False
+ elif location.exists() and location.isFile():
+ os.remove(location.path())
+
if self.path().lower() == location.path().lower():
location.dirnameCreate();
os.rename(self.path(), location.path()+'.sublime-temp')
os.rename(location.path()+'.sublime-temp', location.path())
self._move_moveViews(self.path(), location.path())
- return True
- elif location.exists():
- return False
else:
location.dirnameCreate();
- os.rename(self.path(), location.path())
+ if location.exists():
+ self.move_recursive(self.path(), location.path())
+ else:
+ os.rename(self.path(), location.path())
self._move_moveViews(self.path(), location.path())
- return True
+ return True
+
+ def move_recursive(self, _from, _to):
+ if os.path.isfile(_from) or os.path.islink(_from):
+ try:
+ os.makedirs(os.path.dirname(_to));
+ except:
+ pass
+ if os.path.exists(_to):
+ os.remove(_to)
+ os.rename(_from, _to)
+ else:
+ try:
+ os.makedirs(_to);
+ except:
+ pass
+ for content in os.listdir(_from):
+ __from = os.path.join(_from, content)
+ __to = os.path.join(_to, content)
+ self.move_recursive(__from, __to)
+ os.rmdir(_from)
def _move_moveViews(self, old, location):
for window in sublime.windows():
@@ -294,17 +357,4 @@ def _move_restoreView(self, view, options):
if len(rs):
view.add_regions("bookmarks", rs, "bookmarks", "bookmark", sublime.HIDDEN | sublime.PERSISTENT)
- view.set_viewport_position(options.scroll, False)
-
- def copy(self, location):
- location = SideBarItem(location, os.path.isdir(location));
- if location.exists():
- return False
- else:
- location.dirnameCreate();
- import shutil
- if self.isDirectory():
- shutil.copytree(self.path(), location.path())
- else:
- shutil.copy2(self.path(), location.path())
- return True
+ view.set_viewport_position(options.scroll, False)
View
4 SideBarSelection.py
@@ -20,7 +20,7 @@ def __init__(self, paths = []):
except:
paths = []
self._paths = paths
-
+ self._paths.sort()
self._obtained_selection_information_basic = False
self._obtained_selection_information_extended = False
@@ -167,7 +167,7 @@ def _itemsWithoutContainingChildItems(self, items, item):
continue
else:
new_list.append(i)
- if item.find(i) == 0:
+ if (item+'\\').find(i+'\\') == 0 or (item+'/').find(i+'/') == 0:
add = False
if add:
new_list.append(item)
View
10 StatusBarFileSize.py
@@ -8,11 +8,17 @@ class StatusBarFileSize(sublime_plugin.EventListener):
def on_load(self, v):
if s.get('statusbar_file_size') and v.file_name():
- self.show(v, size(getsize(v.file_name())))
+ try:
+ self.show(v, size(getsize(v.file_name())))
+ except:
+ pass
def on_post_save(self, v):
if s.get('statusbar_file_size') and v.file_name():
- self.show(v, size(getsize(v.file_name())))
+ try:
+ self.show(v, size(getsize(v.file_name())))
+ except:
+ pass
def show(self, v, size):
v.set_status('statusbar_file_size', size);
View
10 StatusBarModifiedTime.py
@@ -7,11 +7,17 @@ class StatusBarModifiedTime(sublime_plugin.EventListener):
def on_load(self, v):
if s.get('statusbar_modified_time') and v.file_name():
- self.show(v, getmtime(v.file_name()))
+ try:
+ self.show(v, getmtime(v.file_name()))
+ except:
+ pass
def on_post_save(self, v):
if s.get('statusbar_modified_time') and v.file_name():
- self.show(v, getmtime(v.file_name()))
+ try:
+ self.show(v, getmtime(v.file_name()))
+ except:
+ pass
def show(self, v, mtime):
v.set_status('statusbar_modified_time', time.strftime(s.get('statusbar_modified_time_format'), time.localtime(mtime)));
Please sign in to comment.
Something went wrong with that request. Please try again.