source: trunk/org.modelica.mdt.breakpoint/src/org/modelica/mdt/breakpoint/MDTBreakpointAdapter.java @ 791

Last change on this file since 791 was 791, checked in by adeas31, 13 years ago
  • Added new bundle org.modelica.mdt.breakpoint to avoid cycles.
  • Added GDB Debugger support.
  • New GDB Specific launch configuration.
File size: 10.2 KB
Line 
1/*******************************************************************************
2 * Copyright (c) 2005 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 *     IBM Corporation - initial API and implementation
10 *     Bjorn Freeman-Benson - initial API and implementation
11 *******************************************************************************/
12package org.modelica.mdt.breakpoint;
13
14import org.eclipse.core.resources.IResource;
15import org.eclipse.core.runtime.CoreException;
16import org.eclipse.debug.core.DebugPlugin;
17import org.eclipse.debug.core.model.IBreakpoint;
18import org.eclipse.debug.core.model.ILineBreakpoint;
19import org.eclipse.debug.ui.actions.IToggleBreakpointsTargetExtension;
20import org.eclipse.jface.text.BadLocationException;
21import org.eclipse.jface.text.IDocument;
22import org.eclipse.jface.text.IRegion;
23import org.eclipse.jface.text.ITextSelection;
24import org.eclipse.jface.viewers.ISelection;
25import org.eclipse.ui.IWorkbenchPart;
26import org.eclipse.ui.texteditor.IDocumentProvider;
27import org.eclipse.ui.texteditor.ITextEditor;
28import org.modelica.mdt.debug.core.MDTDebugCorePlugin;
29import org.modelica.mdt.debug.core.breakpoints.MDTLineBreakpoint;
30import org.modelica.mdt.debug.core.breakpoints.MDTWatchpoint;
31
32/**
33 * Adapter to create breakpoints in MDT files.
34 */
35public class MDTBreakpointAdapter implements IToggleBreakpointsTargetExtension {
36    /* (non-Javadoc)
37     * @see org.eclipse.debug.ui.actions.IToggleBreakpointsTarget#toggleLineBreakpoints(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
38     */
39    public void toggleLineBreakpoints(IWorkbenchPart part, ISelection selection) throws CoreException {
40        ITextEditor textEditor = getEditor(part);
41        if (textEditor != null) {
42            IResource resource = (IResource) textEditor.getEditorInput().getAdapter(IResource.class);
43            ITextSelection textSelection = (ITextSelection) selection;
44            int lineNumber = textSelection.getStartLine();
45            IBreakpoint[] breakpoints = DebugPlugin.getDefault().getBreakpointManager().getBreakpoints(MDTDebugCorePlugin.ID_MDT_DEBUG_MODEL);
46            for (int i = 0; i < breakpoints.length; i++) {
47                IBreakpoint breakpoint = breakpoints[i];
48                if (breakpoint instanceof ILineBreakpoint && resource.equals(breakpoint.getMarker().getResource())) {
49                    if (((ILineBreakpoint)breakpoint).getLineNumber() == (lineNumber + 1)) {
50                        // remove
51                        breakpoint.delete();
52                        return;
53                    }
54                }
55            }
56            // create line breakpoint (doc line numbers start at 0)
57            MDTLineBreakpoint lineBreakpoint = new MDTLineBreakpoint(resource, lineNumber + 1);
58            DebugPlugin.getDefault().getBreakpointManager().addBreakpoint(lineBreakpoint);
59        }
60    }
61    /* (non-Javadoc)
62     * @see org.eclipse.debug.ui.actions.IToggleBreakpointsTarget#canToggleLineBreakpoints(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
63     */
64    public boolean canToggleLineBreakpoints(IWorkbenchPart part, ISelection selection) {
65        return getEditor(part) != null;
66    }
67   
68    /**
69     * Returns the editor being used to edit a MDT file, associated with the
70     * given part, or <code>null</code> if none.
71     * 
72     * @param part workbench part
73     * @return the editor being used to edit a MDT file, associated with the
74     * given part, or <code>null</code> if none
75     */
76    private ITextEditor getEditor(IWorkbenchPart part) {
77        if (part instanceof ITextEditor) {
78            ITextEditor editorPart = (ITextEditor) part;
79            IResource resource = (IResource) editorPart.getEditorInput().getAdapter(IResource.class);
80            if (resource != null) {
81                String extension = resource.getFileExtension();
82                if (extension != null && extension.equals("mo")) {
83                    return editorPart;
84                }
85            }
86        }
87        return null;       
88    }
89   
90    /* (non-Javadoc)
91     * @see org.eclipse.debug.ui.actions.IToggleBreakpointsTarget#toggleMethodBreakpoints(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
92     */
93    public void toggleMethodBreakpoints(IWorkbenchPart part, ISelection selection) throws CoreException {
94    }
95    /* (non-Javadoc)
96     * @see org.eclipse.debug.ui.actions.IToggleBreakpointsTarget#canToggleMethodBreakpoints(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
97     */
98    public boolean canToggleMethodBreakpoints(IWorkbenchPart part, ISelection selection) {
99        return false;
100    }
101    /* (non-Javadoc)
102     * @see org.eclipse.debug.ui.actions.IToggleBreakpointsTarget#toggleWatchpoints(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
103     */
104    public void toggleWatchpoints(IWorkbenchPart part, ISelection selection) throws CoreException {
105        String[] variableAndFunctionName = getVariableAndFunctionName(part, selection);
106        if (variableAndFunctionName != null && part instanceof ITextEditor && selection instanceof ITextSelection) {
107            ITextEditor editorPart = (ITextEditor)part;
108            int lineNumber = ((ITextSelection)selection).getStartLine();
109            IResource resource = (IResource) editorPart.getEditorInput().getAdapter(IResource.class);
110            String var = variableAndFunctionName[0];
111            String fcn = variableAndFunctionName[1];
112            // look for existing watchpoint to delete
113            IBreakpoint[] breakpoints = DebugPlugin.getDefault().getBreakpointManager().getBreakpoints(MDTDebugCorePlugin.ID_MDT_DEBUG_MODEL);
114            for (int i = 0; i < breakpoints.length; i++) {
115                IBreakpoint breakpoint = breakpoints[i];
116                if (breakpoint instanceof MDTWatchpoint && resource.equals(breakpoint.getMarker().getResource())) {
117                    MDTWatchpoint watchpoint = (MDTWatchpoint)breakpoint;
118                    String otherVar = watchpoint.getVariableName();
119                    String otherFcn = watchpoint.getFunctionName();
120                    if (otherVar.equals(var) && otherFcn.equals(fcn)) {
121                        breakpoint.delete();
122                        return;
123                    }
124                }
125            }
126            // create watchpoint
127            MDTWatchpoint watchpoint = new MDTWatchpoint(resource, lineNumber + 1, fcn, var, true, true);
128            DebugPlugin.getDefault().getBreakpointManager().addBreakpoint(watchpoint);         
129        }
130    }
131    /* (non-Javadoc)
132     * @see org.eclipse.debug.ui.actions.IToggleBreakpointsTarget#canToggleWatchpoints(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
133     */
134    public boolean canToggleWatchpoints(IWorkbenchPart part, ISelection selection) {
135        return getVariableAndFunctionName(part, selection) != null;
136    }
137   
138    /**
139     * Returns the variable and function names at the current line, or <code>null</code> if none.
140     *
141     * @param part text editor
142     * @param selection text selection
143     * @return the variable and function names at the current line, or <code>null</code> if none.
144     *  The array has two elements, the first is the variable name, the second is the function name.
145     */
146    private String[] getVariableAndFunctionName(IWorkbenchPart part, ISelection selection) {
147        ITextEditor editor = getEditor(part);
148        if (editor != null && selection instanceof ITextSelection) {
149            ITextSelection textSelection = (ITextSelection) selection;
150            IDocumentProvider documentProvider = editor.getDocumentProvider();
151            try {
152                documentProvider.connect(this);
153                IDocument document = documentProvider.getDocument(editor.getEditorInput());
154                IRegion region = document.getLineInformationOfOffset(textSelection.getOffset());
155                String string = document.get(region.getOffset(), region.getLength()).trim();
156                if (string.startsWith("var ")) {
157                    String varName = string.substring(4).trim(); 
158                    String fcnName = getFunctionName(document, varName, document.getLineOfOffset(textSelection.getOffset()));
159                    return new String[] {varName, fcnName};
160                }
161            } catch (CoreException e) {
162            } catch (BadLocationException e) {
163            } finally {
164                documentProvider.disconnect(this);
165            }
166        }       
167        return null;
168    }
169   
170    /**
171     * Returns the name of the function containing the given variable defined at the given
172     * line number in the specified document.
173     *
174     * @param document MDT source file
175     * @param varName variable name
176     * @param line line numbner at which the variable is defined
177     * @return name of function defining the variable
178     */
179    private String getFunctionName(IDocument document, String varName, int line) {
180        // This is a simple guess at the function name - look for the labels preceeding
181        // the variable definition, and then see if there are any 'calls' to that
182        // label. If none, assumet the variable is in the "_main_" function
183        String source = document.get();
184        int lineIndex = line - 1;
185        while (lineIndex >= 0) {
186            try {
187                IRegion information = document.getLineInformation(lineIndex);
188                String lineText = document.get(information.getOffset(), information.getLength());
189                if (lineText.startsWith(":")) {
190                    String label = lineText.substring(1);
191                    if (source.indexOf("call " + label) >= 0) {
192                        return label;
193                    }
194                }
195                lineIndex--;
196            } catch (BadLocationException e) {
197            }
198        }
199        return "_main_";
200    }
201   
202    /* (non-Javadoc)
203     * @see org.eclipse.debug.ui.actions.IToggleBreakpointsTargetExtension#toggleBreakpoints(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
204     */
205    public void toggleBreakpoints(IWorkbenchPart part, ISelection selection) throws CoreException {
206        if (canToggleWatchpoints(part, selection)) {
207            toggleWatchpoints(part, selection);
208        } else {
209            toggleLineBreakpoints(part, selection);
210        }   
211    }
212   
213    /* (non-Javadoc)
214     * @see org.eclipse.debug.ui.actions.IToggleBreakpointsTargetExtension#canToggleBreakpoints(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
215     */
216    public boolean canToggleBreakpoints(IWorkbenchPart part, ISelection selection) {
217        return canToggleLineBreakpoints(part, selection) || canToggleWatchpoints(part, selection);
218    }
219}
Note: See TracBrowser for help on using the repository browser.