diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/AddIncludeAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/AddIncludeAction.java index caa1ec19e52..f6af972a253 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/AddIncludeAction.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/AddIncludeAction.java @@ -22,6 +22,7 @@ import org.eclipse.jface.text.ITextSelection; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.jface.window.Window; +import org.eclipse.osgi.util.NLS; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.text.edits.MalformedTreeException; @@ -48,7 +49,7 @@ import org.eclipse.cdt.internal.ui.refactoring.includes.IElementSelector; import org.eclipse.cdt.internal.ui.refactoring.includes.IncludeCreator; /** - * Organizes the include directives and forward declarations of a source or header file. + * Adds an '#include' statement and, optionally, a 'using' declaration necessary to resolve a name. */ public class AddIncludeAction extends TextEditorAction { private IElementSelector fAmbiguityResolver; @@ -103,6 +104,11 @@ public class AddIncludeAction extends TextEditorAction { SharedASTJob job = new SharedASTJob(CEditorMessages.AddInclude_action, tu) { @Override public IStatus runOnAST(ILanguage lang, IASTTranslationUnit ast) throws CoreException { + if (ast == null) { + return CUIPlugin.createErrorStatus( + NLS.bind(CEditorMessages.AddInclude_ast_not_available, tu.getPath().toOSString())); + } + IIndex index= CCorePlugin.getIndexManager().getIndex(tu.getCProject(), IIndexManager.ADD_DEPENDENCIES | IIndexManager.ADD_EXTENSION_FRAGMENTS_ADD_IMPORT); try { diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditorMessages.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditorMessages.java index c06c006c49f..df53af91e0a 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditorMessages.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditorMessages.java @@ -35,11 +35,13 @@ public final class CEditorMessages extends NLS { public static String AddInclude_label; public static String AddInclude_description; public static String AddInclude_action; + public static String AddInclude_ast_not_available; public static String AddInclude_error_title; public static String AddInclude_insertion_failed; public static String OrganizeIncludes_label; public static String OrganizeIncludes_description; public static String OrganizeIncludes_action; + public static String OrganizeIncludes_ast_not_available; public static String OrganizeIncludes_error_title; public static String OrganizeIncludes_insertion_failed; public static String OrganizeIncludes_help_provider_error; diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditorMessages.properties b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditorMessages.properties index 5ed1c32779d..43e1782ba7c 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditorMessages.properties +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditorMessages.properties @@ -19,12 +19,14 @@ AddInclude_label=Add Include AddInclude_description=Add include statement for selected name AddInclude_action=Adding include statement +AddInclude_ast_not_available=Unable to parse ''{0}'' AddInclude_error_title=Error Adding Include Statement AddInclude_insertion_failed=Adding include statement failed OrganizeIncludes_label=Organize Includes OrganizeIncludes_description=Organize includes for current file OrganizeIncludes_action=Organizing includes +OrganizeIncludes_ast_not_available=Unable to parse ''{0}'' OrganizeIncludes_error_title=Error Organizing Includes OrganizeIncludes_insertion_failed=Adding include statements failed OrganizeIncludes_help_provider_error=Help provider error diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/OrganizeIncludesAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/OrganizeIncludesAction.java index 30fd09f1201..15d005e044f 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/OrganizeIncludesAction.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/OrganizeIncludesAction.java @@ -17,6 +17,7 @@ import org.eclipse.core.runtime.Status; import org.eclipse.jface.dialogs.ErrorDialog; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; +import org.eclipse.osgi.util.NLS; import org.eclipse.text.edits.MalformedTreeException; import org.eclipse.text.edits.MultiTextEdit; import org.eclipse.text.undo.DocumentUndoManagerRegistry; @@ -70,6 +71,11 @@ public class OrganizeIncludesAction extends TextEditorAction { SharedASTJob job = new SharedASTJob(CEditorMessages.OrganizeIncludes_action, tu) { @Override public IStatus runOnAST(ILanguage lang, IASTTranslationUnit ast) throws CoreException { + if (ast == null) { + return CUIPlugin.createErrorStatus( + NLS.bind(CEditorMessages.OrganizeIncludes_ast_not_available, tu.getPath().toOSString())); + } + IIndex index= CCorePlugin.getIndexManager().getIndex(tu.getCProject(), IIndexManager.ADD_DEPENDENCIES | IIndexManager.ADD_EXTENSION_FRAGMENTS_ADD_IMPORT); try { diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/CUIPlugin.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/CUIPlugin.java index 42954fe9b3e..bde3442859f 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/CUIPlugin.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/CUIPlugin.java @@ -342,7 +342,16 @@ public class CUIPlugin extends AbstractUIPlugin { /** * Creates an error status. * - * @since 5.7 + * @noreference This method is not intended to be referenced by clients. + */ + public static Status createErrorStatus(String message) { + return createErrorStatus(message, null); + } + + /** + * Creates an error status. + * + * @noreference This method is not intended to be referenced by clients. */ public static Status createErrorStatus(String message, Throwable e) { return new Status(IStatus.ERROR, PLUGIN_ID, IStatus.ERROR, message, e);