Skip to content
This repository has been archived by the owner on May 7, 2021. It is now read-only.

Commit

Permalink
Asynchronous save in Atom 1.19+ (#659)
Browse files Browse the repository at this point in the history
* handle asynchronous TextBuffer::save

Fixes #658

* fix specs for atom < 1.19
  • Loading branch information
zmb3 authored Jun 15, 2017
1 parent 999edd6 commit 64863c8
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 52 deletions.
37 changes: 20 additions & 17 deletions lib/rename/gorename.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,23 @@ class Gorename {
const dialog = new RenameDialog({
identifier: word,
callback: (newName) => {
this.saveAllEditors()
const file = editor.getBuffer().getPath()
const cwd = path.dirname(file)

// restore cursor position after gorename completes and the buffer is reloaded
if (cursor) {
const disp = editor.getBuffer().onDidReload(() => {
editor.setCursorBufferPosition(cursor, {autoscroll: false})
const element = atom.views.getView(editor)
if (element) {
element.focus()
}
disp.dispose()
})
}
this.runGorename(file, offset, cwd, newName, cmd)
this.saveAllEditors().then(() => {
const file = editor.getBuffer().getPath()
const cwd = path.dirname(file)

// restore cursor position after gorename completes and the buffer is reloaded
if (cursor) {
const disp = editor.getBuffer().onDidReload(() => {
editor.setCursorBufferPosition(cursor, {autoscroll: false})
const element = atom.views.getView(editor)
if (element) {
element.focus()
}
disp.dispose()
})
}
this.runGorename(file, offset, cwd, newName, cmd)
})
}
})

Expand All @@ -74,11 +75,13 @@ class Gorename {
}

saveAllEditors () {
const promises = []
for (const editor of atom.workspace.getTextEditors()) {
if (editor.isModified() && isValidEditor(editor)) {
editor.save()
promises.push(editor.save())
}
}
return Promise.all(promises)
}

wordAndOffset (editor) {
Expand Down
24 changes: 12 additions & 12 deletions spec/format/formatter-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import fs from 'fs'
import path from 'path'
import {lifecycle} from './../spec-helpers'

function setTextAndSave (editor, text) {
const buffer = editor.getBuffer()
buffer.setText(unformattedText)
return Promise.resolve(buffer.save())
}

const nl = '\n'
const unformattedText = 'package main' + nl + nl + 'func main() {' + nl + '}' + nl
const formattedText = 'package main' + nl + nl + 'func main() {' + nl + '}' + nl
Expand Down Expand Up @@ -78,10 +84,8 @@ describe('formatter', () => {
})

it('does not format the file on save', () => {
runs(() => {
const buffer = editor.getBuffer()
buffer.setText(unformattedText)
buffer.save()
waitsForPromise(() => {
return setTextAndSave(editor, unformattedText)
})

waitsFor(() => { return actual })
Expand All @@ -93,10 +97,8 @@ describe('formatter', () => {
})

it('formats the file on command', () => {
runs(() => {
const buffer = editor.getBuffer()
buffer.setText(unformattedText)
buffer.save()
waitsForPromise(() => {
return setTextAndSave(editor, unformattedText)
})

waitsFor(() => {
Expand Down Expand Up @@ -127,10 +129,8 @@ describe('formatter', () => {
atom.config.set('go-plus.format.tool', tool)

it('formats on save using ' + tool, () => {
runs(() => {
const buffer = editor.getBuffer()
buffer.setText(unformattedText)
buffer.save()
waitsForPromise(() => {
return setTextAndSave(editor, unformattedText)
})

waitsFor(() => {
Expand Down
12 changes: 6 additions & 6 deletions spec/orchestrator-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('orchestrator', () => {
waitsForPromise(() => {
const filepath = path.join(__dirname, 'fixtures', 'main.go')
return atom.workspace.open(filepath).then((e) => {
e.save()
return e.save()
})
})

Expand All @@ -72,7 +72,7 @@ describe('orchestrator', () => {
waitsForPromise(() => {
const filepath = path.join(__dirname, 'fixtures', 'main.go')
return atom.workspace.open(filepath).then((e) => {
e.save()
return e.save()
})
})

Expand All @@ -99,7 +99,7 @@ describe('orchestrator', () => {
waitsForPromise(() => {
const filepath = path.join(__dirname, 'fixtures', 'main.go')
return atom.workspace.open(filepath).then((e) => {
e.save()
return e.save()
})
})

Expand Down Expand Up @@ -139,7 +139,7 @@ describe('orchestrator', () => {
waitsForPromise(() => {
const filepath = path.join(__dirname, 'fixtures', 'main.go')
return atom.workspace.open(filepath).then((e) => {
e.save()
return e.save()
})
})

Expand All @@ -161,7 +161,7 @@ describe('orchestrator', () => {
waitsForPromise(() => {
const filepath = path.join(__dirname, 'fixtures', 'main.go')
return atom.workspace.open(filepath).then((e) => {
e.save()
return e.save()
})
})

Expand All @@ -184,7 +184,7 @@ describe('orchestrator', () => {
waitsForPromise(() => {
const filepath = path.join(__dirname, 'fixtures', 'main.go')
return atom.workspace.open(filepath).then((e) => {
e.save()
return e.save()
})
})

Expand Down
52 changes: 35 additions & 17 deletions spec/test/tester-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,24 @@ describe('tester', () => {
})

it('displays coverage for go source', () => {
let buffer = editor.getBuffer()
buffer.setText('package main\n\nimport "fmt"\n\nfunc main() {\n\tfmt.Println(Hello())\n}\n\nfunc Hello() string {\n\treturn "Hello, 世界"\n}\n')
buffer.save()
let testBuffer = testEditor.getBuffer()
testBuffer.setText('package main\n\nimport "testing"\n\nfunc TestHello(t *testing.T) {\n\tresult := Hello()\n\tif result != "Hello, 世界" {\n\t\tt.Errorf("Expected %s - got %s", "Hello, 世界", result)\n\t}\n}')
testBuffer.save()
let p = tester.runTests(editor)
let buffer
let testBuffer

waitsForPromise(() => { return p })
runs(() => {
buffer = editor.getBuffer()
buffer.setText('package main\n\nimport "fmt"\n\nfunc main() {\n\tfmt.Println(Hello())\n}\n\nfunc Hello() string {\n\treturn "Hello, 世界"\n}\n')
testBuffer = testEditor.getBuffer()
testBuffer.setText('package main\n\nimport "testing"\n\nfunc TestHello(t *testing.T) {\n\tresult := Hello()\n\tif result != "Hello, 世界" {\n\t\tt.Errorf("Expected %s - got %s", "Hello, 世界", result)\n\t}\n}')
})

waitsForPromise(() => {
return Promise.all([
buffer.save(),
testBuffer.save()
])
})

waitsForPromise(() => { return tester.runTests(editor) })

runs(() => {
const layers = tester.markedEditors.get(editor.id)
Expand Down Expand Up @@ -110,15 +119,24 @@ describe('tester', () => {
})

it('clears coverage for go source', () => {
let buffer = editor.getBuffer()
buffer.setText('package main\n\nimport "fmt"\n\nfunc main() {\n\tfmt.Println(Hello())\n}\n\nfunc Hello() string {\n\treturn "Hello, 世界"\n}\n')
buffer.save()
let testBuffer = testEditor.getBuffer()
testBuffer.setText('package main\n\nimport "testing"\n\nfunc TestHello(t *testing.T) {\n\tresult := Hello()\n\tif result != "Hello, 世界" {\n\t\tt.Errorf("Expected %s - got %s", "Hello, 世界", result)\n\t}\n}')
testBuffer.save()
let p = tester.runTests(editor)

waitsForPromise(() => { return p })
let buffer
let testBuffer

runs(() => {
buffer = editor.getBuffer()
buffer.setText('package main\n\nimport "fmt"\n\nfunc main() {\n\tfmt.Println(Hello())\n}\n\nfunc Hello() string {\n\treturn "Hello, 世界"\n}\n')
testBuffer = testEditor.getBuffer()
testBuffer.setText('package main\n\nimport "testing"\n\nfunc TestHello(t *testing.T) {\n\tresult := Hello()\n\tif result != "Hello, 世界" {\n\t\tt.Errorf("Expected %s - got %s", "Hello, 世界", result)\n\t}\n}')
})

waitsForPromise(() => {
return Promise.all([
buffer.save(),
testBuffer.save()
])
})

waitsForPromise(() => { return tester.runTests(editor) })

runs(() => {
let layerids = tester.markedEditors.get(editor.id).split(',')
Expand Down

0 comments on commit 64863c8

Please sign in to comment.