1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-02 05:45:58 +02:00

Bug 293634 - Improve ToggleBreakpointAdapter.getCElementFromSelection

This commit is contained in:
Anton Leherbauer 2009-11-06 10:24:34 +00:00
parent 842fa12cbf
commit e80914811e

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2004, 2008 QNX Software Systems and others. * Copyright (c) 2004, 2009 QNX Software Systems and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -38,6 +38,7 @@ import org.eclipse.cdt.debug.internal.ui.views.disassembly.DisassemblyView;
import org.eclipse.cdt.debug.ui.CDebugUIPlugin; import org.eclipse.cdt.debug.ui.CDebugUIPlugin;
import org.eclipse.cdt.debug.ui.disassembly.IElementToggleBreakpointAdapter; import org.eclipse.cdt.debug.ui.disassembly.IElementToggleBreakpointAdapter;
import org.eclipse.cdt.internal.ui.util.ExternalEditorInput; import org.eclipse.cdt.internal.ui.util.ExternalEditorInput;
import org.eclipse.cdt.ui.CDTUITools;
import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot; import org.eclipse.core.resources.IWorkspaceRoot;
@ -250,18 +251,32 @@ public class ToggleBreakpointAdapter implements IToggleBreakpointsTarget {
ITextSelection textSelection = (ITextSelection)selection; ITextSelection textSelection = (ITextSelection)selection;
String text = textSelection.getText(); String text = textSelection.getText();
if ( text != null ) { if ( text != null ) {
IResource resource = getResource( part ); if (part instanceof ITextEditor) {
if ( resource instanceof IFile ) { ICElement editorElement = CDTUITools.getEditorInputCElement(((ITextEditor) part).getEditorInput());
ITranslationUnit tu = getTranslationUnit( (IFile)resource ); if (editorElement instanceof ITranslationUnit) {
if ( tu != null ) { ITranslationUnit tu = (ITranslationUnit) editorElement;
try { try {
ICElement element = tu.getElement( text.trim() ); if (tu.isStructureKnown() && tu.isConsistent()) {
if ( element == null ) { return tu.getElementAtOffset( textSelection.getOffset() );
element = tu.getElementAtLine( textSelection.getStartLine() );
} }
return element; } catch (CModelException exc) {
// ignored on purpose
} }
catch( CModelException e ) { }
} else {
IResource resource = getResource( part );
if ( resource instanceof IFile ) {
ITranslationUnit tu = getTranslationUnit( (IFile)resource );
if ( tu != null ) {
try {
ICElement element = tu.getElement( text.trim() );
if ( element == null ) {
element = tu.getElementAtLine( textSelection.getStartLine() );
}
return element;
}
catch( CModelException e ) {
}
} }
} }
} }
@ -297,33 +312,9 @@ public class ToggleBreakpointAdapter implements IToggleBreakpointsTarget {
} }
protected IVariable getVariableFromSelection( IWorkbenchPart part, ISelection selection ) { protected IVariable getVariableFromSelection( IWorkbenchPart part, ISelection selection ) {
if ( selection instanceof ITextSelection ) { ICElement element = getCElementFromSelection(part, selection);
String text = ((ITextSelection)selection).getText(); if (element instanceof IVariable) {
if ( text != null ) { return (IVariable) element;
IResource resource = getResource( part );
if ( resource instanceof IFile ) {
ITranslationUnit tu = getTranslationUnit( (IFile)resource );
if ( tu != null ) {
try {
ICElement element = tu.getElement( text.trim() );
if (element instanceof IVariable) {
return (IVariable)element;
}
}
catch( CModelException e ) {
}
}
}
}
}
else if ( selection instanceof IStructuredSelection ) {
IStructuredSelection ss = (IStructuredSelection)selection;
if ( ss.size() == 1 ) {
Object selected = ss.getFirstElement();
if (selected instanceof IVariable) {
return (IVariable)selected;
}
}
} }
return null; return null;
} }
@ -451,8 +442,8 @@ public class ToggleBreakpointAdapter implements IToggleBreakpointsTarget {
StringBuffer name = new StringBuffer(); StringBuffer name = new StringBuffer();
String methodName = method.getElementName(); String methodName = method.getElementName();
ICElement parent = method.getParent(); ICElement parent = method.getParent();
while ( parent != null && ( parent.getElementType() == ICElement.C_NAMESPACE || parent.getElementType() == ICElement.C_CLASS ) while ( parent != null && ( parent.getElementType() == ICElement.C_NAMESPACE || parent.getElementType() == ICElement.C_CLASS
|| parent.getElementType() == ICElement.C_STRUCT || parent.getElementType() == ICElement.C_UNION) { || parent.getElementType() == ICElement.C_STRUCT || parent.getElementType() == ICElement.C_UNION ) ) {
name.append( parent.getElementName() ).append( "::" ); //$NON-NLS-1$ name.append( parent.getElementName() ).append( "::" ); //$NON-NLS-1$
parent = parent.getParent(); parent = parent.getParent();
} }