Want to open a eclipse xml file in code and navigate to a specific line number with reference to its IFile

我的未来我决定 提交于 2019-12-18 09:36:54

问题


I have a reference to an xml file in eclipse IDE through its IFile instance. I know want to add an action on my view that opens the file in the xml editor and navigate to a specific line number. Anyone have any ideas on how to go about this?


回答1:


Assuming you know the file's URL:

IWorkbenchPage page = activeWorkbenchPage();
if (page == null) {
    throw new RuntimeException();
}

IFile file;
IFile[] files = ResourcesPlugin.getWorkspace().getRoot()
            .findFilesForLocationURI(url.toURI());
file = files[0];

IMarker marker;
marker = file.createMarker(IMarker.TEXT);
HashMap<String, Object> map = new HashMap<String, Object>();
map.put(IMarker.LINE_NUMBER, lineNumber);
marker.setAttributes(map);
IDE.openEditor(page, marker);
marker.delete();

Of course you will need to catch/throw a couple of Exceptions as well, but I omitted this here for simplicity.



来源:https://stackoverflow.com/questions/13556957/want-to-open-a-eclipse-xml-file-in-code-and-navigate-to-a-specific-line-number-w

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