Skip to content

Commit

Permalink
Merge pull request #14 from jmooney/next
Browse files Browse the repository at this point in the history
Update to Version 2.0.0
  • Loading branch information
jmooney committed May 6, 2019
2 parents 8fa26d4 + 9d92cd7 commit d8a3d3a
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 58 deletions.
14 changes: 13 additions & 1 deletion Default.sublime-commands
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,17 @@
{
"caption": "EasyWorkspace: Delete Workspace",
"command": "delete_easy_workspace"
},

// show opened easy workspace
{
"caption": "EasyWorkspace: Show Opened Workspaces",
"command": "show_opened_easy_workspace"
},

// reopen last easy workspace
{
"caption": "EasyWorkspace: Reopen Last Workspace",
"command": "reopen_last_easy_workspace"
}
]
]
12 changes: 6 additions & 6 deletions EasyWorkspace.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,23 @@
// all these settings are ignored if easy_ws_save_on is set to false

// prompt save when all folders are closed
"easy_ws_save_on_close_folders" : true,
"easy_ws_save_on_close_folder_list" : true,

// prompt save when project is closed
"easy_ws_save_on_close_project" : true,

// prompt save when new project is opened
"easy_ws_save_on_open_project" : true,
"easy_ws_save_on_open_project_or_workspace" : true,

// prompt save when project is changed
"easy_ws_save_on_switch_project" : true,
"easy_ws_save_on_switch_project_or_workspace" : true,

// prompt save when project is changed
"easy_ws_save_on_select_project" : true,
// prompt save when sublime workspace is changed
"easy_ws_save_on_select_workspace" : true,

// prompt save when all views are closed
"easy_ws_save_on_close_all" : true,

// prompt save when window is closed
"easy_ws_save_on_close_window" : true
}
}
10 changes: 9 additions & 1 deletion Main.sublime-menu
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@
{
"caption": "Delete",
"command": "delete_easy_workspace"
},
{
"caption": "Reopen Last Workspace",
"command": "reopen_last_easy_workspace"
},
{
"caption": "Show Opened",
"command": "show_opened_easy_workspace"
}
]
}
Expand Down Expand Up @@ -79,4 +87,4 @@
}
]
}
]
]
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ Requirements
Installing
----------
### With Package Control

Not Available Yet, but Coming Soon!

1. Install [Package Control](https://packagecontrol.io/installation)
2. Run "Package Control: Install Package" command
3. Find and install the 'EasyWorkspace' plugin
Expand Down Expand Up @@ -49,10 +46,12 @@ Tools->Packages->EasyWorkspace
Commands
--------

OpenEasyWorkspace | Opens an existing easy workspace
SaveEasyWorkspace | Saves open files and folders to an easy workspace file
SaveAsEasyWorkspace | Saves open files and folders to a specified easy workspace file
DeleteEasyWorkspace | Delete an existing easy workspace
OpenEasyWorkspace | Opens an existing easy workspace
SaveEasyWorkspace | Saves open files and folders to an easy workspace file
SaveAsEasyWorkspace | Saves open files and folders to a specified easy workspace file
DeleteEasyWorkspace | Delete an existing easy workspace
ReopenLastEasyWorkspace | Reopens the most recently closed workspace
ShowOpenedEasyWorkspace | Shows the currently opened workspaces

Git Integration
---------------
Expand Down
126 changes: 83 additions & 43 deletions easy-workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ def __init__(self):
self.active = ()
self.groups = []

self.filename = ""

############################################################################
# File IO

Expand All @@ -65,11 +67,11 @@ def saveToFile(self, filename):

# write the file
with open(filename, 'w') as f:
self.file = filename

wsJSON = sublime.encode_value(vars(self), True)
f.write(wsJSON)

self.filename = filename


def loadFromFile(self, filename):
""" Loads a workspace from a file
Expand Down Expand Up @@ -111,20 +113,24 @@ def buildFromWindow(self, window):
sViews = window.views_in_group(i)
activeSView = window.active_view_in_group(i)

# ensure we have

# create the group
group = dict()
group['active'] = sViews.index(activeSView) if sViews else 0
group['views'] = []

# fill with views
for sView in window.views_in_group(i):
for sView in sViews:
# ignore any temporary or unsaved views
fileExistsOnDisk = sView.file_name()
if not fileExistsOnDisk:
continue

view = dict()

view['file'] = sView.file_name()
view['visible'] = (sView.visible_region().a, sView.visible_region().b)
view['selection'] = (sView.sel()[0].a, sView.sel()[0].b)
view['read_only'] = sView.is_read_only()

group['views'].append(view)

Expand Down Expand Up @@ -161,7 +167,8 @@ def applyToWindow(self, window):
# and ready to have 'visible'/'selection' modified
time.sleep(0.05)

# set view region and selection
# set view attributes, region and selection
sView.set_read_only(view.get('read_only', False))

# when setting the visible region, we want the top of the
# view to match saved workspace. We achieve this by showing
Expand Down Expand Up @@ -237,7 +244,10 @@ class EasyWorkspaceCommand:

# shared dictionary which stores the currently open workspace file for each
# open window in sublime
openWorkspaceFiles = dict()
_openWorkspaceFiles = dict()

# store the last workspace to reopen as needed
_reopenWorkspace = ""

def run(self, **kwargs):
""" runs a command and garbage collects openWorkspaceFiles """
Expand All @@ -248,19 +258,19 @@ def run(self, **kwargs):
def __garbageCollectOpenWorkspaceFiles(self):
""" removes all closed window ids from our shared state data """
openIds = [window.id() for window in sublime.windows()]
invalidIds = [wid for wid in self.openWorkspaceFiles if wid not in openIds]
invalidIds = [wid for wid in self._openWorkspaceFiles if wid not in openIds]
for wid in invalidIds:
del self.openWorkspaceFiles[wid]
del self._openWorkspaceFiles[wid]


############################################################################

def __getWorkspacesDir(self):
def getWorkspacesDir(self):
""" returns the EasyWorkspace workspaces directory from settings """
settings = sublime.load_settings("EasyWorkspace.sublime-settings")
wsFolder = settings.get("easy_ws_save_directory", "EasyWorkspace/workspaces")
return os.path.join(sublime.packages_path(), wsFolder + os.path.sep)

############################################################################

def getWorkspaceFilepath(self, filename):
""" Resolves a filename into its full easyworkspace path,
including directory and extension
Expand All @@ -270,25 +280,28 @@ def getWorkspaceFilepath(self, filename):
"""
settings = sublime.load_settings("EasyWorkspace.sublime-settings")

workspacesDir = self.__getWorkspacesDir()
workspacesDir = self.getWorkspacesDir()
baseName, extension = os.path.splitext(filename)
if not extension:
extension = settings.get('easy_ws_file_extension', '.ws')
return os.path.join(workspacesDir, baseName+extension)

def getAllWorkspaceFiles(self):
""" returns a list of all easy workspace files """
workspacesDir = self.__getWorkspacesDir()
workspacesDir = self.getWorkspacesDir()
workspaceFiles = []
for root, dirs, files in os.walk(workspacesDir):
for file in files:
# ignore any hidden files
if file.startswith('.'):
continue

# trim base workspace directory for display
subdir = root[len(workspacesDir):]
workspaceFiles.append(os.path.join(subdir, file))

return workspaceFiles


################################################################################

class SaveEasyWorkspaceCommand(EasyWorkspaceCommand, sublime_plugin.WindowCommand):
Expand All @@ -305,7 +318,7 @@ def run(self, **kwargs):
super().run(**kwargs)

# are we saving a new workspace?
isNewWorkspace = self.window.id() not in self.openWorkspaceFiles
isNewWorkspace = self.window.id() not in EasyWorkspaceCommand._openWorkspaceFiles
noFileProvided = kwargs.get('filename', None) == None

if isNewWorkspace and noFileProvided:
Expand All @@ -319,7 +332,7 @@ def run(self, **kwargs):
ws.buildFromWindow(self.window)

# resolve the full filepath
fullFilePath = self.getWorkspaceFilepath(kwargs.get('filename', self.openWorkspaceFiles.get(self.window.id())))
fullFilePath = self.getWorkspaceFilepath(kwargs.get('filename', EasyWorkspaceCommand._openWorkspaceFiles.get(self.window.id())))

# prompt for overwrite or create new
doSaveDialogResult = sublime.DIALOG_YES
Expand All @@ -333,7 +346,7 @@ def run(self, **kwargs):
self.window.status_message("Canceled")
else:
ws.saveToFile(fullFilePath)
self.openWorkspaceFiles[self.window.id()] = fullFilePath
EasyWorkspaceCommand._openWorkspaceFiles[self.window.id()] = fullFilePath
self.window.status_message("Saved " + fullFilePath)


Expand Down Expand Up @@ -411,7 +424,7 @@ def run(self, **kwargs):
ws.loadFromFile(fullFilePath)
ws.applyToWindow(targetWindow)

self.openWorkspaceFiles[targetWindow.id()] = fullFilePath
EasyWorkspaceCommand._openWorkspaceFiles[targetWindow.id()] = fullFilePath

sublime.status_message("Opened {}".format(fullFilePath))

Expand Down Expand Up @@ -450,7 +463,7 @@ def run(self, **kwargs):
""" Delete an easy workspace """
super().run(**kwargs)

# get list of all saves workspaces
# get list of all saved workspaces
workspaceFiles = self.getAllWorkspaceFiles()

def onWorkspaceFileSelected(index):
Expand All @@ -472,6 +485,39 @@ def onWorkspaceFileSelected(index):
self.window.show_quick_panel(workspaceFiles, onWorkspaceFileSelected)


################################################################################

class ShowOpenedEasyWorkspaceCommand(EasyWorkspaceCommand, sublime_plugin.WindowCommand):
""" A sublime window command which shows the user this window's current opened workspace """

def run(self, **kwargs):
""" Show the opened easy workspace """
super().run(**kwargs)

# get open workspace files relative to workspaces directory
openWorkspaces = {k:v.replace(self.getWorkspacesDir(), "") for k,v in EasyWorkspaceCommand._openWorkspaceFiles.items()}

# prepend an "*" to our window's open workspace if applicable
if self.window.id() in openWorkspaces:
openWorkspaces[self.window.id()] = " * " + openWorkspaces[self.window.id()]

# show the open workspaces
self.window.show_quick_panel(list(openWorkspaces.values()), None)

################################################################################

class ReopenLastEasyWorkspaceCommand(EasyWorkspaceCommand, sublime_plugin.WindowCommand):
""" A sublime window command which reopens the last easy workspace """

def run(self, **kwargs):
""" Reopen the last easy workspace """
super().run(**kwargs)

if EasyWorkspaceCommand._reopenWorkspace and os.path.isfile(EasyWorkspaceCommand._reopenWorkspace):
self.window.run_command("open_easy_workspace", dict(filename=EasyWorkspaceCommand._reopenWorkspace))
else:
self.window.status_message("Unable to Reopen Workspace " + EasyWorkspaceCommand._reopenWorkspace)

################################################################################
# Plugin Listeners
################################################################################
Expand All @@ -484,7 +530,7 @@ def on_window_command(self, window, command_name, args):
settings = sublime.load_settings("EasyWorkspace.sublime-settings")

# should we autosave?
usingEasyWs = window.id() in self.openWorkspaceFiles
usingEasyWs = window.id() in EasyWorkspaceCommand._openWorkspaceFiles
saveEnabled = settings.get('easy_ws_save_on', False)
if not (usingEasyWs and saveEnabled):
return
Expand All @@ -494,25 +540,19 @@ def on_window_command(self, window, command_name, args):
# the following lists highlight these commands, and are ordered for easy
# comparison of command-setting
#
commandsThatTriggerAutoSave = ["close_folder_list", "close_project",
"prompt_open_project_or_workspace",
"prompt_switch_project_or_workspace",
"prompt_select_workspace",
"close_all", "close_window"]

autoSaveSettings = ["easy_ws_save_on_close_folders", "easy_ws_save_on_close_project",
"easy_ws_save_on_open_project", "easy_ws_save_on_switch_project",
"easy_ws_save_on_select_project",
"easy_ws_save_on_close_all", "easy_ws_save_on_close_window"]

# ensure there isn't programmer error
assert(len(commandsThatTriggerAutoSave) == len(autoSaveSettings))

# should we autosave?
doSave = False
for i, cmd in enumerate(commandsThatTriggerAutoSave):
if command_name == cmd and settings.get(autoSaveSettings[i], False):
doSave = True

if doSave:
result = window.run_command("save_easy_workspace", dict(promptOverwrite=True, promptSave=True))
# these commands are considered to 'close' the workspace, and will also
# store the current workspace to be reopened via the OpenLastWorkspace command
#
commandsThatCloseWorkspace = ["close_folder_list", "close_project",
"prompt_open_project_or_workspace",
"prompt_switch_project_or_workspace",
"prompt_select_workspace",
"close_all", "close_window"]

if command_name in commandsThatCloseWorkspace:
EasyWorkspaceCommand._reopenWorkspace = EasyWorkspaceCommand._openWorkspaceFiles[window.id()]

# Are autosave settings enabled for this command?
autosaveSettingName = "easy_ws_save_on_" + command_name
if settings.get(autosaveSettingName, False):
result = window.run_command("save_easy_workspace", dict(promptOverwrite=True, promptSave=True))

0 comments on commit d8a3d3a

Please sign in to comment.