CompileContext#addMessage doesn't honor line and column
已回答
Hello,
I am writing a CompileTask plugin. I have performed all the analysis that I desired and now want to output information to the Build view. I have found that I can use CompileTask#addMessage. However, when the message is shown in the Build view, the line information is not presented (it always shows 1) and the URL link does not jump to the error location in the file.


How do I create a message in the Build view that has the proper line and column information as well as the correct URL?
Thanks
p.s. When I use virtualFile.getPresentableUrl(), there is no URL in the Build view associated with the error.
------------------------
This is what I am doing to generate the information for the addMessage function:
// Note: When I use virtualFile.getPresentableUrl(),
// there is no URL in the Build view associated with the error.
final String url = virtualFile.getUrl();
final Set<MessageInfo> messageInfoSet = new TreeSet<>();
for (final HighlightInfo highlightInfo : fileWithErrors.getHighlightInfoSet())
{
String message = "One or more inspections has resulted in an error";
int line = -1;
int column = -1;
final RangeHighlighterEx highlighter = highlightInfo.getHighlighter();
if (null != highlighter && highlighter.isValid())
{
message = highlightInfo.getDescription();
final int startOffset = highlighter.getStartOffset();
final LineColumn lineColumn =
StringUtil.offsetToLineColumn(psiFile.getText(), startOffset);
line = lineColumn.line;
column = lineColumn.column;
}
compileContext.addMessage(
CompilerMessageCategory.ERROR,message, url, line, column, psiFile))
// NOTE: psiFile was determined in another method and is associated with the
// virtualFile
}
请先登录再写评论。
This is how I retrieved the psiFile:
Hi Todd,
Please try to (EDIT: try one of the following):
In case you pass Navigatable, which is PsiFile in your case, line and column are ignored, and position of the passed Navigatable is used (in the case of PsiFile, it is always 1).
Thanks, Karol Lewandowski. This is a long post as I want to show you the code I am using.
Could you give me another hint? Any help is appreciated.
I wrote a method to check for types that are Navigatable in method return types and field types in the classes I have found accessible to me. Unfortunately, the only results out of 1020 classes were PsiDirectory, PsiFileSystemItem, PsiFile, and, of course, Navigatable; nothing related to errors
I examined the following classes as those were the classes I have accessible to me:
I performed the search using the following code:
This is how I find the HighlightInfo (psiFile and project are known):
FileWithErrors is my own POJO DAO.
I am at a loss as to how to find the proper information to make a useful CompileContext message.
Hi Todd,
It is unclear to me if you tried solutions from my answer. If yes, what is the result?
For me, it seems that the problem is in adding the message:
In this case, line and column are ignored, and the psiFile element offset is applied, which for files is naturally 1.
If you have the correct line and column, just try:
so skip the psiFile.
Hi Karol Lewandowski,
Thank you for clearing that up. I misinterpreted your first post to mean that I should not pass the psiFile as the Navigatable and that I had to find a Navigatable associated with the error to use in it's place. That was why I started searching for other Navigatables.
Leaving off the Navigatable all together worked as you expected it would.
Thank you again for all of your help!