2014-04-24 47 views
3

我正在Eclipse中爲特定於域的語言構建自定義文本編輯器插件。Eclipse編輯器中的標記未顯示消息

我可以檢測編輯器內容格式的錯誤,並希望使用eclipse的標記來指出錯誤給用戶。

我在插件中下面的代碼:

public static void createMarkerForResource(int linenumber, String message) throws CoreException { 
    IResource resource = getFile(); 
    createMarkerForResource(resource, linenumber, message); 
    } 

    public static void createMarkerForResource(IResource resource, int linenumber, String message) 
     throws CoreException { 
    HashMap<String, Object> map = new HashMap<String, Object>(); 
    MarkerUtilities.setLineNumber(map, linenumber); 
    MarkerUtilities.setMessage(map, message); 
    MarkerUtilities.createMarker(resource, map, IMarker.PROBLEM); 
    IMarker[] markers = resource.findMarkers(null, true, IResource.DEPTH_INFINITE); 
    for (IMarker marker : markers){ 
     System.out.println("Marker contents"+MarkerUtilities.getMessage(marker)); 
    } 
    } 

我運行此代碼的命令:

createMarkerForResource(2, "hello"); 

這成功地給了我一個圖像上的正確路線

enter image description here

,如果我把鼠標懸停在它上面,我會得到一個'你可以點擊這個東西'的光標。 但我無法得到消息。

消息肯定已經被放置,這是因爲:

for (IMarker marker : markers){ 
      System.out.println("Marker contents"+MarkerUtilities.getMessage(marker)); 
     } 

代碼產生 「標記contentshello」 輸出按預期方式。我究竟做錯了什麼?

編輯:

消息出現在問題視圖:

enter image description here

回答

0

您需要使用適當的IAnnotationHover,它可以例如在SourceViewerConfiguration定義是這樣的:

@Override 
public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) { 
    return new DefaultAnnotationHover(false); 
} 
2

Njol的答案是正確的,對我的作品(Eclipse的Neon.1)。

但另外兩個建議:

  1. 我重用創建領域,使批註胡佛並不總是新創建(吸氣......不能創造法)
  2. 默認註釋胡佛確實顯示所有註釋。所以當你只想顯示標記(而沒有其他的 - 例如來自GIT的Diff標註)時,你應該重寫isIncluded,就像我下面的例子。

例子:

import org.eclipse.jface.text.source.SourceViewerConfiguration; 
    ... 
    public class MySourceViewerConfiguration extends SourceViewerConfiguration { 
    ... 

     private IAnnotationHover annotationHoover; 
    ... 

     public MySourceViewerConfiguration(){ 
      this.annotationHoover=new MyAnnotationHoover(); 
     } 
    ... 
     @Override 
     public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) { 
     return annotationHoover; 
    } 
} 

而且這裏的註釋胡佛類

private class MyAnnotationHoover extends DefaultAnnotationHover{ 
    @Override 
    protected boolean isIncluded(Annotation annotation) { 
     if (annotation instanceof MarkerAnnotation){ 
       return true; 
     } 
     /* we do not support other annotations than markers*/ 
     return false; 
    } 
}