mirror of
https://github.com/atom/atom.git
synced 2026-01-23 13:58:08 -05:00
Merge pull request #5768 from atom/mb-save-state-based-on-project-paths
Base state path on project paths
This commit is contained in:
@@ -3,6 +3,8 @@ Exec = require('child_process').exec
|
||||
path = require 'path'
|
||||
Package = require '../src/package'
|
||||
ThemeManager = require '../src/theme-manager'
|
||||
_ = require "underscore-plus"
|
||||
temp = require "temp"
|
||||
|
||||
describe "the `atom` global", ->
|
||||
describe 'window sizing methods', ->
|
||||
@@ -124,3 +126,29 @@ describe "the `atom` global", ->
|
||||
line: 2
|
||||
column: 3
|
||||
originalError: error
|
||||
|
||||
describe "saving and loading", ->
|
||||
afterEach -> atom.mode = "spec"
|
||||
|
||||
it "selects the state based on the current project paths", ->
|
||||
Atom = atom.constructor
|
||||
[dir1, dir2] = [temp.mkdirSync("dir1-"), temp.mkdirSync("dir2-")]
|
||||
|
||||
loadSettings = _.extend Atom.getLoadSettings(),
|
||||
initialPaths: [dir1]
|
||||
windowState: null
|
||||
|
||||
spyOn(Atom, 'getLoadSettings').andCallFake -> loadSettings
|
||||
spyOn(Atom, 'getStorageDirPath').andReturn(temp.mkdirSync("storage-dir-"))
|
||||
|
||||
atom.mode = "editor"
|
||||
atom.state.stuff = "cool"
|
||||
atom.project.setPaths([dir1, dir2])
|
||||
atom.saveSync.originalValue.call(atom)
|
||||
|
||||
atom1 = Atom.loadOrCreate("editor")
|
||||
expect(atom1.state.stuff).toBeUndefined()
|
||||
|
||||
loadSettings.initialPaths = [dir1, dir2]
|
||||
atom2 = Atom.loadOrCreate("editor")
|
||||
expect(atom2.state.stuff).toBe("cool")
|
||||
|
||||
@@ -73,7 +73,7 @@ class Atom extends Model
|
||||
# Loads and returns the serialized state corresponding to this window
|
||||
# if it exists; otherwise returns undefined.
|
||||
@loadState: (mode) ->
|
||||
statePath = @getStatePath(mode)
|
||||
statePath = @getStatePath(@getLoadSettings().initialPaths, mode)
|
||||
|
||||
if fs.existsSync(statePath)
|
||||
try
|
||||
@@ -90,14 +90,13 @@ class Atom extends Model
|
||||
|
||||
# Returns the path where the state for the current window will be
|
||||
# located if it exists.
|
||||
@getStatePath: (mode) ->
|
||||
@getStatePath: (paths, mode) ->
|
||||
switch mode
|
||||
when 'spec'
|
||||
filename = 'spec'
|
||||
when 'editor'
|
||||
{initialPaths} = @getLoadSettings()
|
||||
if initialPaths?.length > 0
|
||||
sha1 = crypto.createHash('sha1').update(initialPaths.join("\n")).digest('hex')
|
||||
if paths?.length > 0
|
||||
sha1 = crypto.createHash('sha1').update(paths.join("\n")).digest('hex')
|
||||
filename = "editor-#{sha1}"
|
||||
|
||||
if filename
|
||||
@@ -773,7 +772,7 @@ class Atom extends Model
|
||||
|
||||
saveSync: ->
|
||||
stateString = JSON.stringify(@state)
|
||||
if statePath = @constructor.getStatePath(@mode)
|
||||
if statePath = @constructor.getStatePath(@project?.getPaths(), @mode)
|
||||
fs.writeFileSync(statePath, stateString, 'utf8')
|
||||
else
|
||||
@getCurrentWindow().loadSettings.windowState = stateString
|
||||
|
||||
Reference in New Issue
Block a user