【问题标题】:Open file to a certain line in Eclipse plugin在 Eclipse 插件中打开文件到某一行
【发布时间】:2014-08-21 07:40:15
【问题描述】:

我正在编写一个插件,当按下按钮时,它必须在某一行打开文件。 我有以下代码在某一行打开文件。

        String filePath = "file path" ;
            final IFile inputFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(Path.fromOSString(filePath));
            if (inputFile != null) {
                IWorkbenchPage page1 = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
                IEditorPart openEditor11 = IDE.openEditor(page1, inputFile);
            }


            int Line = 20;

                    if (openEditor11 instanceof ITextEditor) {
                        ITextEditor textEditor = (ITextEditor) openEditor ;
                        IDocument document= textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
                        textEditor.selectAndReveal(document.getLineOffset(Line - 1), document.getLineLength(Line-1));
                    }

我的问题是 if 语句中的变量 openEditor11 给出了错误:openEditor11 无法解析为变量。可能是什么问题?

【问题讨论】:

  • 问题是openEditor11无法解析为变量。 :) 如果您重新学习 Java 课程,您会帮自己一个忙,因为在掌握基础知识之前潜入任何编程都是一种痛苦。
  • 我现在在一个项目中尝试学习 Java。
  • 我已经完成了那个名为 eclipselink 的项目。多年前在 eclipseplugincentral.com 下可用。现在在这里服务:webmasterwork.com/page/…
  • 谢谢!我只是尝试学习一些Java和Eclipse插件:)
  • 我在决定如果文件被打开两次时我应该关注哪个编辑器以及如果 eclipse 窗口被克隆并且如果用户启动多个 eclipse 实例时我应该使用哪个窗口时遇到问题,eclipse 应该采取什么请求(最后一个问题可能不是你的,因为你知道你的日食)。

标签: java eclipse plugins eclipse-plugin


【解决方案1】:

这是因为嵌套在 if 语句中的变量声明在完成条件时超出了范围。因此,该变量被释放,并且在您的第二条语句中不再存在。

您应该事先声明它以规避这样的错误:

IEditorPart openEditor11;
String filePath = "file path" ;
final IFile inputFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(Path.fromOSString(filePath));
if (inputFile != null) {
    IWorkbenchPage page1 = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    openEditor11 = IDE.openEditor(page1, inputFile);
}


int Line = 20;

if (openEditor11 instanceof ITextEditor) {
    ITextEditor textEditor = (ITextEditor) openEditor ;
    IDocument document= textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
    textEditor.selectAndReveal(document.getLineOffset(Line - 1), document.getLineLength(Line-1));
}

到达第二个条件块,如果第一个条件不适用,编辑器可能为 null,但这不是问题,因为 instanceof 在 null 上返回 false。

【讨论】:

    【解决方案2】:

    根据https://wiki.eclipse.org/FAQ_How_do_I_open_an_editor_on_a_file_in_the_workspace%3F,实际上有一个更直接的解决方案

    int lineNumber = ...
    IPath path = ...
    IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
    IMarker marker = file.createMarker(IMarker.TEXT);
    marker.setAttribute(IMarker.LINE_NUMBER, lineNumber);
    IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    IDE.openEditor(page, marker);
    marker.delete();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-11
      • 2021-03-19
      • 1970-01-01
      • 2019-01-28
      • 2014-08-07
      • 1970-01-01
      相关资源
      最近更新 更多