-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove running 'AutoCorrect' automatically on background and assign i…
…t to IDEA action (#55) * 53 Remove AutoCorrect functionality and allow to users make auto correction manually Auto correction action can be called: Right click on the open kotlin source file -> 'Refactor' -> 'AutoCorrect by Detekt Rules' * 53: Remove 'AutoCorrect' automatical option * 53: Refactor code
- Loading branch information
1 parent
c683a4b
commit df77386
Showing
11 changed files
with
206 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/main/kotlin/io/gitlab/arturbosch/detekt/action/AutoCorrectAction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package io.gitlab.arturbosch.detekt.action | ||
|
||
import com.intellij.openapi.actionSystem.AnAction | ||
import com.intellij.openapi.actionSystem.AnActionEvent | ||
import com.intellij.openapi.actionSystem.CommonDataKeys | ||
import com.intellij.openapi.command.WriteCommandAction | ||
import com.intellij.openapi.fileEditor.FileDocumentManager | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.openapi.util.Computable | ||
import com.intellij.openapi.vfs.VirtualFile | ||
import io.gitlab.arturbosch.detekt.config.DetektConfigStorage | ||
import io.gitlab.arturbosch.detekt.core.ProcessingSettings | ||
import io.gitlab.arturbosch.detekt.util.DetektPluginService | ||
import io.gitlab.arturbosch.detekt.util.FileExtensions | ||
import io.gitlab.arturbosch.detekt.util.absolutePath | ||
import java.io.File | ||
|
||
class AutoCorrectAction : AnAction() { | ||
private lateinit var detektPluginService: DetektPluginService | ||
|
||
override fun update(event: AnActionEvent) { | ||
val file: VirtualFile = event.getData(CommonDataKeys.VIRTUAL_FILE) ?: return | ||
val project = event.getData(CommonDataKeys.PROJECT) ?: return | ||
val configuration = DetektConfigStorage.instance(project) | ||
|
||
if (file.extension == FileExtensions.KOTLIN_FILE_EXTENSION) { | ||
// enable auto corrrect option only when plugin is enabled | ||
event.presentation.isEnabledAndVisible = configuration.enableDetekt | ||
} else { | ||
// hide action for non-Kotlin source files | ||
event.presentation.isEnabledAndVisible = false | ||
} | ||
} | ||
|
||
override fun actionPerformed(event: AnActionEvent) { | ||
val virtualFile = event.getData(CommonDataKeys.VIRTUAL_FILE) | ||
val project = event.getData(CommonDataKeys.PROJECT) | ||
println("Update file") | ||
forceUpdateFile(project, virtualFile) | ||
|
||
if (virtualFile != null && project != null) { | ||
val configuration = DetektConfigStorage.instance(project) | ||
detektPluginService = DetektPluginService(configuration) | ||
|
||
val settings = processingSettings(project, virtualFile, configuration) | ||
|
||
settings?.let { | ||
detektPluginService | ||
.createFacade(settings) | ||
.run() | ||
|
||
virtualFile.refresh(false, false) | ||
println("AutoCorrect should be complete") | ||
} | ||
} | ||
} | ||
|
||
private fun forceUpdateFile(project: Project?, virtualFile: VirtualFile?) { | ||
WriteCommandAction.runWriteCommandAction(project, Computable<Boolean> { | ||
val documentManager = FileDocumentManager.getInstance() | ||
val document = documentManager.getDocument(virtualFile!!) | ||
if (document != null) { | ||
documentManager.saveDocument(document) | ||
println("Force update was completed") | ||
return@Computable false | ||
} | ||
true | ||
}) | ||
} | ||
|
||
private fun processingSettings( | ||
project: Project, | ||
virtualFile: VirtualFile, | ||
configStorage: DetektConfigStorage | ||
): ProcessingSettings? { | ||
val rulesPath = absolutePath(project, configStorage.rulesPath) | ||
if (rulesPath.isNotEmpty()) { | ||
if (!File(rulesPath).exists()) { | ||
return null | ||
} | ||
} | ||
|
||
return detektPluginService.getProcessSettings( | ||
virtualFile = virtualFile, | ||
rulesPath = rulesPath, | ||
configStorage = configStorage, | ||
autoCorrect = true | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.