8

上記をプログラム的に実行したいと思います。Eclipse TextEditor でカーソル位置を取得する方法とEclipse プラグインで現在のテキスト エディターのコルサー位置を取得する方法を調べた

ので、現在開いているエディターからカーソル オフセットを取得する方法がわかりました。ただし、プログラムで開いた新しいエディターでカーソルオフセットを設定しようとしています。

現在、新しいエディターを開く方法は次のとおりです。

IWorkbenchWindow win = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    IWorkbenchPage page = win.getActivePage();
    if (page != null) {
        IEditorPart editor = page.getActiveEditor();
        if (editor != null) {
            IEditorInput input = editor.getEditorInput();
            if (input instanceof IFileEditorInput) {
                String fileLocation = ((IFileEditorInput) input).getFile().getLocation().toOSString();
                String newFileLocartion = generateNewFileLocation(fileLocation);
                File file = new File(newFileLocartion);
                IFileStore fileStore = EFS.getLocalFileSystem().getStore(file.toURI());
                try {
                    IDE.openEditorOnFileStore(page, fileStore);
                } catch (PartInitException e) {
                    // TODO error handling
                }
            }
        }
    }

新しいエディターを特定のオフセットで開くように設定する方法はありますか (事前にオフセットを知っていると仮定します)。

ありがとう!

4

2 に答える 2

4

以下を使用しました。これは、前の回答よりも簡単です。int offsetIWorkbenchPage pageandがあると仮定しますIFile file(そして、それらはすべてOPの質問に存在するようです):

ITextEditor editor = (ITextEditor) IDE.openEditor(page, file);
editor.selectAndReveal(offset, 0);

この回答からこれを行う方法を見つけました。(ただし、selectAndRevealの 2 番目のパラメーターをゼロに設定すると、テキストは強調表示されません)

于 2015-02-22T20:38:31.733 に答える
3

このスニペットを使用して、ファイル内の指定された行に移動します。

public static void navigateToLine(IFile file, Integer line)
{
    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put(IMarker.LINE_NUMBER, line);
    IMarker marker = null;
    try {
        marker = file.createMarker(IMarker.TEXT);
        marker.setAttributes(map);
        try {
            IDE.openEditor(getActivePage(), marker);
        } catch ( PartInitException e ) {
            //complain
        }
    } catch ( CoreException e1 ) {
        //complain
    } finally {
        try {
            if (marker != null)
                marker.delete();
        } catch ( CoreException e ) {
            //whatever
        }
    }
}

おそらく必要なものではありませんが、役立つ可能性があります。(//complain は、これが使用されている製品に固有のエラー処理コードを置き換えます)

于 2012-09-04T06:38:00.720 に答える