override fun handle()

in src/main/kotlin/org/jetbrains/mcpserverplugin/general/formatting.kt [64:90]


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

        val projectDir = project.guessProjectDir()?.toNioPathOrNull()
            ?: return Response(error = "project dir not found")

        val file = runReadAction {
            LocalFileSystem.getInstance()
                .refreshAndFindFileByNioFile(projectDir.resolveRel(args.pathInProject))
        }

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

        val psiFile = runReadAction {
            return@runReadAction PsiManager.getInstance(project).findFile(file)
        }

        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")
    }