Skip to content

Commit

Permalink
Log errors to debug if stack traces without detekt mentioned are thro…
Browse files Browse the repository at this point in the history
…wn by our plugin (#268)

* Log errors to try debug if errors without detekt stacktraces are thrown by our plugin

* Do not allow autocorrect action on readonly files like kt files inside jars

* Prepare 1.21.2 release
  • Loading branch information
arturbosch committed Aug 21, 2022
1 parent f500764 commit 9dee6d6
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 7 deletions.
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -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" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,6 +26,7 @@ import kotlin.io.path.Path

class ConfiguredService(private val project: Project) {

private val logger = logger<ConfiguredService>()
private val settings = project.service<DetektPluginSettings>()

private val projectBasePath = project.basePath?.let(::Path)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,6 +18,8 @@ import org.jetbrains.kotlin.idea.KotlinLanguage

class DetektAnnotator : ExternalAnnotator<PsiFile, List<Finding>>() {

private val logger = logger<DetektAnnotator>()

override fun collectInformation(file: PsiFile): PsiFile = file

override fun doAnnotate(collectedInfo: PsiFile): List<Finding> {
Expand All @@ -35,7 +38,9 @@ class DetektAnnotator : ExternalAnnotator<PsiFile, List<Finding>>() {
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,12 +17,14 @@ import io.gitlab.arturbosch.detekt.idea.util.showNotification

class AutoCorrectAction : AnAction() {

private val logger = logger<AutoCorrectAction>()

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
Expand All @@ -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) {
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@

<change-notes><![CDATA[
<ul>
<li>1.21.2
<br>
<ul>
<li>Do not allow autocorrect action on read-only files</li>
<li>Introduce some debug logging to find out if stack traces without detekt package are thrown by our plugin</li>
</ul>
</li>
<li>1.21.1
<br>
<ul>
Expand Down

0 comments on commit 9dee6d6

Please sign in to comment.