override fun handle()

in src/main/kotlin/org/jetbrains/mcpserverplugin/general/errorTools.kt [38:60]


    override fun handle(project: Project, args: NoArgs): Response {
        return runReadAction {
            try {
                val fileEditorManager = FileEditorManager.getInstance(project)
                val editor = fileEditorManager.selectedTextEditor
                    ?: return@runReadAction Response(error = "no file open")

                val document = editor.document
                val psiFile = PsiDocumentManager.getInstance(project).getPsiFile(document)
                    ?: return@runReadAction Response(error = "could not get PSI file")

                val projectDir = project.guessProjectDir()?.toNioPathOrNull()
                val filePath = psiFile.virtualFile?.toNioPathOrNull()?.relativizeByProjectDir(projectDir) ?: return@runReadAction Response(error = "could not get file path")

                val highlightInfos = getHighlightInfos(project, psiFile, document)
                val errorInfos = formatHighlightInfos(document, highlightInfos, filePath)

                Response(errorInfos.joinToString(",\n", prefix = "[", postfix = "]"))
            } catch (e: Exception) {
                Response(error = "Error analyzing file: ${e.message}")
            }
        }
    }