Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## 0.3.2 - Third Release, multiTabs partial support
* Added partial support for multiple panes (Saves the files opened, not the splitted panes)
* Avoid saving and filling the localStorage with empty elements for branches without tabs opened.
* TODO: activeTab is not properly loaded, code cleanup, multi-pane support, tests.

## 0.3.1 - Second Release, localStorage
* Added localStorage to save the tabs
* TODO: activeTab is not properly loaded, code cleanup.

## 0.1.0 - First Release
* Every feature added
* Every bug fixed
36 changes: 23 additions & 13 deletions lib/git-tabs.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,24 @@ module.exports =
@activeBranch = @git.getCurrentBranch()

# Set up storageFolder
@storageFolder = new StorageFolder getStorageDir()
if not fs.existsSync(@storageFolder.getPath() + "/#{@projectName}.json")
@storageFolder.store(@projectName, {})
@storageFolder = new StorageFolder
@branchTabsName = [@projectName, @activeBranch].join('-')

# Set up local 'cache' for tabs
@tabs = {}

# Set up the active pane shortcut
@activePane = atom.workspace.paneContainer.activePane
@activeTabIndex = 0

# Load tabs if the user has already stored them in a previous session
if @storageFolder.exists(@activeBranch)
@storedBranchTabs = @storageFolder.load(@activeBranch)
if @storedBranchTabsName
@clearTabs()
@loadTabs(@activeBranch)

@setActiveTab()

# Save the current tabs to cache
@saveTabs()

Expand All @@ -69,6 +72,7 @@ module.exports =
subscribeToRepositories: ->
@git.onDidChangeBranch (data) =>
@handleBranchChange(data)
@setActiveTab()

handleNewTab: (event) ->
@saveTab(event.item, event.index)
Expand All @@ -91,33 +95,33 @@ module.exports =

# Save all tabs to local 'cache'
saveTabs: ->
for tab, i in @activePane.items
@tabs[@activeBranch] = {}
tabs = atom.workspace.paneContainer.getPaneItems()
for tab, i in tabs
@saveTab(tab, i)

# Save a tab to the local 'cache'
saveTab: (tab, index) ->
isActive = atom.workspace.getActivePaneItem() == tab
if not @tabs[@activeBranch]
@tabs[@activeBranch] = {}
# the item path is the most unique way to hash it.
@tabs[@activeBranch][getItemPath tab] =
'index': index
'active': isActive
'tab': tab.serialize()

# Store tabs as JSON
storeTabs: ->
@saveTabs()
@storageFolder.store(@projectName, @tabs)
@storageFolder.store(@activeBranch, @tabs[@activeBranch])

# Load tabs from JSON
loadTabs: (branch) ->
@tabs = @storageFolder.load(@projectName)
@tabs[branch] = @storageFolder.load(branch)
workspace = atom.workspace

for id, item of @tabs[branch]
deserializedTab = atom.deserializers.deserialize item.tab
@activePane.addItem deserializedTab, item.index
workspace.open(id)
if item.active
@activePane.setActiveItem(deserializedTab)
@activeTabIndex = item.index

# Remove a tab from the local 'cache'
unsaveTab: (tab) ->
Expand All @@ -128,3 +132,9 @@ module.exports =
if item.index < tab.index
@tabs[branch][id][index]--
delete @tabs[branch]?[tab.id]

# Set the correct active tab.
# Still not working because Atom has little to none management of this available (or haven't found it yet)
setActiveTab: ->
# activeTab = atom.workspace.paneContainer.activePane.itemAtIndex(@activeTabIndex)
# @activePane.setActiveItem(activeTab)
32 changes: 18 additions & 14 deletions lib/storage-folder.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,32 @@ fs = require "fs-plus"

module.exports =
class StorageFolder
constructor: (containingPath) ->
@path = path.join(containingPath, "storage")

store: (name, object) ->
fs.writeFileSync(@pathForKey(name + '.json'), JSON.stringify(object), 'utf8')
@projectName = path.basename(atom.project.getPaths()?[0])
branchedName = [@projectName, name].join('-')

if object
delete object["undefined"]
stringifiedObject = JSON.stringify(object)
if stringifiedObject == "{}"
localStorage.removeItem(branchedName)
else
localStorage.setItem(branchedName, stringifiedObject)

load: (name) ->
statePath = @pathForKey(name + '.json')
@projectName = path.basename(atom.project.getPaths()?[0])
branchedName = [@projectName, name].join('-')
retrievedItem = null

try
stateString = fs.readFileSync(statePath, 'utf8')
retrievedItem = localStorage.getItem(branchedName)
catch error
unless error.code is 'ENOENT'
console.warn "Error reading state file: #{statePath}", error.stack, error
console.warn "Local storage could not find an element for: #{branchedName}", error.stack, error
return undefined

try
JSON.parse(stateString)
return JSON.parse(retrievedItem)
catch error
console.warn "Error parsing state file: #{statePath}", error.stack, error

exists: (name) ->
return fs.exists(@pathForKey(name + '.json'))

pathForKey: (name) -> path.join(@getPath(), name)
getPath: -> @path
console.warn "Error parsing retrieved item: #{retrievedItem}", error.stack, error
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "git-tabs",
"main": "./lib/git-tabs",
"version": "0.3.0",
"version": "0.3.2",
"description": "Preserve your tabs on a per-branch and per-repo basis",
"keywords": [],
"activationCommands": {},
Expand Down