From 067a60004c1c185da4ecf1bd0a400af76486d9e6 Mon Sep 17 00:00:00 2001 From: LucasPadovan Date: Sun, 3 Apr 2016 18:10:53 -0300 Subject: [PATCH 1/4] localStorage is on! --- CHANGELOG.md | 6 +++--- lib/git-tabs.coffee | 32 ++++++++++++++++++++------------ lib/storage-folder.coffee | 28 ++++++++++++++-------------- lib/utils.coffee | 1 + package.json | 4 ++-- 5 files changed, 40 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c3d858c..bc975ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,3 @@ -## 0.1.0 - First Release -* Every feature added -* Every bug fixed +## 0.3.1 - First Release of the fork +* Added localStorage to save the tabs +* TODO: licence, activeTab is not properly loaded, code cleanup. diff --git a/lib/git-tabs.coffee b/lib/git-tabs.coffee index aabf8fa..4a42d49 100644 --- a/lib/git-tabs.coffee +++ b/lib/git-tabs.coffee @@ -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() @@ -69,6 +72,7 @@ module.exports = subscribeToRepositories: -> @git.onDidChangeBranch (data) => @handleBranchChange(data) + @setActiveTab() handleNewTab: (event) -> @saveTab(event.item, event.index) @@ -91,33 +95,32 @@ module.exports = # Save all tabs to local 'cache' saveTabs: -> + @tabs[@activeBranch] = {} for tab, i in @activePane.items @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) -> @@ -128,3 +131,8 @@ module.exports = if item.index < tab.index @tabs[branch][id][index]-- delete @tabs[branch]?[tab.id] + + # Set the correct active tab + setActiveTab: -> + # activeTab = atom.workspace.paneContainer.activePane.itemAtIndex(@activeTabIndex) + # @activePane.setActiveItem(activeTab) diff --git a/lib/storage-folder.coffee b/lib/storage-folder.coffee index 936594f..1c4b970 100644 --- a/lib/storage-folder.coffee +++ b/lib/storage-folder.coffee @@ -5,28 +5,28 @@ 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"] + localStorage.setItem(branchedName, JSON.stringify(object)) 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 diff --git a/lib/utils.coffee b/lib/utils.coffee index d92dd23..d842f7e 100644 --- a/lib/utils.coffee +++ b/lib/utils.coffee @@ -3,6 +3,7 @@ module.exports = return process.env.ATOM_HOME + '/git-tabs' getItemPath: (item) -> + # path = if item.buffer.file then item.buffer.file.path else '' return item.buffer?.file?.path getActiveItemPath: -> diff --git a/package.json b/package.json index ef48f3c..bbd3112 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,11 @@ { "name": "git-tabs", "main": "./lib/git-tabs", - "version": "0.3.0", + "version": "0.3.1", "description": "Preserve your tabs on a per-branch and per-repo basis", "keywords": [], "activationCommands": {}, - "repository": "https://github.com/anderoonies/git-tabs", + "repository": "https://github.com/lucaspadovan/git-tabs", "license": "MIT", "engines": { "atom": ">=1.0.0 <2.0.0" From 3ac99544d98be81c1ab8d6866acc3cd354c1e0c7 Mon Sep 17 00:00:00 2001 From: LucasPadovan Date: Tue, 5 Apr 2016 02:07:06 -0300 Subject: [PATCH 2/4] Second release, added support for multiple panes --- CHANGELOG.md | 5 +++++ lib/git-tabs.coffee | 3 ++- lib/storage-folder.coffee | 6 +++++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bc975ac..4b53a3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.3.2 - Second Release of the fork +* 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: licence, activeTab is not properly loaded, code cleanup, multi-pane support, tests. + ## 0.3.1 - First Release of the fork * Added localStorage to save the tabs * TODO: licence, activeTab is not properly loaded, code cleanup. diff --git a/lib/git-tabs.coffee b/lib/git-tabs.coffee index 4a42d49..0a80bb8 100644 --- a/lib/git-tabs.coffee +++ b/lib/git-tabs.coffee @@ -96,7 +96,8 @@ module.exports = # Save all tabs to local 'cache' saveTabs: -> @tabs[@activeBranch] = {} - for tab, i in @activePane.items + tabs = atom.workspace.paneContainer.getPaneItems() + for tab, i in tabs @saveTab(tab, i) # Save a tab to the local 'cache' diff --git a/lib/storage-folder.coffee b/lib/storage-folder.coffee index 1c4b970..814dd68 100644 --- a/lib/storage-folder.coffee +++ b/lib/storage-folder.coffee @@ -12,7 +12,11 @@ class StorageFolder if object delete object["undefined"] - localStorage.setItem(branchedName, JSON.stringify(object)) + stringifiedObject = JSON.stringify(object) + if stringifiedObject == "{}" + localStorage.removeItem(branchedName) + else + localStorage.setItem(branchedName, stringifiedObject) load: (name) -> @projectName = path.basename(atom.project.getPaths()?[0]) From 9e0d07fb457a643bad593f466754105f40be4eab Mon Sep 17 00:00:00 2001 From: LucasPadovan Date: Wed, 29 Jun 2016 15:31:15 -0300 Subject: [PATCH 3/4] Fixes for a proper Pull Request --- CHANGELOG.md | 12 ++++++++---- lib/utils.coffee | 1 - package.json | 4 ++-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b53a3a..cadc691 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,12 @@ -## 0.3.2 - Second Release of the fork +## 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: licence, activeTab is not properly loaded, code cleanup, multi-pane support, tests. +* TODO: activeTab is not properly loaded, code cleanup, multi-pane support, tests. -## 0.3.1 - First Release of the fork +## 0.3.1 - Second Release, localStorage * Added localStorage to save the tabs -* TODO: licence, activeTab is not properly loaded, code cleanup. +* TODO: activeTab is not properly loaded, code cleanup. + +## 0.1.0 - First Release +* Every feature added +* Every bug fixed diff --git a/lib/utils.coffee b/lib/utils.coffee index d842f7e..d92dd23 100644 --- a/lib/utils.coffee +++ b/lib/utils.coffee @@ -3,7 +3,6 @@ module.exports = return process.env.ATOM_HOME + '/git-tabs' getItemPath: (item) -> - # path = if item.buffer.file then item.buffer.file.path else '' return item.buffer?.file?.path getActiveItemPath: -> diff --git a/package.json b/package.json index bbd3112..256e800 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,11 @@ { "name": "git-tabs", "main": "./lib/git-tabs", - "version": "0.3.1", + "version": "0.3.2", "description": "Preserve your tabs on a per-branch and per-repo basis", "keywords": [], "activationCommands": {}, - "repository": "https://github.com/lucaspadovan/git-tabs", + "repository": "https://github.com/anderoonies/git-tabs", "license": "MIT", "engines": { "atom": ">=1.0.0 <2.0.0" From bc0cf028962ecc56f1f4b985b15325188be30c9a Mon Sep 17 00:00:00 2001 From: LucasPadovan Date: Wed, 29 Jun 2016 15:35:54 -0300 Subject: [PATCH 4/4] Inline comment for empty method. --- lib/git-tabs.coffee | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/git-tabs.coffee b/lib/git-tabs.coffee index 0a80bb8..c0443f9 100644 --- a/lib/git-tabs.coffee +++ b/lib/git-tabs.coffee @@ -133,7 +133,8 @@ module.exports = @tabs[branch][id][index]-- delete @tabs[branch]?[tab.id] - # Set the correct active tab + # 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)