Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlain Magloire2004-09-03 19:22:25 +0000
committerAlain Magloire2004-09-03 19:22:25 +0000
commit6fc219f9ccc6f8270de8adc8b348733a9e6a14c3 (patch)
tree4227c3a80e76b0622976bd1340b980a3bf1b6e77
parentdee933ea8cd9a815edea2c3e0b5a18bc3803061e (diff)
downloadorg.eclipse.cdt-6fc219f9ccc6f8270de8adc8b348733a9e6a14c3.tar.gz
org.eclipse.cdt-6fc219f9ccc6f8270de8adc8b348733a9e6a14c3.tar.xz
org.eclipse.cdt-6fc219f9ccc6f8270de8adc8b348733a9e6a14c3.zip
2004-09-03 Alain Magloire
Provide key binding for AddInclude action * src/org/eclipse/cdt/internal/ui/editor/AddIncludeOnSelectionAction.java * src/org/eclipse/cdt/internal/ui/editor/CEditor.java * src/org/eclipse/cdt/internal/ui/editor/CEditorMessages.properties * plugin.xml * plugin.properties
-rw-r--r--core/org.eclipse.cdt.ui/ChangeLog9
-rw-r--r--core/org.eclipse.cdt.ui/plugin.properties4
-rw-r--r--core/org.eclipse.cdt.ui/plugin.xml12
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/AddIncludeOnSelectionAction.java26
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditor.java4
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditorMessages.properties2
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CWordFinder.java194
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/actions/SelectionDispatchAction.java3
8 files changed, 129 insertions, 125 deletions
diff --git a/core/org.eclipse.cdt.ui/ChangeLog b/core/org.eclipse.cdt.ui/ChangeLog
index 60846085d7c..d697d7d0a66 100644
--- a/core/org.eclipse.cdt.ui/ChangeLog
+++ b/core/org.eclipse.cdt.ui/ChangeLog
@@ -1,3 +1,12 @@
+2004-09-03 Alain Magloire
+
+ Provide key binding for AddInclude action
+ * src/org/eclipse/cdt/internal/ui/editor/AddIncludeOnSelectionAction.java
+ * src/org/eclipse/cdt/internal/ui/editor/CEditor.java
+ * src/org/eclipse/cdt/internal/ui/editor/CEditorMessages.properties
+ * plugin.xml
+ * plugin.properties
+
2004-09-02 Alain Magloire
Deal with WorkingCopy vs TranslationUnit in the hashCode.
diff --git a/core/org.eclipse.cdt.ui/plugin.properties b/core/org.eclipse.cdt.ui/plugin.properties
index 4cb83b80219..f4218779ca2 100644
--- a/core/org.eclipse.cdt.ui/plugin.properties
+++ b/core/org.eclipse.cdt.ui/plugin.properties
@@ -134,6 +134,10 @@ OpenTypeAction.tooltip= Open Type
ActionDefinition.openType.name= Open Type
ActionDefinition.openType.description= Open a type in a C editor
+#Add include
+ActionDefinition.addInclude.name= Add Include
+ActionDefinition.addInclude.description= Create include statement on selection
+
CElementWorkingSetPage.name = C/C++
CEditorFontDefinition.description = The C/C++ editor text font is used by C/C++ editors.
diff --git a/core/org.eclipse.cdt.ui/plugin.xml b/core/org.eclipse.cdt.ui/plugin.xml
index 08d062d7290..e96efdf3aea 100644
--- a/core/org.eclipse.cdt.ui/plugin.xml
+++ b/core/org.eclipse.cdt.ui/plugin.xml
@@ -698,6 +698,18 @@
command="org.eclipse.cdt.ui.edit.text.c.open.type.hierarchy"
configuration="org.eclipse.ui.defaultAcceleratorConfiguration">
</keyBinding>
+ <command
+ name="%ActionDefinition.addInclude.name"
+ description="%ActionDefinition.addInclude.description"
+ category="org.eclipse.cdt.ui.category.source"
+ id="org.eclipse.cdt.ui.edit.text.c.add.include">
+ </command>
+ <keyBinding
+ string="Ctrl+Shift+N"
+ scope="org.eclipse.cdt.ui.cEditorScope"
+ command="org.eclipse.cdt.ui.edit.text.c.add.include"
+ configuration="org.eclipse.ui.defaultAcceleratorConfiguration">
+ </keyBinding>
</extension>
<extension
id="org.eclipse.cdt.ui.CSearchPage"
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 ce48fc4470a..5c3ba68af55 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
@@ -37,6 +37,7 @@ import org.eclipse.cdt.internal.ui.CCompletionContributorManager;
import org.eclipse.cdt.internal.ui.ICHelpContextIds;
import org.eclipse.cdt.internal.ui.actions.WorkbenchRunnableAdapter;
import org.eclipse.cdt.internal.ui.codemanipulation.AddIncludesOperation;
+import org.eclipse.cdt.internal.ui.text.CWordFinder;
import org.eclipse.cdt.internal.ui.util.ExceptionHandler;
import org.eclipse.cdt.ui.CSearchResultLabelProvider;
import org.eclipse.cdt.ui.CUIPlugin;
@@ -53,6 +54,7 @@ import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.window.Window;
@@ -136,20 +138,6 @@ public class AddIncludeOnSelectionAction extends Action implements IUpdate {
private Shell getShell() {
return fEditor.getSite().getShell();
}
-
- private int getNameStart(IDocument doc, int pos) throws BadLocationException {
- if (pos > 0 && doc.getChar(pos - 1) == '.') {
- pos--;
- while (pos > 0) {
- char ch= doc.getChar(pos - 1);
- if (!Character.isJavaIdentifierPart(ch) && ch != '.') {
- return pos;
- }
- pos--;
- }
- }
- return pos;
- }
/**
* @see IAction#actionPerformed
@@ -185,11 +173,11 @@ public class AddIncludeOnSelectionAction extends Action implements IUpdate {
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();
+ IRegion region = CWordFinder.findWord(doc, selection.getOffset());
+ if (region == null || region.getLength() == 0) {
+ return;
+ }
+ String name = doc.get(region.getOffset(), region.getLength());
if (name.length() == 0) {
return;
}
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditor.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditor.java
index 0e0aef8f277..073906ec715 100644
--- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditor.java
+++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditor.java
@@ -622,7 +622,9 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IS
action.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_CONTEXT_INFORMATION);
setAction("ContentAssistTip", action); //$NON-NLS-1$
- setAction("AddIncludeOnSelection", new AddIncludeOnSelectionAction(this)); //$NON-NLS-1$
+ action = new AddIncludeOnSelectionAction(this);
+ action.setActionDefinitionId(ICEditorActionDefinitionIds.ADD_INCLUDE);
+ setAction("AddIncludeOnSelection", action); //$NON-NLS-1$
action = new OpenDeclarationsAction(this);
action.setActionDefinitionId(ICEditorActionDefinitionIds.OPEN_DECL);
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditorMessages.properties b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditorMessages.properties
index 45b20f5a25e..25d97c8f8b4 100644
--- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditorMessages.properties
+++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditorMessages.properties
@@ -10,7 +10,7 @@ AddIncludeOnSelection.error.message1=Adding include statements failed
AddIncludeOnSelection.error.message2=Error
AddIncludeOnSelection.error.message3=Error
AddIncludeOnSelection.error.message4=BadLocationException:
-AddIncludeOnSelection.label=Add I&nclude@Ctrl+Shift+N
+AddIncludeOnSelection.label=Add I&nclude
AddIncludeOnSelection.tooltip=Add Include Statement on Selection
ShowInCView.description=Show the current resource in the C/C++ Projects view
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CWordFinder.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CWordFinder.java
index d809ba77231..73722186b31 100644
--- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CWordFinder.java
+++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CWordFinder.java
@@ -10,110 +10,101 @@
*******************************************************************************/
package org.eclipse.cdt.internal.ui.text;
-
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.Region;
/**
- * This is a helper class for the text editor to be able to determine,
- * given a particular offset in a document, various candidates segments
- * for things like context help, proposals and hovering.
+ * This is a helper class for the text editor to be able to determine, given a
+ * particular offset in a document, various candidates segments for things like
+ * context help, proposals and hovering.
*/
-public class CWordFinder
-{
+public class CWordFinder {
/**
- * This method determines for a given offset into a given document
- * what the region is which defines the current word. A word is
- * defined as the set of non "C" identifiers. So assuming that !
- * indicated the current cursor postion:
- * !afunction(int a, int b) --> word = length 0
- * afunc!tion(int a, int b) --> word = afunction
- * afunction!(int a, int b) --> word = afunction
- * afunction(!int a, int b) --> word = length 0
- * afunction(int a,! int b) --> word = length 0
- * afunction(!) --> word = length 0
- * @param document The document to be examined
- * @param offset The offset into the document where a word should
- * be idendified.
- * @return The region defining the current word, which may be a
- * region of length 0 if the offset is not in a word, or null if
- * there is an error accessing the docment data.
+ * This method determines for a given offset into a given document what the
+ * region is which defines the current word. A word is defined as the set of
+ * non "C" identifiers. So assuming that ! indicated the current cursor
+ * postion: !afunction(int a, int b) --> word = length 0 afunc!tion(int a,
+ * int b) --> word = afunction afunction!(int a, int b) --> word = afunction
+ * afunction(!int a, int b) --> word = length 0 afunction(int a,! int b) -->
+ * word = length 0 afunction(!) --> word = length 0
+ *
+ * @param document
+ * The document to be examined
+ * @param offset
+ * The offset into the document where a word should be
+ * idendified.
+ * @return The region defining the current word, which may be a region of
+ * length 0 if the offset is not in a word, or null if there is an
+ * error accessing the docment data.
*/
- public static IRegion findWord( IDocument document, int offset )
- {
+ public static IRegion findWord(IDocument document, int offset) {
int start = -1;
int end = -1;
-
- try
- {
+
+ try {
int pos = offset;
char c;
-
- while( pos >= 0 )
- {
- c = document.getChar( pos );
- if ( !Character.isJavaIdentifierPart( c ) )
+
+ while (pos >= 0) {
+ c = document.getChar(pos);
+ if (!Character.isJavaIdentifierPart(c))
break;
--pos;
}
-
+
start = pos;
-
+
pos = offset;
int length = document.getLength();
-
- while( pos < length )
- {
- c = document.getChar( pos );
- if ( !Character.isJavaIdentifierPart( c ) )
+
+ while (pos < length) {
+ c = document.getChar(pos);
+ if (!Character.isJavaIdentifierPart(c))
break;
++pos;
}
-
+
end = pos;
-
- }
- catch( BadLocationException x )
- {
+
+ } catch (BadLocationException x) {
}
-
- if ( start > -1 && end > -1 )
- {
- if ( start == offset && end == offset )
- return new Region( offset, 0 );
- else if ( start == offset )
- return new Region( start, end - start );
+
+ if (start > -1 && end > -1) {
+ if (start == offset && end == offset)
+ return new Region(offset, 0);
+ else if (start == offset)
+ return new Region(start, end - start);
else
- return new Region( start + 1, end - start - 1 );
+ return new Region(start + 1, end - start - 1);
}
-
+
return null;
}
-
+
/**
- * This method will determine the region for the name of the function
- * within which the current offset is contained.
- * @param document The document to be examined
- * @param offset The offset into the document where a word should
- * be idendified.
- * @return The region defining the current word, which may be a
- * region of length 0 if the offset is not in a function, or null if
- * there is an error accessing the docment data.
+ * This method will determine the region for the name of the function within
+ * which the current offset is contained.
+ *
+ * @param document
+ * The document to be examined
+ * @param offset
+ * The offset into the document where a word should be
+ * idendified.
+ * @return The region defining the current word, which may be a region of
+ * length 0 if the offset is not in a function, or null if there is
+ * an error accessing the docment data.
*/
- public static IRegion findFunction( IDocument document, int offset )
- {
+ public static IRegion findFunction(IDocument document, int offset) {
int leftbracket = -1;
int leftbracketcount = 0;
int rightbracket = -1;
int rightbracketcount = 0;
int functionstart = -1;
int functionend = -1;
-
-
- try
- {
+
+ try {
int length = document.getLength();
int pos;
char c;
@@ -121,92 +112,91 @@ public class CWordFinder
//Find most relevant right bracket from our position
pos = offset;
rightbracketcount = leftbracketcount = 0;
- while(pos < length) {
- c = document.getChar( pos );
+ while (pos < length) {
+ c = document.getChar(pos);
- if( c == ')') {
+ if (c == ')') {
rightbracketcount++;
- if(rightbracketcount >= leftbracketcount) {
+ if (rightbracketcount >= leftbracketcount) {
rightbracket = pos;
break;
}
}
-
- if(c == '(') {
+
+ if (c == '(') {
leftbracketcount++;
}
-
- if(c == ';') {
+
+ if (c == ';') {
break;
}
-
+
pos++;
}
-
- if ( rightbracket == -1 ) {
+
+ if (rightbracket == -1) {
return new Region(offset, 0);
}
//Now backtrack our way from the rightbracket to the left
pos = rightbracket;
rightbracketcount = leftbracketcount = 0;
- while(pos >= 0) {
- c = document.getChar( pos );
+ while (pos >= 0) {
+ c = document.getChar(pos);
- if( c == ')') {
+ if (c == ')') {
rightbracketcount++;
}
-
- if(c == '(') {
+
+ if (c == '(') {
leftbracketcount++;
- if(leftbracketcount >= rightbracketcount) {
+ if (leftbracketcount >= rightbracketcount) {
leftbracket = pos;
break;
}
}
-
- if(c == ';') {
+
+ if (c == ';') {
break;
}
-
+
pos--;
}
- if ( leftbracket == -1 ) {
+ if (leftbracket == -1) {
return new Region(offset, 0);
}
-
+
//Now work our way to the function name
pos = leftbracket - 1;
- while(pos >= 0) {
- c = document.getChar( pos );
- if(functionend == -1 && c == ' ' ) {
+ while (pos >= 0) {
+ c = document.getChar(pos);
+ if (functionend == -1 && c == ' ') {
pos--;
continue;
}
-
- if(!Character.isJavaIdentifierPart(c)) {
+
+ if (!Character.isJavaIdentifierPart(c)) {
break;
}
-
+
functionstart = pos;
- if(functionend == -1) {
+ if (functionend == -1) {
functionend = pos;
}
pos--;
}
- } catch( BadLocationException x ) {
+ } catch (BadLocationException x) {
/* Ignore */
}
-
+
if (functionstart > -1 && functionend > -1) {
return new Region(functionstart, functionend - functionstart + 1);
}
-
+
return null;
}
-
-}
+}
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/actions/SelectionDispatchAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/actions/SelectionDispatchAction.java
index fdef4167998..09e38422328 100644
--- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/actions/SelectionDispatchAction.java
+++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/actions/SelectionDispatchAction.java
@@ -80,8 +80,7 @@ public abstract class SelectionDispatchAction extends Action implements ISelecti
public ISelection getSelection() {
if (getSelectionProvider() != null)
return getSelectionProvider().getSelection();
- else
- return null;
+ return null;
}
/**

Back to the top