1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00
Cache the result of the include query.
From Thomas Fletcher
This commit is contained in:
Alain Magloire 2004-02-03 16:51:06 +00:00
parent 877147feef
commit cfb48d06f2
2 changed files with 72 additions and 32 deletions

View file

@ -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 2004-02-02 Alain Magloire
Dealing with PR 50792, give more flexibility in the behaviour Dealing with PR 50792, give more flexibility in the behaviour

View file

@ -38,11 +38,12 @@ 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 [] fCachedRequiredIncludes;
public AddIncludeOnSelectionAction() { public AddIncludeOnSelectionAction() {
this(null); this(null);
} }
public AddIncludeOnSelectionAction(ITextEditor editor) { public AddIncludeOnSelectionAction(ITextEditor editor) {
super(CEditorMessages.getString("AddIncludeOnSelection.label")); //$NON-NLS-1$ super(CEditorMessages.getString("AddIncludeOnSelection.label")); //$NON-NLS-1$
setToolTipText(CEditorMessages.getString("AddIncludeOnSelection.tooltip")); //$NON-NLS-1$ setToolTipText(CEditorMessages.getString("AddIncludeOnSelection.tooltip")); //$NON-NLS-1$
@ -51,6 +52,7 @@ public class AddIncludeOnSelectionAction extends Action implements IUpdate {
fEditor= editor; fEditor= editor;
//WorkbenchHelp.setHelp(this, new Object[] { IJavaHelpContextIds.ADD_IMPORT_ON_SELECTION_ACTION }); //WorkbenchHelp.setHelp(this, new Object[] { IJavaHelpContextIds.ADD_IMPORT_ON_SELECTION_ACTION });
} }
private void addInclude(IRequiredInclude[] inc, CFileElementWorkingCopy tu) { private void addInclude(IRequiredInclude[] inc, CFileElementWorkingCopy tu) {
AddIncludeOperation op= new AddIncludeOperation(fEditor, tu, inc, false); AddIncludeOperation op= new AddIncludeOperation(fEditor, tu, inc, false);
try { try {
@ -114,41 +116,67 @@ public class AddIncludeOnSelectionAction extends Action implements IUpdate {
doc.replace(nameStart, packLen + 1, ""); //$NON-NLS-1$ doc.replace(nameStart, packLen + 1, ""); //$NON-NLS-1$
} }
} */ } */
/** /**
* @see IAction#actionPerformed * @see IAction#actionPerformed
*/ */
public void run() { public void run() {
IRequiredInclude [] requiredIncludes;
CFileElementWorkingCopy tu= getTranslationUnit(); if(fCachedRequiredIncludes != null) {
if (tu != null) { requiredIncludes = fCachedRequiredIncludes;
ISelection s= fEditor.getSelectionProvider().getSelection(); } else {
IDocument doc= fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput()); requiredIncludes = extractIncludes(fEditor);
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$
}
}
} }
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) { /* private IType selectResult(IType[] results, String packName, Shell shell) {
int nResults= results.length; int nResults= results.length;
@ -178,12 +206,14 @@ public class AddIncludeOnSelectionAction extends Action implements IUpdate {
} }
return null; return null;
} */ } */
public void setContentEditor(ITextEditor editor) { public void setContentEditor(ITextEditor editor) {
fEditor= editor; fEditor= editor;
} }
public void update() { public void update() {
ISelection selection= fEditor.getSelectionProvider().getSelection(); fCachedRequiredIncludes = extractIncludes(fEditor);
setEnabled(!selection.isEmpty()); setEnabled(fCachedRequiredIncludes != null);
} }
} }