Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Update setPath to work with custom files
Browse files Browse the repository at this point in the history
Contributed under CC0.
  • Loading branch information
hansonw committed Nov 1, 2017
1 parent a1e36f3 commit 11aba80
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
18 changes: 17 additions & 1 deletion spec/text-buffer-io-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,10 @@ describe('TextBuffer IO', () => {

describe('.save', () => {
let filePath
let tempDir

beforeEach(() => {
const tempDir = temp.mkdirSync()
tempDir = temp.mkdirSync()
filePath = path.join(tempDir, 'temp.txt')
fs.writeFileSync(filePath, '')
buffer = new TextBuffer()
Expand Down Expand Up @@ -430,6 +431,17 @@ describe('TextBuffer IO', () => {
done()
}, buffer.fileChangeDelay))
})

it('passes setPath to the custom File object', (done) => {
const newPath = path.join(tempDir, 'temp2.txt')
fs.writeFileSync(newPath, '')
buffer.setPath(newPath)
buffer.setText('test')
buffer.save().then(() => {
expect(fs.readFileSync(newPath, 'utf8')).toEqual('TEST')
done()
})
})
})

describe('when a permission error occurs', () => {
Expand Down Expand Up @@ -1153,6 +1165,10 @@ class ReverseCaseFile {
return this.path
}

setPath (path) {
this.path = path
}

createReadStream () {
return fs.createReadStream(this.path).pipe(new Transform({
transform (chunk, encoding, callback) {
Expand Down
9 changes: 8 additions & 1 deletion src/text-buffer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,10 @@ class TextBuffer
# * `filePath` A {String} representing the new file path
setPath: (filePath) ->
return if filePath is @getPath()
if @file? and filePath
@file.setPath filePath
@updateFilePath()
return
@setFile(new File(filePath) if filePath)

# Experimental: Set a custom {File} object as the buffer's backing store.
Expand All @@ -588,8 +592,11 @@ class TextBuffer
setFile: (file) ->
return if file?.getPath() is @getPath()
@file = file
@file?.setEncoding?(@getEncoding())
@updateFilePath()

updateFilePath: ->
if @file?
@file.setEncoding?(@getEncoding())
@subscribeToFile()
@emitter.emit 'did-change-path', @getPath()

Expand Down

0 comments on commit 11aba80

Please sign in to comment.