1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-04 14:55:41 +02:00

Support for addInclude action, implementation base

on the indexer.
This commit is contained in:
Alain Magloire 2004-07-28 20:23:07 +00:00
parent ccb8357725
commit 6baa96771d
3 changed files with 302 additions and 168 deletions

View file

@ -25,6 +25,7 @@ public interface ICHelpContextIds {
// Actions // 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_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_FIELDS_ACTION= PREFIX + "filter_fields_action"; //$NON-NLS-1$
public static final String FILTER_STATIC_ACTION= PREFIX + "filter_static_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 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_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_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$ public static final String TODO_TASK_PREFERENCE_PAGE = PREFIX + "tasktags_preference_page_context"; //$NON-NLS-1$

View file

@ -10,11 +10,17 @@ import org.eclipse.cdt.core.model.IInclude;
import org.eclipse.cdt.core.model.IParent; import org.eclipse.cdt.core.model.IParent;
import org.eclipse.cdt.core.model.ISourceRange; import org.eclipse.cdt.core.model.ISourceRange;
import org.eclipse.cdt.core.model.ITranslationUnit; 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.CEditor;
import org.eclipse.cdt.internal.ui.editor.CEditorMessages; 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.IRequiredInclude;
import org.eclipse.cdt.ui.IWorkingCopyManager;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.NullProgressMonitor;
@ -35,10 +41,11 @@ public class AddIncludeOperation extends WorkspaceModifyOperation {
private ITranslationUnit fTranslationUnit; private ITranslationUnit fTranslationUnit;
private IRequiredInclude[] fIncludes; private IRequiredInclude[] fIncludes;
private String[] fUsings;
private boolean fDoSave; private boolean fDoSave;
private ITextEditor fEditor; private ITextEditor fEditor;
private IInclude[] fAddedIncludes; private String newLine = System.getProperty("line.separator", "\n");
/** /**
* Generate import statements for the passed java elements * Generate import statements for the passed java elements
@ -46,106 +53,186 @@ public class AddIncludeOperation extends WorkspaceModifyOperation {
* (on-demand-import). Other JavaElements are ignored * (on-demand-import). Other JavaElements are ignored
*/ */
public AddIncludeOperation(ITextEditor ed, ITranslationUnit tu, IRequiredInclude[] includes, boolean save) { 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(); super();
fEditor = ed; fEditor = ed;
fIncludes= includes; fIncludes= includes;
fTranslationUnit= tu; fUsings = using;
fAddedIncludes= null; fTranslationUnit = tu;
fDoSave= save; fDoSave= save;
} }
public void execute(IProgressMonitor monitor) throws CoreException { public void executeInludes(ITranslationUnit root, IProgressMonitor monitor) throws CoreException {
try { // Sanity
ArrayList toAdd = new ArrayList(); if (fIncludes == null || fIncludes.length == 0) {
return;
}
if (monitor == null) { ArrayList toAdd = new ArrayList();
monitor= new NullProgressMonitor();
}
monitor.beginTask(CEditorMessages.getString("AddIncludesOperation.description"), 2); //$NON-NLS-1$ monitor.beginTask(CEditorMessages.getString("AddIncludesOperation.description"), 2); //$NON-NLS-1$
ICElement root; if (root != null) {
// Look in content outline List elements = ((IParent)root).getChildrenOfType(ICElement.C_INCLUDE);
if(fEditor instanceof CEditor) { for (int i = 0; i < fIncludes.length; ++i) {
CContentOutlinePage outline = ((CEditor)fEditor).getOutlinePage(); String name = fIncludes[i].getIncludeName();
root = outline.getRoot(); boolean found = false;
} else { for (int j = 0; j < elements.size(); ++j) {
root = fTranslationUnit; IInclude include = (IInclude)elements.get(j);
} if (name.equals(include.getElementName())) {
if (root != null && root instanceof IParent && ((IParent)root).hasChildren()) { found = true;
//// Get children of tu break;
// 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]);
}
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 (!found) {
toAdd.add(fIncludes[i]);
}
}
if(toAdd.size() > 0) { if (toAdd.size() > 0) {
// So we have our list. Now insert. // So we have our list. Now insert.
StringBuffer insert = new StringBuffer(""); //$NON-NLS-1$ StringBuffer insert = new StringBuffer(""); //$NON-NLS-1$
for(int j = 0; j < toAdd.size(); j++) { for(int j = 0; j < toAdd.size(); j++) {
insert.append("#include <" + ((IRequiredInclude)toAdd.get(j)).getIncludeName() + ">\n"); //$NON-NLS-1$ //$NON-NLS-2$ IRequiredInclude req = (IRequiredInclude)toAdd.get(j);
} if (req.isStandard()) {
int pos; insert.append("#include <" + req.getIncludeName() + ">\n"); //$NON-NLS-1$ //$NON-NLS-2$
if(lastInclude != null) {
ISourceRange range = ((IInclude)lastInclude).getSourceRange();
pos = range.getStartPos() + range.getLength();
} else { } else {
pos = 0; insert.append("#include \"" + req.getIncludeName() + "\"\n"); //$NON-NLS-1$ //$NON-NLS-2$
} }
}
IDocument document = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput()); int pos;
try { if (elements.size() > 0) {
// Now find the next newline and insert after that IInclude lastInclude = (IInclude)elements.get(elements.size() - 1);
if(pos > 0) { ISourceRange range = ((IInclude)lastInclude).getSourceRange();
while(document.getChar(pos) != '\n') pos++; pos = range.getStartPos() + range.getLength();
if(document.getChar(pos) == '\r') pos++; } 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++; pos++;
} }
document.replace(pos, 0, insert.toString()); if (document.getChar(pos) == '\r') {
} catch (BadLocationException e) {} 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]);
}
} }
if (toAdd.size() > 0) {
/*for (int i= 0; i < nImports; i++) { // So we have our list. Now insert.
IJavaElement imp= fIncludes[i]; StringBuffer insert = new StringBuffer(""); //$NON-NLS-1$
if (imp instanceof IType) { for(int j = 0; j < toAdd.size(); j++) {
IType type= (IType)imp; String using = (String)toAdd.get(j);
String packageName= type.getPackageFragment().getElementName(); insert.append("using namespace " + using + ";").append(newLine); //$NON-NLS-1$ //$NON-NLS-2$
impStructure.addImport(packageName, type.getElementName());
} else if (imp instanceof IPackageFragment) {
String packageName= ((IPackageFragment)imp).getElementName();
impStructure.addImport(packageName, "*"); //$NON-NLS-1$
} }
} */
monitor.worked(1); int pos;
//fAddedImports= impStructure.create(fDoSave, null); List includes = ((IParent)root).getChildrenOfType(ICElement.C_INCLUDE);
monitor.worked(1); 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 { } finally {
monitor.done(); monitor.done();
} }
} }
} }

View file

@ -21,15 +21,20 @@ import org.eclipse.cdt.core.search.IMatch;
import org.eclipse.cdt.core.search.OrPattern; import org.eclipse.cdt.core.search.OrPattern;
import org.eclipse.cdt.core.search.SearchEngine; import org.eclipse.cdt.core.search.SearchEngine;
import org.eclipse.cdt.internal.ui.CCompletionContributorManager; 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.codemanipulation.AddIncludeOperation;
import org.eclipse.cdt.internal.ui.util.ExceptionHandler; 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.CUIPlugin;
import org.eclipse.cdt.ui.IFunctionSummary; import org.eclipse.cdt.ui.IFunctionSummary;
import org.eclipse.cdt.ui.IRequiredInclude; import org.eclipse.cdt.ui.IRequiredInclude;
import org.eclipse.cdt.ui.browser.typeinfo.TypeInfoLabelProvider;
import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IProgressMonitor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set; import java.util.Set;
import org.eclipse.swt.widgets.Shell; 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.IEditorInput;
import org.eclipse.ui.PlatformUI; import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.ElementListSelectionDialog; 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.ITextEditor;
import org.eclipse.ui.texteditor.IUpdate; import org.eclipse.ui.texteditor.IUpdate;
@ -55,6 +60,8 @@ import org.eclipse.ui.texteditor.IUpdate;
public class AddIncludeOnSelectionAction extends Action implements IUpdate { public class AddIncludeOnSelectionAction extends Action implements IUpdate {
private ITextEditor fEditor; private ITextEditor fEditor;
private IRequiredInclude[] fRequiredIncludes;
private String[] fUsings;
class RequiredIncludes implements IRequiredInclude { class RequiredIncludes implements IRequiredInclude {
String name; String name;
@ -90,11 +97,11 @@ public class AddIncludeOnSelectionAction extends Action implements IUpdate {
setDescription(CEditorMessages.getString("AddIncludeOnSelection.description")); //$NON-NLS-1$ setDescription(CEditorMessages.getString("AddIncludeOnSelection.description")); //$NON-NLS-1$
fEditor= editor; 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) { private void addInclude(ITranslationUnit tu) {
AddIncludeOperation op= new AddIncludeOperation(fEditor, tu, inc, false); AddIncludeOperation op= new AddIncludeOperation(fEditor, tu, fRequiredIncludes, fUsings, false);
try { try {
ProgressMonitorDialog dialog= new ProgressMonitorDialog(getShell()); ProgressMonitorDialog dialog= new ProgressMonitorDialog(getShell());
dialog.run(false, true, op); dialog.run(false, true, op);
@ -104,11 +111,12 @@ public class AddIncludeOnSelectionAction extends Action implements IUpdate {
} catch (InterruptedException e) { } catch (InterruptedException e) {
// Do nothing. Operation has been canceled. // Do nothing. Operation has been canceled.
} }
} }
ITranslationUnit getTranslationUnit () { ITranslationUnit getTranslationUnit () {
ITranslationUnit unit = null; ITranslationUnit unit = null;
if(fEditor != null) { if (fEditor != null) {
IEditorInput editorInput= fEditor.getEditorInput(); IEditorInput editorInput= fEditor.getEditorInput();
unit = CUIPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(editorInput); unit = CUIPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(editorInput);
} }
@ -133,32 +141,18 @@ public class AddIncludeOnSelectionAction extends Action implements IUpdate {
return pos; 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 * @see IAction#actionPerformed
*/ */
public void run() { public void run() {
IRequiredInclude [] requiredIncludes; extractIncludes(fEditor);
requiredIncludes = extractIncludes(fEditor);
if (requiredIncludes != null && requiredIncludes.length > 0) { ITranslationUnit tu= getTranslationUnit();
ITranslationUnit tu= getTranslationUnit(); if (tu != null) {
if (tu != null) { addInclude(tu);
addInclude(requiredIncludes, 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. * @return IRequiredInclude [] An array of the required includes, or null if this action is invalid.
*/ */
private IRequiredInclude [] extractIncludes(ITextEditor editor) { private void extractIncludes(ITextEditor editor) {
if(editor == null) { if (editor == null) {
return null; return;
} }
ISelection s= editor.getSelectionProvider().getSelection(); ISelection s= editor.getSelectionProvider().getSelection();
IDocument doc= editor.getDocumentProvider().getDocument(editor.getEditorInput()); IDocument doc= editor.getDocumentProvider().getDocument(editor.getEditorInput());
if (s.isEmpty() || !(s instanceof ITextSelection) || doc == null) { if (s.isEmpty() || !(s instanceof ITextSelection) || doc == null) {
return null; return;
} }
ITextSelection selection= (ITextSelection) s; ITextSelection selection= (ITextSelection) s;
IRequiredInclude [] requiredIncludes = null;
try { try {
int selStart= selection.getOffset(); int selStart= selection.getOffset();
int nameStart= getNameStart(doc, selStart); int nameStart= getNameStart(doc, selStart);
int len= selStart - nameStart + selection.getLength(); int len= selStart - nameStart + selection.getLength();
String name= doc.get(nameStart, len).trim(); String name = doc.get(nameStart, len).trim();
if (name.length() == 0) {
IFunctionSummary fs = CCompletionContributorManager.getDefault().getFunctionInfo(name); return;
if(fs != null) {
requiredIncludes = fs.getIncludes();
} }
// Try contributed plugins.
findContribution(name);
// Try the type caching. // Try the type caching.
if (requiredIncludes == null) { if (fRequiredIncludes == null && fUsings == null) {
ITypeInfo[] types= findTypeInfos(name); ITypeInfo[] typeInfos= findTypeInfos(name);
requiredIncludes = selectResult(types, name, getShell()); if (typeInfos != null && typeInfos.length > 0) {
selectResult(typeInfos, name, getShell());
}
} }
// Do a full search // Do a full search
if (requiredIncludes == null) { if (fRequiredIncludes == null && fUsings == null) {
IMatch[] matches = findMatches(name); IMatch[] matches = findMatches(name);
requiredIncludes = selectResult(matches, name, getShell()); if (matches != null && matches.length > 0) {
selectResult(matches, name, getShell());
}
} }
} catch (BadLocationException e) { } catch (BadLocationException e) {
MessageDialog.openError(getShell(), CEditorMessages.getString("AddIncludeOnSelection.error.message3"), CEditorMessages.getString("AddIncludeOnSelection.error.message4") + e.getMessage()); //$NON-NLS-2$ //$NON-NLS-1$ 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 orPattern = new OrPattern();
orPattern.addPattern(SearchEngine.createSearchPattern( orPattern.addPattern(SearchEngine.createSearchPattern(
name, ICSearchConstants.TYPE, ICSearchConstants.DECLARATIONS, false)); name, ICSearchConstants.TYPE, ICSearchConstants.DECLARATIONS, false));
orPattern.addPattern(SearchEngine.createSearchPattern(
name, ICSearchConstants.TYPE, ICSearchConstants.DEFINITIONS, false));
orPattern.addPattern(SearchEngine.createSearchPattern( orPattern.addPattern(SearchEngine.createSearchPattern(
name, ICSearchConstants.ENUM, ICSearchConstants.DECLARATIONS, false)); name, ICSearchConstants.ENUM, ICSearchConstants.DECLARATIONS, false));
orPattern.addPattern(SearchEngine.createSearchPattern( orPattern.addPattern(SearchEngine.createSearchPattern(
@ -276,80 +283,118 @@ public class AddIncludeOnSelectionAction extends Action implements IUpdate {
return null; return null;
} }
private IRequiredInclude[] selectResult(ITypeInfo[] results, String name, Shell shell) { private void selectResult(ITypeInfo[] results, String name, Shell shell) {
int nResults= results.length; int nResults= results.length;
IProject project = getTranslationUnit().getCProject().getProject(); IProject project = getTranslationUnit().getCProject().getProject();
if (nResults == 0) { if (nResults == 0) {
return null; return; // bail out
} else if (nResults == 1) { }
ITypeReference ref = results[0].getResolvedReference();
if (ref != null) { int occurences = 0;
return new IRequiredInclude[] {new RequiredIncludes(ref.getRelativeIncludePath(project).toString())}; int index = 0;
for (int i = 0; i < results.length; i++) {
if (name.equals(results[i].getName())) {
occurences++;
index = i;
} }
} }
if (name.length() != 0) { // if only one
for (int i= 0; i < results.length; i++) { if (occurences == 1 || results.length == 1) {
ITypeInfo curr= results[i]; ITypeInfo curr= results[index];
if (name.equals(curr.getName())) { ITypeReference ref = curr.getResolvedReference();
ITypeReference ref = results[0].getResolvedReference(); if (ref != null) {
if (ref != null) { fRequiredIncludes = new IRequiredInclude[]{new RequiredIncludes(ref.getRelativeIncludePath(project).toString())};
return 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 WorkbenchLabelProvider()); ElementListSelectionDialog dialog= new ElementListSelectionDialog(getShell(), new TypeInfoLabelProvider(TypeInfoLabelProvider.SHOW_TYPE_ONLY));
dialog.setElements(results); dialog.setElements(results);
dialog.setTitle(CEditorMessages.getString("AddIncludeOnSelection.label")); //$NON-NLS-1$ dialog.setTitle(CEditorMessages.getString("AddIncludeOnSelection.label")); //$NON-NLS-1$
dialog.setMessage(CEditorMessages.getString("AddIncludeOnSelection.description")); //$NON-NLS-1$ dialog.setMessage(CEditorMessages.getString("AddIncludeOnSelection.description")); //$NON-NLS-1$
if (dialog.open() == Window.OK) { if (dialog.open() == Window.OK) {
ITypeInfo[] selects = (ITypeInfo[])dialog.getResult(); ITypeInfo[] selects = (ITypeInfo[])dialog.getResult();
IRequiredInclude[] reqs = new IRequiredInclude[selects.length]; fRequiredIncludes = new IRequiredInclude[selects.length];
for (int i = 0; i < reqs.length; i++) { List usings = new ArrayList(selects.length);
ITypeReference ref = selects[0].getResolvedReference(); for (int i = 0; i < fRequiredIncludes.length; i++) {
ITypeReference ref = selects[i].getResolvedReference();
if (ref != null) { 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 { } 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; int nResults = results.length;
if (nResults == 0) { if (nResults == 0) {
return null; return;
} else if (nResults == 1) {
return new IRequiredInclude[] {new RequiredIncludes(results[0].getLocation().lastSegment())};
} }
if (name.length() != 0) { int occurences = 0;
for (int i= 0; i < results.length; i++) { int index = 0;
IMatch curr= results[i]; for (int i= 0; i < results.length; i++) {
if (name.equals(curr.getName())) { IMatch curr= results[i];
return new IRequiredInclude[]{new RequiredIncludes(curr.getLocation().lastSegment())}; 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.setElements(results);
dialog.setTitle(CEditorMessages.getString("AddIncludeOnSelection.label")); //$NON-NLS-1$ dialog.setTitle(CEditorMessages.getString("AddIncludeOnSelection.label")); //$NON-NLS-1$
dialog.setMessage(CEditorMessages.getString("AddIncludeOnSelection.description")); //$NON-NLS-1$ dialog.setMessage(CEditorMessages.getString("AddIncludeOnSelection.description")); //$NON-NLS-1$
if (dialog.open() == Window.OK) { if (dialog.open() == Window.OK) {
IMatch[] selects = (IMatch[])dialog.getResult(); IMatch[] selects = (IMatch[])dialog.getResult();
IRequiredInclude[] reqs = new IRequiredInclude[selects.length]; fRequiredIncludes = new IRequiredInclude[selects.length];
for (int i = 0; i < reqs.length; i++) { List usings = new ArrayList(selects.length);
reqs[i] = new RequiredIncludes(selects[i].getLocation().lastSegment()); 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) { public void setContentEditor(ITextEditor editor) {