Skip to content

Commit

Permalink
fix: dataflow and navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
bastiandoetsch committed Feb 21, 2024
1 parent 5f763e2 commit 79268a5
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ class SnykToolWindowPanel(val project: Project) : JPanel(), Disposable {
} else {
ApplicationManager.getApplication().invokeLater {
val snykCachedResults = getSnykCachedResults(project) ?: return@invokeLater
val codeResultsLS = snykCachedResults.currentSnykCodeResultsLS ?: return@invokeLater
val codeResultsLS = snykCachedResults.currentSnykCodeResultsLS

scanListenerLS?.displaySnykCodeResults(codeResultsLS)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ 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 @@ -81,7 +79,7 @@ class SnykToolWindowSnykCodeScanListenerLS(

displayResultsForCodeRoot(
rootSecurityIssuesTreeNode,
securityResultsToDisplay.toSortedMap(SnykCodeFileIssueComparator(securityResultsToDisplay))
securityResultsToDisplay
)
}
}
Expand Down Expand Up @@ -116,7 +114,7 @@ class SnykToolWindowSnykCodeScanListenerLS(
}.toMap()
displayResultsForCodeRoot(
rootQualityIssuesTreeNode,
qualityResultsToDisplay.toSortedMap(SnykCodeFileIssueComparator(qualityResultsToDisplay))
qualityResultsToDisplay
)
}
}
Expand All @@ -133,7 +131,7 @@ class SnykToolWindowSnykCodeScanListenerLS(

private fun displayResultsForCodeRoot(
rootNode: DefaultMutableTreeNode,
issues: SortedMap<SnykCodeFile, List<ScanIssue>>
issues: Map<SnykCodeFile, List<ScanIssue>>
) {
fun navigateToSource(virtualFile: VirtualFile, textRange: TextRange): () -> Unit = {
io.snyk.plugin.navigateToSource(project, virtualFile, textRange.startOffset, textRange.endOffset)
Expand All @@ -149,7 +147,7 @@ class SnykToolWindowSnykCodeScanListenerLS(
val fileTreeNode =
SnykCodeFileTreeNodeFromLS(entry, productType)
rootNode.add(fileTreeNode)
entry.value.sortedByDescending { it.getSeverityAsEnum() }
entry.value.sortedByDescending { it.additionalData.priorityScore }
.forEach { issue ->
fileTreeNode.add(
SuggestionTreeNodeFromLS(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.snyk.plugin.ui.toolwindow.panels

import com.intellij.icons.AllIcons
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiFile
import com.intellij.ui.HyperlinkLabel
import com.intellij.ui.ScrollPaneFactory
Expand All @@ -19,6 +18,7 @@ import io.snyk.plugin.net.FalsePositiveContext
import io.snyk.plugin.net.FalsePositivePayload
import io.snyk.plugin.snykcode.core.PDU
import io.snyk.plugin.snykcode.core.SnykCodeFile
import io.snyk.plugin.toVirtualFile
import io.snyk.plugin.ui.DescriptionHeaderPanel
import io.snyk.plugin.ui.SnykBalloonNotificationHelper
import io.snyk.plugin.ui.baseGridConstraintsAnchorWest
Expand All @@ -28,7 +28,6 @@ import io.snyk.plugin.ui.toolwindow.ReportFalsePositiveDialog
import io.snyk.plugin.ui.toolwindow.ReportFalsePositiveDialog.Companion.FALSE_POSITIVE_REPORTED_TEXT
import io.snyk.plugin.ui.toolwindow.ReportFalsePositiveDialog.Companion.REPORT_FALSE_POSITIVE_TEXT
import snyk.common.lsp.DataFlow
import snyk.common.lsp.MarkerPosition
import snyk.common.lsp.ScanIssue
import java.awt.Color
import java.awt.Dimension
Expand Down Expand Up @@ -119,16 +118,6 @@ class SuggestionDescriptionPanelFromLS(
}
}

private fun getLineOfCode(range: MarkerPosition, file: SnykCodeFile?): String {
val document = file?.virtualFile?.getDocument() ?: return ""
range.rows?.let {
val lineStartOffset = document.getLineStartOffset(it[0])
return document.getText(TextRange(lineStartOffset, document.getLineEndOffset(it[0])))
}

return ""
}

private fun dataFlowPanel(): JPanel? {
val dataFlow = issue.additionalData.dataFlow

Expand Down Expand Up @@ -159,18 +148,18 @@ class SuggestionDescriptionPanelFromLS(
.maxOrNull() ?: 0

val allStepPanels = mutableListOf<JPanel>()
dataflow.forEachIndexed { index, flow ->
dataflow.forEach { flow ->
val stepPanel = stepPanel(
index = index,
index = flow.position,
flow = flow,
maxFilenameLength = max(snykCodeFile.virtualFile.name.length, maxFilenameLength),
maxFilenameLength = max(flow.filePath.toVirtualFile().name.length, maxFilenameLength),
allStepPanels = allStepPanels
)

panel.add(
stepPanel,
baseGridConstraintsAnchorWest(
row = index,
row = flow.position,
fill = GridConstraints.FILL_BOTH,
indent = 0
)
Expand All @@ -193,7 +182,8 @@ class SuggestionDescriptionPanelFromLS(

val paddedStepNumber = (index + 1).toString().padStart(2, ' ')

val fileName = snykCodeFile.virtualFile.name
val virtualFile = flow.filePath.toVirtualFile()
val fileName = virtualFile.name

val lineNumber = flow.flowRange.start.line + 1
val positionLinkText = "$fileName:$lineNumber".padEnd(maxFilenameLength + 5, ' ')
Expand All @@ -205,16 +195,15 @@ class SuggestionDescriptionPanelFromLS(
toolTipText = "Click to show in the Editor",
customFont = JTextArea().font
) {
val file = snykCodeFile.virtualFile
if (!file.isValid) return@linkLabel
if (!virtualFile.isValid) return@linkLabel

val document = file.getDocument()
val lineStartOffset = document?.getLineStartOffset(flow.flowRange.start.line) ?: 0
val startOffset = lineStartOffset + (flow.flowRange.start.character)
val lineEndOffset = document?.getLineStartOffset(flow.flowRange.end.line) ?: 0
val endOffset = lineEndOffset + flow.flowRange.end.character - 1
val document = virtualFile.getDocument()
val startLineStartOffset = document?.getLineStartOffset(flow.flowRange.start.line) ?: 0
val startOffset = startLineStartOffset + (flow.flowRange.start.character)
val endLineStartOffset = document?.getLineStartOffset(flow.flowRange.end.line) ?: 0
val endOffset = endLineStartOffset + flow.flowRange.end.character - 1

navigateToSource(project, snykCodeFile.virtualFile, startOffset, endOffset)
navigateToSource(project, virtualFile, startOffset, endOffset)

allStepPanels.forEach {
it.background = UIUtil.getTextFieldBackground()
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/snyk/common/SnykCachedResults.kt
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ internal class SnykCodeFileIssueComparator(
private val snykCodeResults: Map<SnykCodeFile, List<ScanIssue>>
) : Comparator<SnykCodeFile> {
override fun compare(o1: SnykCodeFile, o2: SnykCodeFile): Int {
val files = o1.virtualFile.path.compareTo(o2.virtualFile.path)
val o1Criticals = getCount(o1, Severity.CRITICAL)
val o2Criticals = getCount(o2, Severity.CRITICAL)
val o1Errors = getCount(o1, Severity.HIGH)
Expand All @@ -166,7 +167,8 @@ internal class SnykCodeFileIssueComparator(
o1Criticals != o2Criticals -> o2Criticals - o1Criticals
o1Errors != o2Errors -> o2Errors - o1Errors
o1Warningss != o2Warningss -> o2Warningss - o1Warningss
else -> o2Infos - o1Infos
o1Infos != o2Infos -> o2Infos - o1Infos
else -> files
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/kotlin/snyk/common/lsp/SnykLanguageClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import io.snyk.plugin.getContentRootVirtualFiles
import io.snyk.plugin.getSyncPublisher
import io.snyk.plugin.pluginSettings
import io.snyk.plugin.snykcode.core.SnykCodeFile
import io.snyk.plugin.toVirtualFile
import io.snyk.plugin.ui.SnykBalloonNotificationHelper
import org.eclipse.lsp4j.ApplyWorkspaceEditParams
import org.eclipse.lsp4j.ApplyWorkspaceEditResponse
Expand Down Expand Up @@ -149,8 +150,8 @@ class SnykLanguageClient : LanguageClient {
private fun getSnykCodeResult(project: Project, snykScan: SnykScanParams): Map<SnykCodeFile, List<ScanIssue>> {
check(snykScan.product == "code") { "Expected Snyk Code scan result" }
val map = snykScan.issues
.groupBy { it.virtualFile }
.map { (file, issues) -> SnykCodeFile(project, file!!) to issues.sorted() }
.groupBy { it.filePath }
.map { (file, issues) -> SnykCodeFile(project, file.toVirtualFile()) to issues.sorted() }
.filter { it.second.isNotEmpty() }
.toMap()
return map.toSortedMap(SnykCodeFileIssueComparator(map))
Expand Down

0 comments on commit 79268a5

Please sign in to comment.