How To get current file problems

Answered

In VS Code, it has a method “ vscode.languages.getDiagnostics” to get open file problems, I tried to implement a similar approach in IDEA.

fun getDiagnostics(project: Project?) : List<Map<String,Any?>> {

    val fileEditorManager = FileEditorManager.getInstance(project!!)
    val virtualFile = fileEditorManager.selectedFiles[0]
    val psiManager = PsiManager.getInstance(project!!)
    val psiFile = psiManager.findFile(virtualFile)

    val daemonCodeAnalyzer = DaemonCodeAnalyzer.getInstance(project)
    daemonCodeAnalyzer.restart(psiFile!!)
    val daemonCodeAnalyzerImpl = daemonCodeAnalyzer as DaemonCodeAnalyzerImpl
    val allInfos = daemonCodeAnalyzerImpl.getFileLevelHighlights(
        project,
        psiFile!!
    )

but,  allInfos always return [] 
 

0
1 comment

Hello. 

in IDEA the daemon is highly concurrent, it can restart and interrupt itself at any moment, so it's hard to tell when it's “done”. 

THe currently highlighted problems are stored in MarkupModel, you can see them via 

`DocumentMarkupModel.forDocument(document, project, true).getAllHighlighters()`

0

Please sign in to leave a comment.