Skip to content

Commit

Permalink
Only require an opened file for actions that actually need it.
Browse files Browse the repository at this point in the history
  • Loading branch information
Artem Chistyakov committed Jul 19, 2014
1 parent 1f455e5 commit a1ae5fd
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
30 changes: 19 additions & 11 deletions github.py
Expand Up @@ -31,10 +31,6 @@ class NotAGithubRepositoryError(Exception):
pass


class NoFileOpenError(Exception):
pass


class NoRemoteError(Exception):
def __init__(self, branch):
self.branch = branch
Expand Down Expand Up @@ -177,17 +173,22 @@ def name(self):

class GithubWindowCommand(sublime_plugin.WindowCommand):
def rootdir(self):
folders = self.window.folders()
return [i for i in folders if self.filename().startswith(i + os.sep)][0]
if self.filename():
return dirname(self.filename())
return self.first_folder()

def first_folder(self):
print(self.window.folders())
return self.window.folders()[0]

def relative_filename(self):
_, _, filename = self.filename().partition(self.rootdir())
return filename

def filename(self):
if not self.window.active_view() or self.window.active_view().file_name() is None:
raise NoFileOpenError
return self.window.active_view().file_name()
if self.window.active_view():
return self.window.active_view().file_name()
return None

@property
def repository(self):
Expand Down Expand Up @@ -221,6 +222,15 @@ def extract_http_auth_credentials(uri):
return (username, password)


def require_file(func):
@wraps(func)
def wrapper(self):
if self.filename():
return func(self)
sublime.message_dialog("Please open a file first.")
return wrapper


def with_repo(func):
@wraps(func)
def wrapper(self):
Expand All @@ -231,8 +241,6 @@ def wrapper(self):
except (NoRemoteError) as e:
sublime.message_dialog(
"The current branch %s has no upstream branch." % e.branch)
except (NoFileOpenError):
sublime.message_dialog("Please open a file first.")

return wrapper

Expand Down
1 change: 1 addition & 0 deletions github_blame.py
Expand Up @@ -6,6 +6,7 @@


class GithubBlameCommand(GithubWindowCommand):
@require_file
@with_repo
def run(self, repo):
webbrowser.open_new_tab(repo.blame_file_url(self.relative_filename()))
1 change: 1 addition & 0 deletions github_copy_link.py
Expand Up @@ -7,6 +7,7 @@

class GithubCopyLinkCommand(GithubWindowCommand):

@require_file
@with_repo
def run(self, repo):
sublime.status_message("Copied URL to clipboard")
Expand Down
1 change: 1 addition & 0 deletions github_open.py
Expand Up @@ -6,6 +6,7 @@


class GithubOpenCommand(GithubWindowCommand):
@require_file
@with_repo
def run(self, repo):
webbrowser.open_new_tab(repo.browse_file_url(self.relative_filename()))

0 comments on commit a1ae5fd

Please sign in to comment.