Skip to content

Commit

Permalink
Complexity is visible in inspection results
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolaikopernik committed Oct 13, 2024
1 parent fa73020 commit 633e54b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 35 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.gradle
.intellijPlatform/
build/
.qodana
!gradle/wrapper/gradle-wrapper.jar
Expand Down
23 changes: 23 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

# Code Complexity Changelog

## [Unreleased]

## [1.6.1]
- Fixes issue #24
- Small refactoring of inlay ui components
- Complexity score is now visible in the inspection results

## [1.6.0]
- Upgraded to Intellij Gradle plugin 2.0.1
- Support for K2 mode
Expand Down Expand Up @@ -46,3 +53,19 @@
## [0.0.1]
- Initial implementation for Java and Kotlin.
- Publishing of the plugin,

[Unreleased]: https://github.com/nikolaikopernik/code-complexity-plugin/compare/v1.6.1...HEAD
[1.6.1]: https://github.com/nikolaikopernik/code-complexity-plugin/compare/v1.6.0...v1.6.1
[1.6.0]: https://github.com/nikolaikopernik/code-complexity-plugin/compare/v1.5.2...v1.6.0
[1.5.2]: https://github.com/nikolaikopernik/code-complexity-plugin/compare/v1.5.1...v1.5.2
[1.5.1]: https://github.com/nikolaikopernik/code-complexity-plugin/compare/v1.5.0...v1.5.1
[1.5.0]: https://github.com/nikolaikopernik/code-complexity-plugin/compare/v1.4.0...v1.5.0
[1.4.0]: https://github.com/nikolaikopernik/code-complexity-plugin/compare/v1.3.0...v1.4.0
[1.3.0]: https://github.com/nikolaikopernik/code-complexity-plugin/compare/v1.2.1...v1.3.0
[1.2.1]: https://github.com/nikolaikopernik/code-complexity-plugin/compare/v1.2.0...v1.2.1
[1.2.0]: https://github.com/nikolaikopernik/code-complexity-plugin/compare/v1.1.0...v1.2.0
[1.1.0]: https://github.com/nikolaikopernik/code-complexity-plugin/compare/v1.0.1...v1.1.0
[1.0.1]: https://github.com/nikolaikopernik/code-complexity-plugin/compare/v1.0.0...v1.0.1
[1.0.0]: https://github.com/nikolaikopernik/code-complexity-plugin/compare/v0.0.2...v1.0.0
[0.0.2]: https://github.com/nikolaikopernik/code-complexity-plugin/compare/v0.0.1...v0.0.2
[0.0.1]: https://github.com/nikolaikopernik/code-complexity-plugin/commits/v0.0.1
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
pluginGroup = com.github.nikolaikopernik.codecomplexity
pluginName = code-complexity-plugin
pluginRepositoryUrl = https://github.com/nikolaikopernik/code-complexity-plugin
pluginVersion = 1.6.0
pluginVersion = 1.6.1

# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild = 233
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.nikolaikopernik.codecomplexity.core

import com.github.nikolaikopernik.codecomplexity.settings.ComplexityLevel
import com.github.nikolaikopernik.codecomplexity.settings.getConfiguredLevel
import com.github.nikolaikopernik.codecomplexity.settings.getValueToShow
import com.intellij.codeInspection.InspectionManager
import com.intellij.codeInspection.LocalInspectionTool
import com.intellij.codeInspection.ProblemDescriptor
Expand Down Expand Up @@ -37,7 +38,7 @@ class HighCodeComplexityInspection : LocalInspectionTool() {
val namedElement = provider.getNameElementFor(element)
val problemRef = "'${namedElement.text}()'"
problems.add(manager.createProblemDescriptor(namedElement,
"fun $problemRef is very complex",
"fun $problemRef has complexity: ${complexitySink.getValueToShow()}",
isOnTheFly,
emptyArray(),
ProblemHighlightType.GENERIC_ERROR_OR_WARNING))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,33 @@ import com.github.nikolaikopernik.codecomplexity.settings.ComplexityLevel.HARD
import com.github.nikolaikopernik.codecomplexity.settings.ComplexityLevel.MIDDLE
import com.github.nikolaikopernik.codecomplexity.settings.ComplexityLevel.SIMPLE
import com.intellij.openapi.application.CachedSingletonsRegistry
import com.intellij.openapi.editor.DefaultLanguageHighlighterColors
import com.intellij.openapi.util.IconLoader
import com.intellij.ui.JBColor
import java.awt.Color
import java.util.function.Supplier
import javax.swing.Icon

private val COLOR_SIMPLE_COMPLEXITY: Color = "#4bb14f".parseColor()
private val COLOR_MIDDLE_COMPLEXITY: Color = "#FF8400".parseColor()
private val COLOR_HIGH_COMPLEXITY: Color = "#FC2947".parseColor()

private val stateSupplier: Supplier<SettingsState> = CachedSingletonsRegistry.lazy { SettingsState.INSTANCE }

private var settings = SettingsState.INSTANCE

fun ComplexitySink.getConfiguredText(): String {
val value = this.getComplexity()
return if (stateSupplier.get().usePlainComplexity) {
stateSupplier.get().determineLevel(value,
this.getPoints().any { it.type == PointType.METHOD },
{ customHintTextWithScore(settings.hintTextSimpleComplex, "$value") },
{ customHintTextWithScore(settings.hintTextMildlyComplex, "$value") },
{ customHintTextWithScore(settings.hintTextVeryComplex, "$value") })
val showValue = this.getValueToShow()
return stateSupplier.get().determineLevel(value,
this.getPoints().any { it.type == PointType.METHOD },
{ customHintTextWithScore(settings.hintTextSimpleComplex, showValue) },
{ customHintTextWithScore(settings.hintTextMildlyComplex, showValue) },
{ customHintTextWithScore(settings.hintTextVeryComplex, showValue) })
}

fun ComplexitySink.getValueToShow(): String =
if (stateSupplier.get().usePlainComplexity) {
this.getComplexity().toString()
} else {
val threshold = if (this.getPoints().any { it.type == PointType.METHOD })
stateSupplier.get().limitSimpleLessThan * 4 else stateSupplier.get().limitSimpleLessThan
val pncValue = this.getComplexity() * 100 / threshold
stateSupplier.get().determineLevel(value,
this.getPoints().any { it.type == PointType.METHOD },
{ customHintTextWithScore(settings.hintTextSimpleComplex, "$pncValue%") },
{ customHintTextWithScore(settings.hintTextMildlyComplex, "$pncValue%") },
{ customHintTextWithScore(settings.hintTextVeryComplex, "$pncValue%") })
"${this.getComplexity() * 100 / threshold}%"
}
}

fun ComplexitySink.getConfiguredColor(): Color {
return stateSupplier.get().determineLevel(this.getComplexity(),
this.getPoints().any { it.type == PointType.METHOD },
{ DefaultLanguageHighlighterColors.INLINE_PARAMETER_HINT.defaultAttributes.foregroundColor },
{ COLOR_MIDDLE_COMPLEXITY },
{ COLOR_HIGH_COMPLEXITY })
}


fun ComplexitySink.getConfiguredIcon(): Icon {
return stateSupplier.get().determineLevel(this.getComplexity(),
Expand Down Expand Up @@ -91,9 +75,4 @@ private fun customHintTextWithScore(template: String, score: String): String {
private fun String.parseColor() = Color(this.drop(1).toInt(16), false)


fun Color.getContrastColor(): Color {
val y = (299 * this.red + 587 * this.green + 114 * this.blue) / 1000.0
return if (y >= 128) JBColor.BLACK else JBColor.WHITE
}

fun Color.toHex(): String = String.format("#%02x%02x%02x", this.red, this.green, this.blue)

0 comments on commit 633e54b

Please sign in to comment.