From 8bc761d4c632a8a5da5e8d789183a52c007b0e5c Mon Sep 17 00:00:00 2001 From: Anton Leherbauer Date: Mon, 11 Dec 2006 15:57:09 +0000 Subject: [PATCH] Stabilize synchronization between editor and outline view --- .../internal/core/model/CModelBuilder2.java | 8 ++-- .../ui/editor/CContentOutlinePage.java | 42 +++---------------- 2 files changed, 10 insertions(+), 40 deletions(-) diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CModelBuilder2.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CModelBuilder2.java index 3d7961b9e9e..20adeee67d6 100644 --- a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CModelBuilder2.java +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CModelBuilder2.java @@ -515,9 +515,9 @@ public class CModelBuilder2 implements IContributedModelBuilder { for (int i= 0; i < declarators.length; i++) { final IASTDeclarator declarator= declarators[i]; final CElement element= createSimpleDeclaration(parent, declSpecifier, declarator, isTemplate); -// if (!isTemplate && element instanceof SourceManipulation) { -// setBodyPosition((SourceManipulation)element, declaration); -// } + if (!isTemplate && element instanceof SourceManipulation && i > 0) { + setBodyPosition((SourceManipulation)element, declarator); + } elements[i]= element; } } else { @@ -874,7 +874,7 @@ public class CModelBuilder2 implements IContributedModelBuilder { // set positions setIdentifierPosition(element, astVariableName); if (!isTemplate) { - setBodyPosition(element, declarator); + setBodyPosition(element, specifier.getParent()); } return element; } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CContentOutlinePage.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CContentOutlinePage.java index e652a3c17f3..aafd4a5820a 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CContentOutlinePage.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CContentOutlinePage.java @@ -52,9 +52,6 @@ import org.eclipse.ui.views.navigator.LocalSelectionTransfer; import org.eclipse.cdt.core.model.CModelException; import org.eclipse.cdt.core.model.ICElement; -import org.eclipse.cdt.core.model.IParent; -import org.eclipse.cdt.core.model.ISourceRange; -import org.eclipse.cdt.core.model.ISourceReference; import org.eclipse.cdt.core.model.ITranslationUnit; import org.eclipse.cdt.refactoring.actions.CRefactoringActionGroup; import org.eclipse.cdt.ui.CUIPlugin; @@ -244,7 +241,7 @@ public class CContentOutlinePage extends Page implements IContentOutlinePage, IS /** - * Sets the selected element to the one at the current line in the editor. + * Sets the selected element to the one at the current cursor position in the editor. */ public void synchronizeSelectionWithEditor() { if(fInput == null || fEditor == null || fTreeViewer == null) @@ -258,43 +255,16 @@ public class CContentOutlinePage extends Page implements IContentOutlinePage, IS ICElement editorElement; try { - editorElement = getElementAtOffset(fInput, offset); + editorElement = fInput.getElementAtOffset(offset); } catch (CModelException e) { return; } - IStructuredSelection selection = (editorElement == null) - ? StructuredSelection.EMPTY - : new StructuredSelection(editorElement); - - fTreeViewer.setSelection(selection, true); - } - - - /** - * Returns the smallest element at the given offset. - */ - private static ICElement getElementAtOffset(ICElement element, int offset) throws CModelException { - ISourceRange range = ((ISourceReference)element).getSourceRange(); - int start = range.getStartPos(); - int end = start + range.getLength(); - - ICElement closest = null; - - if((offset >= start && offset <= end) || element instanceof ITranslationUnit) { - closest = element; - if(element instanceof IParent) { - ICElement[] children = ((IParent)element).getChildren(); - for(int i = 0; i < children.length; i++) { - ICElement found = getElementAtOffset(children[i], offset); - if(found != null) - closest = found; - } - } + if (editorElement != null) { + IStructuredSelection selection = new StructuredSelection(editorElement); + fTreeViewer.setSelection(selection, true); } - return closest; - } - + } /** * called to create the context menu of the outline