Skip to content

Commit

Permalink
fix: make sort order in tree view equal to the old sort order
Browse files Browse the repository at this point in the history
  • Loading branch information
bastiandoetsch committed Feb 20, 2024
1 parent e7e1db5 commit d1c90ec
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import io.snyk.plugin.ui.toolwindow.nodes.root.RootQualityIssuesTreeNode
import io.snyk.plugin.ui.toolwindow.nodes.root.RootSecurityIssuesTreeNode
import io.snyk.plugin.ui.toolwindow.nodes.secondlevel.SnykCodeFileTreeNodeFromLS
import snyk.common.ProductType
import snyk.common.SnykCodeFileIssueComparator
import snyk.common.lsp.ScanIssue
import snyk.common.lsp.ScanState
import snyk.common.lsp.SnykScanParams
import java.util.SortedMap
import javax.swing.JTree
import javax.swing.tree.DefaultMutableTreeNode

Expand Down Expand Up @@ -76,7 +78,11 @@ class SnykToolWindowSnykCodeScanListenerLS(
entry.key to entry.value
.filter { pluginSettings().hasSeverityEnabledAndFiltered(it.getSeverityAsEnum()) }
}.toMap()
displayResultsForCodeRoot(rootSecurityIssuesTreeNode, securityResultsToDisplay)

displayResultsForCodeRoot(
rootSecurityIssuesTreeNode,
securityResultsToDisplay.toSortedMap(SnykCodeFileIssueComparator(securityResultsToDisplay))
)
}
}
snykToolWindowPanel.updateTreeRootNodesPresentation(
Expand Down Expand Up @@ -108,7 +114,10 @@ class SnykToolWindowSnykCodeScanListenerLS(
entry.key to entry.value
.filter { pluginSettings().hasSeverityEnabledAndFiltered(it.getSeverityAsEnum()) }
}.toMap()
displayResultsForCodeRoot(rootQualityIssuesTreeNode, qualityResultsToDisplay)
displayResultsForCodeRoot(
rootQualityIssuesTreeNode,
qualityResultsToDisplay.toSortedMap(SnykCodeFileIssueComparator(qualityResultsToDisplay))
)
}
}
snykToolWindowPanel.updateTreeRootNodesPresentation(
Expand All @@ -124,7 +133,7 @@ class SnykToolWindowSnykCodeScanListenerLS(

private fun displayResultsForCodeRoot(
rootNode: DefaultMutableTreeNode,
issues: Map<SnykCodeFile, List<ScanIssue>>
issues: SortedMap<SnykCodeFile, List<ScanIssue>>
) {
fun navigateToSource(virtualFile: VirtualFile, textRange: TextRange): () -> Unit = {
io.snyk.plugin.navigateToSource(project, virtualFile, textRange.startOffset, textRange.endOffset)
Expand Down
26 changes: 26 additions & 0 deletions src/main/kotlin/snyk/common/SnykCachedResults.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package snyk.common
import com.intellij.openapi.components.Service
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.project.Project
import io.snyk.plugin.Severity
import io.snyk.plugin.events.SnykCodeScanListenerLS
import io.snyk.plugin.events.SnykScanListener
import io.snyk.plugin.snykcode.SnykCodeResults
Expand Down Expand Up @@ -147,3 +148,28 @@ class SnykCachedResults(val project: Project) {
)
}
}

internal class SnykCodeFileIssueComparator(
private val snykCodeResults: Map<SnykCodeFile, List<ScanIssue>>
) : Comparator<SnykCodeFile> {
override fun compare(o1: SnykCodeFile, o2: SnykCodeFile): Int {
val o1Criticals = getCount(o1, Severity.CRITICAL)
val o2Criticals = getCount(o2, Severity.CRITICAL)
val o1Errors = getCount(o1, Severity.HIGH)
val o2Errors = getCount(o2, Severity.HIGH)
val o1Warningss = getCount(o1, Severity.MEDIUM)
val o2Warningss = getCount(o2, Severity.MEDIUM)
val o1Infos = getCount(o1, Severity.LOW)
val o2Infos = getCount(o2, Severity.LOW)

return when {
o1Criticals != o2Criticals -> o2Criticals - o1Criticals
o1Errors != o2Errors -> o2Errors - o1Errors
o1Warningss != o2Warningss -> o2Warningss - o1Warningss
else -> o2Infos - o1Infos
}
}

private fun getCount(file: SnykCodeFile, severity: Severity) =
snykCodeResults[file]?.filter { it.getSeverityAsEnum() == severity }?.size ?: 0
}

0 comments on commit d1c90ec

Please sign in to comment.