Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"),
Expand All @@ -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"),
Expand All @@ -58,4 +61,26 @@ class OxcFixAllAction : AnAction(), DumbAware {
}
}
}

private fun AnActionEvent.getFileAndDocument(): Pair<VirtualFile, Document>? {
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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
class="com.github.oxc.project.oxcintellijplugin.actions.OxcFixAllAction"
>
<add-to-group group-id="EditorPopupMenu" anchor="last"/>
<add-to-group group-id="ProjectViewPopupMenu" anchor="last"/>
</action>
</actions>

Expand Down
Loading