Skip to content

Commit 8715196

Browse files
authored
Merge pull request #173 from skirge/bug-modules
fix bug for multimodule projects and adds support for decompiled code
2 parents 6ee65b8 + e5e9623 commit 8715196

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

src/main/kotlin/com/github/adrienpessu/sarifviewer/toolWindow/SarifViewerWindowFactory.kt

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@ import com.intellij.openapi.fileEditor.OpenFileDescriptor
2727
import com.intellij.openapi.project.DumbService
2828
import com.intellij.openapi.project.Project
2929
import com.intellij.openapi.ui.ComboBox
30+
import com.intellij.openapi.vfs.VirtualFile
3031
import com.intellij.openapi.vfs.VirtualFileManager
3132
import com.intellij.openapi.ui.SimpleToolWindowPanel
3233
import com.intellij.openapi.wm.ToolWindow
3334
import com.intellij.openapi.wm.ToolWindowFactory
35+
import com.intellij.psi.search.FilenameIndex
36+
import com.intellij.psi.search.GlobalSearchScope
3437
import com.intellij.ui.ScrollPaneFactory
3538
import com.intellij.ui.components.JBPanel
3639
import com.intellij.ui.components.JBTabbedPane
@@ -41,6 +44,7 @@ import git4idea.GitLocalBranch
4144
import git4idea.repo.GitRepository
4245
import git4idea.repo.GitRepositoryChangeListener
4346
import git4idea.repo.GitRepositoryManager
47+
import org.apache.commons.lang.mutable.Mutable
4448
import java.awt.Component
4549
import java.awt.Cursor
4650
import java.awt.Desktop
@@ -52,7 +56,6 @@ import java.io.File
5256
import java.net.URI
5357
import java.nio.charset.Charset
5458
import java.nio.file.Files
55-
import java.nio.file.Path
5659
import javax.swing.*
5760
import javax.swing.event.TreeSelectionEvent
5861
import javax.swing.event.TreeSelectionListener
@@ -599,6 +602,26 @@ class SarifViewerWindowFactory : ToolWindowFactory {
599602
UIManager.put("Tree.leafIcon", icon)
600603
}
601604

605+
private fun findPath(project: Project, path: String): VirtualFile? {
606+
val fileName = path.substring(path.lastIndexOf('/')+1)
607+
val pathOnly = path.substring(0, path.lastIndexOf('/')+1)
608+
609+
val lastDot = fileName.lastIndexOf('.')
610+
val decompiledFileName = if (lastDot != -1) {
611+
val nameWithoutExt = fileName.substring(0, lastDot)
612+
val ext = fileName.substring(lastDot + 1)
613+
when (ext) {
614+
"scala", "kt" -> "$nameWithoutExt.java"
615+
else -> fileName
616+
}
617+
} else fileName
618+
val collection : MutableCollection<VirtualFile> = FilenameIndex.getVirtualFilesByName(fileName, GlobalSearchScope.allScope(project))
619+
// during decompilation original file ext is lost
620+
collection.addAll(FilenameIndex.getVirtualFilesByName(decompiledFileName, GlobalSearchScope.allScope(project)))
621+
622+
return collection.find { virtualFile -> virtualFile.path.endsWith(path) or virtualFile.path.endsWith("${pathOnly}$decompiledFileName") }
623+
}
624+
602625
private fun openFile(
603626
project: Project,
604627
path: String,
@@ -609,15 +632,16 @@ class SarifViewerWindowFactory : ToolWindowFactory {
609632
description: String = ""
610633
) {
611634

612-
val editor: Editor = FileEditorManager.getInstance(project).selectedTextEditor ?: return
613-
val inlayModel = editor.inlayModel
635+
val editor: Editor? = FileEditorManager.getInstance(project).selectedTextEditor
636+
if (editor != null) {
637+
val inlayModel = editor.inlayModel
614638

615-
inlayModel.getBlockElementsInRange(0, editor.document.textLength)
616-
.filter { it.renderer is MyCustomInlayRenderer }
617-
.forEach { it.dispose() }
639+
inlayModel.getBlockElementsInRange(0, editor.document.textLength)
640+
.filter { it.renderer is MyCustomInlayRenderer }
641+
.forEach { it.dispose() }
642+
}
643+
val virtualFile : VirtualFile? = findPath(project,path)
618644

619-
val virtualFile =
620-
VirtualFileManager.getInstance().findFileByNioPath(Path.of("${project.basePath}/$path"))
621645
if (virtualFile != null) {
622646
FileEditorManager.getInstance(project).openTextEditor(
623647
OpenFileDescriptor(
@@ -649,7 +673,7 @@ class SarifViewerWindowFactory : ToolWindowFactory {
649673
Notification(
650674
"Sarif viewer",
651675
"File not found",
652-
"Can't find the file ${project.basePath}/$path",
676+
"Can't find the file $path",
653677
NotificationType.WARNING
654678
), project
655679
)

0 commit comments

Comments
 (0)