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

Commit

Permalink
fix temporary file not removed, closes #25
Browse files Browse the repository at this point in the history
  • Loading branch information
chemzqm committed Jun 20, 2019
1 parent 348d2b0 commit 229ed09
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
1 change: 1 addition & 0 deletions .ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tests
12 changes: 6 additions & 6 deletions src/formatters/baseFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Uri, OutputChannel, workspace } from 'coc.nvim'
import { TextDocument, FormattingOptions, Range, TextEdit } from 'vscode-languageserver-types'
import { CancellationToken } from 'vscode-jsonrpc'
import { emptyFn } from '../common/function'
import { promisify } from 'util'

export abstract class BaseFormatter {
protected readonly outputChannel: OutputChannel
Expand Down Expand Up @@ -72,18 +73,17 @@ export abstract class BaseFormatter {
this.handleError(this.Id, error, Uri.parse(document.uri)).catch(() => { })
return [] as TextEdit[]
})
.then(edits => {
this.deleteTempFile(filepath, tempFile).catch(emptyFn)
return edits
})
// tslint:disable-next-line: no-floating-promises
promise.then(() => {
this.deleteTempFile(filepath, tempFile).catch(emptyFn)
workspace.showMessage(`Formatted with ${this.Id}`)
let { nvim } = workspace
setTimeout(async () => {
let line = await nvim.call('coc#util#echo_line') as string
if (line && /Formatted/.test(line)) nvim.command('echo ""', true)
}, 2000)
}, () => {
this.deleteTempFile(filepath, tempFile).catch(emptyFn)
})
return promise
}
Expand All @@ -107,9 +107,9 @@ export abstract class BaseFormatter {
return getTempFileWithDocumentContents(document)
}

private deleteTempFile(originalFile: string, tempFile: string): Promise<void> {
private deleteTempFile(originalFile: string, tempFile: string): Promise<any> {
if (originalFile !== tempFile) {
return fs.unlink(tempFile)
return promisify(fs.unlink)(tempFile)
}
return Promise.resolve()
}
Expand Down

0 comments on commit 229ed09

Please sign in to comment.