feat(SublimeText2.GitPackages): cache packages

This commit is contained in:
Iristyle
2013-04-04 08:55:33 -04:00
parent c3efdad2c2
commit c0f9c6d45a
109 changed files with 15317 additions and 0 deletions

View File

@@ -0,0 +1,317 @@
# coding=utf8
import sublime
import os
import subprocess
from SideBarItem import SideBarItem
class Object():
pass
s = sublime.load_settings('SideBarGit.sublime-settings')
path_to_git_unixes = s.get('path_to_git_unixes');
class SideBarGit:
last_stdout = ''
def run(
self,
object,
modal = False,
background = False,
refresh_funct_view = False,
refresh_funct_command = False,
refresh_funct_item = False,
refresh_funct_to_status_bar = False,
refresh_funct_title = False,
refresh_funct_no_results = False,
refresh_funct_syntax_file = False
):
if not refresh_funct_view:
pass
else:
object = Object()
object.command = refresh_funct_command
object.item = SideBarItem(refresh_funct_item, os.path.isdir(refresh_funct_item))
object.to_status_bar = refresh_funct_to_status_bar
object.title = refresh_funct_title
object.no_results = refresh_funct_no_results
object.syntax_file = refresh_funct_syntax_file
debug = False
if debug:
print '----------------------------------------------------------'
print 'GIT:'
print object.command
print 'CWD:'
print object.item.forCwdSystemPath()
print 'PATH:'
print object.item.forCwdSystemName()
failed = False
if sublime.platform() == 'windows':
object.command = map(self.escapeCMDWindows, object.command)
if sublime.platform() is not 'windows' and object.command[0] == 'git':
if path_to_git_unixes != '':
object.command[0] = s.get('path_to_git_unixes')
elif os.path.exists('/usr/local/git/bin'):
object.command[0] = '/usr/local/git/bin/git'
cwd = object.item.forCwdSystemPath()
try:
if sublime.platform() == 'windows':
process = subprocess.Popen(
#" ".join(object.command),
object.command,
cwd=cwd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True,
universal_newlines=True)
else:
process = subprocess.Popen(
object.command,
cwd=cwd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=False,
universal_newlines=True)
if background:
if debug:
print 'SUCCESS'
print '----------------------------------------------------------'
return True
stdout, stderr = process.communicate()
SideBarGit.last_stdout = str(stdout).rstrip()
self.last_stdout = str(stdout).rstrip()
stdout = stdout.strip()
if stdout.find('fatal:') == 0 or stdout.find('error:') == 0 or stdout.find('Permission denied') == 0 or stderr:
print 'FAILED'
failed = True
else:
if debug:
print 'SUCCESS'
if stdout:
if debug:
print 'STDOUT'
print stdout
if stderr:
print 'STDERR'
print stderr
except OSError as (errno, strerror):
print 'FAILED'
failed = True
print errno
print strerror
SideBarGit.last_stdout = ''
self.last_stdout = ''
except IOError as (errno, strerror):
print 'FAILED'
failed = True
print errno
print strerror
SideBarGit.last_stdout = ''
self.last_stdout = ''
if debug:
print '----------------------------------------------------------'
try:
object.to_status_bar
except:
object.to_status_bar = False
try:
object.silent
return
except:
pass
if failed:
try:
strerror
if errno == 2:
self.alert(strerror+'\nPossible error:\n'+object.command[0]+' not found on $PATH')
else:
self.alert(strerror)
return False
except:
if not stdout and not stderr:
return False
if stdout.find('Permission denied') == 0 or stdout.find('fatal: The remote end hung up unexpectedly') == 0:
self.alert((stdout or '')+'\n'+(stderr or '')+'\nPossible error:\nssh keys not in .ssh/ directory or keys not opened')
else:
self.alert((stdout or '')+'\n'+(stderr or ''))
return False
else:
if stdout != '' and refresh_funct_view == False and (object.to_status_bar or " ".join(object.command).find('git push') == 0 or stdout.find('nothing to commit') == 0):
self.status(stdout)
else:
if stdout == '' and refresh_funct_view == False:
try:
self.status(object.no_results)
except:
self.status('No output to show')
return True
if stdout == '' and refresh_funct_view != False:
try:
stdout = object.no_results
except:
stdout = 'No output to show'
if stdout == '':
return True
if refresh_funct_view == False:
view = sublime.active_window().new_file()
else:
view = refresh_funct_view
try:
view.set_name(object.title.decode('utf-8'))
except:
view.set_name('No Title')
try:
if object.syntax_file != False:
view.set_syntax_file(object.syntax_file)
except:
pass
try:
object.word_wrap
view.settings().set('word_wrap', False)
except:
pass
view.settings().set('fallback_encoding', 'UTF-8')
view.settings().set('encoding', 'UTF-8')
view.settings().set('default_dir', object.item.dirname())
view.set_scratch(True)
if refresh_funct_view == False:
view.settings().set('SideBarGitIsASideBarGitTab', True)
view.settings().set('SideBarGitCommand', object.command)
view.settings().set('SideBarGitModal', modal)
view.settings().set('SideBarGitBackground', background)
view.settings().set('SideBarGitItem', object.item.path())
try:
view.settings().set('SideBarGitToStatusBar', object.to_status_bar)
except:
view.settings().set('SideBarGitToStatusBar', False)
try:
view.settings().set('SideBarGitTitle', object.title)
except:
view.settings().set('SideBarGitTitle', 'No Title')
try:
view.settings().set('SideBarGitNoResults', object.no_results)
except:
view.settings().set('SideBarGitNoResults', 'No output to show')
try:
view.settings().set('SideBarGitSyntaxFile', object.syntax_file)
except:
view.settings().set('SideBarGitSyntaxFile', False)
content = "[SideBarGit@SublimeText "
content += object.item.name().decode('utf-8')
content += "/] "
content += (" ".join(object.command)).decode('utf-8')
content += "\n\n"
content += "# Improve this command, the output or the tab title by posting here:"
content += "\n"
content += "# http://www.sublimetext.com/forum/viewtopic.php?f=5&t=3405"
content += "\n"
content += "# Tip: F5 will run the command again and refresh the contents of this tab"
content += "\n\n"
try:
content += stdout
except:
content += unicode(stdout, 'UTF-8', errors='ignore')
edit = view.begin_edit()
view.replace(edit, sublime.Region(0, view.size()), content);
view.sel().clear()
view.sel().add(sublime.Region(0))
view.end_edit(edit)
return True
def confirm(self, message, function, arg1):
if int(sublime.version()) >= 2186:
if sublime.ok_cancel_dialog(u'Side Bar Git : '+message):
function(arg1, True)
else:
import functools
sublime.active_window().run_command('hide_panel');
sublime.active_window().show_input_panel("Confirmation Required:", message.decode('utf-8'), functools.partial(function, arg1, True), None, None)
def prompt(self, message, default, function, arg1):
import functools
sublime.active_window().run_command('hide_panel');
sublime.active_window().show_input_panel(message.decode('utf-8'), default.decode('utf-8'), functools.partial(function, arg1, True), None, None)
def alert(self, message):
try:
sublime.error_message('Git : '+(message.decode('utf-8')))
except:
try:
sublime.error_message('Git : '+message)
except:
print message
def status(self, message):
message = message[:200] + (message[200:] and '')
message = message.replace('\n', ' ')
try:
v = sublime.active_window().active_view()
v.set_status('SideBarGit', 'Git : '+(message.decode('utf-8')))
sublime.set_timeout(lambda: SideBarGit().statusRemove(v), 16000)
except:#there is no tabs opened
sublime.status_message('Git : '+(message.decode('utf-8')))
def statusRemove(self, v):
try:
v.erase_status('SideBarGit')
except:#this view is not there
pass
def quickPanel(self, function, extra, data):
import functools
window = sublime.active_window()
# window.show_input_panel("BUG!", '', '', None, None)
# window.run_command('hide_panel');
data = [item[:70] for item in data]
window.show_quick_panel(data, functools.partial(self.quickPanelDone, function, extra, data))
def quickPanelDone(self, function, extra, data, result):
if result != -1:
function(extra, data, result)
def getSelectedRepos(self, items):
repos = []
reposTemp = []
for item in items:
original = item.path()
while not os.path.exists(item.join('.git')):
if item.dirname() == item.path():
break;
item.path(item.dirname())
if os.path.exists(item.join('.git')):
try:
index = reposTemp.index(item.path())
except ValueError:
reposTemp.append(item.path())
index = reposTemp.index(item.path())
repos.append(Object())
repos[index].repository = item
repos[index].items = []
repos[index].items.append(SideBarItem(original, os.path.isdir(original)))
return repos
def escapeCMDWindows(self, string):
return string.replace('^', '^^')

View File

@@ -0,0 +1,480 @@
# coding=utf8
import sublime
import os
import re
import shutil
from SideBarProject import SideBarProject
try:
import desktop
except:
pass
class Object():
pass
def expand_vars(path):
for k, v in os.environ.iteritems():
try:
# dirty hack, this should be autofixed in python3
k = unicode(k.encode('utf8'))
v = unicode(v.encode('utf8'))
path = path.replace(u'%'+k+'%', v).replace(u'%'+k.lower()+'%', v)
except:
pass
return path
class SideBarItem:
def __init__(self, path, is_directory):
self._path = path
self._is_directory = is_directory
def path(self, path = ''):
if path == '':
return self._path
else:
self._path = path
self._is_directory = os.path.isdir(path)
return path
def pathSystem(self):
import sys
return self.path().encode(sys.getfilesystemencoding())
def pathWithoutProject(self):
path = self.path()
for directory in SideBarProject().getDirectories():
path = path.replace(directory, '', 1)
return path.replace('\\', '/')
def pathProject(self):
path = self.path()
for directory in SideBarProject().getDirectories():
path2 = path.replace(directory, '', 1)
if path2 != path:
return directory
return False
def projectURL(self, type):
filename = os.path.normpath(os.path.join(sublime.packages_path(), '..', 'Settings', 'SideBarEnhancements.json'))
if os.path.lexists(filename):
#try:
import json
data = file(filename, 'r').read()
data = data.replace('\t', ' ').replace('\\', '/').replace('\\', '/').replace('//', '/').replace('//', '/').replace('http:/', 'http://').replace('https:/', 'https://')
data = json.loads(data, strict=False)
for path in data.keys():
path2 = expand_vars(path)
print '-------------------------------------------------------'
print 'searching:'
path2 = path2.replace('\\', '/').replace('\\', '/').replace('//', '/').replace('//', '/')
print path2
print 'in:'
path3 = self.path().replace('\\', '/').replace('\\', '/').replace('//', '/').replace('//', '/')
print path3
print '-------------------------------------------------------'
path4 = re.sub(re.compile("^"+re.escape(path2), re.IGNORECASE), '', path3);
print path4
if path4 != path3:
url = data[path][type]
if url:
if url[-1:] != '/':
url = url+'/'
import urllib
return url+(re.sub("^/", '', urllib.quote(path4.encode('utf-8'))));
#except:
# return False
else:
return False
def isUnderCurrentProject(self):
path = self.path()
path2 = self.path()
for directory in SideBarProject().getDirectories():
path2 = path2.replace(directory, '', 1)
return path != path2
def pathRelativeFromProject(self):
return re.sub('^/+', '', self.pathWithoutProject())
def pathRelativeFromProjectEncoded(self):
import urllib
return urllib.quote(self.pathRelativeFromProject().encode('utf-8'))
def pathRelativeFromView(self):
return os.path.relpath(self.path(), os.path.dirname(sublime.active_window().active_view().file_name())).replace('\\', '/')
def pathRelativeFromViewEncoded(self):
import urllib
return urllib.quote(os.path.relpath(self.path(), os.path.dirname(sublime.active_window().active_view().file_name())).replace('\\', '/').encode('utf-8'))
def pathAbsoluteFromProject(self):
return self.pathWithoutProject()
def pathAbsoluteFromProjectEncoded(self):
import urllib
return urllib.quote(self.pathAbsoluteFromProject().encode('utf-8'))
def uri(self):
import urllib
return 'file:'+urllib.pathname2url(self.path().encode('utf-8'));
def join(self, name):
return os.path.join(self.path(), name)
def dirname(self):
branch, leaf = os.path.split(self.path())
return branch;
def forCwdSystemPath(self):
if self.isDirectory():
return self.pathSystem()
else:
return self.dirnameSystem()
def forCwdSystemName(self):
if self.isDirectory():
return '.'
else:
path = self.pathSystem()
branch = self.dirnameSystem()
leaf = path.replace(branch, '', 1).replace('\\', '').replace('/', '')
return leaf
def forCwdSystemPathRelativeFrom(self, relativeFrom):
relative = SideBarItem(relativeFrom, os.path.isdir(relativeFrom))
path = self.pathSystem().replace(relative.pathSystem(), '', 1).replace('\\', '/')
if path == '':
return '.'
else:
return re.sub('^/+', '', path)
def forCwdSystemPathRelativeFromRecursive(self, relativeFrom):
relative = SideBarItem(relativeFrom, os.path.isdir(relativeFrom))
path = self.pathSystem().replace(relative.pathSystem(), '', 1).replace('\\', '/')
if path == '':
return '.'
else:
if self.isDirectory():
return re.sub('^/+', '', path)+'/'
else:
return re.sub('^/+', '', path)
def dirnameSystem(self):
import sys
return self.dirname().encode(sys.getfilesystemencoding())
def dirnameCreate(self):
try:
os.makedirs(self.dirname())
except:
pass
def name(self):
branch, leaf = os.path.split(self.path())
return leaf;
def nameSystem(self):
import sys
return self.name().encode(sys.getfilesystemencoding())
def nameEncoded(self):
import urllib
return urllib.quote(self.name().encode('utf-8'));
def namePretty(self):
return self.name().replace(self.extension(), '').replace('-', ' ').replace('_', ' ').strip();
def open(self):
if sublime.platform() == 'osx':
import subprocess
subprocess.Popen(['open', '-a', self.nameSystem()], cwd=self.dirnameSystem())
elif sublime.platform() == 'windows':
import subprocess
subprocess.Popen([self.nameSystem()], cwd=self.dirnameSystem(), shell=True)
else:
desktop.open(self.path())
def edit(self):
return sublime.active_window().open_file(self.path())
def isDirectory(self):
return self._is_directory
def isFile(self):
return self.isDirectory() == False
def contentUTF8(self):
import codecs
return codecs.open(self.path(), 'r', 'utf-8').read()
def contentBinary(self):
return file(self.path(), "rb").read()
def contentBase64(self):
return 'data:'+self.mime()+';base64,'+(file(self.path(), "rb").read().encode("base64").replace('\n', ''))
def reveal(self):
sublime.active_window().run_command("open_dir", {"dir": self.dirname(), "file": self.name()} )
def write(self, content):
file(self.path(), 'w+').write(content)
def mime(self):
import mimetypes
return mimetypes.guess_type(self.path())[0] or 'application/octet-stream'
def extension(self):
return os.path.splitext('name'+self.name())[1].lower()
def exists(self):
return os.path.isdir(self.path()) or os.path.isfile(self.path())
def create(self):
if self.isDirectory():
self.dirnameCreate()
os.makedirs(self.path())
else:
self.dirnameCreate()
self.write('')
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:
if self.path().lower() == location.path().lower():
pass
else:
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())
else:
location.dirnameCreate();
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
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():
active_view = window.active_view()
views = []
for view in window.views():
if view.file_name():
views.append(view)
views.reverse();
for view in views:
if old == view.file_name():
active_view = self._move_moveView(window, view, location, active_view)
elif view.file_name().find(old+'\\') == 0:
active_view = self._move_moveView(window, view, view.file_name().replace(old+'\\', location+'\\', 1), active_view)
elif view.file_name().find(old+'/') == 0:
active_view = self._move_moveView(window, view, view.file_name().replace(old+'/', location+'/', 1), active_view)
def _move_moveView(self, window, view, location, active_view):
if active_view == view:
is_active_view = True
else:
is_active_view = False
options = Object()
options.scroll = view.viewport_position()
options.selections = [[item.a, item.b] for item in view.sel()]
options.marks = [[item.a, item.b] for item in view.get_regions("mark")]
options.bookmarks = [[item.a, item.b] for item in view.get_regions("bookmarks")]
if int(sublime.version()) >= 2167:
options.folds = [[item.a, item.b] for item in view.folded_regions()]
else:
options.folds = [[item.a, item.b] for item in view.unfold(sublime.Region(0, view.size()))]
options.syntax = view.settings().get('syntax')
try:
_window = window or view.window() or sublime.active_window()
options.position = _window.get_view_index(view)
except:
options.position = False
window.focus_view(view)
if view.is_dirty():
options.content = view.substr(sublime.Region(0, view.size()))
view.window().run_command('revert')
else:
options.content = False
_view = view
view = window.open_file(location)
window.focus_view(_view)
window.run_command('close')
sublime.set_timeout(lambda: self._move_restoreView(view, options, window), 200)
if is_active_view:
window.focus_view(view)
return view
else:
window.focus_view(active_view)
return active_view
def _move_restoreView(self, view, options, window):
if view.is_loading():
sublime.set_timeout(lambda: self._move_restoreView(view, options, window), 100)
else:
if options.content != False:
edit = view.begin_edit()
view.replace(edit, sublime.Region(0, view.size()), options.content);
view.sel().clear()
view.sel().add(sublime.Region(0))
view.end_edit(edit)
if options.position != False:
try:
_window = window or view.window() or sublime.active_window()
group, index = options.position
_window.set_view_index(view, group, index)
except:
pass
if options.syntax:
view.settings().set('syntax', options.syntax);
for r in options.folds:
view.fold(sublime.Region(r[0], r[1]))
view.sel().clear()
for r in options.selections:
view.sel().add(sublime.Region(r[0], r[1]))
rs = []
for r in options.marks:
rs.append(sublime.Region(r[0], r[1]))
if len(rs):
view.add_regions("mark", rs, "mark", "dot", sublime.HIDDEN | sublime.PERSISTENT)
rs = []
for r in options.bookmarks:
rs.append(sublime.Region(r[0], r[1]))
if len(rs):
view.add_regions("bookmarks", rs, "bookmarks", "bookmark", sublime.HIDDEN | sublime.PERSISTENT)
view.set_viewport_position(options.scroll, False)
def close_associated_buffers(self):
path = self.path()
closed_items = []
for window in sublime.windows():
active_view = window.active_view()
views = []
for view in window.views():
if view.file_name():
views.append(view)
views.reverse();
for view in views:
if path == view.file_name():
if view.window():
closed_items.append([view.file_name(), view.window(), view.window().get_view_index(view)])
if len(window.views()) == 1:
window.new_file()
window.focus_view(view)
window.run_command('revert')
window.run_command('close')
elif view.file_name().find(path+'\\') == 0:
if view.window():
closed_items.append([view.file_name(), view.window(), view.window().get_view_index(view)])
if len(window.views()) == 1:
window.new_file()
window.focus_view(view)
window.run_command('revert')
window.run_command('close')
elif view.file_name().find(path+'/') == 0:
if view.window():
closed_items.append([view.file_name(), view.window(), view.window().get_view_index(view)])
if len(window.views()) == 1:
window.new_file()
window.focus_view(view)
window.run_command('revert')
window.run_command('close')
# try to repaint
try:
window.focus_view(active_view)
window.focus_view(window.active_view())
except:
try:
window.focus_view(window.active_view())
except:
pass
return closed_items

View File

@@ -0,0 +1,119 @@
import sublime
import re
import os
class SideBarProject:
def getDirectories(self):
return sublime.active_window().folders()
def hasOpenedProject(self):
return self.getProjectFile() != None
def getDirectoryFromPath(self, path):
for directory in self.getDirectories():
maybe_path = path.replace(directory, '', 1)
if maybe_path != path:
return directory
def getProjectFile(self):
if not self.getDirectories():
return None
import json
data = file(os.path.normpath(os.path.join(sublime.packages_path(), '..', 'Settings', 'Session.sublime_session')), 'r').read()
data = data.replace('\t', ' ')
data = json.loads(data, strict=False)
projects = data['workspaces']['recent_workspaces']
if os.path.lexists(os.path.join(sublime.packages_path(), '..', 'Settings', 'Auto Save Session.sublime_session')):
data = file(os.path.normpath(os.path.join(sublime.packages_path(), '..', 'Settings', 'Auto Save Session.sublime_session')), 'r').read()
data = data.replace('\t', ' ')
data = json.loads(data, strict=False)
if 'workspaces' in data and 'recent_workspaces' in data['workspaces'] and data['workspaces']['recent_workspaces']:
projects += data['workspaces']['recent_workspaces']
projects = list(set(projects))
for project_file in projects:
project_file = re.sub(r'^/([^/])/', '\\1:/', project_file);
project_json = json.loads(file(project_file, 'r').read(), strict=False)
if 'folders' in project_json:
folders = project_json['folders']
found_all = True
for directory in self.getDirectories():
found = False
for folder in folders:
folder_path = re.sub(r'^/([^/])/', '\\1:/', folder['path']);
if folder_path == directory.replace('\\', '/'):
found = True
break;
if found == False:
found_all = False
break;
if found_all:
return project_file
return None
def getProjectJson(self):
if not self.hasOpenedProject():
return None
import json
return json.loads(file(self.getProjectFile(), 'r').read(), strict=False)
def excludeDirectory(self, path):
import json
project_file = self.getProjectFile();
project = self.getProjectJson()
path = re.sub(r'^([^/])\:/', '/\\1/', path.replace('\\', '/'))
for folder in project['folders']:
if path.find(folder['path']) == 0:
try:
folder['folder_exclude_patterns'].append(re.sub(r'/+$', '', path.replace(folder['path']+'/', '', 1)))
except:
folder['folder_exclude_patterns'] = [re.sub(r'/+$', '', path.replace(folder['path']+'/', '', 1))]
file(project_file, 'w+').write(json.dumps(project, indent=1))
return
def excludeFile(self, path):
import json
project_file = self.getProjectFile();
project = self.getProjectJson()
path = re.sub(r'^([^/])\:/', '/\\1/', path.replace('\\', '/'))
for folder in project['folders']:
if path.find(folder['path']) == 0:
try:
folder['file_exclude_patterns'].append(path.replace(folder['path']+'/', '', 1))
except:
folder['file_exclude_patterns'] = [path.replace(folder['path']+'/', '', 1)]
file(project_file, 'w+').write(json.dumps(project, indent=1))
return
def rootAdd(self, path):
import json
project_file = self.getProjectFile();
project = self.getProjectJson()
path = re.sub(r'^([^/])\:/', '/\\1/', path.replace('\\', '/'))
project['folders'].append({'path':path});
file(project_file, 'w+').write(json.dumps(project, indent=1))
def refresh(self):
try:
sublime.set_timeout(lambda:sublime.active_window().run_command('refresh_folder_list'), 200);
sublime.set_timeout(lambda:sublime.active_window().run_command('refresh_folder_list'), 600);
sublime.set_timeout(lambda:sublime.active_window().run_command('refresh_folder_list'), 1300);
sublime.set_timeout(lambda:sublime.active_window().run_command('refresh_folder_list'), 2300);
except:
pass
def getPreference(self, name):
if not self.hasOpenedProject():
return None
project = self.getProjectJson()
try:
return project[name]
except:
return None

View File

@@ -0,0 +1,186 @@
# coding=utf8
import sublime
import os
import re
from SideBarProject import SideBarProject
from SideBarItem import SideBarItem
class SideBarSelection:
def __init__(self, paths = []):
if len(paths) < 1:
try:
path = sublime.active_window().active_view().file_name()
if self.isNone(path):
paths = []
else:
paths = [path]
except:
paths = []
self._paths = paths
self._paths.sort()
self._obtained_selection_information_basic = False
self._obtained_selection_information_extended = False
def len(self):
return len(self._paths)
def hasDirectories(self):
self._obtainSelectionInformationBasic()
return self._has_directories
def hasFiles(self):
self._obtainSelectionInformationBasic()
return self._has_files
def hasOnlyDirectories(self):
self._obtainSelectionInformationBasic()
return self._only_directories
def hasOnlyFiles(self):
self._obtainSelectionInformationBasic()
return self._only_files
def hasProjectDirectories(self):
if self.hasDirectories():
project_directories = SideBarProject().getDirectories()
for item in self.getSelectedDirectories():
if item.path() in project_directories:
return True
return False
else:
return False
def hasItemsUnderProject(self):
for item in self.getSelectedItems():
if item.isUnderCurrentProject():
return True
return False
def hasImages(self):
return self.hasFilesWithExtension('gif|jpg|jpeg|png')
def hasFilesWithExtension(self, extensions):
extensions = re.compile('('+extensions+')$', re.I);
for item in self.getSelectedFiles():
if extensions.search(item.path()):
return True;
return False
def getSelectedItems(self):
self._obtainSelectionInformationExtended()
return self._files + self._directories;
def getSelectedItemsWithoutChildItems(self):
self._obtainSelectionInformationExtended()
items = []
for item in self._items_without_containing_child_items:
items.append(SideBarItem(item, os.path.isdir(item)))
return items
def getSelectedDirectories(self):
self._obtainSelectionInformationExtended()
return self._directories;
def getSelectedFiles(self):
self._obtainSelectionInformationExtended()
return self._files;
def getSelectedDirectoriesOrDirnames(self):
self._obtainSelectionInformationExtended()
return self._directories_or_dirnames;
def getSelectedImages(self):
return self.getSelectedFilesWithExtension('gif|jpg|jpeg|png')
def getSelectedFilesWithExtension(self, extensions):
items = []
extensions = re.compile('('+extensions+')$', re.I);
for item in self.getSelectedFiles():
if extensions.search(item.path()):
items.append(item)
return items
def _obtainSelectionInformationBasic(self):
if not self._obtained_selection_information_basic:
self._obtained_selection_information_basic = True
self._has_directories = False
self._has_files = False
self._only_directories = False
self._only_files = False
for path in self._paths:
if self._has_directories == False and os.path.isdir(path):
self._has_directories = True
if self._has_files == False and os.path.isdir(path) == False:
self._has_files = True
if self._has_files and self._has_directories:
break
if self._has_files and self._has_directories:
self._only_directories = False
self._only_files = False
elif self._has_files:
self._only_files = True
elif self._has_directories:
self._only_directories = True
def _obtainSelectionInformationExtended(self):
if not self._obtained_selection_information_extended:
self._obtained_selection_information_extended = True
self._directories = []
self._files = []
self._directories_or_dirnames = []
self._items_without_containing_child_items = []
_directories = []
_files = []
_directories_or_dirnames = []
_items_without_containing_child_items = []
for path in self._paths:
if os.path.isdir(path):
item = SideBarItem(path, True)
if item.path() not in _directories:
_directories.append(item.path())
self._directories.append(item)
if item.path() not in _directories_or_dirnames:
_directories_or_dirnames.append(item.path())
self._directories_or_dirnames.append(item)
_items_without_containing_child_items = self._itemsWithoutContainingChildItems(_items_without_containing_child_items, item.path())
else:
item = SideBarItem(path, False)
if item.path() not in _files:
_files.append(item.path())
self._files.append(item)
_items_without_containing_child_items = self._itemsWithoutContainingChildItems(_items_without_containing_child_items, item.path())
item = SideBarItem(os.path.dirname(path), True)
if item.path() not in _directories_or_dirnames:
_directories_or_dirnames.append(item.path())
self._directories_or_dirnames.append(item)
self._items_without_containing_child_items = _items_without_containing_child_items
def _itemsWithoutContainingChildItems(self, items, item):
new_list = []
add = True
for i in items:
if i.find(item+'\\') == 0 or i.find(item+'/') == 0:
continue
else:
new_list.append(i)
if (item+'\\').find(i+'\\') == 0 or (item+'/').find(i+'/') == 0:
add = False
if add:
new_list.append(item)
return new_list
def isNone(self, path):
if path == None or path == '' or path == '.' or path == '..' or path == './' or path == '/' or path == '//' or path == '\\' or path == '\\\\' or path == '\\\\\\\\':
return True
else:
return False