From d535cea64cf62284e89d958d57108a835cb9521a Mon Sep 17 00:00:00 2001 From: Alena Laskavaia Date: Fri, 13 Aug 2010 01:01:13 +0000 Subject: [PATCH] Bug 321881 - CDT editor should have selection expansion - contribution from Tomasz Wesolowski --- core/org.eclipse.cdt.ui/plugin.properties | 11 +- core/org.eclipse.cdt.ui/plugin.xml | 97 ++++++++++++++ .../StructureSelectEnclosingAction.java | 72 +++++++++++ .../actions/StructureSelectHistoryAction.java | 41 ++++++ .../ui/actions/StructureSelectNextAction.java | 80 ++++++++++++ .../StructureSelectPreviousAction.java | 80 ++++++++++++ .../ui/actions/StructureSelectionAction.java | 120 ++++++++++++++++++ .../cdt/internal/ui/editor/CEditor.java | 37 +++++- .../ui/editor/CEditorActionContributor.java | 35 ++++- .../internal/ui/editor/CEditorMessages.java | 1 + .../ui/editor/CEditorMessages.properties | 1 + .../ConstructedCEditorMessages.properties | 16 +++ .../editor/ICEditorActionDefinitionIds.java | 25 ++++ .../internal/ui/editor/SelectionHistory.java | 74 +++++++++++ 14 files changed, 685 insertions(+), 5 deletions(-) create mode 100644 core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/StructureSelectEnclosingAction.java create mode 100644 core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/StructureSelectHistoryAction.java create mode 100644 core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/StructureSelectNextAction.java create mode 100644 core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/StructureSelectPreviousAction.java create mode 100644 core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/StructureSelectionAction.java create mode 100644 core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/SelectionHistory.java diff --git a/core/org.eclipse.cdt.ui/plugin.properties b/core/org.eclipse.cdt.ui/plugin.properties index 68ac02e3987..a5fe7832684 100644 --- a/core/org.eclipse.cdt.ui/plugin.properties +++ b/core/org.eclipse.cdt.ui/plugin.properties @@ -12,6 +12,7 @@ # Sergey Prigogin (Google) # QNX Software Systems - [272416] Rework the working set configurations # Axel Mueller - [289339] Surround with +# Tomasz Wesolowski ############################################################################### pluginName=C/C++ Development Tools UI providerName=Eclipse CDT @@ -577,4 +578,12 @@ excluded-file.name = C/C++ Files Excluded from Build templatesViewName= Templates deleteConfigsCommand.name = Reset to Default -excludeCommand.name = Exclude from Build \ No newline at end of file +excludeCommand.name = Exclude from Build +ActionDefinition.selectEnclosing.description = Expand the selection to enclosing C/C++ element +ActionDefinition.selectEnclosing.name = Select Enclosing C/C++ Element +ActionDefinition.selectNext.description = Expand the selection to next C/C++ element +ActionDefinition.selectNext.name = Select Next C/C++ Element +ActionDefinition.selectPrevious.description = Expand the selection to enclosing C/C++ element +ActionDefinition.selectPrevious.name = Select Previous C/C++ Element +ActionDefinition.selectLast.description = Restore last selection in C/C++ editor +ActionDefinition.selectLast.name = Restore Last C/C++ Selection \ No newline at end of file diff --git a/core/org.eclipse.cdt.ui/plugin.xml b/core/org.eclipse.cdt.ui/plugin.xml index 47996073534..bd268dc27ea 100644 --- a/core/org.eclipse.cdt.ui/plugin.xml +++ b/core/org.eclipse.cdt.ui/plugin.xml @@ -1628,6 +1628,79 @@ + + + + + + + + + + + + + + + + + + + + + + + + + = 0) { + IASTNode prevNode = children[firstSelectedChildIndex-1]; + int endingOffset = current.getStartPos() + current.getLength(); + return new SourceRange(prevNode.getFileLocation().getNodeOffset(), endingOffset - prevNode.getFileLocation().getNodeOffset()); + } + return null; + } + +} diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/StructureSelectionAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/StructureSelectionAction.java new file mode 100644 index 00000000000..3266dbb844c --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/StructureSelectionAction.java @@ -0,0 +1,120 @@ +/******************************************************************************* + * Copyright (c) 2010 Tomasz Wesolowski and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Tomasz Wesolowski - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.internal.ui.actions; + +import java.util.ResourceBundle; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.jface.text.ITextSelection; +import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.texteditor.ITextEditor; +import org.eclipse.ui.texteditor.TextEditorAction; + +import org.eclipse.cdt.core.dom.ast.IASTFileLocation; +import org.eclipse.cdt.core.dom.ast.IASTNode; +import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; +import org.eclipse.cdt.core.model.ILanguage; +import org.eclipse.cdt.core.model.ISourceRange; +import org.eclipse.cdt.core.model.ITranslationUnit; +import org.eclipse.cdt.ui.CDTUITools; +import org.eclipse.cdt.ui.text.SharedASTJob; + +import org.eclipse.cdt.internal.core.model.ext.SourceRange; + +import org.eclipse.cdt.internal.ui.editor.CEditor; +import org.eclipse.cdt.internal.ui.editor.SelectionHistory; + +public abstract class StructureSelectionAction extends TextEditorAction { + + protected final SelectionHistory history; + + protected StructureSelectionAction(ResourceBundle bundle, String prefix, ITextEditor editor, SelectionHistory history) { + super(bundle, prefix, editor); + this.history = history; + } + + private final class ExpandSelectionJob extends SharedASTJob { + + public ISourceRange newSourceRange; + private SourceRange currentSourceRange; + + private ExpandSelectionJob(String name, ITranslationUnit tUnit, CEditor cEditor, SourceRange range) { + super(name, tUnit); + currentSourceRange = range; + newSourceRange = null; + } + + @Override + public IStatus runOnAST(ILanguage lang, IASTTranslationUnit ast) throws CoreException { + newSourceRange = doExpand(ast,currentSourceRange); + return Status.OK_STATUS; + } + } + + public static final String ENCLOSING = "StructureSelectEnclosing"; //$NON-NLS-1$ + public static final String NEXT = "StructureSelectNext"; //$NON-NLS-1$ + public static final String PREVIOUS = "StructureSelectPrevious"; //$NON-NLS-1$ + public static final String HISTORY = "StructureSelectHistory"; //$NON-NLS-1$ + + @Override + public void run() { + + IEditorPart editorPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor(); + if (!(editorPart instanceof CEditor)) { + return; + } + final CEditor cEditor = (CEditor) editorPart; + + ITranslationUnit tu = (ITranslationUnit) CDTUITools.getEditorInputCElement(cEditor.getEditorInput()); + + ITextSelection selection; + try { + selection = (ITextSelection) cEditor.getSelectionProvider().getSelection(); + } catch (ClassCastException e) { + return; + } + + final int offset = selection.getOffset(); + final int length = selection.getLength(); + + ExpandSelectionJob expandSelectionJob = new ExpandSelectionJob("expand selection", tu, cEditor, new SourceRange(offset, length)); //$NON-NLS-1$ + + expandSelectionJob.schedule(); + try { + expandSelectionJob.join(); + } catch (InterruptedException e) { + return; + } + + if (expandSelectionJob.newSourceRange != null) { + history.ignoreSelectionChanges(); + cEditor.setSelection(expandSelectionJob.newSourceRange, true); + history.listenToSelectionChanges(); + } + } + + protected abstract ISourceRange doExpand(IASTTranslationUnit ast, SourceRange currentSourceRange); + + protected boolean nodeContains(IASTNode node, int position) { + IASTFileLocation fl = node.getFileLocation(); + return (position >= fl.getNodeOffset() && position <= fl.getNodeOffset() + fl.getNodeLength()); + } + + protected boolean samePosition(IASTNode node, SourceRange current) { + IASTFileLocation fl = node.getFileLocation(); + return (fl.getNodeOffset() == current.getStartPos() && fl.getNodeLength() == current.getLength()); + } + + +} 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 5317723f0e3..e8c3e12b244 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 @@ -87,10 +87,10 @@ import org.eclipse.jface.text.formatter.IFormattingContext; import org.eclipse.jface.text.link.ILinkedModeListener; import org.eclipse.jface.text.link.LinkedModeModel; import org.eclipse.jface.text.link.LinkedModeUI; -import org.eclipse.jface.text.link.LinkedPosition; -import org.eclipse.jface.text.link.LinkedPositionGroup; import org.eclipse.jface.text.link.LinkedModeUI.ExitFlags; import org.eclipse.jface.text.link.LinkedModeUI.IExitPolicy; +import org.eclipse.jface.text.link.LinkedPosition; +import org.eclipse.jface.text.link.LinkedPositionGroup; import org.eclipse.jface.text.source.Annotation; import org.eclipse.jface.text.source.IAnnotationModel; import org.eclipse.jface.text.source.IAnnotationModelExtension; @@ -198,15 +198,20 @@ import org.eclipse.cdt.internal.ui.CPluginImages; import org.eclipse.cdt.internal.ui.ICHelpContextIds; import org.eclipse.cdt.internal.ui.IContextMenuConstants; import org.eclipse.cdt.internal.ui.actions.AddBlockCommentAction; +import org.eclipse.cdt.internal.ui.actions.StructureSelectHistoryAction; +import org.eclipse.cdt.internal.ui.actions.StructureSelectEnclosingAction; +import org.eclipse.cdt.internal.ui.actions.StructureSelectNextAction; +import org.eclipse.cdt.internal.ui.actions.StructureSelectPreviousAction; import org.eclipse.cdt.internal.ui.actions.FindWordAction; import org.eclipse.cdt.internal.ui.actions.FoldingActionGroup; 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.StructureSelectionAction; 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.OccurrencesFinder; import org.eclipse.cdt.internal.ui.search.actions.SelectionSearchGroup; import org.eclipse.cdt.internal.ui.text.CHeuristicScanner; import org.eclipse.cdt.internal.ui.text.CPairMatcher; @@ -1304,6 +1309,8 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IC private CTemplatesPage fTemplatesPage; + private SelectionHistory fSelectionHistory; + private static final Set angularIntroducers = new HashSet(); static { angularIntroducers.add("template"); //$NON-NLS-1$ @@ -2093,6 +2100,11 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IC fEditorSelectionChangedListener.uninstall(getSelectionProvider()); fEditorSelectionChangedListener = null; } + + if (fSelectionHistory != null) { + fSelectionHistory.dispose(); + fSelectionHistory = null; + } super.dispose(); } @@ -2238,6 +2250,25 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IC action.setActionDefinitionId(ICEditorActionDefinitionIds.OPEN_QUICK_MACRO_EXPLORER); setAction("OpenMacroExplorer", action); //$NON-NLS-1$*/ + fSelectionHistory = new SelectionHistory(this); + + action = new StructureSelectEnclosingAction(bundle, this, fSelectionHistory); + action.setActionDefinitionId(ICEditorActionDefinitionIds.SELECT_ENCLOSING); + setAction(StructureSelectionAction.ENCLOSING, action); + + action = new StructureSelectNextAction(bundle, this, fSelectionHistory); + action.setActionDefinitionId(ICEditorActionDefinitionIds.SELECT_NEXT); + setAction(StructureSelectionAction.NEXT, action); + + action = new StructureSelectPreviousAction(bundle, this, fSelectionHistory); + action.setActionDefinitionId(ICEditorActionDefinitionIds.SELECT_PREVIOUS); + setAction(StructureSelectionAction.PREVIOUS, action); + + action = new StructureSelectHistoryAction(bundle, this, fSelectionHistory); + action.setActionDefinitionId(ICEditorActionDefinitionIds.SELECT_LAST); + setAction(StructureSelectionAction.HISTORY, action); + + // Assorted action groupings fSelectionSearchGroup = createSelectionSearchGroup(); fTextSearchGroup= new TextSearchGroup(this); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditorActionContributor.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditorActionContributor.java index 503753495ed..1338d9d257a 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditorActionContributor.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditorActionContributor.java @@ -10,6 +10,7 @@ * QNX Software System * Markus Schorn (Wind River Systems) * Anton Leherbauer (Wind River Systems) + * Tomasz Wesolowski *******************************************************************************/ package org.eclipse.cdt.internal.ui.editor; @@ -17,6 +18,7 @@ import java.util.ResourceBundle; import org.eclipse.jface.action.IAction; import org.eclipse.jface.action.IMenuManager; +import org.eclipse.jface.action.MenuManager; import org.eclipse.jface.action.Separator; import org.eclipse.ui.IActionBars; import org.eclipse.ui.IEditorPart; @@ -31,6 +33,11 @@ import org.eclipse.ui.texteditor.RetargetTextEditorAction; import org.eclipse.cdt.ui.actions.CdtActionConstants; import org.eclipse.cdt.internal.ui.IContextMenuConstants; +import org.eclipse.cdt.internal.ui.actions.StructureSelectHistoryAction; +import org.eclipse.cdt.internal.ui.actions.StructureSelectNextAction; +import org.eclipse.cdt.internal.ui.actions.StructureSelectPreviousAction; +import org.eclipse.cdt.internal.ui.actions.StructureSelectionAction; +import org.eclipse.cdt.internal.ui.actions.StructureSelectEnclosingAction; import org.eclipse.cdt.internal.ui.actions.FindWordAction; import org.eclipse.cdt.internal.ui.actions.GoToNextPreviousMemberAction; import org.eclipse.cdt.internal.ui.actions.GotoNextBookmarkAction; @@ -51,6 +58,10 @@ public class CEditorActionContributor extends TextEditorActionContributor { private RetargetTextEditorAction fToggleSourceHeader; private ToggleMarkOccurrencesAction fToggleMarkOccurrencesAction; private RetargetTextEditorAction fFindWord; + private RetargetTextEditorAction fExpandSelectionToEnclosing; + private RetargetTextEditorAction fExpandSelectionToNext; + private RetargetTextEditorAction fExpandSelectionToPrevious; + private RetargetTextEditorAction fExpandSelectionToHistory; public CEditorActionContributor() { super(); @@ -93,6 +104,15 @@ public class CEditorActionContributor extends TextEditorActionContributor { fFindWord = new RetargetTextEditorAction(bundle, "FindWord."); //$NON-NLS-1$ fFindWord.setActionDefinitionId(ICEditorActionDefinitionIds.FIND_WORD); + + fExpandSelectionToEnclosing = new RetargetTextEditorAction(bundle, StructureSelectEnclosingAction.PREFIX); + fExpandSelectionToEnclosing.setActionDefinitionId(ICEditorActionDefinitionIds.SELECT_ENCLOSING); + fExpandSelectionToNext= new RetargetTextEditorAction(bundle, StructureSelectNextAction.PREFIX); + fExpandSelectionToNext.setActionDefinitionId(ICEditorActionDefinitionIds.SELECT_NEXT); + fExpandSelectionToPrevious= new RetargetTextEditorAction(bundle, StructureSelectPreviousAction.PREFIX); + fExpandSelectionToPrevious.setActionDefinitionId(ICEditorActionDefinitionIds.SELECT_PREVIOUS); + fExpandSelectionToHistory= new RetargetTextEditorAction(bundle, StructureSelectHistoryAction.PREFIX); + fExpandSelectionToHistory.setActionDefinitionId(ICEditorActionDefinitionIds.SELECT_LAST); } /* @@ -118,6 +138,15 @@ public class CEditorActionContributor extends TextEditorActionContributor { // editMenu.appendToGroup(ITextEditorActionConstants.GROUP_GENERATE, new Separator()); editMenu.appendToGroup(IContextMenuConstants.GROUP_ADDITIONS, fToggleInsertModeAction); + + { + MenuManager structureSelection = new MenuManager(CEditorMessages.CEditorActionContributor_ExpandSelectionMenu_label,"expandSelection"); //$NON-NLS-1$ + editMenu.insertAfter(ITextEditorActionConstants.SELECT_ALL, structureSelection); + structureSelection.add(fExpandSelectionToEnclosing); + structureSelection.add(fExpandSelectionToNext); + structureSelection.add(fExpandSelectionToPrevious); + structureSelection.add(fExpandSelectionToHistory); + } } IMenuManager navigateMenu= menu.findMenuUsingPath(IWorkbenchActionConstants.M_NAVIGATE); @@ -135,7 +164,6 @@ public class CEditorActionContributor extends TextEditorActionContributor { gotoMenu.appendToGroup("additions2", fGotoNextBookmark); //$NON-NLS-1$ } } - } /** @@ -183,6 +211,11 @@ public class CEditorActionContributor extends TextEditorActionContributor { fToggleSourceHeader.setAction(getAction(textEditor, "ToggleSourceHeader")); //$NON-NLS-1$ fToggleInsertModeAction.setAction(getAction(textEditor, ITextEditorActionConstants.TOGGLE_INSERT_MODE)); fFindWord.setAction(getAction(textEditor, FindWordAction.FIND_WORD)); + + fExpandSelectionToEnclosing.setAction(getAction(textEditor, StructureSelectionAction.ENCLOSING)); + fExpandSelectionToNext.setAction(getAction(textEditor, StructureSelectionAction.NEXT)); + fExpandSelectionToPrevious.setAction(getAction(textEditor, StructureSelectionAction.PREVIOUS)); + fExpandSelectionToHistory.setAction(getAction(textEditor, StructureSelectionAction.HISTORY)); // Source menu. IActionBars bars= getActionBars(); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditorMessages.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditorMessages.java index 4d55ab6110b..70b63ae18c3 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditorMessages.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditorMessages.java @@ -95,6 +95,7 @@ public final class CEditorMessages extends NLS { public static String SemanticHighlighting_problem; public static String SemanticHighlighting_externalSDK; public static String CEditor_markOccurrences_job_name; + public static String CEditorActionContributor_ExpandSelectionMenu_label; static { NLS.initializeMessages(CEditorMessages.class.getName(), CEditorMessages.class); 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 4c61694a5df..795fc19fb3b 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 @@ -92,3 +92,4 @@ SemanticHighlighting_problem= Problems SemanticHighlighting_externalSDK= External SDK calls CEditor_markOccurrences_job_name= Occurrences Marker +CEditorActionContributor_ExpandSelectionMenu_label=E&xpand Selection To diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/ConstructedCEditorMessages.properties b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/ConstructedCEditorMessages.properties index c14964f394e..5a0345ca611 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/ConstructedCEditorMessages.properties +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/ConstructedCEditorMessages.properties @@ -133,6 +133,22 @@ CSelectAnnotationRulerAction.GotoAnnotation.tooltip= Go to Annotation CSelectAnnotationRulerAction.GotoAnnotation.description= Selects the annotation in the editor CSelectAnnotationRulerAction.GotoAnnotation.image= +StructureSelectNext.label=&Next Element +StructureSelectNext.tooltip=Expand Selection to Include Next Sibling +StructureSelectNext.description=Expand selection to include next sibling + +StructureSelectPrevious.label=&Previous Element +StructureSelectPrevious.tooltip=Expand Selection to Include Previous Sibling +StructureSelectPrevious.description=Expand selection to include previous sibling + +StructureSelectEnclosing.label=&Enclosing Element +StructureSelectEnclosing.tooltip=Expand Selection to Include Enclosing Element +StructureSelectEnclosing.description=Expand selection to include enclosing element + +StructureSelectHistory.label=&Restore Last Selection +StructureSelectHistory.tooltip=Restore Last Selection +StructureSelectHistory.description=Restore last selection + CSelectAnnotationRulerAction.OpenSuperImplementation.label= &Open Super Implementation CSelectAnnotationRulerAction.OpenSuperImplementation.tooltip= Open Super Implementation CSelectAnnotationRulerAction.OpenSuperImplementation.description= Opens the super implementation 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 c4ddd29b1fe..887c69a1afd 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 @@ -11,6 +11,7 @@ * Markus Schorn (Wind River Systems) * Anton Leherbauer (Wind River Systems) * Sergey Prigogin (Google) + * Tomasz Wesolowski *******************************************************************************/ package org.eclipse.cdt.internal.ui.editor; @@ -247,4 +248,28 @@ public interface ICEditorActionDefinitionIds extends ITextEditorActionDefinition * @since 5.2 */ public static final String SORT_LINES = "org.eclipse.cdt.ui.edit.text.c.sort.lines"; //$NON-NLS-1$ + + /** + * Action definition ID of the edit -> select enclosing action + * (value "org.eclipse.cdt.ui.edit.text.c.select.enclosing"). + */ + public static final String SELECT_ENCLOSING = "org.eclipse.cdt.ui.edit.text.c.select.enclosing"; //$NON-NLS-1$ + + /** + * Action definition ID of the edit -> select next action + * (value "org.eclipse.cdt.ui.edit.text.c.select.next"). + */ + public static final String SELECT_NEXT = "org.eclipse.cdt.ui.edit.text.c.select.next"; //$NON-NLS-1$ + + /** + * Action definition ID of the edit -> select previous action + * (value "org.eclipse.cdt.ui.edit.text.c.select.previous"). + */ + public static final String SELECT_PREVIOUS = "org.eclipse.cdt.ui.edit.text.c.select.previous"; //$NON-NLS-1$ + + /** + * Action definition ID of the edit -> select restore last action + * (value "org.eclipse.cdt.ui.edit.text.c.select.last"). + */ + public static final String SELECT_LAST = "org.eclipse.cdt.ui.edit.text.c.select.last"; //$NON-NLS-1$ } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/SelectionHistory.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/SelectionHistory.java new file mode 100644 index 00000000000..8f196837588 --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/SelectionHistory.java @@ -0,0 +1,74 @@ +/******************************************************************************* + * Copyright (c) 2000, 2010 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation (JDT) + * Tomasz Wesolowski - port from JDT + *******************************************************************************/ +package org.eclipse.cdt.internal.ui.editor; + +import java.util.Stack; + +import org.eclipse.core.runtime.Assert; +import org.eclipse.jface.viewers.ISelectionChangedListener; +import org.eclipse.jface.viewers.SelectionChangedEvent; + +import org.eclipse.cdt.core.model.ISourceRange; + +public class SelectionHistory { + + private Stack fHistory; + private CEditor fEditor; + private ISelectionChangedListener fSelectionListener; + private int fSelectionChangeListenerCounter; + + public SelectionHistory(CEditor editor) { + Assert.isNotNull(editor); + fEditor= editor; + fHistory= new Stack(); + fSelectionListener= new ISelectionChangedListener() { + public void selectionChanged(SelectionChangedEvent event) { + if (fSelectionChangeListenerCounter == 0) + flush(); + } + }; + fEditor.getSelectionProvider().addSelectionChangedListener(fSelectionListener); + } + + public boolean isEmpty() { + return fHistory.isEmpty(); + } + + public void remember(ISourceRange range) { + fHistory.push(range); + } + + public ISourceRange getLast() { + if (isEmpty()) + return null; + ISourceRange result= fHistory.pop(); + return result; + } + + public void flush() { + if (fHistory.isEmpty()) + return; + fHistory.clear(); + } + + public void ignoreSelectionChanges() { + fSelectionChangeListenerCounter++; + } + + public void listenToSelectionChanges() { + fSelectionChangeListenerCounter--; + } + + public void dispose() { + fEditor.getSelectionProvider().removeSelectionChangedListener(fSelectionListener); + } +} \ No newline at end of file