Skip to content

Commit

Permalink
Close file endpoint (#322)
Browse files Browse the repository at this point in the history
* close file endpoint

* create VirtualFile with jar

Co-authored-by: azdrojowa123 <[email protected]>
  • Loading branch information
azdrojowa123 and azdrojowa123 authored Nov 19, 2022
1 parent de84b54 commit 8c2a6e0
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8c2a6e0

Please sign in to comment.