From cfb48d06f294f87bb461008091e0205caaea633b Mon Sep 17 00:00:00 2001 From: Alain Magloire Date: Tue, 3 Feb 2004 16:51:06 +0000 Subject: [PATCH] PR 51115 Cache the result of the include query. From Thomas Fletcher --- core/org.eclipse.cdt.ui/ChangeLog | 10 ++ .../editor/AddIncludeOnSelectionAction.java | 94 ++++++++++++------- 2 files changed, 72 insertions(+), 32 deletions(-) diff --git a/core/org.eclipse.cdt.ui/ChangeLog b/core/org.eclipse.cdt.ui/ChangeLog index e694b8a020c..93ad1de5733 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-02-02 Alain Magloire Dealing with PR 50792, give more flexibility in the behaviour 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() { - - 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(); - - 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$ - } - } + IRequiredInclude [] requiredIncludes; + if(fCachedRequiredIncludes != null) { + requiredIncludes = fCachedRequiredIncludes; + } else { + requiredIncludes = extractIncludes(fEditor); } - getShell().getDisplay().beep(); + + 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; + } + + 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(); + + //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$ + } + + 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); } }