|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2010-2016 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 | + * Genuitec LLC - adapted for TypeScript editor |
| 11 | + *******************************************************************************/ |
| 12 | +package ts.eclipse.ide.jsdt.internal.ui.editor.breakpoints; |
| 13 | + |
| 14 | +import java.util.HashMap; |
| 15 | + |
| 16 | +import org.eclipse.core.resources.IFile; |
| 17 | +import org.eclipse.core.resources.IResource; |
| 18 | +import org.eclipse.core.runtime.CoreException; |
| 19 | +import org.eclipse.core.runtime.IProgressMonitor; |
| 20 | +import org.eclipse.core.runtime.IStatus; |
| 21 | +import org.eclipse.core.runtime.Status; |
| 22 | +import org.eclipse.core.runtime.jobs.Job; |
| 23 | +import org.eclipse.debug.core.DebugPlugin; |
| 24 | +import org.eclipse.debug.core.model.IBreakpoint; |
| 25 | +import org.eclipse.debug.ui.actions.IToggleBreakpointsTargetExtension; |
| 26 | +import org.eclipse.jface.text.BadLocationException; |
| 27 | +import org.eclipse.jface.text.IDocument; |
| 28 | +import org.eclipse.jface.text.IRegion; |
| 29 | +import org.eclipse.jface.text.ITextSelection; |
| 30 | +import org.eclipse.jface.text.TextSelection; |
| 31 | +import org.eclipse.jface.viewers.ISelection; |
| 32 | +import org.eclipse.swt.widgets.Display; |
| 33 | +import org.eclipse.ui.IEditorInput; |
| 34 | +import org.eclipse.ui.IEditorPart; |
| 35 | +import org.eclipse.ui.IWorkbenchPart; |
| 36 | +import org.eclipse.ui.texteditor.IDocumentProvider; |
| 37 | +import org.eclipse.ui.texteditor.IEditorStatusLine; |
| 38 | +import org.eclipse.ui.texteditor.ITextEditor; |
| 39 | +import org.eclipse.wst.jsdt.debug.core.breakpoints.IJavaScriptBreakpoint; |
| 40 | +import org.eclipse.wst.jsdt.debug.core.breakpoints.IJavaScriptLineBreakpoint; |
| 41 | +import org.eclipse.wst.jsdt.debug.core.model.JavaScriptDebugModel; |
| 42 | + |
| 43 | +/** |
| 44 | + * Adapter for toggling JavaScript breakpoints in the TypeScript editor |
| 45 | + */ |
| 46 | +public class ToggleBreakpointAdapter implements IToggleBreakpointsTargetExtension { |
| 47 | + |
| 48 | + public boolean canToggleBreakpoints(IWorkbenchPart part, ISelection selection) { |
| 49 | + return selection instanceof ITextSelection; |
| 50 | + } |
| 51 | + |
| 52 | + public boolean canToggleLineBreakpoints(IWorkbenchPart part, ISelection selection) { |
| 53 | + return selection instanceof ITextSelection; |
| 54 | + } |
| 55 | + |
| 56 | + public boolean canToggleWatchpoints(IWorkbenchPart part, ISelection selection) { |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + public boolean canToggleMethodBreakpoints(IWorkbenchPart part, ISelection selection) { |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + public void toggleWatchpoints(IWorkbenchPart part, ISelection selection) throws CoreException { |
| 65 | + //do nothing |
| 66 | + } |
| 67 | + |
| 68 | + public void toggleMethodBreakpoints(final IWorkbenchPart part, final ISelection selection) throws CoreException { |
| 69 | + //do nothing |
| 70 | + } |
| 71 | + |
| 72 | + public void toggleBreakpoints(IWorkbenchPart part, ISelection selection) throws CoreException { |
| 73 | + if(selection instanceof ITextSelection) { |
| 74 | + ITextEditor textEditor = getTextEditor(part); |
| 75 | + if(textEditor == null) { |
| 76 | + reportToStatusLine(part, "No editor could be found for the associated part"); |
| 77 | + return; |
| 78 | + } |
| 79 | + toggleLineBreakpoint(part, (ITextSelection) selection, ((TextSelection)selection).getStartLine()+1); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public void toggleLineBreakpoints(final IWorkbenchPart part, final ISelection selection) throws CoreException { |
| 84 | + if (!(part instanceof IEditorPart) || !(selection instanceof ITextSelection)) { |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + ITextEditor textEditor = (ITextEditor) part.getAdapter(ITextEditor.class); |
| 89 | + if (textEditor != null) { |
| 90 | + IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput()); |
| 91 | + if (document != null) { |
| 92 | + int lineNumber; |
| 93 | + try { |
| 94 | + lineNumber = document.getLineOfOffset(((ITextSelection) selection).getOffset()); |
| 95 | + IResource res = getResource((IEditorPart) part); |
| 96 | + if (res != null) { |
| 97 | + addBreakpoint(res, document, lineNumber + 1); |
| 98 | + } |
| 99 | + } |
| 100 | + catch (BadLocationException e) { |
| 101 | + e.printStackTrace(); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + void toggleLineBreakpoint(final IWorkbenchPart part, final ITextSelection selection, final int linenumber) { |
| 108 | + Job job = new Job("Toggle Line Breakpoints") { |
| 109 | + protected IStatus run(IProgressMonitor monitor) { |
| 110 | + try { |
| 111 | + ITextEditor editor = getTextEditor(part); |
| 112 | + if(editor != null && part instanceof IEditorPart) { |
| 113 | + IResource resource = getResource((IEditorPart)part); |
| 114 | + if(resource == null) { |
| 115 | + resource = getResource((IEditorPart)part); |
| 116 | + reportToStatusLine(part, "Failed to create Javascript line breakpoint - the resource could no be computed"); |
| 117 | + return Status.CANCEL_STATUS; |
| 118 | + } |
| 119 | + IBreakpoint bp = lineBreakpointExists(resource, linenumber); |
| 120 | + if(bp != null) { |
| 121 | + DebugPlugin.getDefault().getBreakpointManager().removeBreakpoint(bp, true); |
| 122 | + return Status.OK_STATUS; |
| 123 | + } |
| 124 | + IDocumentProvider documentProvider = editor.getDocumentProvider(); |
| 125 | + IDocument document = documentProvider.getDocument(editor.getEditorInput()); |
| 126 | + int charstart = -1, charend = -1; |
| 127 | + try { |
| 128 | + IRegion line = document.getLineInformation(linenumber - 1); |
| 129 | + charstart = line.getOffset(); |
| 130 | + charend = charstart + line.getLength(); |
| 131 | + } |
| 132 | + catch (BadLocationException ble) {} |
| 133 | + HashMap<String, String> attributes = new HashMap<String, String>(); |
| 134 | + attributes.put(IJavaScriptBreakpoint.TYPE_NAME, null); |
| 135 | + attributes.put(IJavaScriptBreakpoint.SCRIPT_PATH, resource.getFullPath().makeAbsolute().toString()); |
| 136 | + attributes.put(IJavaScriptBreakpoint.ELEMENT_HANDLE, null); |
| 137 | + JavaScriptDebugModel.createLineBreakpoint(resource, linenumber, charstart, charend, attributes, true); |
| 138 | + return Status.OK_STATUS; |
| 139 | + } |
| 140 | + reportToStatusLine(part, "Failed to create Javascript line breakpoint"); |
| 141 | + return Status.CANCEL_STATUS; |
| 142 | + } |
| 143 | + catch(CoreException ce) { |
| 144 | + return ce.getStatus(); |
| 145 | + } |
| 146 | + } |
| 147 | + }; |
| 148 | + job.setPriority(Job.INTERACTIVE); |
| 149 | + job.setSystem(true); |
| 150 | + job.schedule(); |
| 151 | + } |
| 152 | + |
| 153 | + void addBreakpoint(IResource resource, IDocument document, int linenumber) throws CoreException { |
| 154 | + IBreakpoint bp = lineBreakpointExists(resource, linenumber); |
| 155 | + if(bp != null) { |
| 156 | + DebugPlugin.getDefault().getBreakpointManager().removeBreakpoint(bp, true); |
| 157 | + } |
| 158 | + int charstart = -1, charend = -1; |
| 159 | + try { |
| 160 | + IRegion line = document.getLineInformation(linenumber - 1); |
| 161 | + charstart = line.getOffset(); |
| 162 | + charend = charstart + line.getLength(); |
| 163 | + } |
| 164 | + catch (BadLocationException ble) {} |
| 165 | + HashMap<String, String> attributes = new HashMap<String, String>(); |
| 166 | + attributes.put(IJavaScriptBreakpoint.TYPE_NAME, null); |
| 167 | + attributes.put(IJavaScriptBreakpoint.SCRIPT_PATH, resource.getFullPath().makeAbsolute().toString()); |
| 168 | + attributes.put(IJavaScriptBreakpoint.ELEMENT_HANDLE, null); |
| 169 | + JavaScriptDebugModel.createLineBreakpoint(resource, linenumber, charstart, charend, attributes, true); |
| 170 | + } |
| 171 | + |
| 172 | + void reportToStatusLine(final IWorkbenchPart part, final String message) { |
| 173 | + getStandardDisplay().asyncExec(new Runnable() { |
| 174 | + public void run() { |
| 175 | + IEditorStatusLine statusLine = (IEditorStatusLine) part.getAdapter(IEditorStatusLine.class); |
| 176 | + if (statusLine != null) { |
| 177 | + if (message != null) { |
| 178 | + statusLine.setMessage(true, message, null); |
| 179 | + } else { |
| 180 | + statusLine.setMessage(true, null, null); |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + }); |
| 185 | + } |
| 186 | + |
| 187 | + Display getStandardDisplay() { |
| 188 | + Display display; |
| 189 | + display = Display.getCurrent(); |
| 190 | + if (display == null) { |
| 191 | + display = Display.getDefault(); |
| 192 | + } |
| 193 | + return display; |
| 194 | + } |
| 195 | + |
| 196 | + IBreakpoint lineBreakpointExists(IResource resource, int linenumber) { |
| 197 | + IBreakpoint[] breakpoints = DebugPlugin.getDefault().getBreakpointManager().getBreakpoints(JavaScriptDebugModel.MODEL_ID); |
| 198 | + IJavaScriptLineBreakpoint breakpoint = null; |
| 199 | + for (int i = 0; i < breakpoints.length; i++) { |
| 200 | + if(breakpoints[i] instanceof IJavaScriptLineBreakpoint) { |
| 201 | + breakpoint = (IJavaScriptLineBreakpoint) breakpoints[i]; |
| 202 | + try { |
| 203 | + if(IJavaScriptLineBreakpoint.MARKER_ID.equals(breakpoint.getMarker().getType()) && |
| 204 | + resource.equals(breakpoint.getMarker().getResource()) && |
| 205 | + linenumber == breakpoint.getLineNumber()) { |
| 206 | + return breakpoint; |
| 207 | + } |
| 208 | + } catch (CoreException e) {} |
| 209 | + } |
| 210 | + } |
| 211 | + return null; |
| 212 | + } |
| 213 | + |
| 214 | + IResource getResource(IEditorPart editor) { |
| 215 | + IEditorInput editorInput = editor.getEditorInput(); |
| 216 | + return (IResource) editorInput.getAdapter(IFile.class); |
| 217 | + } |
| 218 | + |
| 219 | + ITextEditor getTextEditor(IWorkbenchPart part) { |
| 220 | + if (part instanceof ITextEditor) { |
| 221 | + return (ITextEditor) part; |
| 222 | + } |
| 223 | + return (ITextEditor) part.getAdapter(ITextEditor.class); |
| 224 | + } |
| 225 | + |
| 226 | +} |
0 commit comments