From ff4e12832dd62bda4b7cb2c725ac50ece060e4d2 Mon Sep 17 00:00:00 2001 From: Damien Urruty Date: Mon, 5 Dec 2022 16:45:23 +0100 Subject: [PATCH] SLI-812 Return PSI elements only from background data providers --- .../sonarlint/intellij/ui/tree/IssueTree.java | 28 +++++++++++++------ .../ui/tree/TaintVulnerabilityTree.kt | 21 +++++++++----- src/main/resources/META-INF/plugin.xml | 1 + 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/sonarlint/intellij/ui/tree/IssueTree.java b/src/main/java/org/sonarlint/intellij/ui/tree/IssueTree.java index 7a8adc0b7f..3e8e4b4355 100644 --- a/src/main/java/org/sonarlint/intellij/ui/tree/IssueTree.java +++ b/src/main/java/org/sonarlint/intellij/ui/tree/IssueTree.java @@ -82,19 +82,16 @@ private void init() { @Nullable @Override public Object getData(@NonNls String dataId) { - if (CommonDataKeys.NAVIGATABLE.is(dataId)) { + // use string literal as the key appeared in newer versions + if ("bgtDataProvider".equals(dataId)) { + return getBackgroundDataProvider(); + } else if (CommonDataKeys.NAVIGATABLE.is(dataId)) { return navigate(); } else if (PlatformDataKeys.TREE_EXPANDER.is(dataId)) { return new DefaultTreeExpander(this); - } else if (PlatformDataKeys.VIRTUAL_FILE.is(dataId)) { + } else if (CommonDataKeys.VIRTUAL_FILE.is(dataId)) { return getSelectedFile(); - } else if (PlatformDataKeys.PSI_FILE.is(dataId)) { - var file = getSelectedFile(); - if (file != null && file.isValid()) { - return PsiManager.getInstance(project).findFile(file); - } - return null; - } else if (PlatformDataKeys.VIRTUAL_FILE_ARRAY.is(dataId)) { + } else if (CommonDataKeys.VIRTUAL_FILE_ARRAY.is(dataId)) { var f = getSelectedFile(); // return empty so that it doesn't find it in parent components return f != null && f.isValid() ? (new VirtualFile[] {f}) : new VirtualFile[0]; @@ -105,6 +102,19 @@ public Object getData(@NonNls String dataId) { return null; } + private DataProvider getBackgroundDataProvider() { + var file = getSelectedFile(); + if (file != null && file.isValid()) { + return otherId -> { + if (CommonDataKeys.PSI_FILE.is(otherId)) { + return PsiManager.getInstance(project).findFile(file); + } + return null; + }; + } + return null; + } + @CheckForNull private OpenFileDescriptor navigate() { var issue = getSelectedIssue(); diff --git a/src/main/java/org/sonarlint/intellij/ui/tree/TaintVulnerabilityTree.kt b/src/main/java/org/sonarlint/intellij/ui/tree/TaintVulnerabilityTree.kt index 7199c7970d..46bb21c1fe 100644 --- a/src/main/java/org/sonarlint/intellij/ui/tree/TaintVulnerabilityTree.kt +++ b/src/main/java/org/sonarlint/intellij/ui/tree/TaintVulnerabilityTree.kt @@ -150,22 +150,18 @@ class TaintVulnerabilityTree(private val project: Project, model: TreeModel) : T return node.file() } - fun getSelectedNode(): DefaultMutableTreeNode? { + private fun getSelectedNode(): DefaultMutableTreeNode? { val path = selectionPath ?: return null return path.lastPathComponent as DefaultMutableTreeNode } override fun getData(dataId: String): Any? { return when { + // use string literal as the key appeared in newer versions + "bgtDataProvider" == dataId -> getBackgroundDataProvider() CommonDataKeys.NAVIGATABLE.`is`(dataId) -> navigate() PlatformDataKeys.TREE_EXPANDER.`is`(dataId) -> DefaultTreeExpander(this) PlatformDataKeys.VIRTUAL_FILE.`is`(dataId) -> getSelectedFile() - PlatformDataKeys.PSI_FILE.`is`(dataId) -> { - val file = getSelectedFile() - if (file != null && file.isValid) { - PsiManager.getInstance(project).findFile(file) - } else null - } PlatformDataKeys.VIRTUAL_FILE_ARRAY.`is`(dataId) -> { val f = getSelectedFile() // return empty so that it doesn't find it in parent components @@ -176,6 +172,17 @@ class TaintVulnerabilityTree(private val project: Project, model: TreeModel) : T } } + private fun getBackgroundDataProvider(): DataProvider? { + val file = getSelectedFile() + return file?.isValid?.let { + DataProvider { otherId -> + return@DataProvider if (CommonDataKeys.PSI_FILE.`is`(otherId)) { + PsiManager.getInstance(project).findFile(file) + } else null + } + } + } + fun getIssueFromSelectedNode(): LocalTaintVulnerability? { val node = getSelectedNode() ?: return null return when (node) { diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 26d24a828d..b213c433be 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -57,6 +57,7 @@ +
  • 7.2.1 - Fix an error when selecting issues or taint vulnerabilities in the tool window in 2022.3.
  • 7.2 - Help users to generate a token when creating a new connection. Add quick fixes in Rider for C#. 2 new rules, 4 new quick fixes for Python. 1 new rule for credentials detection for Java. Support analysis of JS in YAML files. New AWS CDK rules. Bug fixes, fewer FPs and improvements for many languages.
  • 7.1.1 - Fix security issue when opening a hotspot from SonarQube.
  • 7.1 - Support analysis of Kotlin 1.7.10. 8 new rules around unit tests for Python. Support analysis of PHP 8.2. Support analysis of TypeScript 4.8. Remove support for Node.JS 12 and deprecate support for Node.JS 14. Add 7 rules for React. Bug fixes, fewer FPs and improvements for many languages.