Relativize buffer paths for replication

TextBuffers now maintain a reference to their containing project to
make it easier to test replication of buffers between environments
without worrying about the value of the `project` global.

We're also starting the process of moving the `git` global into Project
as the `Project.repository` property.
This commit is contained in:
Kevin Sawicki & Nathan Sobo
2013-07-22 14:13:55 -07:00
parent d051cbe0f6
commit fa59aafffb
16 changed files with 94 additions and 52 deletions

View File

@@ -8,6 +8,7 @@ remote = require 'remote'
crypto = require 'crypto'
path = require 'path'
dialog = remote.require 'dialog'
app = remote.require 'app'
telepath = require 'telepath'
window.atom =
@@ -224,7 +225,7 @@ window.atom =
remote.getCurrentWindow().hide()
exit: (status) ->
remote.require('app').exit(status)
app.exit(status)
toggleFullScreen: ->
@setFullScreen(!@isFullScreen())
@@ -235,6 +236,9 @@ window.atom =
isFullScreen: ->
remote.getCurrentWindow().isFullScreen()
getHomeDirPath: ->
app.getHomeDir()
getWindowStatePath: ->
switch @windowMode
when 'config', 'spec'

View File

@@ -1,5 +1,5 @@
{View, $$} = require 'space-pen'
Buffer = require 'text-buffer'
TextBuffer = require 'text-buffer'
Gutter = require 'gutter'
{Point, Range} = require 'telepath'
EditSession = require 'edit-session'
@@ -96,7 +96,7 @@ class Editor extends View
@edit(editSession)
else if @mini
@edit(new EditSession
buffer: new Buffer()
buffer: new TextBuffer
softWrap: false
tabLength: 2
softTabs: true

View File

@@ -223,6 +223,8 @@ class Git
getConfigValue: (key) -> @getRepo().getConfigValue(key)
getOriginUrl: -> @getConfigValue('remote.origin.url')
getReferenceTarget: (reference) -> @getRepo().getReferenceTarget(reference)
getAheadBehindCount: (reference) -> @getRepo().getAheadBehindCount(reference)

View File

@@ -1,14 +1,17 @@
fsUtils = require 'fs-utils'
path = require 'path'
url = require 'url'
_ = require 'underscore'
$ = require 'jquery'
telepath = require 'telepath'
{Range} = telepath
Buffer = require 'text-buffer'
TextBuffer = require 'text-buffer'
EditSession = require 'edit-session'
EventEmitter = require 'event-emitter'
Directory = require 'directory'
BufferedProcess = require 'buffered-process'
Git = require 'git'
# Public: Represents a project that's opened in Atom.
#
@@ -43,6 +46,8 @@ class Project
destroy: ->
editSession.destroy() for editSession in @getEditSessions()
buffer.release() for buffer in @getBuffers()
window.git?.destroy()
delete window.git
### Public ###
@@ -55,9 +60,13 @@ class Project
if pathOrState instanceof telepath.Document
@state = pathOrState
@setPath(pathOrState.get('path'))
if projectPath = @state.get('path')
@setPath(projectPath)
else
@setPath(@pathForRepositoryUrl(@state.get('repoUrl')))
@state.get('buffers').each (bufferState) =>
if buffer = deserialize(bufferState)
if buffer = deserialize(bufferState, project: this)
@addBuffer(buffer, updateState: false)
else
@state = telepath.Document.create(deserializer: @constructor.name, version: @constructor.version, buffers: [])
@@ -69,10 +78,16 @@ class Project
for removedBuffer in removed
@removeBufferAtIndex(index, updateState: false)
for insertedBuffer, i in inserted
@addBufferAtIndex(deserialize(insertedBuffer), index + i, updateState: false)
@addBufferAtIndex(deserialize(insertedBuffer, project: this), index + i, updateState: false)
pathForRepositoryUrl: (repoUrl) ->
[repoName] = url.parse(repoUrl).path.split('/')[-1..]
repoName = repoName.replace(/\.git$/, '')
path.join(config.get('core.projectHome'), repoName)
serialize: ->
state = @state.clone()
state.set('path', @getPath())
state.set('buffers', buffer.serialize() for buffer in @getBuffers())
state
@@ -93,10 +108,15 @@ class Project
if projectPath?
directory = if fsUtils.isDirectorySync(projectPath) then projectPath else path.dirname(projectPath)
@rootDirectory = new Directory(directory)
window.git = Git.open(projectPath)
else
@rootDirectory = null
window.git?.destroy()
delete window.git
if originUrl = window.git?.getOriginUrl()
@state.set('repoUrl', originUrl)
@state.set('path', projectPath)
@trigger "path-changed"
# Retrieves the name of the root directory.
@@ -246,8 +266,8 @@ class Project
# text - The {String} text to use as a buffer
#
# Returns the {Buffer}.
buildBuffer: (filePath, text) ->
buffer = new Buffer(filePath, text)
buildBuffer: (filePath, initialText) ->
buffer = new TextBuffer({project: this, filePath, initialText})
@addBuffer(buffer)
@trigger 'buffer-created', buffer
buffer
@@ -263,7 +283,8 @@ class Project
#
# Returns the removed {Buffer}.
removeBuffer: (buffer) ->
@removeBufferAtIndex(@buffers.indexOf(buffer))
index = @buffers.indexOf(buffer)
@removeBufferAtIndex(index) unless index is -1
removeBufferAtIndex: (index, options={}) ->
[buffer] = @buffers.splice(index, 1)

View File

@@ -1,3 +1,4 @@
path = require 'path'
$ = require 'jquery'
{$$} = require 'space-pen'
fsUtils = require 'fs-utils'
@@ -24,6 +25,7 @@ class RootView extends View
ignoredNames: [".git", ".svn", ".DS_Store"]
disabledPackages: []
themes: ['atom-dark-ui', 'atom-dark-syntax']
projectHome: path.join(atom.getHomeDirPath(), 'github')
### Internal ###
@acceptsDocuments: true

View File

@@ -18,8 +18,8 @@ class TextBuffer
@version: 2
registerDeserializer(this)
@deserialize: (state) ->
new TextBuffer(state)
@deserialize: (state, params) ->
new this(state, params)
stoppedChangingDelay: 300
stoppedChangingTimeout: null
@@ -34,14 +34,15 @@ class TextBuffer
#
# path - A {String} representing the file path
# initialText - A {String} setting the starting text
constructor: (args...) ->
if args[0] instanceof telepath.Document
@state = args[0]
constructor: (optionsOrState={}, params={}) ->
if optionsOrState instanceof telepath.Document
@state = optionsOrState
{@project} = params
@text = @state.get('text')
path = @state.get('path')
filePath = @state.get('relativePath')
@id = @state.get('id')
else
[path, initialText] = args
{@project, filePath, initialText} = optionsOrState
@text = telepath.Document.create(initialText, shareStrings: true) if initialText
@id = guid.create().toString()
@state = telepath.Document.create
@@ -49,13 +50,13 @@ class TextBuffer
deserializer: @constructor.name
version: @constructor.version
if path
@setPath(path)
if filePath
@setPath(@project.resolve(filePath))
if @text
@updateCachedDiskContents()
else
@text = telepath.Document.create('', shareStrings: true)
@reload() if fsUtils.exists(path)
@reload() if fsUtils.exists(filePath)
else
@text ?= telepath.Document.create('', shareStrings: true)
@@ -148,7 +149,13 @@ class TextBuffer
@file?.getPath()
getUri: ->
project?.relativize(@getPath()) ? @getPath()
@getRelativePath()
getRelativePath: ->
@state.get('relativePath')
setRelativePath: (relativePath) ->
@setPath(@project.resolve(relativePath))
# Sets the path for the file.
#
@@ -160,9 +167,7 @@ class TextBuffer
@file = new File(path)
@file.read() if @file.exists()
@subscribeToFile()
@state.set('path', path)
@state.set('relativePath', @project.relativize(path))
@trigger "path-changed", this
# Retrieves the current buffer's file extension.

View File

@@ -132,14 +132,10 @@ window.deserializeEditorWindow = ->
$(rootViewParentSelector).append(rootView)
window.git = Git.open(project.getPath())
project.on 'path-changed', ->
projectPath = project.getPath()
atom.getLoadSettings().initialPath = projectPath
window.git?.destroy()
window.git = Git.open(projectPath)
window.deserializeConfigWindow = ->
ConfigView = require 'config-view'
window.configView = deserialize(atom.getWindowState('configView')) ? new ConfigView()
@@ -232,13 +228,13 @@ window.registerDeferredDeserializer = (name, fn) ->
window.unregisterDeserializer = (klass) ->
delete deserializers[klass.name]
window.deserialize = (state) ->
window.deserialize = (state, params) ->
if deserializer = getDeserializer(state)
stateVersion = state.get?('version') ? state.version
return if deserializer.version? and deserializer.version isnt stateVersion
if (state instanceof telepath.Document) and not deserializer.acceptsDocuments
state = state.toObject()
deserializer.deserialize(state)
deserializer.deserialize(state, params)
window.getDeserializer = (state) ->
return unless state?