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

Update setPath to work with custom files #277

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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