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

Commit b619fce

Browse files
committed
Update setPath to work with custom files
Contributed under CC0.
1 parent 8294d3f commit b619fce

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

spec/text-buffer-io-spec.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ class SimpleFile {
1919
return this.path
2020
}
2121

22+
setPath (path) {
23+
this.path = path
24+
}
25+
2226
createReadStream () {
2327
return fs.createReadStream(this.path)
2428
}
@@ -149,9 +153,10 @@ describe('TextBuffer IO', () => {
149153

150154
describe('.save', () => {
151155
let filePath
156+
let tempDir
152157

153158
beforeEach(() => {
154-
const tempDir = temp.mkdirSync()
159+
tempDir = temp.mkdirSync()
155160
filePath = path.join(tempDir, 'temp.txt')
156161
fs.writeFileSync(filePath, '')
157162
buffer = new TextBuffer()
@@ -257,6 +262,17 @@ describe('TextBuffer IO', () => {
257262
done()
258263
})
259264
})
265+
266+
it('passes setPath to the custom File object', (done) => {
267+
const newPath = path.join(tempDir, 'temp2.txt')
268+
buffer.setPath(newPath)
269+
buffer.setText('test')
270+
buffer.save().then(() => {
271+
expect(fs.readFileSync(newPath, 'utf8')).toEqual(buffer.getText())
272+
expect(file.createWriteStream).toHaveBeenCalled()
273+
done()
274+
})
275+
})
260276
})
261277
})
262278

src/text-buffer.coffee

+8-1
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,10 @@ class TextBuffer
585585
# * `filePath` A {String} representing the new file path
586586
setPath: (filePath) ->
587587
return if filePath is @getPath()
588+
if @file? and filePath
589+
@file.setPath filePath
590+
@updateFilePath()
591+
return
588592
@setFile(new File(filePath) if filePath)
589593

590594
# Experimental: Set a custom {File} object as the buffer's backing store.
@@ -607,8 +611,11 @@ class TextBuffer
607611
setFile: (file) ->
608612
return if file?.getPath() is @getPath()
609613
@file = file
614+
@file?.setEncoding?(@getEncoding())
615+
@updateFilePath()
616+
617+
updateFilePath: ->
610618
if @file?
611-
@file.setEncoding?(@getEncoding())
612619
@subscribeToFile()
613620
@emitter.emit 'did-change-path', @getPath()
614621

0 commit comments

Comments
 (0)