简体   繁体   English

在Eclipse插件中将文件打开到特定行

[英]Open file to a certain line in Eclipse plugin

I am writting a plugin that has to open a file at a certain line when a button is pressed. 我正在编写一个插件,当按下按钮时,该插件必须在某一行打开文件。 I have the following code to open the file at a certain line. 我有以下代码在某一行打开文件。

        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));
                    }

My problem is that the variable openEditor11 in the if statement gives the error: openEditor11 cannot be resolved to a variable. 我的问题是if语句中的变量openEditor11给出错误:openEditor11无法解析为变量。 What can be the problem ? 可能是什么问题?

That's because the declaration of the variable, nested inside an if statement goes out of scope when finishing the condition. 这是因为完成条件后,嵌套在if语句中的变量声明超出了范围。 Therefore the variable is freed, and in your second statement, doesn't exist anymore. 因此,该变量被释放,并且在您的第二条语句中,该变量不再存在。

You should declare it previously to circumvent this error like this : 您应该事先声明它以避免这种错误:

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));
}

Reaching the second condition block, the editor might be null, if first condition didn't apply, but this is not a problem, since instanceof return false on nulls. 到达第二个条件块时,如果第一个条件不适用,则编辑器可能为null,但这不是问题,因为instanceof对null返回false。

There is actually a more straightforward solution as per https://wiki.eclipse.org/FAQ_How_do_I_open_an_editor_on_a_file_in_the_workspace%3F 实际上,根据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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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