Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdt-patch] Add Include enable/disable patch

Folks,

  As has been indicated a number of times, the Add Include action which
is available on the editor right click menu is often confusing to users
since there is a very narrow range of scope within which it is applicable.

This patch is an update to the 1.2 stream (should be fine against the
head as well) which puts some additional smarts into the current 
implementation so that it is only enabled when it can in fact work.

ChangeLog

- Only enable the Add Include action when there are actually includes
  to add in to the the file.

--- PATCH START ---
Index: 
src/org/eclipse/cdt/internal/ui/editor/AddIncludeOnSelectionAction.java
===================================================================
RCS 
file: /home/tools/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/A
ddIncludeOnSelectionAction.java,v
retrieving revision 1.3
diff -u -r1.3 AddIncludeOnSelectionAction.java
--- src/org/eclipse/cdt/internal/ui/editor/AddIncludeOnSelectionAction.java
	4 Feb 2003 20:00:46 -0000	1.3
+++ src/org/eclipse/cdt/internal/ui/editor/AddIncludeOnSelectionAction.java
	3 Feb 2004 14:48:27 -0000
@@ -38,11 +38,12 @@
 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 @@
 		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 @@
 			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 @@
 		}
 		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);
 	}
 }
 

--- PATCH END ---
Index: src/org/eclipse/cdt/internal/ui/editor/AddIncludeOnSelectionAction.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/AddIncludeOnSelectionAction.java,v
retrieving revision 1.3
diff -u -r1.3 AddIncludeOnSelectionAction.java
--- src/org/eclipse/cdt/internal/ui/editor/AddIncludeOnSelectionAction.java	4 Feb 2003 20:00:46 -0000	1.3
+++ src/org/eclipse/cdt/internal/ui/editor/AddIncludeOnSelectionAction.java	3 Feb 2004 14:48:27 -0000
@@ -38,11 +38,12 @@
 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 @@
 		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 @@
 			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 @@
 		}
 		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