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

Commit 11aba80

Browse files
committed
Update setPath to work with custom files
Contributed under CC0.
1 parent a1e36f3 commit 11aba80

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
@@ -249,9 +249,10 @@ describe('TextBuffer IO', () => {
249249

250250
describe('.save', () => {
251251
let filePath
252+
let tempDir
252253

253254
beforeEach(() => {
254-
const tempDir = temp.mkdirSync()
255+
tempDir = temp.mkdirSync()
255256
filePath = path.join(tempDir, 'temp.txt')
256257
fs.writeFileSync(filePath, '')
257258
buffer = new TextBuffer()
@@ -430,6 +431,17 @@ describe('TextBuffer IO', () => {
430431
done()
431432
}, buffer.fileChangeDelay))
432433
})
434+
435+
it('passes setPath to the custom File object', (done) => {
436+
const newPath = path.join(tempDir, 'temp2.txt')
437+
fs.writeFileSync(newPath, '')
438+
buffer.setPath(newPath)
439+
buffer.setText('test')
440+
buffer.save().then(() => {
441+
expect(fs.readFileSync(newPath, 'utf8')).toEqual('TEST')
442+
done()
443+
})
444+
})
433445
})
434446

435447
describe('when a permission error occurs', () => {
@@ -1153,6 +1165,10 @@ class ReverseCaseFile {
11531165
return this.path
11541166
}
11551167

1168+
setPath (path) {
1169+
this.path = path
1170+
}
1171+
11561172
createReadStream () {
11571173
return fs.createReadStream(this.path).pipe(new Transform({
11581174
transform (chunk, encoding, callback) {

src/text-buffer.coffee

+8-1
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,10 @@ class TextBuffer
566566
# * `filePath` A {String} representing the new file path
567567
setPath: (filePath) ->
568568
return if filePath is @getPath()
569+
if @file? and filePath
570+
@file.setPath filePath
571+
@updateFilePath()
572+
return
569573
@setFile(new File(filePath) if filePath)
570574

571575
# Experimental: Set a custom {File} object as the buffer's backing store.
@@ -588,8 +592,11 @@ class TextBuffer
588592
setFile: (file) ->
589593
return if file?.getPath() is @getPath()
590594
@file = file
595+
@file?.setEncoding?(@getEncoding())
596+
@updateFilePath()
597+
598+
updateFilePath: ->
591599
if @file?
592-
@file.setEncoding?(@getEncoding())
593600
@subscribeToFile()
594601
@emitter.emit 'did-change-path', @getPath()
595602

0 commit comments

Comments
 (0)