diff --git a/core/org.eclipse.cdt.ui/plugin.properties b/core/org.eclipse.cdt.ui/plugin.properties index 12824d657a2..9d91129eef6 100644 --- a/core/org.eclipse.cdt.ui/plugin.properties +++ b/core/org.eclipse.cdt.ui/plugin.properties @@ -11,6 +11,7 @@ # Anton Leherbauer (Wind River Systems) # Sergey Prigogin (Google) # QNX Software Systems - [272416] Rework the working set configurations +# Axel Mueller - [289339] Surround with ############################################################################### pluginName=C/C++ Development Tools UI providerName=Eclipse CDT @@ -152,6 +153,9 @@ ActionDefinition.implementMethod.description= Implements a method for a selected ActionDefinition.gettersAndSetters.name = Generate Getters and Setters... ActionDefinition.gettersAndSetters.description = Generates getters and setters for a selected field +ActionDefinition.surroundWith.quickMenu.name= Surround With Quick Menu +ActionDefinition.surroundWith.quickMenu.description= Shows the Surround With quick menu + # Action Set CodingActionSet.label= C/C++ Coding CodingActionSet.description= Action set containing coding related C/C++ actions @@ -459,6 +463,8 @@ SpecificContentAssist.name= C/C++ Content Assist SpecificContentAssist.desc= A parameterizable command that invokes content assist with a single completion proposal category SpecificContentAssist.param= type +SurroundWithTemplateAction.label= Surround &With + # CView context cViewScope.name = In C/C++ Views cViewScope.description = In C/C++ Views diff --git a/core/org.eclipse.cdt.ui/plugin.xml b/core/org.eclipse.cdt.ui/plugin.xml index fbf55e0e0e0..953b3892fd4 100644 --- a/core/org.eclipse.cdt.ui/plugin.xml +++ b/core/org.eclipse.cdt.ui/plugin.xml @@ -1276,6 +1276,14 @@ id="org.eclipse.cdt.ui.actions.ImplementMethod" retarget="true"> + + + + + + + + + proposals= templateComputer.computeCompletionProposals(context, null); + if (proposals == null || proposals.size() == 0) + return null; + + return getActionsFromProposals(proposals, context.getInvocationOffset(), editor.getViewer()); + } + + private static ITextSelection getTextSelection(CEditor editor) { + ISelectionProvider selectionProvider= editor.getSelectionProvider(); + if (selectionProvider == null) + return null; + + ISelection selection= selectionProvider.getSelection(); + if (!(selection instanceof ITextSelection)) + return null; + + return (ITextSelection)selection; + } + + private static IAction[] getActionsFromProposals(List proposals, final int offset, final ITextViewer viewer) { + List result= new ArrayList(); + int j = 1; + for (Iterator it= proposals.iterator(); it.hasNext();) { + final ICompletionProposal proposal= it.next(); + + StringBuffer actionName= new StringBuffer(); + if (j<10) { + actionName.append('&').append(j).append(' '); + } + actionName.append(proposal.getDisplayString()); + Action action= new Action(actionName.toString()) { + /** + * {@inheritDoc} + */ + @Override + public void run() { + applyProposal(proposal, viewer, (char)0, 0, offset); + } + }; + + result.add(action); + j++; + } + if (result.size() == 0) + return null; + + return result.toArray(new IAction[result.size()]); + } + + private static void applyProposal(ICompletionProposal proposal, ITextViewer viewer, char trigger, int stateMask, final int offset) { + Assert.isTrue(proposal instanceof ICompletionProposalExtension2); + + IRewriteTarget target= null; + IEditingSupportRegistry registry= null; + IEditingSupport helper= new IEditingSupport() { + + public boolean isOriginator(DocumentEvent event, IRegion focus) { + return focus.getOffset() <= offset && focus.getOffset() + focus.getLength() >= offset; + } + + public boolean ownsFocusShell() { + return false; + } + + }; + + try { + IDocument document= viewer.getDocument(); + + if (viewer instanceof ITextViewerExtension) { + ITextViewerExtension extension= (ITextViewerExtension) viewer; + target= extension.getRewriteTarget(); + } + + if (target != null) + target.beginCompoundChange(); + + if (viewer instanceof IEditingSupportRegistry) { + registry= (IEditingSupportRegistry) viewer; + registry.register(helper); + } + + ((ICompletionProposalExtension2)proposal).apply(viewer, trigger, stateMask, offset); + + Point selection= proposal.getSelection(document); + if (selection != null) { + viewer.setSelectedRange(selection.x, selection.y); + viewer.revealRange(selection.x, selection.y); + } + } finally { + if (target != null) + target.endCompoundChange(); + + if (registry != null) + registry.unregister(helper); + } + } +} 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 dea52237d7f..b762e312d75 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 @@ -11,6 +11,7 @@ * Anton Leherbauer (Wind River Systems) * Markus Schorn (Wind River Systems) * Sergey Prigogin (Google) + * Axel Mueller - [289339] Surround with *******************************************************************************/ package org.eclipse.cdt.internal.ui.editor; @@ -200,6 +201,7 @@ import org.eclipse.cdt.internal.ui.actions.GoToNextPreviousMemberAction; import org.eclipse.cdt.internal.ui.actions.GotoNextBookmarkAction; import org.eclipse.cdt.internal.ui.actions.IndentAction; import org.eclipse.cdt.internal.ui.actions.RemoveBlockCommentAction; +import org.eclipse.cdt.internal.ui.actions.SurroundWithActionGroup; import org.eclipse.cdt.internal.ui.search.OccurrencesFinder; import org.eclipse.cdt.internal.ui.search.IOccurrencesFinder.OccurrenceLocation; import org.eclipse.cdt.internal.ui.search.actions.SelectionSearchGroup; @@ -1206,6 +1208,9 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IC /** Generate action group filling the "Source" submenu */ private GenerateActionGroup fGenerateActionGroup; + + /** Generate action group filling the "Surround with" submenu */ + private SurroundWithActionGroup fSurroundWithActionGroup; /** Pairs of brackets, used to match. */ protected final static char[] BRACKETS = { '{', '}', '(', ')', '[', ']', '<', '>' }; @@ -2048,7 +2053,12 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IC fGenerateActionGroup.dispose(); fGenerateActionGroup= null; } - + + if (fSurroundWithActionGroup != null) { + fSurroundWithActionGroup.dispose(); + fSurroundWithActionGroup= null; + } + if (fEditorSelectionChangedListener != null) { fEditorSelectionChangedListener.uninstall(getSelectionProvider()); fEditorSelectionChangedListener = null; @@ -2198,7 +2208,8 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IC fRefactoringActionGroup= new CRefactoringActionGroup(this, ITextEditorActionConstants.GROUP_EDIT); fOpenInViewGroup= createOpenViewActionGroup(); fGenerateActionGroup= new GenerateActionGroup(this, ITextEditorActionConstants.GROUP_EDIT); - + fSurroundWithActionGroup = new SurroundWithActionGroup(this, ITextEditorActionConstants.GROUP_EDIT); + action = getAction(ITextEditorActionConstants.SHIFT_RIGHT); if (action != null) { action.setId(ITextEditorActionConstants.SHIFT_RIGHT); @@ -2259,6 +2270,10 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IC fGenerateActionGroup.fillContextMenu(menu); fGenerateActionGroup.setContext(null); + fSurroundWithActionGroup.setContext(context); + fSurroundWithActionGroup.fillContextMenu(menu); + fSurroundWithActionGroup.setContext(null); + if (hasCElement) { fSelectionSearchGroup.fillContextMenu(menu); } @@ -2984,6 +2999,7 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IC fRefactoringActionGroup.fillActionBars(actionBars); fGenerateActionGroup.fillActionBars(actionBars); fFoldingGroup.updateActionBars(); + fSurroundWithActionGroup.fillActionBars(actionBars); } /*