Skip to content

Commit

Permalink
Get current file info
Browse files Browse the repository at this point in the history
  • Loading branch information
pplam committed Jun 16, 2024
1 parent 70a81c7 commit dcddd02
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/main/kotlin/ai/devchat/plugin/IDEServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }))
}
Expand Down Expand Up @@ -257,6 +266,13 @@ fun Project.getPsiFile(filePath: String): PsiFile = ReadAction.compute<PsiFile,
PsiManager.getInstance(this).findFile(virtualFile!!)
}

fun Project.getCurrentFile(): VirtualFile = ReadAction.compute<VirtualFile, Throwable> {
val editor: Editor? = FileEditorManager.getInstance(this).selectedTextEditor
editor?.document?.let { document ->
FileDocumentManager.getInstance().getFile(document)
}
}

fun Project.computeOffset(
psiFile: PsiFile,
lineNumber: Int?,
Expand Down

0 comments on commit dcddd02

Please sign in to comment.