diff --git a/core/api/src/main/scala/org/virtuslab/ideprobe/protocol/Endpoints.scala b/core/api/src/main/scala/org/virtuslab/ideprobe/protocol/Endpoints.scala index 4cd7faf4..7c592c41 100644 --- a/core/api/src/main/scala/org/virtuslab/ideprobe/protocol/Endpoints.scala +++ b/core/api/src/main/scala/org/virtuslab/ideprobe/protocol/Endpoints.scala @@ -37,6 +37,7 @@ object Endpoints extends ConfigFormat { val SetConfig = Request[String, Unit]("config/set") val BuildArtifact = Request[(ProjectRef, String), Unit]("buildArtifact") val OpenEditor = Request[FileRef, Unit]("project/editors/open") + val CloseEditor = Request[FileRef, Unit]("project/editors/close") val GoToLineColumn = Request[(ProjectRef, Int, Int), Unit]("project/editors/current/goto") val AddTrustedPath = Request[Path, Unit]("trustedPaths/add") diff --git a/core/driver/sources/src/main/scala/org/virtuslab/ideprobe/ProbeDriver.scala b/core/driver/sources/src/main/scala/org/virtuslab/ideprobe/ProbeDriver.scala index ba7feea9..98da5969 100644 --- a/core/driver/sources/src/main/scala/org/virtuslab/ideprobe/ProbeDriver.scala +++ b/core/driver/sources/src/main/scala/org/virtuslab/ideprobe/ProbeDriver.scala @@ -319,6 +319,13 @@ class ProbeDriver( send(Endpoints.OpenEditor, FileRef(file, project)) } + /** + * Close file in editor + */ + def closeEditor(file: Path, project: ProjectRef = ProjectRef.Default): Unit = { + send(Endpoints.CloseEditor, FileRef(file, project)) + } + /** * Go to specific location in current editor 1-based index */ diff --git a/core/driver/tests/src/test/scala/org/virtuslab/ideprobe/ProbeDriverTest.scala b/core/driver/tests/src/test/scala/org/virtuslab/ideprobe/ProbeDriverTest.scala index e177ddee..d1152c4d 100644 --- a/core/driver/tests/src/test/scala/org/virtuslab/ideprobe/ProbeDriverTest.scala +++ b/core/driver/tests/src/test/scala/org/virtuslab/ideprobe/ProbeDriverTest.scala @@ -232,6 +232,26 @@ final class ProbeDriverTest extends IdeProbeFixture with Assertions with RobotPl } } + @Test + def closeFileTest(): Unit = { + buildTestFixture.run { intelliJ => + val projectDir = intelliJ.workspace.resolve("simple-sbt-project") + intelliJ.probe.withRobot.openProject(projectDir) + + val bClass = projectDir.resolve("src/main/scala/B.scala") + bClass.write(""" + | + |class B { + |} + |""".stripMargin) + intelliJ.probe.syncFiles() + intelliJ.probe.openEditor(bClass) + intelliJ.probe.closeEditor(bClass) + val files = intelliJ.probe.listOpenEditors() + assert(files.isEmpty) + } + } + @Test def collectHighlightsTest(): Unit = { buildTestFixture.run { intelliJ => diff --git a/core/probePlugin/src/main/scala/org/virtuslab/ideprobe/BaseProbeHandlerContributor.scala b/core/probePlugin/src/main/scala/org/virtuslab/ideprobe/BaseProbeHandlerContributor.scala index bae8d5b5..87262e71 100644 --- a/core/probePlugin/src/main/scala/org/virtuslab/ideprobe/BaseProbeHandlerContributor.scala +++ b/core/probePlugin/src/main/scala/org/virtuslab/ideprobe/BaseProbeHandlerContributor.scala @@ -44,6 +44,7 @@ class BaseProbeHandlerContributor extends ProbeHandlerContributor { .on(Endpoints.ExpandMacro)(ExpandMacro.expand) .on(Endpoints.BuildArtifact)((Builds.buildArtifact _).tupled) .on(Endpoints.OpenEditor)(Editors.open) + .on(Endpoints.CloseEditor)(Editors.close) .on(Endpoints.GoToLineColumn)((Editors.goToLineColumn _).tupled) .on(Endpoints.ListOpenEditors)(Editors.all) .on(Endpoints.AddTrustedPath)(TrustedPaths.add) diff --git a/core/probePlugin/src/main/scala/org/virtuslab/ideprobe/handlers/Editors.scala b/core/probePlugin/src/main/scala/org/virtuslab/ideprobe/handlers/Editors.scala index 11637883..53ae2f55 100644 --- a/core/probePlugin/src/main/scala/org/virtuslab/ideprobe/handlers/Editors.scala +++ b/core/probePlugin/src/main/scala/org/virtuslab/ideprobe/handlers/Editors.scala @@ -24,6 +24,12 @@ object Editors extends IntelliJApi { new OpenFileDescriptor(project, vFile).navigate(true) } + def close(fileRef: FileRef): Unit = + runOnUISync { + val vFile = VFS.toVirtualFile(fileRef.path, refresh = true) + editorManager(fileRef.project).closeFile(vFile) + } + def goToLineColumn(projectRef: ProjectRef, line: Int, column: Int): Unit = { runOnUISync { val editor = editorManager(projectRef).getSelectedTextEditor diff --git a/core/probePlugin/src/main/scala/org/virtuslab/ideprobe/handlers/VFS.scala b/core/probePlugin/src/main/scala/org/virtuslab/ideprobe/handlers/VFS.scala index 9590a238..e6732fcc 100644 --- a/core/probePlugin/src/main/scala/org/virtuslab/ideprobe/handlers/VFS.scala +++ b/core/probePlugin/src/main/scala/org/virtuslab/ideprobe/handlers/VFS.scala @@ -31,7 +31,9 @@ object VFS extends IntelliJApi { } def toVirtualFile(path: Path, refresh: Boolean = false): VirtualFile = { - if (refresh) { + if (path.toString.contains(".jar!")) { + VirtualFileManager.getInstance().findFileByUrl(s"jar://${path.toString}") + } else if (refresh) { LocalFileSystem.getInstance.refreshAndFindFileByNioFile(path) } else { LocalFileSystem.getInstance.findFileByNioFile(path)