Skip to content

Commit

Permalink
fix: auto-save race condition (#26)
Browse files Browse the repository at this point in the history
* fix: execution order

* refactor: remove formatter provider
  • Loading branch information
victor-teles authored Jan 13, 2024
1 parent dba5d5d commit 7d5742f
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,34 @@ import com.github.biomejs.intellijbiome.BiomeBundle
import com.github.biomejs.intellijbiome.BiomeStdinRunner
import com.github.biomejs.intellijbiome.settings.BiomeSettings
import com.intellij.ide.actionsOnSave.impl.ActionsOnSaveFileDocumentManagerListener
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.editor.Document
import com.intellij.openapi.progress.util.ProgressIndicatorUtils
import com.intellij.openapi.project.Project
import java.util.concurrent.CompletableFuture


class ApplySafeFixesOnSaveAction : ActionsOnSaveFileDocumentManagerListener.ActionOnSave() {

override fun isEnabledForProject(project: Project): Boolean =
BiomeSettings.getInstance(project).applySafeFixesOnSave

override fun processDocuments(project: Project, documents: Array<Document?>) {
val runner = BiomeStdinRunner(project)
val onSaveHelper = OnSaveHelper()
val future = CompletableFuture<Void>()

OnSaveHelper().formatDocuments(
onSaveHelper.formatDocuments(
project,
documents.filterNotNull().toList(),
BiomeBundle.message("biome.apply.safe.fix.with.biome")
) { request -> runner.applySafeFixes(request) }
) { request ->
val response = runner.applySafeFixes(request)
future.complete(null)

response
}

ProgressIndicatorUtils.awaitWithCheckCanceled(future)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import com.github.biomejs.intellijbiome.BiomeStdinRunner
import com.github.biomejs.intellijbiome.settings.BiomeSettings
import com.intellij.ide.actionsOnSave.impl.ActionsOnSaveFileDocumentManagerListener
import com.intellij.openapi.editor.Document
import com.intellij.openapi.progress.util.ProgressIndicatorUtils
import com.intellij.openapi.project.Project
import java.util.concurrent.CompletableFuture


class ApplyUnsafeFixesOnSaveAction : ActionsOnSaveFileDocumentManagerListener.ActionOnSave() {
Expand All @@ -14,12 +16,21 @@ class ApplyUnsafeFixesOnSaveAction : ActionsOnSaveFileDocumentManagerListener.Ac

override fun processDocuments(project: Project, documents: Array<Document?>) {
val runner = BiomeStdinRunner(project)
val onSaveHelper = OnSaveHelper()
val future = CompletableFuture<Void>()

OnSaveHelper().formatDocuments(
onSaveHelper.formatDocuments(
project,
documents.filterNotNull().toList(),
BiomeBundle.message("biome.apply.unsafe.fix.with.biome")
) { request -> runner.applyUnsafeFixes(request) }
) { request ->
val response = runner.applyUnsafeFixes(request)
future.complete(null)

response
}

ProgressIndicatorUtils.awaitWithCheckCanceled(future)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,28 @@ import com.github.biomejs.intellijbiome.BiomeBundle
import com.github.biomejs.intellijbiome.BiomeStdinRunner
import com.github.biomejs.intellijbiome.settings.BiomeSettings
import com.intellij.ide.actionsOnSave.impl.ActionsOnSaveFileDocumentManagerListener
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.editor.Document
import com.intellij.openapi.project.Project
import java.util.concurrent.CompletableFuture


class FormatOnSaveAction : ActionsOnSaveFileDocumentManagerListener.ActionOnSave() {
override fun isEnabledForProject(project: Project): Boolean = BiomeSettings.getInstance(project).formatOnSave

override fun processDocuments(project: Project, documents: Array<Document?>) {
val runner = BiomeStdinRunner(project)
val onSaveHelper = OnSaveHelper()
val future = CompletableFuture<Void>()

OnSaveHelper().formatDocuments(
onSaveHelper.formatDocuments(
project,
documents.filterNotNull().toList(),
BiomeBundle.message("biome.apply.safe.fix.with.biome")
) { request -> runner.format(request) }
) { request ->
val response = runner.format(request)
future.complete(null)
response
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ class OnSaveHelper {
is BiomeRunner.Response.Success -> {
WriteCommandAction.writeCommandAction(project)
.withName(request.commandDescription)
.run<Exception> { request.document.setText(response.code) }
.run<Exception> {
request.document.setText(response.code)
FileDocumentManager.getInstance().saveDocument(request.document)
}
}

is BiomeRunner.Response.Failure -> {
Expand Down

This file was deleted.

1 change: 0 additions & 1 deletion src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
nonDefaultProject="false"
key="biome.settings.name"
instance="com.github.biomejs.intellijbiome.settings.BiomeConfigurable"/>
<formattingService implementation="com.github.biomejs.intellijbiome.formatter.FormatterProvider"/>
<actionOnSaveInfoProvider id="BiomeOnSaveInfoProvider"
implementation="com.github.biomejs.intellijbiome.settings.BiomeOnSaveInfoProvider"
order="after FormatOnSaveInfoProvider, after EsLintOnSaveInfoProvider, before BuildOnSaveInfoProvider, before FileWatcherOnSaveInfoProvider, before UploadOnSaveInfoProvider"/>
Expand Down
6 changes: 4 additions & 2 deletions src/main/resources/META-INF/pluginIcon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion src/main/resources/icons/pluginIcon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 7d5742f

Please sign in to comment.