diff --git a/core/org.eclipse.cdt.ui/plugin.properties b/core/org.eclipse.cdt.ui/plugin.properties index 932efcf7a01..2d44df6f655 100644 --- a/core/org.eclipse.cdt.ui/plugin.properties +++ b/core/org.eclipse.cdt.ui/plugin.properties @@ -27,6 +27,8 @@ AddBlockCommentAction.label= Add &Block Comment RemoveBlockCommentAction.label= Remove Bloc&k Comment +JoinLinesAction.label= Join Lines + # The Wizards # C newCWizardsCategory.name=C @@ -102,6 +104,9 @@ ActionDefinition.addBlockComment.description= Enclose the selection with a block ActionDefinition.removeBlockComment.name= Remove Block Comment ActionDefinition.removeBlockComment.description= Remove the block comment enclosing the selection +ActionDefinition.joinLines.name= Join Lines +ActionDefinition.joinLines.description= Join the current and next line together + ActionDefinition.format.name=Format ActionDefinition.format.description=Format Source Code diff --git a/core/org.eclipse.cdt.ui/plugin.xml b/core/org.eclipse.cdt.ui/plugin.xml index 06cddcaa07d..4e7a1a72b12 100644 --- a/core/org.eclipse.cdt.ui/plugin.xml +++ b/core/org.eclipse.cdt.ui/plugin.xml @@ -948,6 +948,11 @@ contextId="org.eclipse.cdt.ui.cEditorScope" commandId="org.eclipse.cdt.ui.edit.text.c.goto.matching.bracket" schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"/> + @@ -986,6 +991,12 @@ categoryId="org.eclipse.cdt.ui.category.source" id="org.eclipse.cdt.ui.edit.text.c.remove.block.comment"> + + ResourceAction constructor), + * or null if none + * @param editor The text editor component on which this Action will work. + */ + public JoinLinesAction(ResourceBundle bundle, String prefix, ITextEditor editor) { + + super(bundle, prefix, editor); + + } // end of constructor + + + /** + * Run is where all the action (sic!) happens. This is a pretty simple + * action that basically joins the current line and next line together. + * It achieves this by determining which line of the editor's document + * we are on, and then replacing the delimiter at the end of the line + * with nothing. + * + * TODO: Currently, we don't bother to remove any excess whitespace. + * Doing so in the future might be a nice touch. + */ + public void run() { + + // Make sure we can proceed. + if ((theEditor != null) && isEnabled() && canModifyEditor()) { + + // Retrieve the current editor's document + IDocument theDocument = getDocument(theEditor); + + if (theDocument != null) { + + try { + + // First, retrieve the current selection + ITextSelection theSelection = getSelection(theEditor); + + if (theSelection != null) { + + // Now, figure out the end line of the selection + int currentLine = theSelection.getEndLine(); + + // What's the offset of this line in the document? + int lineOffset = theDocument.getLineOffset(currentLine); + + // And the length of the line? + int lineLength = theDocument.getLineLength(currentLine); + + // What delimeter do we have? + String delim = theDocument.getLineDelimiter(currentLine); + + // How long is it? + int delimLength = delim.length(); + + // Now back track to the last real char in the line + int newLineEnd = lineOffset + lineLength - delimLength; + + // Replace the delimter char(s) with nothing + theDocument.replace(newLineEnd, delimLength, null); + + } + + } + catch (BadLocationException e) { + + e.printStackTrace(); + } + + } // end of if (document) + + } // end of isEnabled() + + } // end of run + + + /** + * Check that we can actually modify the document of the current editor. + */ + public void update() { + + super.update(); + + // Make sure we can proceed. + if (isEnabled() && canModifyEditor()) { + + // Retrieve the text editor and store it for later + theEditor = getTextEditor(); + } + + } // end of update + + + /** + * Get Document attempts to retrieve 'editor's document. + * + * @param editor The editor whose document we want to retrieve. + * + * @return An IDocument if there is one, or null. + */ + private IDocument getDocument(ITextEditor editor) { + + // What we will return + IDocument theDocument = null; + + // Retrieve the document provider + IDocumentProvider documentProvider = editor.getDocumentProvider(); + + if (documentProvider != null) { + + // Retrieve the actual document + theDocument = documentProvider.getDocument(editor.getEditorInput()); + } + + return theDocument; + + } // end of getDocument + + + /** + * Get selection attempts to retrieve 'editor's current selection. + * + * @param editor The editor whose selection we want to retrieve. + * + * @return An ITextSelection if there is one, or null. + */ + private ITextSelection getSelection(ITextEditor editor) { + + // What we will return + ITextSelection theSelection = null; + + // First try to retrieve the editor's selection provider + ISelectionProvider selectionProvider = editor.getSelectionProvider(); + + if (selectionProvider != null) { + + // Now try to retrieve the selection + ISelection selection = selectionProvider.getSelection(); + + // Is this of the correct type? + if (selection instanceof ITextSelection) { + + // Ok, cast it and assign it + theSelection = (ITextSelection) selection; + } + } + + return theSelection; + + } // end of getSelection + +} // end of JoinLinesAction 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 b46b6b3ea17..33d399adbbb 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 @@ -29,6 +29,7 @@ import org.eclipse.cdt.internal.ui.IContextMenuConstants; import org.eclipse.cdt.internal.ui.actions.AddBlockCommentAction; import org.eclipse.cdt.internal.ui.actions.FoldingActionGroup; import org.eclipse.cdt.internal.ui.actions.GoToNextPreviousMemberAction; +import org.eclipse.cdt.internal.ui.actions.JoinLinesAction; import org.eclipse.cdt.internal.ui.actions.RemoveBlockCommentAction; import org.eclipse.cdt.internal.ui.browser.typehierarchy.OpenTypeHierarchyAction; import org.eclipse.cdt.internal.ui.search.actions.OpenDeclarationsAction; @@ -665,7 +666,11 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IS Action action= new GotoMatchingBracketAction(this); action.setActionDefinitionId(ICEditorActionDefinitionIds.GOTO_MATCHING_BRACKET); setAction(GotoMatchingBracketAction.GOTO_MATCHING_BRACKET, action); - + + action = new JoinLinesAction(CEditorMessages.getResourceBundle(), "JoinLines.", this); //$NON-NLS-1$ + action.setActionDefinitionId(ICEditorActionDefinitionIds.JOIN_LINES); + setAction("Join Lines", action); //$NON-NLS-1$ + action = new TextOperationAction(CEditorMessages.getResourceBundle(), "Comment.", this, ITextOperationTarget.PREFIX); //$NON-NLS-1$ action.setActionDefinitionId(ICEditorActionDefinitionIds.COMMENT); setAction("Comment", action); //$NON-NLS-1$ 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 b357022a6a4..305093b5d00 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 @@ -144,6 +144,10 @@ RemoveBlockComment.label=Remove Bloc&k Comment RemoveBlockComment.tooltip=Remove Block Comment Markers Enclosing the Caret RemoveBlockComment.description=Removes any block comment markers enclosing the caret +JoinLines.label=Join Lines +JoinLines.tooltip=Join Lines +JoinLines.description=Join the current and next line together + Format.label=F&ormat Format.tooltip=Format the Selected Text Format.description=Format the selected text diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/ICEditorActionDefinitionIds.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/ICEditorActionDefinitionIds.java index 4ca406215e1..94d9a3785c0 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/ICEditorActionDefinitionIds.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/ICEditorActionDefinitionIds.java @@ -49,8 +49,14 @@ public interface ICEditorActionDefinitionIds extends ITextEditorActionDefinition * @since 3.0 */ public static final String REMOVE_BLOCK_COMMENT= "org.eclipse.cdt.ui.edit.text.c.remove.block.comment"; //$NON-NLS-1$ - - + + /** + * Action definition ID of the source -> join lines action + * (value "org.eclipse.cdt.ui.edit.text.c.join.lines"). + * @since 3.0.2 + */ + public static final String JOIN_LINES= "org.eclipse.cdt.ui.edit.text.c.join.lines"; //$NON-NLS-1$ + /** * Action definition ID of the source -> format action * (value "org.eclipse.cdt.ui.edit.text.c.format"). @@ -69,27 +75,30 @@ public interface ICEditorActionDefinitionIds extends ITextEditorActionDefinition */ public static final String OPEN_DECL= "org.eclipse.cdt.ui.edit.opendecl"; //$NON-NLS-1$ - /** - * Action definition ID of the open definition action - * (value "org.eclipse.cdt.ui.edit.opendef"). - */ - public static final String OPEN_DEF= "org.eclipse.cdt.ui.edit.opendef"; //$NON-NLS-1$ + /** + * Action definition ID of the open definition action + * (value "org.eclipse.cdt.ui.edit.opendef"). + */ + public static final String OPEN_DEF= "org.eclipse.cdt.ui.edit.opendef"; //$NON-NLS-1$ /** * Action definition ID of the show in C/C++ Projects View action * (value "org.eclipse.cdt.ui.edit.opencview"). */ public static final String OPEN_CVIEW= "org.eclipse.cdt.ui.edit.opencview"; //$NON-NLS-1$ + /** * Action definition ID of the refactor -> rename element action * (value "org.eclipse.cdt.ui.edit.text.rename.element"). */ public static final String RENAME_ELEMENT= "org.eclipse.cdt.ui.edit.text.rename.element"; //$NON-NLS-1$ + /** * Action definition ID of the refactor -> undo action * (value "org.eclipse.cdt.ui.edit.text.undo.action"). */ public static final String UNDO_ACTION= "org.eclipse.cdt.ui.edit.text.undo.action"; //$NON-NLS-1$ + /** * Action definition ID of the refactor -> redo action * (value "org.eclipse.cdt.ui.edit.text.redo.action"). @@ -101,6 +110,7 @@ public interface ICEditorActionDefinitionIds extends ITextEditorActionDefinition * (value "org.eclipse.cdt.ui.search.findrefs"). */ public static final String FIND_REFS= "org.eclipse.cdt.ui.search.findrefs"; //$NON-NLS-1$ + /** * Action definition ID of the find declarations in workspace action * (value "org.eclipse.cdt.ui.search.finddecl"). @@ -119,23 +129,23 @@ public interface ICEditorActionDefinitionIds extends ITextEditorActionDefinition */ public static final String OPEN_EDITOR= "org.eclipse.cdt.ui.edit.text.c.open.editor"; //$NON-NLS-1$ - /** - * Action definition ID of the open quick outline. - * (value "org.eclipse.cdt.ui.edit.open.outline"). - */ - public static final String OPEN_OUTLINE= "org.eclipse.cdt.ui.edit.open.outline"; //$NON-NLS-1$ + /** + * Action definition ID of the open quick outline. + * (value "org.eclipse.cdt.ui.edit.open.outline"). + */ + public static final String OPEN_OUTLINE= "org.eclipse.cdt.ui.edit.open.outline"; //$NON-NLS-1$ - /** - * Action definition ID for go to next c member. - * (value "org.eclipse.cdt.ui.edit.text.c.goto.next.memeber") - */ - public static final String GOTO_NEXT_MEMBER = "org.eclipse.cdt.ui.edit.text.c.goto.next.member"; //$NON-NLS-1$ + /** + * Action definition ID for go to next c member. + * (value "org.eclipse.cdt.ui.edit.text.c.goto.next.memeber") + */ + public static final String GOTO_NEXT_MEMBER = "org.eclipse.cdt.ui.edit.text.c.goto.next.member"; //$NON-NLS-1$ - /** - * Action definition ID for go to previous c member. - * (value "org.eclipse.cdt.ui.edit.text.c.goto.prev.memeber") - */ - public static final String GOTO_PREVIOUS_MEMBER = "org.eclipse.cdt.ui.edit.text.c.goto.prev.member"; //$NON-NLS-1$ + /** + * Action definition ID for go to previous c member. + * (value "org.eclipse.cdt.ui.edit.text.c.goto.prev.memeber") + */ + public static final String GOTO_PREVIOUS_MEMBER = "org.eclipse.cdt.ui.edit.text.c.goto.prev.member"; //$NON-NLS-1$ /** * Action definition ID of the edit -> go to matching bracket action