简体   繁体   中英

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

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM