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

后端 未结 1 1322
清歌不尽
清歌不尽 2020-12-22 00:55

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

相关标签:
1条回答
  • 2020-12-22 01:49

    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.

    0 讨论(0)
提交回复
热议问题