Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlain Magloire2004-02-03 16:59:47 +0000
committerAlain Magloire2004-02-03 16:59:47 +0000
commita26a745733f5ed4442db841d218cd161722a56f6 (patch)
treef7a06215bdcf956c8236640f01cb477d6d44db05
parentc07227b386921d1542a638475427458a2b784d6c (diff)
downloadorg.eclipse.cdt-a26a745733f5ed4442db841d218cd161722a56f6.tar.gz
org.eclipse.cdt-a26a745733f5ed4442db841d218cd161722a56f6.tar.xz
org.eclipse.cdt-a26a745733f5ed4442db841d218cd161722a56f6.zip
Patch from thomas fletcher to deal with
PR 51115
-rw-r--r--core/org.eclipse.cdt.ui/ChangeLog10
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/AddIncludeOnSelectionAction.java86
2 files changed, 68 insertions, 28 deletions
diff --git a/core/org.eclipse.cdt.ui/ChangeLog b/core/org.eclipse.cdt.ui/ChangeLog
index d9c5aa16b2a..0deac77b1e5 100644
--- a/core/org.eclipse.cdt.ui/ChangeLog
+++ b/core/org.eclipse.cdt.ui/ChangeLog
@@ -1,3 +1,13 @@
+2004-02-03 Alain Magloire
+
+ PR 51115
+ Patch from Thomas Fletcher to cache the include queries.
+
+ - Only enable the Add Include action when there are actually includes
+ to add in to the the file.
+
+ * src/org/eclipse/cdt/internal/ui/editor/AddIncludeOnSelectionAction.java
+
2004-01-07 Alain Magloire
Fix for bug 49595
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 c676d46c052..d5f81ed56fb 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
@@ -38,11 +38,12 @@ import org.eclipse.ui.texteditor.IUpdate;
public class AddIncludeOnSelectionAction extends Action implements IUpdate {
private ITextEditor fEditor;
-
+ private IRequiredInclude [] fCachedRequiredIncludes;
public AddIncludeOnSelectionAction() {
this(null);
}
+
public AddIncludeOnSelectionAction(ITextEditor editor) {
super(CEditorMessages.getString("AddIncludeOnSelection.label")); //$NON-NLS-1$
setToolTipText(CEditorMessages.getString("AddIncludeOnSelection.tooltip")); //$NON-NLS-1$
@@ -51,6 +52,7 @@ public class AddIncludeOnSelectionAction extends Action implements IUpdate {
fEditor= editor;
//WorkbenchHelp.setHelp(this, new Object[] { IJavaHelpContextIds.ADD_IMPORT_ON_SELECTION_ACTION });
}
+
private void addInclude(IRequiredInclude[] inc, CFileElementWorkingCopy tu) {
AddIncludeOperation op= new AddIncludeOperation(fEditor, tu, inc, false);
try {
@@ -114,41 +116,67 @@ public class AddIncludeOnSelectionAction extends Action implements IUpdate {
doc.replace(nameStart, packLen + 1, ""); //$NON-NLS-1$
}
} */
+
/**
* @see IAction#actionPerformed
*/
public void run() {
+ IRequiredInclude [] requiredIncludes;
+ if(fCachedRequiredIncludes != null) {
+ requiredIncludes = fCachedRequiredIncludes;
+ } else {
+ requiredIncludes = extractIncludes(fEditor);
+ }
+
+ if(requiredIncludes != null && requiredIncludes.length > 0) {
+ CFileElementWorkingCopy tu= getTranslationUnit();
+ if(tu != null) {
+ addInclude(requiredIncludes, tu);
+ }
+ }
+ }
+
+ /**
+ * Extract the includes for the given selection. This can be both used to perform
+ * the work as well as being invoked when there is a change. The actual results
+ * can and should be cached as the lookup process could be potentially costly.
+ *
+ * @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;
+ }
- CFileElementWorkingCopy tu= getTranslationUnit();
- if (tu != null) {
- ISelection s= fEditor.getSelectionProvider().getSelection();
- IDocument doc= fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
- if (!s.isEmpty() && doc != null) {
- ITextSelection selection= (ITextSelection) s;
- try {
- int selStart= selection.getOffset();
- int nameStart= getNameStart(doc, selStart);
- int len= selStart - nameStart + selection.getLength();
+ ISelection s= editor.getSelectionProvider().getSelection();
+ IDocument doc= editor.getDocumentProvider().getDocument(editor.getEditorInput());
+
+ if (s.isEmpty() || !(s instanceof ITextSelection) || doc == null) {
+ return null;
+ }
+
+ 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();
+ String name= doc.get(nameStart, len).trim();
- //IType[] types= StubUtility.findAllTypes(typeName, cu.getJavaProject(), null);
- //IType chosen= selectResult(types, packName, getShell());
- IFunctionSummary fs = CCompletionContributorManager.getDefault().getFunctionInfo(name);
- if(fs != null) {
- IRequiredInclude[] ri = fs.getIncludes();
- if(ri != null && ri.length > 0) {
- addInclude(ri, tu);
- return;
- }
- }
- } catch (BadLocationException e) {
- MessageDialog.openError(getShell(), CEditorMessages.getString("AddIncludeOnSelection.error.message3"), CEditorMessages.getString("AddIncludeOnSelection.error.message4") + e.getMessage()); //$NON-NLS-2$ //$NON-NLS-1$
- }
+ //IType[] types= StubUtility.findAllTypes(typeName, cu.getJavaProject(), null);
+ //IType chosen= selectResult(types, packName, getShell());
+ IFunctionSummary fs = CCompletionContributorManager.getDefault().getFunctionInfo(name);
+ if(fs != null) {
+ requiredIncludes = fs.getIncludes();
}
+ } catch (BadLocationException e) {
+ MessageDialog.openError(getShell(), CEditorMessages.getString("AddIncludeOnSelection.error.message3"), CEditorMessages.getString("AddIncludeOnSelection.error.message4") + e.getMessage()); //$NON-NLS-2$ //$NON-NLS-1$
}
- getShell().getDisplay().beep();
+
+ return requiredIncludes;
}
+
/* private IType selectResult(IType[] results, String packName, Shell shell) {
int nResults= results.length;
@@ -178,12 +206,14 @@ public class AddIncludeOnSelectionAction extends Action implements IUpdate {
}
return null;
} */
+
public void setContentEditor(ITextEditor editor) {
fEditor= editor;
}
+
public void update() {
- ISelection selection= fEditor.getSelectionProvider().getSelection();
- setEnabled(!selection.isEmpty());
+ fCachedRequiredIncludes = extractIncludes(fEditor);
+ setEnabled(fCachedRequiredIncludes != null);
}
}

Back to the top