@@ -27,10 +27,13 @@ import com.intellij.openapi.fileEditor.OpenFileDescriptor
2727import com.intellij.openapi.project.DumbService
2828import com.intellij.openapi.project.Project
2929import com.intellij.openapi.ui.ComboBox
30+ import com.intellij.openapi.vfs.VirtualFile
3031import com.intellij.openapi.vfs.VirtualFileManager
3132import com.intellij.openapi.ui.SimpleToolWindowPanel
3233import com.intellij.openapi.wm.ToolWindow
3334import com.intellij.openapi.wm.ToolWindowFactory
35+ import com.intellij.psi.search.FilenameIndex
36+ import com.intellij.psi.search.GlobalSearchScope
3437import com.intellij.ui.ScrollPaneFactory
3538import com.intellij.ui.components.JBPanel
3639import com.intellij.ui.components.JBTabbedPane
@@ -41,6 +44,7 @@ import git4idea.GitLocalBranch
4144import git4idea.repo.GitRepository
4245import git4idea.repo.GitRepositoryChangeListener
4346import git4idea.repo.GitRepositoryManager
47+ import org.apache.commons.lang.mutable.Mutable
4448import java.awt.Component
4549import java.awt.Cursor
4650import java.awt.Desktop
@@ -52,7 +56,6 @@ import java.io.File
5256import java.net.URI
5357import java.nio.charset.Charset
5458import java.nio.file.Files
55- import java.nio.file.Path
5659import javax.swing.*
5760import javax.swing.event.TreeSelectionEvent
5861import 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