override fun handle()

in src/main/kotlin/org/jetbrains/mcpserverplugin/general/formatting.kt [30:48]


    override fun handle(project: Project, args: NoArgs): Response {
        val latch = CountDownLatch(1)

        val psiFile = runReadAction {
            return@runReadAction getInstance(project).selectedTextEditor?.document?.run {
                PsiDocumentManager.getInstance(project).getPsiFile(this)
            }
        }

        if (psiFile == null) {return Response(error = "file doesn't exist or can't be opened")}

        val codeProcessor: ReformatCodeProcessor = ReformatCodeProcessor(psiFile, false)
        codeProcessor.setPostRunnable(Runnable {
            latch.countDown()
        })
        ApplicationManager.getApplication().invokeLater(Runnable { codeProcessor.run() })
        latch.await()
        return Response("ok")
    }