Community
Participate
Working Groups
Trying to run a quick fix (for example creating local variable on code below) when the file is only open in a non-CEditor it causes an NPE because the editor has no TU and the quick fix fails to run. void func() { x = 2; } The method org.eclipse.cdt.codan.ui.AbstractCodanCMarkerResolution.getTranslationUnitViaEditor(IMarker) is not documented as being able to return null, but it can. Here is the stack trace. !ENTRY org.eclipse.cdt.codan.internal.ui.cxx 4 1 2016-11-08 22:07:14.810 !MESSAGE Error !STACK 0 java.lang.NullPointerException at org.eclipse.cdt.codan.internal.checkers.ui.quickfix.QuickFixCreateLocalVariable.modifyAST(QuickFixCreateLocalVariable.java:41) at org.eclipse.cdt.codan.ui.AbstractAstRewriteQuickFix.apply(AbstractAstRewriteQuickFix.java:52) at org.eclipse.cdt.codan.ui.AbstractCodanCMarkerResolution.run(AbstractCodanCMarkerResolution.java:96) at org.eclipse.ui.internal.views.markers.QuickFixPage.lambda$3(QuickFixPage.java:490) at org.eclipse.jface.operation.ModalContext.runInCurrentThread(ModalContext.java:437)
Some notes on reproducing this: - One way to open a C++ file in a non-CEditor is to right-click on it in Project Explorer -> Open With... -> Text Editor. - One way to run a quick fix on a file not open in a CEditor is open the Problems view, right click on the problem in question, and select Quick fix. A couple of additional observations: - You can perform a quick fix from the Problems view even if the affected file isn't open at all. In such a case, the affected file is opened in a CEditor. - A file can be open simultaneously in a CEditor and a non- CEditor. Therefore, it seems like an appropriate fix would be to ignore the non-CEditor in which the file is open, and open it again in a CEditor.
I'd suggest to modify the method org.eclipse.cdt.codan.ui.CodanEditorUtility.openInEditor Currently it opens the editor via: IEditorPart part = page.openEditor(new FileEditorInput(efile), desc.getId()); org.eclipse.ui.IWorkbenchPage provides a (IMHO) more suitable method: openEditor(final IEditorInput input,final String editorId, final boolean activate, final int matchFlags) So I think what we need to call is: IEditorPart part = page.openEditor(new FileEditorInput(efile), desc.getId(), true, IWorkbenchPage.MATCH_INPUT|IWorkbenchPage.MATCH_ID); What I'm not sure about is, what to do if the file is not inside the Eclipse workspace. In this case IDE.openEditorOnFileStore(page, fileStore) is used.
(In reply to Dominic Scharfe from comment #2) > What I'm not sure about is, what to do if the file is not inside the Eclipse > workspace. In this case IDE.openEditorOnFileStore(page, fileStore) is used. Can that even happen? A marker resolution applies to an IMarker, and markers are associated with an IResource. Can you have an IResource that's not part of the workspace?
(In reply to Nathan Ridge from comment #3) > (In reply to Dominic Scharfe from comment #2) > > What I'm not sure about is, what to do if the file is not inside the Eclipse > > workspace. In this case IDE.openEditorOnFileStore(page, fileStore) is used. > > Can that even happen? A marker resolution applies to an IMarker, and markers > are associated with an IResource. Can you have an IResource that's not part > of the workspace? You can't have an IResource outside the workspace, but you can have a marker that is. The marker for items outside the workspace have an IResource of the workspace root and an extra attribute ICModelMarker.C_MODEL_MARKER_EXTERNAL_LOCATION with that external location. As the location is workspace, double-clicking on the error marker does not open the file. However a action is on new context menu "Open external location" --> org.eclipse.cdt.internal.ui.util.OpenExternalProblemAction