diff --git a/src/main/kotlin/com/github/oxc/project/oxcintellijplugin/actions/OxcFixAllAction.kt b/src/main/kotlin/com/github/oxc/project/oxcintellijplugin/actions/OxcFixAllAction.kt index 95ebbdd..57162aa 100644 --- a/src/main/kotlin/com/github/oxc/project/oxcintellijplugin/actions/OxcFixAllAction.kt +++ b/src/main/kotlin/com/github/oxc/project/oxcintellijplugin/actions/OxcFixAllAction.kt @@ -11,11 +11,16 @@ import com.intellij.notification.NotificationType import com.intellij.openapi.actionSystem.AnAction import com.intellij.openapi.actionSystem.AnActionEvent import com.intellij.openapi.actionSystem.CommonDataKeys +import com.intellij.openapi.editor.Document +import com.intellij.openapi.editor.EditorFactory import com.intellij.openapi.fileEditor.FileDocumentManager import com.intellij.openapi.options.ShowSettingsUtil import com.intellij.openapi.project.DumbAware +import com.intellij.openapi.vfs.VirtualFile +import com.intellij.openapi.vfs.readText import com.intellij.platform.ide.progress.runWithModalProgressBlocking import kotlinx.coroutines.withTimeout +import java.io.IOException class OxcFixAllAction : AnAction(), DumbAware { init { @@ -24,13 +29,11 @@ class OxcFixAllAction : AnAction(), DumbAware { override fun actionPerformed(event: AnActionEvent) { val project = event.project ?: return - val editor = event.getData(CommonDataKeys.EDITOR) ?: return + val (virtualFile, document) = event.getFileAndDocument() ?: return val notificationGroup = NotificationGroupManager.getInstance().getNotificationGroup("Oxc") val settings = OxcSettings.getInstance(project) - val manager = FileDocumentManager.getInstance() - val virtualFile = manager.getFile(editor.document) ?: return if (!settings.fileSupported(virtualFile)) { notificationGroup.createNotification(title = OxcBundle.message("oxc.file.not.supported.title"), @@ -46,7 +49,7 @@ class OxcFixAllAction : AnAction(), DumbAware { OxcBundle.message("oxc.run.fix.all")) { try { withTimeout(5_000) { - OxcServerService.getInstance(project).fixAll(editor.document) + OxcServerService.getInstance(project).fixAll(virtualFile, document) } notificationGroup.createNotification(title = OxcBundle.message("oxc.fix.all.success.label"), content = OxcBundle.message("oxc.fix.all.success.description"), @@ -58,4 +61,26 @@ class OxcFixAllAction : AnAction(), DumbAware { } } } + + private fun AnActionEvent.getFileAndDocument(): Pair? { + getData(CommonDataKeys.EDITOR)?.let { + val document = it.document + + val manager = FileDocumentManager.getInstance() + val file = manager.getFile(document) ?: return null + + return file to document + } + + val file = getData(CommonDataKeys.VIRTUAL_FILE) ?: return null + val text = try { + file.readText() + } catch (_: IOException) { + return null + } + + val document = EditorFactory.getInstance().createDocument(text) + + return file to document + } } diff --git a/src/main/kotlin/com/github/oxc/project/oxcintellijplugin/services/OxcServerService.kt b/src/main/kotlin/com/github/oxc/project/oxcintellijplugin/services/OxcServerService.kt index 27ce751..4cb6c1b 100644 --- a/src/main/kotlin/com/github/oxc/project/oxcintellijplugin/services/OxcServerService.kt +++ b/src/main/kotlin/com/github/oxc/project/oxcintellijplugin/services/OxcServerService.kt @@ -34,6 +34,11 @@ class OxcServerService(private val project: Project) { suspend fun fixAll(document: Document) { val manager = FileDocumentManager.getInstance() val file = manager.getFile(document) ?: return + + fixAll(file, document) + } + + suspend fun fixAll(file: VirtualFile, document: Document) { val server = getServer(file) ?: return val commandName = OxcBundle.message("oxc.run.quickfix") diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 96d5143..5d90b36 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -48,6 +48,7 @@ class="com.github.oxc.project.oxcintellijplugin.actions.OxcFixAllAction" > +