Ensure read-only text editors are not considered 'modified'. Also clear read-only flag on successful save.

This commit is contained in:
Damien Guard
2017-12-04 13:12:08 -08:00
parent a993742f7f
commit 70e773fa7b
2 changed files with 24 additions and 1 deletions

View File

@@ -85,6 +85,23 @@ describe('TextEditor', () => {
})
})
describe('when the editor is readonly', () => {
it('overrides TextBuffer.isModified to return false', async () => {
const editor = await atom.workspace.open(null, {readOnly: true})
editor.setText('I am altering the buffer, pray I do not alter it any further')
expect(editor.isModified()).toBe(false)
editor.setReadOnly(false)
expect(editor.isModified()).toBe(true)
})
it('clears the readonly status when saved', async () => {
const editor = await atom.workspace.open(null, {readOnly: true})
editor.setText('I am altering the buffer, pray I do not alter it any further')
expect(editor.isReadOnly()).toBe(true)
await editor.saveAs(temp.openSync('was-readonly').path)
expect(editor.isReadOnly()).toBe(false)
})
})
describe('when the editor is constructed with the largeFileMode option set to true', () => {
it("loads the editor but doesn't tokenize", async () => {
editor = await atom.workspace.openTextFile('sample.js', {largeFileMode: true})

View File

@@ -411,6 +411,7 @@ class TextEditor {
if (this.component != null) {
this.component.scheduleUpdate()
}
this.buffer.emitModifiedStatusChanged(this.isModified())
}
break
@@ -575,6 +576,11 @@ class TextEditor {
this.disposables.add(this.buffer.onDidChangeModified(() => {
if (!this.hasTerminatedPendingState && this.buffer.isModified()) this.terminatePendingState()
}))
this.disposables.add(this.buffer.onDidSave(() => {
if (this.isReadOnly()) {
this.setReadOnly(false)
}
}))
}
terminatePendingState () {
@@ -1126,7 +1132,7 @@ class TextEditor {
setEncoding (encoding) { this.buffer.setEncoding(encoding) }
// Essential: Returns {Boolean} `true` if this editor has been modified.
isModified () { return this.buffer.isModified() }
isModified () { return this.isReadOnly() ? false : this.buffer.isModified() }
// Essential: Returns {Boolean} `true` if this editor has no content.
isEmpty () { return this.buffer.isEmpty() }