From 6baa96771d3ff4fc03c53c14cde2fb92ab3ccb70 Mon Sep 17 00:00:00 2001 From: Alain Magloire Date: Wed, 28 Jul 2004 20:23:07 +0000 Subject: [PATCH] Support for addInclude action, implementation base on the indexer. --- .../cdt/internal/ui/ICHelpContextIds.java | 2 + .../codemanipulation/AddIncludeOperation.java | 253 ++++++++++++------ .../editor/AddIncludeOnSelectionAction.java | 215 +++++++++------ 3 files changed, 302 insertions(+), 168 deletions(-) diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/ICHelpContextIds.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/ICHelpContextIds.java index 1679cd7ca6d..0e92e6545a9 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/ICHelpContextIds.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/ICHelpContextIds.java @@ -25,6 +25,7 @@ public interface ICHelpContextIds { // Actions + public static final String ADD_INCLUDE_ON_SELECTION_ACTION = PREFIX + "add_includes_on_selection_action_context"; //$NON-NLS-1$; public static final String FILTER_PUBLIC_ACTION= PREFIX + "filter_public_action"; //$NON-NLS-1$ public static final String FILTER_FIELDS_ACTION= PREFIX + "filter_fields_action"; //$NON-NLS-1$ public static final String FILTER_STATIC_ACTION= PREFIX + "filter_static_action"; //$NON-NLS-1$ @@ -53,6 +54,7 @@ public interface ICHelpContextIds { public static final String PROJ_CONF_BLOCK = PREFIX + "new_proj_conf_block_context"; //$NON-NLS-1$ public static final String TODO_TASK_INPUT_DIALOG = PREFIX + "todo_task_input_dialog_context"; //$NON-NLS-1$ + public static final String TODO_TASK_PROPERTY_PAGE = PREFIX + "tasktags_property_page_context"; //$NON-NLS-1$ public static final String TODO_TASK_PREFERENCE_PAGE = PREFIX + "tasktags_preference_page_context"; //$NON-NLS-1$ diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/codemanipulation/AddIncludeOperation.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/codemanipulation/AddIncludeOperation.java index 6fde6253a93..a9ce6854f37 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/codemanipulation/AddIncludeOperation.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/codemanipulation/AddIncludeOperation.java @@ -10,11 +10,17 @@ import org.eclipse.cdt.core.model.IInclude; import org.eclipse.cdt.core.model.IParent; import org.eclipse.cdt.core.model.ISourceRange; import org.eclipse.cdt.core.model.ITranslationUnit; -import org.eclipse.cdt.internal.ui.editor.CContentOutlinePage; +import org.eclipse.cdt.core.model.IUsing; import org.eclipse.cdt.internal.ui.editor.CEditor; import org.eclipse.cdt.internal.ui.editor.CEditorMessages; +import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.ui.IRequiredInclude; +import org.eclipse.cdt.ui.IWorkingCopyManager; + +import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; +import java.util.List; + import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.NullProgressMonitor; @@ -35,117 +41,198 @@ public class AddIncludeOperation extends WorkspaceModifyOperation { private ITranslationUnit fTranslationUnit; private IRequiredInclude[] fIncludes; + private String[] fUsings; private boolean fDoSave; private ITextEditor fEditor; - - private IInclude[] fAddedIncludes; - + + private String newLine = System.getProperty("line.separator", "\n"); + /** * Generate import statements for the passed java elements * Elements must be of type IType (-> single import) or IPackageFragment * (on-demand-import). Other JavaElements are ignored */ public AddIncludeOperation(ITextEditor ed, ITranslationUnit tu, IRequiredInclude[] includes, boolean save) { + this (ed, tu, includes, null, save); + } + + /** + * Generate import statements for the passed java elements + * Elements must be of type IType (-> single import) or IPackageFragment + * (on-demand-import). Other JavaElements are ignored + */ + public AddIncludeOperation(ITextEditor ed, ITranslationUnit tu, IRequiredInclude[] includes, String[] using, boolean save) { super(); fEditor = ed; fIncludes= includes; - fTranslationUnit= tu; - fAddedIncludes= null; + fUsings = using; + fTranslationUnit = tu; fDoSave= save; } - public void execute(IProgressMonitor monitor) throws CoreException { - try { - ArrayList toAdd = new ArrayList(); - - if (monitor == null) { - monitor= new NullProgressMonitor(); - } - - monitor.beginTask(CEditorMessages.getString("AddIncludesOperation.description"), 2); //$NON-NLS-1$ - - ICElement root; - // Look in content outline - if(fEditor instanceof CEditor) { - CContentOutlinePage outline = ((CEditor)fEditor).getOutlinePage(); - root = outline.getRoot(); - } else { - root = fTranslationUnit; - } - if (root != null && root instanceof IParent && ((IParent)root).hasChildren()) { - //// Get children of tu - // Build list of include statement - //fTranslationUnit.update(); - //if(fTranslationUnit.hasChildren()) { - ICElement lastInclude = null; - ICElement[] elements = ((IParent)root).getChildren(); - - for(int j = 0; j < fIncludes.length; j++) { - //System.out.println("Comparing to " + fIncludes[j].getIncludeName()); - toAdd.add(fIncludes[j]); + public void executeInludes(ITranslationUnit root, IProgressMonitor monitor) throws CoreException { + // Sanity + if (fIncludes == null || fIncludes.length == 0) { + return; + } + + ArrayList toAdd = new ArrayList(); + + monitor.beginTask(CEditorMessages.getString("AddIncludesOperation.description"), 2); //$NON-NLS-1$ + + if (root != null) { + List elements = ((IParent)root).getChildrenOfType(ICElement.C_INCLUDE); + for (int i = 0; i < fIncludes.length; ++i) { + String name = fIncludes[i].getIncludeName(); + boolean found = false; + for (int j = 0; j < elements.size(); ++j) { + IInclude include = (IInclude)elements.get(j); + if (name.equals(include.getElementName())) { + found = true; + break; + } } + if (!found) { + toAdd.add(fIncludes[i]); + } + } - for(int i = 0; i < elements.length; i++) { - if(elements[i].getElementType() == ICElement.C_INCLUDE) { - lastInclude = elements[i]; - //System.out.println("Element " + elements[i].getElementName() + "sys " + ((IInclude)elements[i]).isStandard()); - for(int j = 0; j < toAdd.size(); j++) { - //System.out.println("Comparing to " + ((IRequiredInclude)toAdd.get(j)).getIncludeName()); - if(elements[i].getElementName().equals(((IRequiredInclude)toAdd.get(j)).getIncludeName())) { - toAdd.remove(j); - } - } + if (toAdd.size() > 0) { + // So we have our list. Now insert. + StringBuffer insert = new StringBuffer(""); //$NON-NLS-1$ + for(int j = 0; j < toAdd.size(); j++) { + IRequiredInclude req = (IRequiredInclude)toAdd.get(j); + if (req.isStandard()) { + insert.append("#include <" + req.getIncludeName() + ">\n"); //$NON-NLS-1$ //$NON-NLS-2$ + } else { + insert.append("#include \"" + req.getIncludeName() + "\"\n"); //$NON-NLS-1$ //$NON-NLS-2$ } } - if(toAdd.size() > 0) { - // So we have our list. Now insert. - StringBuffer insert = new StringBuffer(""); //$NON-NLS-1$ - for(int j = 0; j < toAdd.size(); j++) { - insert.append("#include <" + ((IRequiredInclude)toAdd.get(j)).getIncludeName() + ">\n"); //$NON-NLS-1$ //$NON-NLS-2$ - } - int pos; - - if(lastInclude != null) { - ISourceRange range = ((IInclude)lastInclude).getSourceRange(); - pos = range.getStartPos() + range.getLength(); - } else { - pos = 0; - } - - IDocument document = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput()); - try { - // Now find the next newline and insert after that - if(pos > 0) { - while(document.getChar(pos) != '\n') pos++; - if(document.getChar(pos) == '\r') pos++; + int pos; + if (elements.size() > 0) { + IInclude lastInclude = (IInclude)elements.get(elements.size() - 1); + ISourceRange range = ((IInclude)lastInclude).getSourceRange(); + pos = range.getStartPos() + range.getLength(); + } else { + pos = 0; + } + + IDocument document = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput()); + try { + // Now find the next newline and insert after that + if (pos > 0) { + while (document.getChar(pos) != '\n') { pos++; } - document.replace(pos, 0, insert.toString()); - } catch (BadLocationException e) {} + if (document.getChar(pos) == '\r') { + pos++; + } + pos++; + } + document.replace(pos, 0, insert.toString()); + } catch (BadLocationException e) {} + } + } + + monitor.worked(1); + monitor.worked(1); + } + + public void executeUsings(ITranslationUnit root, IProgressMonitor monitor) throws CoreException { + // Sanity + if (fUsings == null || fUsings.length == 0) { + return; + } + + ArrayList toAdd = new ArrayList(); + + monitor.beginTask(CEditorMessages.getString("AddIncludesOperation.description"), 2); //$NON-NLS-1$ + + if (root != null) { + List elements = ((IParent)root).getChildrenOfType(ICElement.C_USING); + for (int i = 0; i < fUsings.length; ++i) { + String name = fUsings[i]; + boolean found = false; + for (int j = 0; j < elements.size(); ++j) { + IUsing using = (IUsing)elements.get(j); + if (name.equals(using.getElementName())) { + found = true; + break; + } + } + if (!found) { + toAdd.add(fUsings[i]); } - } - - /*for (int i= 0; i < nImports; i++) { - IJavaElement imp= fIncludes[i]; - if (imp instanceof IType) { - IType type= (IType)imp; - String packageName= type.getPackageFragment().getElementName(); - impStructure.addImport(packageName, type.getElementName()); - } else if (imp instanceof IPackageFragment) { - String packageName= ((IPackageFragment)imp).getElementName(); - impStructure.addImport(packageName, "*"); //$NON-NLS-1$ + if (toAdd.size() > 0) { + // So we have our list. Now insert. + StringBuffer insert = new StringBuffer(""); //$NON-NLS-1$ + for(int j = 0; j < toAdd.size(); j++) { + String using = (String)toAdd.get(j); + insert.append("using namespace " + using + ";").append(newLine); //$NON-NLS-1$ //$NON-NLS-2$ } - } */ - monitor.worked(1); - //fAddedImports= impStructure.create(fDoSave, null); - monitor.worked(1); + + int pos; + List includes = ((IParent)root).getChildrenOfType(ICElement.C_INCLUDE); + if (includes.size() > 0) { + IInclude lastInclude = (IInclude)includes.get(includes.size() - 1); + ISourceRange range = lastInclude.getSourceRange(); + pos = range.getStartPos() + range.getLength(); + } else if (elements.size() > 0){ + IUsing lastUsing = (IUsing)includes.get(includes.size() - 1); + ISourceRange range = lastUsing.getSourceRange(); + pos = range.getStartPos() + range.getLength(); + } else { + pos = 0; + } + + IDocument document = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput()); + try { + // Now find the next newline and insert after that + if (pos > 0) { + while (document.getChar(pos) != '\n') { + pos++; + } + if (document.getChar(pos) == '\r') { + pos++; + } + pos++; + } + document.replace(pos, 0, insert.toString()); + } catch (BadLocationException e) {} + } + } + monitor.worked(1); + } + + /* (non-Javadoc) + * @see org.eclipse.ui.actions.WorkspaceModifyOperation#execute(org.eclipse.core.runtime.IProgressMonitor) + */ + protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException { + + if (monitor == null) { + monitor= new NullProgressMonitor(); + } + + ITranslationUnit root; + // Look in content outline + if (fEditor instanceof CEditor) { + IWorkingCopyManager mgr = CUIPlugin.getDefault().getWorkingCopyManager(); + root = mgr.getWorkingCopy(fEditor.getEditorInput()); + } else { + root = fTranslationUnit; + } + + try { + executeUsings(root, monitor); + executeInludes(root, monitor); } finally { monitor.done(); } } + } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/AddIncludeOnSelectionAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/AddIncludeOnSelectionAction.java index 222e43c24da..8df8c8fe3a5 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/AddIncludeOnSelectionAction.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/AddIncludeOnSelectionAction.java @@ -21,15 +21,20 @@ import org.eclipse.cdt.core.search.IMatch; import org.eclipse.cdt.core.search.OrPattern; import org.eclipse.cdt.core.search.SearchEngine; import org.eclipse.cdt.internal.ui.CCompletionContributorManager; +import org.eclipse.cdt.internal.ui.ICHelpContextIds; import org.eclipse.cdt.internal.ui.codemanipulation.AddIncludeOperation; import org.eclipse.cdt.internal.ui.util.ExceptionHandler; +import org.eclipse.cdt.ui.CSearchResultLabelProvider; import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.ui.IFunctionSummary; import org.eclipse.cdt.ui.IRequiredInclude; +import org.eclipse.cdt.ui.browser.typeinfo.TypeInfoLabelProvider; import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.IProgressMonitor; import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; +import java.util.List; import java.util.Set; import org.eclipse.swt.widgets.Shell; @@ -47,7 +52,7 @@ import org.eclipse.jface.window.Window; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.dialogs.ElementListSelectionDialog; -import org.eclipse.ui.model.WorkbenchLabelProvider; +import org.eclipse.ui.help.WorkbenchHelp; import org.eclipse.ui.texteditor.ITextEditor; import org.eclipse.ui.texteditor.IUpdate; @@ -55,6 +60,8 @@ import org.eclipse.ui.texteditor.IUpdate; public class AddIncludeOnSelectionAction extends Action implements IUpdate { private ITextEditor fEditor; + private IRequiredInclude[] fRequiredIncludes; + private String[] fUsings; class RequiredIncludes implements IRequiredInclude { String name; @@ -76,7 +83,7 @@ public class AddIncludeOnSelectionAction extends Action implements IUpdate { public boolean isStandard() { return true; } - + } @@ -90,11 +97,11 @@ public class AddIncludeOnSelectionAction extends Action implements IUpdate { setDescription(CEditorMessages.getString("AddIncludeOnSelection.description")); //$NON-NLS-1$ fEditor= editor; - //WorkbenchHelp.setHelp(this, new Object[] { IJavaHelpContextIds.ADD_IMPORT_ON_SELECTION_ACTION }); + WorkbenchHelp.setHelp(this, ICHelpContextIds.ADD_INCLUDE_ON_SELECTION_ACTION); } - private void addInclude(IRequiredInclude[] inc, ITranslationUnit tu) { - AddIncludeOperation op= new AddIncludeOperation(fEditor, tu, inc, false); + private void addInclude(ITranslationUnit tu) { + AddIncludeOperation op= new AddIncludeOperation(fEditor, tu, fRequiredIncludes, fUsings, false); try { ProgressMonitorDialog dialog= new ProgressMonitorDialog(getShell()); dialog.run(false, true, op); @@ -104,11 +111,12 @@ public class AddIncludeOnSelectionAction extends Action implements IUpdate { } catch (InterruptedException e) { // Do nothing. Operation has been canceled. } + } ITranslationUnit getTranslationUnit () { ITranslationUnit unit = null; - if(fEditor != null) { + if (fEditor != null) { IEditorInput editorInput= fEditor.getEditorInput(); unit = CUIPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(editorInput); } @@ -133,32 +141,18 @@ public class AddIncludeOnSelectionAction extends Action implements IUpdate { return pos; } -/* private void removeQualification(IDocument doc, int nameStart, IType type) throws BadLocationException { - String packName= type.getPackageFragment().getElementName(); - int packLen= packName.length(); - if (packLen > 0) { - for (int k= 0; k < packLen; k++) { - if (doc.getChar(nameStart + k) != packName.charAt(k)) { - return; - } - } - doc.replace(nameStart, packLen + 1, ""); //$NON-NLS-1$ - } - } */ - /** * @see IAction#actionPerformed */ public void run() { - IRequiredInclude [] requiredIncludes; - requiredIncludes = extractIncludes(fEditor); + extractIncludes(fEditor); - if (requiredIncludes != null && requiredIncludes.length > 0) { - ITranslationUnit tu= getTranslationUnit(); - if (tu != null) { - addInclude(requiredIncludes, tu); - } - } + ITranslationUnit tu= getTranslationUnit(); + if (tu != null) { + addInclude(tu); + } + fUsings = null; + fRequiredIncludes = null; } /** @@ -168,48 +162,59 @@ public class AddIncludeOnSelectionAction extends Action implements IUpdate { * * @return IRequiredInclude [] An array of the required includes, or null if this action is invalid. */ - private IRequiredInclude [] extractIncludes(ITextEditor editor) { - if(editor == null) { - return null; + private void extractIncludes(ITextEditor editor) { + if (editor == null) { + return; } ISelection s= editor.getSelectionProvider().getSelection(); IDocument doc= editor.getDocumentProvider().getDocument(editor.getEditorInput()); if (s.isEmpty() || !(s instanceof ITextSelection) || doc == null) { - return null; + return; } ITextSelection selection= (ITextSelection) s; - IRequiredInclude [] requiredIncludes = null; try { int selStart= selection.getOffset(); int nameStart= getNameStart(doc, selStart); int len= selStart - nameStart + selection.getLength(); - String name= doc.get(nameStart, len).trim(); - - IFunctionSummary fs = CCompletionContributorManager.getDefault().getFunctionInfo(name); - if(fs != null) { - requiredIncludes = fs.getIncludes(); + String name = doc.get(nameStart, len).trim(); + if (name.length() == 0) { + return; } + // Try contributed plugins. + findContribution(name); + // Try the type caching. - if (requiredIncludes == null) { - ITypeInfo[] types= findTypeInfos(name); - requiredIncludes = selectResult(types, name, getShell()); + if (fRequiredIncludes == null && fUsings == null) { + ITypeInfo[] typeInfos= findTypeInfos(name); + if (typeInfos != null && typeInfos.length > 0) { + selectResult(typeInfos, name, getShell()); + } } // Do a full search - if (requiredIncludes == null) { + if (fRequiredIncludes == null && fUsings == null) { IMatch[] matches = findMatches(name); - requiredIncludes = selectResult(matches, name, getShell()); + if (matches != null && matches.length > 0) { + selectResult(matches, name, getShell()); + } } } catch (BadLocationException e) { MessageDialog.openError(getShell(), CEditorMessages.getString("AddIncludeOnSelection.error.message3"), CEditorMessages.getString("AddIncludeOnSelection.error.message4") + e.getMessage()); //$NON-NLS-2$ //$NON-NLS-1$ } - return requiredIncludes; + } + + void findContribution (String name) { + IFunctionSummary fs = CCompletionContributorManager.getDefault().getFunctionInfo(name); + if(fs != null) { + fRequiredIncludes = fs.getIncludes(); + fUsings = new String[] {fs.getNamespace()}; + } } /** @@ -245,6 +250,8 @@ public class AddIncludeOnSelectionAction extends Action implements IUpdate { OrPattern orPattern = new OrPattern(); orPattern.addPattern(SearchEngine.createSearchPattern( name, ICSearchConstants.TYPE, ICSearchConstants.DECLARATIONS, false)); + orPattern.addPattern(SearchEngine.createSearchPattern( + name, ICSearchConstants.TYPE, ICSearchConstants.DEFINITIONS, false)); orPattern.addPattern(SearchEngine.createSearchPattern( name, ICSearchConstants.ENUM, ICSearchConstants.DECLARATIONS, false)); orPattern.addPattern(SearchEngine.createSearchPattern( @@ -276,80 +283,118 @@ public class AddIncludeOnSelectionAction extends Action implements IUpdate { return null; } - private IRequiredInclude[] selectResult(ITypeInfo[] results, String name, Shell shell) { + private void selectResult(ITypeInfo[] results, String name, Shell shell) { int nResults= results.length; IProject project = getTranslationUnit().getCProject().getProject(); if (nResults == 0) { - return null; - } else if (nResults == 1) { - ITypeReference ref = results[0].getResolvedReference(); - if (ref != null) { - return new IRequiredInclude[] {new RequiredIncludes(ref.getRelativeIncludePath(project).toString())}; - } + return; // bail out } - - if (name.length() != 0) { - for (int i= 0; i < results.length; i++) { - ITypeInfo curr= results[i]; - if (name.equals(curr.getName())) { - ITypeReference ref = results[0].getResolvedReference(); - if (ref != null) { - return new IRequiredInclude[]{new RequiredIncludes(ref.getRelativeIncludePath(project).toString())}; - } - } + + int occurences = 0; + int index = 0; + for (int i = 0; i < results.length; i++) { + if (name.equals(results[i].getName())) { + occurences++; + index = i; } } - ElementListSelectionDialog dialog= new ElementListSelectionDialog(getShell(), new WorkbenchLabelProvider()); + // if only one + if (occurences == 1 || results.length == 1) { + ITypeInfo curr= results[index]; + ITypeReference ref = curr.getResolvedReference(); + if (ref != null) { + fRequiredIncludes = new IRequiredInclude[]{new RequiredIncludes(ref.getRelativeIncludePath(project).toString())}; + } + if (curr.hasEnclosedTypes()) { + ITypeInfo[] ns = curr.getEnclosedTypes(); + fUsings = new String[ns.length]; + for (int j = 0; j < fUsings.length; j++) { + fUsings[j] = ns[j].getName(); + } + } + return; + } + + ElementListSelectionDialog dialog= new ElementListSelectionDialog(getShell(), new TypeInfoLabelProvider(TypeInfoLabelProvider.SHOW_TYPE_ONLY)); dialog.setElements(results); dialog.setTitle(CEditorMessages.getString("AddIncludeOnSelection.label")); //$NON-NLS-1$ dialog.setMessage(CEditorMessages.getString("AddIncludeOnSelection.description")); //$NON-NLS-1$ if (dialog.open() == Window.OK) { ITypeInfo[] selects = (ITypeInfo[])dialog.getResult(); - IRequiredInclude[] reqs = new IRequiredInclude[selects.length]; - for (int i = 0; i < reqs.length; i++) { - ITypeReference ref = selects[0].getResolvedReference(); + fRequiredIncludes = new IRequiredInclude[selects.length]; + List usings = new ArrayList(selects.length); + for (int i = 0; i < fRequiredIncludes.length; i++) { + ITypeReference ref = selects[i].getResolvedReference(); if (ref != null) { - reqs[i] = new RequiredIncludes(ref.getRelativeIncludePath(project).toString()); + fRequiredIncludes[i] = new RequiredIncludes(ref.getRelativeIncludePath(project).toString()); + if (selects[i].hasEnclosedTypes()) { + ITypeInfo[] ns = results[0].getEnclosedTypes(); + for (int j = 0; j < ns.length; j++) { + usings.add(ns[j].getName()); + } + } + } else { - reqs[i] = new RequiredIncludes(""); //$NON-NLS-1$ + fRequiredIncludes[i] = new RequiredIncludes(""); //$NON-NLS-1$ } } - return reqs; + if (!usings.isEmpty()) { + fUsings = new String[usings.size()]; + usings.toArray(fUsings); + } } - return null; } - private IRequiredInclude[] selectResult(IMatch[] results, String name, Shell shell) { + private void selectResult(IMatch[] results, String name, Shell shell) { int nResults = results.length; if (nResults == 0) { - return null; - } else if (nResults == 1) { - return new IRequiredInclude[] {new RequiredIncludes(results[0].getLocation().lastSegment())}; + return; } - - if (name.length() != 0) { - for (int i= 0; i < results.length; i++) { - IMatch curr= results[i]; - if (name.equals(curr.getName())) { - return new IRequiredInclude[]{new RequiredIncludes(curr.getLocation().lastSegment())}; - } + + int occurences = 0; + int index = 0; + for (int i= 0; i < results.length; i++) { + IMatch curr= results[i]; + if (curr.getName().startsWith(name)) { + occurences++; + index = i; } } - ElementListSelectionDialog dialog= new ElementListSelectionDialog(getShell(), new WorkbenchLabelProvider()); + // if only one + if (occurences == 1 || results.length == 1) { + IMatch curr = results[index]; + fRequiredIncludes = new IRequiredInclude[1]; + fRequiredIncludes[0] = new RequiredIncludes(curr.getLocation().lastSegment()); + String parentName = curr.getParentName(); + if (parentName != null && parentName.length() > 0) { + fUsings = new String[] {parentName}; + } + return; + } + + // Make them choose + ElementListSelectionDialog dialog= new ElementListSelectionDialog(getShell(), new CSearchResultLabelProvider(null)); dialog.setElements(results); dialog.setTitle(CEditorMessages.getString("AddIncludeOnSelection.label")); //$NON-NLS-1$ dialog.setMessage(CEditorMessages.getString("AddIncludeOnSelection.description")); //$NON-NLS-1$ if (dialog.open() == Window.OK) { IMatch[] selects = (IMatch[])dialog.getResult(); - IRequiredInclude[] reqs = new IRequiredInclude[selects.length]; - for (int i = 0; i < reqs.length; i++) { - reqs[i] = new RequiredIncludes(selects[i].getLocation().lastSegment()); + fRequiredIncludes = new IRequiredInclude[selects.length]; + List usings = new ArrayList(selects.length); + for (int i = 0; i < fRequiredIncludes.length; i++) { + fRequiredIncludes[i] = new RequiredIncludes(selects[i].getLocation().lastSegment()); + String parentName = selects[i].getParentName(); + if (parentName != null && parentName.length() > 0) { + usings.add(parentName); + } + } + if (!usings.isEmpty()) { + fUsings = new String [usings.size()]; + usings.toArray(fUsings); } - return reqs; } - return null; } public void setContentEditor(ITextEditor editor) {