對于我的自定義編輯器,我想添加一個標記,但有一個例外。例外是 資源“/c/runtime-EclipseApplication/HelloWorld/hello.me”不存在。 但是 res 不為空,知道嗎?
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>
uj5u.com熱心網友回復:
ResourcesPlugin.getWorkspace().getRoot().getFile(path)需要一個相對于作業空間根目錄的路徑,你給它一個絕對檔案路徑。
用于getFileForLocation完整路徑:
ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path)
另請注意,如果IEditorInput是IFileEditorInputthen 的實體,您可以獲得IFileusingIFileEditorInput.getFile()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/444801.html
標籤:蚀 eclipse-rcp
