Plug-in Development: Creating Problem Marker for a Given Resource

霸气de小男生 提交于 2019-12-13 02:03:30

问题


I seem to be having a problem with associating a problem marker with a resource; in my case, I'm trying to create a problem marker for the editor.

To achieve this, I've tried to do the following:

public class MyEditor extends TextEditor{

private ColorManager colorManager;

public MyEditor() {
         super();
         ...

         IResource resource = (IResource) getEditorInput().getAdapter(IResource.class);

         try 
         {
             marker = resource.createMarker(IMarker.PROBLEM);            
         }
         catch (CoreException e) 
         {
             e.printStackTrace();
         }
    }

However, the problem is getEditorInput() keeps returning null. I assume I am not calling it at the right location. I thought it would be ideal to create the marker once I'm setting up the editor, but this proves otherwise.

Does anyone have any advice to obtaining the proper resource I want so that I may create the problem marker? I would like to show errors and such within the editor.

I've looked at samples online for creating the marker, but most just show methods that pass the ITextEditor object without showing where the method call is. (for example: Creating Error Marker for Compiler -- see reportError method)

Thank you. Paul

Edit: I have also viewed the following link regarding problem markers, but again, it calls createMarker from a resource(res, in this case), but does not show the setup for it. See Show Syntax Errors in An Eclipse Editor Plugin


回答1:


EditorInput is initialize in init method You can override init or

public class MyEditor extends TextEditor{

private ColorManager colorManager;

public MyEditor() {
         super();
         ...
    }

public void init(IEditorSite site, IEditorInput input)
            throws PartInitException {
         super.init(site, input);
         IResource resource = (IResource) getEditorInput().getAdapter(IResource.class);

         try 
         {
             marker = resource.createMarker(IMarker.PROBLEM);            
         }
         catch (CoreException e) 
         {
             e.printStackTrace();
         }
}



回答2:


I create a marker (including a call to getEditorInput()) from the run() method of an Action object.

public class MyAction extends Action {
   ...
   public void run() {
     ...

     int line = ...;
     IEditorInput ei = editor.getEditorInput()
     if (ei != null)
        createMarkerAt(line, ei);
   }    
}

Addition (Following Paul's comment) How to get an Editor?

Well, In my app I am subclassing AbstractRulerActionDelegate, by overriding the createAction(ITextEditor e, IVerticalRulerInfo ri) method (which, BTW, Is a must - this method is abstract) my app can get the relevant ITextEditor object.



来源:https://stackoverflow.com/questions/3078751/plug-in-development-creating-problem-marker-for-a-given-resource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!