1

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. What can be the problem ?

asked Aug 21, 2014 at 7:40
6
  • The problems is that openEditor11 cannot be resolved to a variable. :) You'll do yourself a favor if you revisit a Java course, since diving into any programming, before having a grasp of the basics is a pain. Commented Aug 21, 2014 at 7:42
  • I try to learn Java now, while in a project. Commented Aug 21, 2014 at 7:53
  • I already did that project named eclipselink. Was years ago available under eclipseplugincentral.com. Now its served here: webmasterwork.com/page/… Commented Aug 21, 2014 at 7:54
  • Thank you ! I only try to learn some Java and Eclipse plugins :) Commented Aug 21, 2014 at 7:57
  • I had problems to decide what editor i shall focus if the file is opend twice and what window i shall use if the eclipse-window is cloned and if users starts multiple instances of eclipse, what eclipse shall take the request (last problem may not yours because you know your eclipse). Commented Aug 21, 2014 at 7:59

2 Answers 2

4

That's because the declaration of the variable, nested inside an if statement goes out of scope when finishing the condition. 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.

answered Aug 21, 2014 at 7:45
Sign up to request clarification or add additional context in comments.

Comments

1

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

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();
answered Jul 27, 2018 at 7:28

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.