How to add LineHighlighter to given set of files.

I have developed a plugin to find errors in code. I have add addLineHighlighter to currently opened file as below,

MarkupModel markupModel = FileEditorManager.getInstance(project).getSelectedTextEditor().getMarkupModel();
markupModel.addLineHighlighter(line, HighlighterLayer.ERROR,attributes);

how can i get markupmodel for given files not currently opened file.
Ex- "com/Abc.java"
0
2 comments

There are two types of markup - document-based and editor-based. The former one will be displayed in all editors for a given document.

If you have a reference to a VirtualFile, you can get corresponding document's markup model as follows:

Document document = FileDocumentManager.getInstance().getDocument(virtualFile);
MarkupModel markupModel = DocumentMarkupModel.forDocument(document, project, true);

If you need editor-based markup, you can obtain opened editor instances from FileEditorManager, e.g. as follows:

FileEditor[] editors = FileEditorManager.getInstance(project).getEditors(virtualFile);
for (FileEditor editor : editors) {
  if (editor instanceof TextEditor) {
    MarkupModel markupModel = ((TextEditor)editor).getEditor().getMarkupModel();
    ...
  }
}

 

0
Avatar
Permanently deleted user

Its working. Thank you very much Dmitry. 

0

Please sign in to leave a comment.