Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ca2fab3df6ee45a4d6b3e7cc71ea9c69dbd5b11b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/*******************************************************************************
 * Copyright (c) 2000, 2017 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
 *
 *******************************************************************************/
package org.eclipse.dltk.internal.debug.ui.actions;

import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.debug.ui.IDebugModelPresentation;
import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.dltk.debug.core.eval.IScriptEvaluationEngine;
import org.eclipse.dltk.debug.core.eval.IScriptEvaluationListener;
import org.eclipse.dltk.debug.core.eval.IScriptEvaluationResult;
import org.eclipse.dltk.debug.core.model.IScriptStackFrame;
import org.eclipse.dltk.debug.core.model.IScriptValue;
import org.eclipse.dltk.debug.core.model.IScriptVariable;
import org.eclipse.dltk.debug.ui.DLTKDebugUIPlugin;
import org.eclipse.dltk.internal.debug.ui.ScriptEvaluationContextManager;
import org.eclipse.dltk.internal.ui.editor.ScriptEditor;
import org.eclipse.dltk.internal.ui.text.ScriptWordFinder;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorActionDelegate;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IPartListener;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.texteditor.ITextEditor;

public class ScriptEvaluationAction implements IWorkbenchWindowActionDelegate,
		IObjectActionDelegate, IPartListener, IScriptEvaluationListener,
		IEditorActionDelegate, IViewActionDelegate {

	private static class ObjectResolver {
		private IWorkbenchPart part;
		private IRegion region;

		protected static ISelection getSelection(IWorkbenchPart part) {
			if (part != null) {
				ISelectionProvider provider = part.getSite()
						.getSelectionProvider();
				if (provider != null) {
					return provider.getSelection();
				}
			}

			return null;
		}

		protected static boolean textHasContent(String text) {
			if (text != null) {
				int length = text.length();
				if (length > 0) {
					for (int i = 0; i < length; i++) {
						if (Character.isLetterOrDigit(text.charAt(i))) {
							return true;
						}
					}
				}
			}

			return false;
		}

		protected Object resolveTextSelection(ITextSelection selection) {
			String text = selection.getText();
			if (textHasContent(text)) {
				region = new Region(selection.getOffset(),
						selection.getLength());
				return text;
			} else if (part instanceof IEditorPart) {
				IEditorPart editor = (IEditorPart) part;
				if (editor instanceof ITextEditor) {
					ITextEditor textEditor = (ITextEditor) editor;
					IDocument doc = textEditor.getDocumentProvider()
							.getDocument(editor.getEditorInput());
					region = ScriptWordFinder.findWord(doc,
							selection.getOffset());
					if (region != null) {
						try {
							return doc.get(region.getOffset(),
									region.getLength());
						} catch (BadLocationException e) {
						}
					}
				}
			}

			return null;
		}

		protected Object resolveStructuredSelection(
				IStructuredSelection selection) {
			if (!selection.isEmpty()) {
				if (part.getSite().getId()
						.equals(IDebugUIConstants.ID_DEBUG_VIEW)) {
					IEditorPart editor = part.getSite().getPage()
							.getActiveEditor();

					ISelection newSelection = getSelection(editor);
					if (newSelection instanceof ITextSelection) {
						return resolveTextSelection(
								(ITextSelection) newSelection);
					}
				} else {
					Iterator elements = selection.iterator();
					while (elements.hasNext()) {
						Object element = elements.next();
						if (!(element instanceof IScriptVariable)) {
							return null;
						}
					}

					return selection;
				}
			}

			return null;
		}

		public ObjectResolver(IWorkbenchPart part) {
			this.part = part;
		}

		public Object resolveSelectedObject() {
			ISelection selection = getSelection(part);

			if (selection instanceof ITextSelection) {
				return resolveTextSelection((ITextSelection) selection);
			} else if (selection instanceof IStructuredSelection) {
				return resolveStructuredSelection(
						(IStructuredSelection) selection);
			}

			return null;
		}

		public IRegion getRegion() {
			return region;
		}
	}

	private IWorkbenchWindow window;
	private IWorkbenchPart part;
	private IAction action;
	private Object selectedObject;
	private boolean evaluation;

	protected static IDebugModelPresentation getDebugModelPresentation(
			String identifier) {
		return DebugUITools.newDebugModelPresentation(identifier);
	}

	private void setWindow(IWorkbenchWindow window) {
		this.window = window;
	}

	protected IWorkbenchWindow getWindow() {
		return this.window;
	}

	private void setPart(IWorkbenchPart part) {
		this.part = part;
	}

	protected IWorkbenchPart getPart() {
		return this.editor != null ? this.editor : this.part;
	}

	private void setAction(IAction action) {
		this.action = action;
	}

	protected IAction getAction() {
		return action;
	}

	private void setSelectedObject(Object object) {
		this.selectedObject = object;
	}

	protected Object getSelectedObject() {
		return this.selectedObject;
	}

	private void setEvaluating(boolean evaluation) {
		this.evaluation = evaluation;
	}

	protected boolean isEvaluating() {
		return this.evaluation;
	}

	protected void evaluationCleanup() {
		setEvaluating(false);
		// setTargetPart(fNewTargetPart);
	}

	private ScriptEditor editor;

	private void setEditor(ScriptEditor editor) {
		this.editor = editor;
	}

	protected ScriptEditor getEditor() {
		return this.editor;
	}

	// IWorkbenchWindowActionDelegate
	@Override
	public void init(IWorkbenchWindow window) {
		setWindow(window);

		IWorkbenchPage page = window.getActivePage();
		if (page != null) {
			setPart(page.getActivePart());
		}

		window.getPartService().addPartListener(this);
		update();
	}

	@Override
	public void dispose() {
		// disposeDebugModelPresentation();

		IWorkbenchWindow window = getWindow();
		if (window != null) {
			window.getPartService().removePartListener(this);
		}
	}

	// IObjectActionDelegate
	@Override
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		setAction(action);
		setPart(targetPart);
		update();
	}

	// IActionDelegate
	@Override
	public void run(IAction action) {
		update();
		run();
	}

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		setAction(action);
	}

	// IPartListener
	@Override
	public void partActivated(IWorkbenchPart part) {
		setPart(part);
	}

	@Override
	public void partBroughtToTop(IWorkbenchPart part) {

	}

	@Override
	public void partClosed(IWorkbenchPart part) {
		if (part == getPart()) {
			setPart(null);
		}
	}

	@Override
	public void partDeactivated(IWorkbenchPart part) {

	}

	@Override
	public void partOpened(IWorkbenchPart part) {

	}

	// Other stuff
	protected void update() {
		IAction action = getAction();
		if (action != null) {
			ObjectResolver or = new ObjectResolver(getPart());
			setSelectedObject(or.resolveSelectedObject());
		}
	}

	protected void showExpressionView() {
		if (getPart().getSite().getId()
				.equals(IDebugUIConstants.ID_EXPRESSION_VIEW)) {
			return;
		}

		IWorkbenchPage page = DLTKDebugUIPlugin.getActivePage();
		if (page != null) {
			IViewPart part = page
					.findView(IDebugUIConstants.ID_EXPRESSION_VIEW);
			if (part == null) {
				try {
					page.showView(IDebugUIConstants.ID_EXPRESSION_VIEW);
				} catch (PartInitException e) {
					// reportError(e.getStatus().getMessage());
				}
			} else {
				page.bringToTop(part);
			}
		}
	}

	public static StyledText getStyledText(IWorkbenchPart part) {
		ITextViewer viewer = part.getAdapter(ITextViewer.class);
		StyledText textWidget = null;
		if (viewer == null) {
			Control control = part.getAdapter(Control.class);
			if (control instanceof StyledText) {
				textWidget = (StyledText) control;
			}
		} else {
			textWidget = viewer.getTextWidget();
		}
		return textWidget;
	}

	public static Point getPopupAnchor(StyledText textWidget) {
		if (textWidget != null) {
			Point docRange = textWidget.getSelectionRange();
			int midOffset = docRange.x + (docRange.y / 2);
			Point point = textWidget.getLocationAtOffset(midOffset);
			point = textWidget.toDisplay(point);

			GC gc = new GC(textWidget);
			gc.setFont(textWidget.getFont());
			int height = gc.getFontMetrics().getHeight();
			gc.dispose();
			point.y += height;
			return point;
		}

		return null;
	}

	protected Shell getShell() {
		if (getPart() != null) {
			return getPart().getSite().getShell();
		}

		return DLTKDebugUIPlugin.getActiveWorkbenchShell();
	}

	protected IScriptStackFrame getStackFrameContext() {
		IWorkbenchPart part = getPart();
		IScriptStackFrame frame = null;
		if (part == null) {
			frame = ScriptEvaluationContextManager
					.getEvaluationContext(getWindow());
		} else {
			frame = ScriptEvaluationContextManager.getEvaluationContext(part);
		}
		return frame;
	}

	protected void run() {
		final IScriptStackFrame stackFrame = getStackFrameContext();
		if (stackFrame == null) {
			reportError(Messages.ScriptEvaluationAction_cannotGetStackFrame);
			return;
		}

		// setNewTargetPart(getTargetPart());

		// Preparing runnable
		IRunnableWithProgress runnable = monitor -> {
			if (stackFrame.isSuspended()) {
				Object selection = getSelectedObject();
				if (!(selection instanceof String)) {
					return;
				}
				String expression = (String) selection;

				IScriptEvaluationEngine engine = stackFrame.getScriptThread()
						.getEvaluationEngine();
				setEvaluating(true);
				engine.asyncEvaluate(expression, stackFrame,
						ScriptEvaluationAction.this);
			} else {
				throw new InvocationTargetException(null,
						Messages.ScriptEvaluationAction_threadIsNotSuspended);
			}
		};

		// Run
		try {
			IWorkbench workbench = DLTKDebugUIPlugin.getDefault()
					.getWorkbench();
			workbench.getProgressService().busyCursorWhile(runnable);
		} catch (InvocationTargetException e) {
			evaluationCleanup();
			String message = e.getMessage();
			if (message == null) {
				message = e.getClass().getName();
				if (e.getCause() != null) {
					message = e.getCause().getClass().getName();
					if (e.getCause().getMessage() != null) {
						message = e.getCause().getMessage();
					}
				}
			}
			reportError(message);
		} catch (InterruptedException e) {
		}
	}

	// Error reporting
	public static String getExceptionMessage(Throwable exception) {
		if (exception instanceof CoreException) {
			CoreException ce = (CoreException) exception;
			Throwable throwable = ce.getStatus().getException();
			if (throwable instanceof CoreException) {
				// Traverse nested CoreExceptions
				return getExceptionMessage(throwable);
			}
			return ce.getStatus().getMessage();
		}
		String message = NLS.bind(
				Messages.ScriptEvaluationAction_anExceptionOccurred,
				exception.getClass());
		if (exception.getMessage() != null) {
			message = NLS.bind(
					Messages.ScriptEvaluationAction_anExceptionOccurred2,
					message, exception.getMessage());
		}
		return message;
	}

	protected static String getErrorMessage(IScriptEvaluationResult result) {
		String[] errors = result.getErrorMessages();
		if (errors.length == 0) {
			return getExceptionMessage(result.getException());
		}
		return getErrorMessage(errors);
	}

	protected static String getErrorMessage(String[] errors) {
		String message = ""; //$NON-NLS-1$
		for (int i = 0; i < errors.length; i++) {
			String msg = errors[i];
			if (i == 0) {
				message = msg;
			} else {
				message = NLS.bind(Messages.ScriptEvaluationAction_errorMessage,
						message, msg);
			}
		}
		return message;
	}

	protected void reportErrors(IScriptEvaluationResult result) {
		String message = getErrorMessage(result);
		reportError(message);
	}

	protected void reportError(String message) {
		Status status = new Status(IStatus.ERROR,
				DLTKDebugUIPlugin.getUniqueIdentifier(), IStatus.ERROR, message,
				null);
		ErrorDialog.openError(getShell(),
				Messages.ScriptEvaluationAction_errorEvaluating, null, status);
	}

	// IScriptEvaluationListener
	@Override
	public void evaluationComplete(IScriptEvaluationResult result) {
		// if plug-in has shutdown, ignore - see bug# 8693
		if (DLTKDebugUIPlugin.getDefault() == null) {
			return;
		}

		final IScriptValue value = result.getValue();
		if (result.hasErrors() || value != null) {
			final Display display = DLTKDebugUIPlugin.getStandardDisplay();
			if (display.isDisposed()) {
				return;
			}

			// Each action should implement this method for own purposes
			displayResult(result);
		}

	}

	protected void displayResult(IScriptEvaluationResult result) {
		// Nothing by default
	}

	// IEditorActionDelegate
	@Override
	public void setActiveEditor(IAction action, IEditorPart targetEditor) {
		setEditor((ScriptEditor) targetEditor);
	}

	// IViewActionDelegate
	@Override
	public void init(IViewPart view) {
		setPart(view);
	}
}

Back to the top