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

StackOverflow https://stackoverflow.com/questions/13556957

Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top