Eclipse标记资源不存在



对于我的自定义编辑器,我想添加一个标记,但遇到了一个异常。例外情况是资源"/c/runtime EclipseApplication/HelloWorld/hello.me"不存在

public static IPath getPathFromEditorInput(IEditorInput input) {
if (input instanceof ILocationProvider)
return ((ILocationProvider) input).getPath(input);
if (input instanceof IURIEditorInput) {
URI uri = ((IURIEditorInput) input).getURI();
if (uri != null) {
IPath path = URIUtil.toPath(uri);
if (path != null)
return path;
}
}
return null;
}
@Override
protected void editorSaved() {
IPath path = getPathFromEditorInput(getEditorInput());
try {
createMarker(ResourcesPlugin.getWorkspace().getRoot().getFile(path), 2);
}
catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
super.editorSaved();
}
public static IMarker createMarker(IResource res, int line) throws CoreException {
IMarker marker = null;
// note: you use the id that is defined in your plugin.xml
if (res != null) {
marker = res.createMarker("com.mymarker");
marker.setAttribute(IMarker.SEVERITY, 0);
marker.setAttribute(IMarker.CHAR_START, 10);
marker.setAttribute(IMarker.CHAR_END, 20);
marker.setAttribute(IMarker.LOCATION, "Snake file");
marker.setAttribute(IMarker.MESSAGE, "Syntax error");
}
return marker;
}

以下是plugin.xml 中的片段

<extension point="org.eclipse.core.resources.markers"  
id="com.mymarker"  
name="ME errors">  
<super type="org.eclipse.core.resources.problemmarker" />  
<super type="org.eclipse.core.resources.textmarker" />  
<persistent value="true" />
</extension>

ResourcesPlugin.getWorkspace().getRoot().getFile(path)需要一个相对于工作区根目录的路径,您给它一个绝对的文件路径。

getFileForLocation用于完整路径:

ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path)

还要注意,如果IEditorInputIFileEditorInput的实例,则可以使用IFileEditorInput.getFile()获得IFile

最新更新