diff --git a/changelog.md b/changelog.md index 35f9cf8..43428c9 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,10 @@ ### Changelog +#### 1.21.2 + +- Do not allow autocorrect action on read-only files - [#268](https://github.com/detekt/detekt-intellij-plugin/pull/268) +- Introduce some debug logging to find out if stack traces without detekt package are thrown by our plugin - [#268](https://github.com/detekt/detekt-intellij-plugin/pull/268) + #### 1.21.1 - Settings UI overhaul - [#240](https://github.com/detekt/detekt-intellij-plugin/pull/240) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 6749d4b..e247b56 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,6 +1,6 @@ [versions] detekt = "1.21.0" -detektIJ = "1.21.1" +detektIJ = "1.21.2" [libraries] detekt-core = { group = "io.gitlab.arturbosch.detekt", name = "detekt-core", version.ref = "detekt" } diff --git a/src/main/kotlin/io/gitlab/arturbosch/detekt/idea/ConfiguredService.kt b/src/main/kotlin/io/gitlab/arturbosch/detekt/idea/ConfiguredService.kt index 66551d2..ae11cca 100644 --- a/src/main/kotlin/io/gitlab/arturbosch/detekt/idea/ConfiguredService.kt +++ b/src/main/kotlin/io/gitlab/arturbosch/detekt/idea/ConfiguredService.kt @@ -3,6 +3,7 @@ package io.gitlab.arturbosch.detekt.idea import com.intellij.openapi.application.runReadAction import com.intellij.openapi.application.runWriteAction import com.intellij.openapi.components.service +import com.intellij.openapi.diagnostic.logger import com.intellij.openapi.project.Project import com.intellij.openapi.project.guessProjectDir import com.intellij.psi.PsiFile @@ -25,6 +26,7 @@ import kotlin.io.path.Path class ConfiguredService(private val project: Project) { + private val logger = logger() private val settings = project.service() private val projectBasePath = project.basePath?.let(::Path) @@ -103,8 +105,12 @@ class ConfiguredService(private val project: Project) { val pathToAnalyze = file.virtualFile ?.canonicalPath ?: return emptyList() - val content = runReadAction { file.text } - return execute(content, pathToAnalyze, autoCorrect) + val content = runCatching { runReadAction { file.text } } + .onFailure { logger.error("Unexpected error while reading file content: ${file.virtualFile.path}", it) } + .getOrThrow() + return runCatching { execute(content, pathToAnalyze, autoCorrect) } + .onFailure { logger.error("Unexpected error while running detekt analysis", it) } + .getOrDefault(emptyList()) } @OptIn(UnstableApi::class) diff --git a/src/main/kotlin/io/gitlab/arturbosch/detekt/idea/DetektAnnotator.kt b/src/main/kotlin/io/gitlab/arturbosch/detekt/idea/DetektAnnotator.kt index 29b9f8e..9e5b233 100644 --- a/src/main/kotlin/io/gitlab/arturbosch/detekt/idea/DetektAnnotator.kt +++ b/src/main/kotlin/io/gitlab/arturbosch/detekt/idea/DetektAnnotator.kt @@ -4,6 +4,7 @@ import com.intellij.lang.annotation.AnnotationHolder import com.intellij.lang.annotation.ExternalAnnotator import com.intellij.lang.annotation.HighlightSeverity import com.intellij.openapi.components.service +import com.intellij.openapi.diagnostic.logger import com.intellij.openapi.util.TextRange import com.intellij.psi.PsiFile import io.gitlab.arturbosch.detekt.api.CorrectableCodeSmell @@ -17,6 +18,8 @@ import org.jetbrains.kotlin.idea.KotlinLanguage class DetektAnnotator : ExternalAnnotator>() { + private val logger = logger() + override fun collectInformation(file: PsiFile): PsiFile = file override fun doAnnotate(collectedInfo: PsiFile): List { @@ -35,7 +38,9 @@ class DetektAnnotator : ExternalAnnotator>() { return emptyList() } - return service.execute(collectedInfo, autoCorrect = false) + return runCatching { service.execute(collectedInfo, autoCorrect = false) } + .onFailure { logger.error("Unexpected error while running detekt service", it) } + .getOrThrow() } override fun apply( diff --git a/src/main/kotlin/io/gitlab/arturbosch/detekt/idea/action/AutoCorrectAction.kt b/src/main/kotlin/io/gitlab/arturbosch/detekt/idea/action/AutoCorrectAction.kt index 0982aa6..d620d77 100644 --- a/src/main/kotlin/io/gitlab/arturbosch/detekt/idea/action/AutoCorrectAction.kt +++ b/src/main/kotlin/io/gitlab/arturbosch/detekt/idea/action/AutoCorrectAction.kt @@ -4,6 +4,7 @@ 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.diagnostic.logger import com.intellij.openapi.fileEditor.FileDocumentManager import com.intellij.openapi.project.Project import com.intellij.openapi.vfs.VirtualFile @@ -16,12 +17,14 @@ import io.gitlab.arturbosch.detekt.idea.util.showNotification class AutoCorrectAction : AnAction() { + private val logger = logger() + override fun update(event: AnActionEvent) { val file: VirtualFile = event.getData(CommonDataKeys.VIRTUAL_FILE) ?: return val project = event.getData(CommonDataKeys.PROJECT) ?: return - if (file.extension in KOTLIN_FILE_EXTENSIONS) { - // enable auto correct option only when plugin is enabled + if (!file.fileSystem.isReadOnly && file.extension in KOTLIN_FILE_EXTENSIONS) { + // enable autocorrect option only when plugin is enabled event.presentation.isEnabledAndVisible = project.isDetektEnabled() } else { // hide action for non-Kotlin source files @@ -33,7 +36,13 @@ class AutoCorrectAction : AnAction() { val virtualFile = event.getData(CommonDataKeys.VIRTUAL_FILE) ?: return val project = event.getData(CommonDataKeys.PROJECT) ?: return val psiFile = PsiManager.getInstance(project).findFile(virtualFile) ?: return - runAction(project, psiFile) + if (virtualFile.fileSystem.isReadOnly) { + logger.trace("Skipping readOnly file: ${virtualFile.path}") + return + } + runCatching { runAction(project, psiFile) } + .onFailure { logger.error("Unexpected error while performing auto correct action", it) } + .getOrThrow() } internal fun runAction(project: Project, psiFile: PsiFile) { diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index d402bf5..6b0fcd7 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -42,6 +42,13 @@ +
  • 1.21.2 +
    +
      +
    • Do not allow autocorrect action on read-only files
    • +
    • Introduce some debug logging to find out if stack traces without detekt package are thrown by our plugin
    • +
    +
  • 1.21.1