1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-07 17:56:01 +02:00

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
This commit is contained in:
Alain Magloire 2005-02-26 01:42:42 +00:00
parent 14a9f65de9
commit 67adb089c5
7 changed files with 195 additions and 2 deletions

View file

@ -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 2005-02-25 Alain Magloire
Fix PR 86718: Dialog shows twice Fix PR 86718: Dialog shows twice
* src/org/eclipse/cdt/internal/ui/cview/OpenProjectGroup.java * src/org/eclipse/cdt/internal/ui/cview/OpenProjectGroup.java

View file

@ -102,6 +102,9 @@ ActionDefinition.removeBlockComment.description= Remove the block comment enclos
ActionDefinition.format.name=Format ActionDefinition.format.name=Format
ActionDefinition.format.description=Format Source Code 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 CEditor.name=C/C++ Editor
CPluginPreferencePage.name=C/C++ CPluginPreferencePage.name=C/C++

View file

@ -1025,6 +1025,19 @@
command="org.eclipse.cdt.ui.edit.text.c.goto.prev.member" command="org.eclipse.cdt.ui.edit.text.c.goto.prev.member"
configuration="org.eclipse.ui.defaultAcceleratorConfiguration"> configuration="org.eclipse.ui.defaultAcceleratorConfiguration">
</keyBinding> </keyBinding>
<command
name="%ActionDefinition.gotoMatchingBracket.name"
description="%ActionDefinition.gotoMatchingBracket.description"
category="org.eclipse.cdt.ui.category.source"
id="org.eclipse.cdt.ui.edit.text.c.goto.matching.bracket">
</command>
<keyBinding
string="Ctrl+Shift+P"
scope="org.eclipse.cdt.ui.cEditorScope"
command="org.eclipse.cdt.ui.edit.text.c.goto.matching.bracket"
configuration="org.eclipse.ui.defaultAcceleratorConfiguration">
</keyBinding>
</extension> </extension>
<extension <extension
id="org.eclipse.cdt.ui.CSearchPage" id="org.eclipse.cdt.ui.CSearchPage"

View file

@ -54,6 +54,7 @@ public interface ICHelpContextIds {
public static final String LAUNCH_PROPERTY_PAGE = PREFIX + "new_launch_property_page_context"; //$NON-NLS-1$ public static final String LAUNCH_PROPERTY_PAGE = PREFIX + "new_launch_property_page_context"; //$NON-NLS-1$
public static final String PROJECT_PROPERTY_PAGE = PREFIX + "new_project_property_page_context"; //$NON-NLS-1$ public static final String PROJECT_PROPERTY_PAGE = PREFIX + "new_project_property_page_context"; //$NON-NLS-1$
public static final String CODEFORMATTER_PREFERENCE_PAGE = PREFIX + "codeformatter_preference_page_context"; //$NON-NLS-1$ public static final String CODEFORMATTER_PREFERENCE_PAGE = PREFIX + "codeformatter_preference_page_context"; //$NON-NLS-1$
public static final String GOTO_MATCHING_BRACKET_ACTION= PREFIX + "goto_matching_bracket_action"; //$NON-NLS-1$Object[] FORMAT_ALL= null;
public static final String PROJ_CONF_BLOCK = PREFIX + "new_proj_conf_block_context"; //$NON-NLS-1$ public static final String PROJ_CONF_BLOCK = PREFIX + "new_proj_conf_block_context"; //$NON-NLS-1$

View file

@ -44,7 +44,7 @@ import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Preferences; import org.eclipse.core.runtime.Preferences;
import org.eclipse.jface.action.IAction; import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuManager; import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IStatusLineManager; import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.preference.IPreferenceStore; import org.eclipse.jface.preference.IPreferenceStore;
@ -56,11 +56,14 @@ import org.eclipse.jface.text.ILineTracker;
import org.eclipse.jface.text.IRegion; import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextOperationTarget; import org.eclipse.jface.text.ITextOperationTarget;
import org.eclipse.jface.text.ITextSelection; import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.text.ITextViewerExtension5;
import org.eclipse.jface.text.Position; import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.contentassist.ContentAssistant; import org.eclipse.jface.text.contentassist.ContentAssistant;
import org.eclipse.jface.text.contentassist.IContentAssistant; import org.eclipse.jface.text.contentassist.IContentAssistant;
import org.eclipse.jface.text.source.Annotation; import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.IAnnotationModel; import org.eclipse.jface.text.source.IAnnotationModel;
import org.eclipse.jface.text.source.ICharacterPairMatcher;
import org.eclipse.jface.text.source.ISharedTextColors; import org.eclipse.jface.text.source.ISharedTextColors;
import org.eclipse.jface.text.source.ISourceViewer; import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.IVerticalRuler; import org.eclipse.jface.text.source.IVerticalRuler;
@ -77,6 +80,7 @@ import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.custom.StyledText; import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorActionBarContributor; import org.eclipse.ui.IEditorActionBarContributor;
import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IEditorInput;
@ -633,8 +637,11 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IS
fFoldingGroup= new FoldingActionGroup(this, getSourceViewer()); fFoldingGroup= new FoldingActionGroup(this, getSourceViewer());
// Default text editing menu items // Default text editing menu items
Action action= new GotoMatchingBracketAction(this);
action.setActionDefinitionId(ICEditorActionDefinitionIds.GOTO_MATCHING_BRACKET);
setAction(GotoMatchingBracketAction.GOTO_MATCHING_BRACKET, action);
IAction action = new TextOperationAction(CEditorMessages.getResourceBundle(), "Comment.", this, ITextOperationTarget.PREFIX); //$NON-NLS-1$ action = new TextOperationAction(CEditorMessages.getResourceBundle(), "Comment.", this, ITextOperationTarget.PREFIX); //$NON-NLS-1$
action.setActionDefinitionId(ICEditorActionDefinitionIds.COMMENT); action.setActionDefinitionId(ICEditorActionDefinitionIds.COMMENT);
setAction("Comment", action); //$NON-NLS-1$ setAction("Comment", action); //$NON-NLS-1$
markAsStateDependentAction("Comment", true); //$NON-NLS-1$ markAsStateDependentAction("Comment", true); //$NON-NLS-1$
@ -835,6 +842,71 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IS
} }
} }
/**
* Jumps to the matching bracket.
*/
public void gotoMatchingBracket() {
ISourceViewer sourceViewer= getSourceViewer();
IDocument document= sourceViewer.getDocument();
if (document == null)
return;
IRegion selection= getSignedSelection(sourceViewer);
int selectionLength= Math.abs(selection.getLength());
if (selectionLength > 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 * Returns whether the given annotation is configured as a target for the
* "Go to Next/Previous Annotation" actions * "Go to Next/Previous Annotation" actions
@ -1283,6 +1355,53 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IS
statusLine.setMessage(false, msg, null); statusLine.setMessage(false, msg, null);
} }
/**
* Returns the signed current selection.
* The length will be negative if the resulting selection
* is right-to-left (RtoL).
* <p>
* The selection offset is model based.
* </p>
*
* @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) /* (non-Javadoc)
* @see org.eclipse.cdt.internal.ui.editor.IReconcilingParticipant#reconciled() * @see org.eclipse.cdt.internal.ui.editor.IReconcilingParticipant#reconciled()
*/ */

View file

@ -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();
}
}

View file

@ -130,4 +130,13 @@ public interface ICEditorActionDefinitionIds extends ITextEditorActionDefinition
* (value <code>"org.eclipse.cdt.ui.edit.text.c.goto.prev.memeber"</code>) * (value <code>"org.eclipse.cdt.ui.edit.text.c.goto.prev.memeber"</code>)
*/ */
public static final String GOTO_PREVIOUS_MEMBER = "org.eclipse.cdt.ui.edit.text.c.goto.prev.member"; //$NON-NLS-1$ 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 <code>"org.eclipse.cdt.ui.edit.text.c.goto.matching.bracket"</code>).
*
* @since 3.0
*/
public static final String GOTO_MATCHING_BRACKET= "org.eclipse.cdt.ui.edit.text.c.goto.matching.bracket"; //$NON-NLS-1$
} }