diff --git a/core/org.eclipse.cdt.ui/ChangeLog b/core/org.eclipse.cdt.ui/ChangeLog index d114ba4abab..6a3fb009132 100644 --- a/core/org.eclipse.cdt.ui/ChangeLog +++ b/core/org.eclipse.cdt.ui/ChangeLog @@ -1,3 +1,12 @@ +2005-02-25 Alain Magloire + Fix PR 69572: Key binding action for Matching Bracket ... Ctrl+Shift+P + * src/org/eclipse/cdt/internal/ui/ICHelpContextIds.java + * src/org/eclipse/cdt/internal/ui/editor/CEditor.java + + src/org/elcipse/cdt/internal/ui/editor/GotoMatchingBracketAction.java + * src/org/eclipse/cdt/internal/ui/editor/ICEditorActionDefinitionIds.java + * plugin.properties + * plugin.xml + 2005-02-25 Alain Magloire Fix PR 86718: Dialog shows twice * src/org/eclipse/cdt/internal/ui/cview/OpenProjectGroup.java diff --git a/core/org.eclipse.cdt.ui/plugin.properties b/core/org.eclipse.cdt.ui/plugin.properties index e2907033d94..46156251fb3 100644 --- a/core/org.eclipse.cdt.ui/plugin.properties +++ b/core/org.eclipse.cdt.ui/plugin.properties @@ -102,6 +102,9 @@ ActionDefinition.removeBlockComment.description= Remove the block comment enclos ActionDefinition.format.name=Format ActionDefinition.format.description=Format Source Code +ActionDefinition.gotoMatchingBracket.name= Go to Matching Bracket +ActionDefinition.gotoMatchingBracket.description= Moves the cursor to the matching bracket + CEditor.name=C/C++ Editor CPluginPreferencePage.name=C/C++ diff --git a/core/org.eclipse.cdt.ui/plugin.xml b/core/org.eclipse.cdt.ui/plugin.xml index 575f800d708..7542a98dc39 100644 --- a/core/org.eclipse.cdt.ui/plugin.xml +++ b/core/org.eclipse.cdt.ui/plugin.xml @@ -1025,6 +1025,19 @@ command="org.eclipse.cdt.ui.edit.text.c.goto.prev.member" configuration="org.eclipse.ui.defaultAcceleratorConfiguration"> + + + + + 1) { + setStatusLineErrorMessage(CEditorMessages.getString("GotoMatchingBracket.error.invalidSelection")); //$NON-NLS-1$ + sourceViewer.getTextWidget().getDisplay().beep(); + return; + } + + // #26314 + int sourceCaretOffset= selection.getOffset() + selection.getLength(); + if (isSurroundedByBrackets(document, sourceCaretOffset)) + sourceCaretOffset -= selection.getLength(); + + IRegion region= fBracketMatcher.match(document, sourceCaretOffset); + if (region == null) { + setStatusLineErrorMessage(CEditorMessages.getString("GotoMatchingBracket.error.noMatchingBracket")); //$NON-NLS-1$ + sourceViewer.getTextWidget().getDisplay().beep(); + return; + } + + int offset= region.getOffset(); + int length= region.getLength(); + + if (length < 1) + return; + + int anchor= fBracketMatcher.getAnchor(); + // http://dev.eclipse.org/bugs/show_bug.cgi?id=34195 + int targetOffset= (ICharacterPairMatcher.RIGHT == anchor) ? offset + 1: offset + length; + + boolean visible= false; + if (sourceViewer instanceof ITextViewerExtension5) { + ITextViewerExtension5 extension= (ITextViewerExtension5) sourceViewer; + visible= (extension.modelOffset2WidgetOffset(targetOffset) > -1); + } else { + IRegion visibleRegion= sourceViewer.getVisibleRegion(); + // http://dev.eclipse.org/bugs/show_bug.cgi?id=34195 + visible= (targetOffset >= visibleRegion.getOffset() && targetOffset <= visibleRegion.getOffset() + visibleRegion.getLength()); + } + + if (!visible) { + setStatusLineErrorMessage(CEditorMessages.getString("GotoMatchingBracket.error.bracketOutsideSelectedElement")); //$NON-NLS-1$ + sourceViewer.getTextWidget().getDisplay().beep(); + return; + } + + if (selection.getLength() < 0) + targetOffset -= selection.getLength(); + + sourceViewer.setSelectedRange(targetOffset, selection.getLength()); + sourceViewer.revealRange(targetOffset, selection.getLength()); + } + + /** * Returns whether the given annotation is configured as a target for the * "Go to Next/Previous Annotation" actions @@ -1283,6 +1355,53 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IS statusLine.setMessage(false, msg, null); } + /** + * Returns the signed current selection. + * The length will be negative if the resulting selection + * is right-to-left (RtoL). + *

+ * The selection offset is model based. + *

+ * + * @param sourceViewer the source viewer + * @return a region denoting the current signed selection, for a resulting RtoL selections length is < 0 + */ + protected IRegion getSignedSelection(ISourceViewer sourceViewer) { + StyledText text= sourceViewer.getTextWidget(); + Point selection= text.getSelectionRange(); + + if (text.getCaretOffset() == selection.x) { + selection.x= selection.x + selection.y; + selection.y= -selection.y; + } + + selection.x= widgetOffset2ModelOffset(sourceViewer, selection.x); + + return new Region(selection.x, selection.y); + } + + private static boolean isBracket(char character) { + for (int i= 0; i != BRACKETS.length; ++i) + if (character == BRACKETS[i]) + return true; + return false; + } + + private static boolean isSurroundedByBrackets(IDocument document, int offset) { + if (offset == 0 || offset == document.getLength()) + return false; + + try { + return + isBracket(document.getChar(offset - 1)) && + isBracket(document.getChar(offset)); + + } catch (BadLocationException e) { + return false; + } + } + + /* (non-Javadoc) * @see org.eclipse.cdt.internal.ui.editor.IReconcilingParticipant#reconciled() */ diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/GotoMatchingBracketAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/GotoMatchingBracketAction.java new file mode 100644 index 00000000000..a7380dedede --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/GotoMatchingBracketAction.java @@ -0,0 +1,39 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ + +package org.eclipse.cdt.internal.ui.editor; + +import org.eclipse.jface.action.Action; +import org.eclipse.jface.text.Assert; + +import org.eclipse.ui.PlatformUI; + +import org.eclipse.cdt.internal.ui.ICHelpContextIds; + + +public class GotoMatchingBracketAction extends Action { + + public final static String GOTO_MATCHING_BRACKET= "GotoMatchingBracket"; //$NON-NLS-1$ + + private final CEditor fEditor; + + public GotoMatchingBracketAction(CEditor editor) { + super(CEditorMessages.getString("GotoMatchingBracket.label")); //$NON-NLS-1$ + Assert.isNotNull(editor); + fEditor= editor; + setEnabled(true); + PlatformUI.getWorkbench().getHelpSystem().setHelp(this, ICHelpContextIds.GOTO_MATCHING_BRACKET_ACTION); + } + + public void run() { + fEditor.gotoMatchingBracket(); + } +} 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 2fbbdedbae1..266e06fefed 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 @@ -130,4 +130,13 @@ public interface ICEditorActionDefinitionIds extends ITextEditorActionDefinition * (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 + * (value "org.eclipse.cdt.ui.edit.text.c.goto.matching.bracket"). + * + * @since 3.0 + */ + public static final String GOTO_MATCHING_BRACKET= "org.eclipse.cdt.ui.edit.text.c.goto.matching.bracket"; //$NON-NLS-1$ + }