From dcddd021628c86e99c854cf446876b633d547903 Mon Sep 17 00:00:00 2001 From: Luo Tim Date: Mon, 17 Jun 2024 00:07:07 +0800 Subject: [PATCH] Get current file info --- src/main/kotlin/ai/devchat/plugin/IDEServer.kt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/main/kotlin/ai/devchat/plugin/IDEServer.kt b/src/main/kotlin/ai/devchat/plugin/IDEServer.kt index be4b4dd..66b176c 100644 --- a/src/main/kotlin/ai/devchat/plugin/IDEServer.kt +++ b/src/main/kotlin/ai/devchat/plugin/IDEServer.kt @@ -20,6 +20,7 @@ import com.intellij.openapi.project.ProjectManagerListener import com.intellij.openapi.util.Computable import com.intellij.openapi.util.TextRange import com.intellij.openapi.vfs.LocalFileSystem +import com.intellij.openapi.vfs.VirtualFile import com.intellij.psi.* import com.intellij.psi.search.searches.ReferencesSearch import com.intellij.psi.util.PsiTreeUtil @@ -118,6 +119,14 @@ class IDEServer(private var project: Project) { call.respond(Result("intellij")) } + get("/current_file_info") { + val file: VirtualFile = project.getCurrentFile() + call.respond(Result(mapOf( + "path" to file.path, + "extension" to file.extension, + ))) + } + post("/registered_languages") { call.respond(Result(Language.getRegisteredLanguages().map { it.id })) } @@ -257,6 +266,13 @@ fun Project.getPsiFile(filePath: String): PsiFile = ReadAction.compute { + val editor: Editor? = FileEditorManager.getInstance(this).selectedTextEditor + editor?.document?.let { document -> + FileDocumentManager.getInstance().getFile(document) + } +} + fun Project.computeOffset( psiFile: PsiFile, lineNumber: Int?,