Skip to content

Commit

Permalink
SLI-812 Return PSI elements only from background data providers
Browse files Browse the repository at this point in the history
  • Loading branch information
damien-urruty-sonarsource committed Dec 5, 2022
1 parent 8818cea commit ff4e128
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
28 changes: 19 additions & 9 deletions src/main/java/org/sonarlint/intellij/ui/tree/IssueTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@

<change-notes><![CDATA[
<ul>
<li>7.2.1 - Fix an error when selecting issues or taint vulnerabilities in the tool window in 2022.3.</li>
<li>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.</li>
<li>7.1.1 - Fix security issue when opening a hotspot from SonarQube.</li>
<li>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.</li>
Expand Down

0 comments on commit ff4e128

Please sign in to comment.