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 ?
-
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.Haris Osmanagić– Haris Osmanagić2014年08月21日 07:42:23 +00:00Commented Aug 21, 2014 at 7:42
-
I try to learn Java now, while in a project.John Smith– John Smith2014年08月21日 07:53:10 +00:00Commented 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/…Grim– Grim2014年08月21日 07:54:39 +00:00Commented Aug 21, 2014 at 7:54
-
Thank you ! I only try to learn some Java and Eclipse plugins :)John Smith– John Smith2014年08月21日 07:57:57 +00:00Commented 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).Grim– Grim2014年08月21日 07:59:34 +00:00Commented Aug 21, 2014 at 7:59
2 Answers 2
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.
Comments
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();
Comments
Explore related questions
See similar questions with these tags.