Skip to main content
summaryrefslogtreecommitdiffstats
blob: 075d1b60aaa2ed61faef6771ec05be2ace8a1c81 (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
/*******************************************************************************
 * Copyright (c) 2005 - 2007 committers of openArchitectureWare 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
 *
 * Contributors:
 *     committers of openArchitectureWare - initial API and implementation
 *******************************************************************************/
package org.eclipse.xtend.ui.debug;

import static org.eclipse.internal.xtend.expression.debug.ExpressionElementAdapter.TYPE;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Stack;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.debug.core.model.IBreakpoint;
import org.eclipse.emf.mwe.ui.debug.model.MWEBreakpoint;
import org.eclipse.emf.mwe.ui.debug.processing.PluginAdapter;
import org.eclipse.emf.mwe.ui.workflow.util.PluginConfigurationElementUtil;
import org.eclipse.internal.xpand2.ast.Statement;
import org.eclipse.internal.xpand2.ast.TextStatement;
import org.eclipse.internal.xtend.expression.ast.BooleanOperation;
import org.eclipse.internal.xtend.expression.ast.Case;
import org.eclipse.internal.xtend.expression.ast.Cast;
import org.eclipse.internal.xtend.expression.ast.ChainExpression;
import org.eclipse.internal.xtend.expression.ast.CollectionExpression;
import org.eclipse.internal.xtend.expression.ast.Expression;
import org.eclipse.internal.xtend.expression.ast.FeatureCall;
import org.eclipse.internal.xtend.expression.ast.ISyntaxElement;
import org.eclipse.internal.xtend.expression.ast.IfExpression;
import org.eclipse.internal.xtend.expression.ast.LetExpression;
import org.eclipse.internal.xtend.expression.ast.ListLiteral;
import org.eclipse.internal.xtend.expression.ast.OperationCall;
import org.eclipse.internal.xtend.expression.ast.SwitchExpression;
import org.eclipse.internal.xtend.expression.ast.SyntaxElement;
import org.eclipse.internal.xtend.expression.debug.BaseSpecialTreatment;
import org.eclipse.internal.xtend.expression.debug.EvaluatedElementWrapper;
import org.eclipse.internal.xtend.expression.debug.ExpressionModelPresentation;
import org.eclipse.internal.xtend.xtend.ast.Around;
import org.eclipse.internal.xtend.xtend.ast.CreateExtensionStatement;
import org.eclipse.internal.xtend.xtend.ast.ExpressionExtensionStatement;
import org.eclipse.internal.xtend.xtend.ast.ExtensionFile;
import org.eclipse.internal.xtend.xtend.ast.JavaExtensionStatement;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.xtend.shared.ui.Activator;
import org.eclipse.xtend.shared.ui.core.IXtendXpandProject;
import org.eclipse.xtend.shared.ui.core.IXtendXpandResource;
import org.eclipse.xtend.ui.XtendEditorPlugin;
import org.eclipse.xtend.ui.editor.XtendEditor;

/**
 * The IPluginAdapter implementation for Expressions.
 *
 * @author Clemens Kadura (zAJKa)
 * @author Karsten Thoms - maintenance
 * @author Aykut Kilic (itemis) - Bug#465802,480646
 */
public class ExpressionPluginAdapter implements PluginAdapter {

	protected ExpressionModelPresentation pres;

	protected Set<BaseSpecialTreatment> specials = new HashSet<BaseSpecialTreatment>();

	// -------------------------------------------------------------------------

	public ExpressionPluginAdapter() {
		pres = new ExpressionModelPresentation(specials);
	}

	// -------------------------------------------------------------------------

	protected String getRequiredExtension() {
		return "ext"; // TODO: CK: high: handle chk as well
	}

	public boolean canHandleResourceExtension(String ext) {
		return getRequiredExtension().equals(ext);
	}

	public boolean canHandleType(String type) {
		return TYPE.equals(type);
	}

	public String getEditorId() {
		// TODO: CK: next: integrate Check editor
		return PluginConfigurationElementUtil.getConfigAttribute("org.eclipse.ui.editors/" + "editor[class=" + XtendEditor.class.getName() + "]/id");
	}

	public boolean isToggleBpEnabled(IResource resource, int start, int end, int line) {
		ISyntaxElement element = findElementForPosition(resource, start, line);
		if (element == null)
			return false;

		return pres.getStart(element) <= start && pres.getStartingEndPosition(element) >= end;
	}

	public MWEBreakpoint createBreakpoint(IResource resource, int start, int end, int line) throws CoreException {
		ISyntaxElement element = findElementForPosition(resource, start, line);
		if (element == null || (!(element instanceof FeatureCall)
		        && !(element instanceof Statement)) || element instanceof TextStatement
		        ||  element.getEnd() == 0 )
			// TODO: CK: Message that no BP could be created
			return null;

		return new MWEBreakpoint(resource, element.getNameString(null), element.getLine(), element.getStart(), element.getEnd());
	}

	public IBreakpoint checkBreakpoints(IBreakpoint[] bps, IResource resource, int start, int end, int line) throws CoreException {
		ISyntaxElement element = findElementForPosition(resource, start, line);

		if(element == null) return null;
		
		for (IBreakpoint bp1 : bps) {
			MWEBreakpoint bp = (MWEBreakpoint) bp1;
			if (bp.getResource().equals(resource.getFullPath().toString())
					&& bp.getLine() == element.getLine())
				return bp;
		}
		return null;
	}

	/**
	 * @since 2.2
	 */
	protected ISyntaxElement findElementForPosition(final IResource resource, final int position, final int line) {
		ISyntaxElement rootElem = getContainingRootElement(resource, position);
		if (rootElem == null)
			return null;

		ISyntaxElement body = getBodyOfRootElement(rootElem);

		if (body instanceof ChainExpression) {
			return getContainingElementOfChainExpression(
					(ChainExpression) body, position);
		} else if (body instanceof LetExpression) {
			return getContainingElementOfLetExpression((LetExpression) body,
					position);
		} else if (body instanceof IfExpression) {
			return getContainingElementOfIfExpression((IfExpression) body, position);
		} else
			return getContainingElement((SyntaxElement)body,position);
	}

	private boolean containsPosition(final ISyntaxElement elem, final int position) {
		return elem.getStart() <= position && elem.getEnd() > position;
	}
	
	/**
	 * @since 2.2
	 */
	protected int shiftPositionIfInside(final int currentPosition,
			final SyntaxElement lowerBound, final SyntaxElement upperBound) {
		return currentPosition >= lowerBound.getStart()
				&& currentPosition <= upperBound.getStart() ? upperBound
				.getStart() : currentPosition;
	}

	/**
	 * @since 2.2
	 */
	protected ISyntaxElement getContainingElement(final SyntaxElement se, final int position) {
		if (!containsPosition(se, position)) return null;
		
		if ( se instanceof ChainExpression )
			return getContainingElementOfChainExpression((ChainExpression) se, position);
		else if ( se instanceof LetExpression ) {
			return getContainingElementOfLetExpression((LetExpression) se, position);
		} else if ( se instanceof SwitchExpression ) {
			return getContainingElementOfSwitchExpression((SwitchExpression) se, position);
		} else if ( se instanceof ListLiteral ) {
			return getContainingElementOfListLiteral((ListLiteral) se, position);
		} else if ( se instanceof IfExpression ) {
			return getContainingElementOfIfExpression((IfExpression) se, position );
		} else if ( se instanceof CollectionExpression ) {
			return getContainingElementOfCollectionExpression((CollectionExpression)se, position);
		} else if ( se instanceof BooleanOperation ) {
			return getContainingElementOfBooleanOperation( (BooleanOperation)se, position );
		} else if ( se instanceof FeatureCall ) {
			return getContainingElementOfFeatureCall( (FeatureCall)se, position );
		}

		return null;
	}
	
	/**
	 * @since 2.2
	 */
	protected ISyntaxElement getContainingChild(final SyntaxElement parent, final Collection<SyntaxElement> children, int position) {
		if(!containsPosition(parent, position)) return null;
		
		ISyntaxElement result = null;
		int newPosition = position;
		SyntaxElement lastElem = parent;
		for( SyntaxElement child : children) {
			if( child == null ) continue;
			fixBoundaries(child);
			if( child.getEnd()==0 ) {
				System.out.println(child.getClass().getSimpleName() + "  " + child.toString());
			}
			
			newPosition = shiftPositionIfInside(newPosition, lastElem, child);
			
			result = getContainingElement(child, newPosition);
			if(result != null) return result;
			lastElem = child;
		}
		
		return parent;
	}
	
	private ISyntaxElement getContainingElementOfLetExpression(final LetExpression le, int position) {
		List<SyntaxElement> children = new ArrayList<SyntaxElement>();
		children.add(le.getVarExpression());
		children.add(le.getTargetExpression());
		return getContainingChild(le, children, position);
	}

	private ISyntaxElement getContainingElementOfIfExpression(final IfExpression ie, int position) {
		List<SyntaxElement> children = new ArrayList<SyntaxElement>();
		children.add(ie.getCondition());
		children.add(ie.getThenPart());
		children.add(ie.getElsePart());
		return getContainingChild(ie, children, position);
	}
	
	private ISyntaxElement getContainingElementOfCollectionExpression(final CollectionExpression ce, int position ) {
		List<SyntaxElement> children = new ArrayList<SyntaxElement>();
		children.add(ce.getClosure());
		return getContainingChild(ce, children, position);    
	}
	
	private SyntaxElement fixBoundaries(SyntaxElement e) {
		if( !( e.getStart() == 0 && e.getEnd() == 0) )
			return e;
		
		if( e instanceof BooleanOperation ) {
			BooleanOperation be = (BooleanOperation)e;
			SyntaxElement l = fixBoundaries(be.getLeft());
			SyntaxElement r = fixBoundaries(be.getRight());
			be.setLine(l.getLine());
			be.setStart(l.getStart());
			be.setEnd(r.getEnd());
		}
		
		return e;
	}
	
	private ISyntaxElement getContainingElementOfBooleanOperation( final BooleanOperation be, int position ) {
		fixBoundaries(be);
		
		List<SyntaxElement> children = new ArrayList<SyntaxElement>();
		children.add(be.getLeft());
		children.add(be.getRight());
		return getContainingChild(be, children, position);
	}
	
	private ISyntaxElement getContainingElementOfFeatureCall( final FeatureCall fe, int position ) {
		if(!containsPosition(fe,position))
		  return null;
		
		Stack<FeatureCall> fcStack = new Stack<FeatureCall>(); 
		Expression e = fe;
		do {
			FeatureCall fc = (FeatureCall)e;
			fcStack.push(fc);
			e = fc.getTarget();
		} while( e instanceof FeatureCall );
		
		FeatureCall fallback = fcStack.peek();
		
		while( !fcStack.isEmpty() ) {
			FeatureCall fc = fcStack.pop();
			if(fc instanceof OperationCall) {
				ISyntaxElement result = getContainingElementOfOperationCall((OperationCall)fc, position);
				if(result != null) 
					return result;
			} else if( fc instanceof CollectionExpression) {
				if(containsPosition(fc, position) ) return getContainingElement(((CollectionExpression)fc).getClosure(),position);
			}
		}
		
		return fallback;
	}
	
	private ISyntaxElement getContainingElementOfOperationCall( final OperationCall oe, int position ) {
		if(isFunctionCall(oe)) return oe;
		List<SyntaxElement> children = new ArrayList<SyntaxElement>(oe.getParamsAsList());
		return getContainingChild(oe, children, position);
	}
	
	private boolean isFunctionCall(OperationCall op) {
		char[] name = op.getName().toString().toCharArray();
		
		if( name.length == 0 ) return false;
		
		for( int i=0; i<name.length; i++ ) {
			if(i==0) {
				if(!Character.isJavaIdentifierStart(name[i])) return false;
			} else{
				if(!Character.isJavaIdentifierPart(name[i])) return false;
			}
		}
		
		return true;
	}

	private ISyntaxElement getContainingElementOfListLiteral(final ListLiteral ll, int position) {
		List<SyntaxElement> children = new ArrayList<SyntaxElement>(ll.getElementsAsList());
		return getContainingChild(ll, children, position);
	}

	private ISyntaxElement getContainingElementOfSwitchExpression(final SwitchExpression se, int position) {
		List<SyntaxElement> children = new ArrayList<SyntaxElement>();
		children.add(se.getSwitchExpr());
		for (Case caze : se.getCases()) {
			children.add(caze.getCondition());
			children.add(caze.getThenPart());
		}
		children.add(se.getDefaultExpr());
		return getContainingChild(se, children, position);
	}

	private ISyntaxElement getContainingElementOfChainExpression(final ChainExpression ce, final int position) {
		List<SyntaxElement> children = new ArrayList<SyntaxElement>();
		children.add(ce.getFirst());
		children.add(ce.getNext());
		return getContainingChild(ce, children, position); 
	}

	/**
	 * @since 2.2
	 */
	protected ISyntaxElement getContainingRootElement(final IResource resource,
			final int position) {
		IXtendXpandResource file = getXtendXpandResource(resource);
		List<ISyntaxElement> elems = collectFirstLevelElements(file);
		for (ISyntaxElement elem : elems)
			if (containsPosition(elem, position))
				return elem;

		return null;
	}

	private IXtendXpandResource getXtendXpandResource(final IResource resource) {
		final IXtendXpandProject project = Activator.getExtXptModelManager().findProject(resource.getProject().getFullPath());
		if (project != null) {
			for (IXtendXpandResource res : project.getAllRegisteredResources()) {
				if (!resource.equals(res.getUnderlyingStorage()))
					continue;
				return res;
			}
		}

		return null;
	}

	// -------------------------------------------------------------------------
	// Expression specific methods

	protected List<ISyntaxElement> collectFirstLevelElements(IXtendXpandResource res) {
		ExtensionFile file = (ExtensionFile) res.getExtXptResource();
		List<ISyntaxElement> result = new ArrayList<ISyntaxElement>();

		result.addAll(file.getArounds());
		result.addAll(file.getChecks());

		for (ISyntaxElement ext : file.getExtensions()) {
			// Hint: we don't create BP internal of Java extension statements
			// Java Debugger to be used instead
			if (ext instanceof JavaExtensionStatement)
				continue;

			result.add(ext);
		}

		return result;
	}

	/**
	 * @since 2.2
	 */
	protected ISyntaxElement getBodyOfRootElement(final ISyntaxElement elem) {
		if (elem instanceof ExpressionExtensionStatement)
			return ((ExpressionExtensionStatement) elem).getExpression();
		else if (elem instanceof CreateExtensionStatement)
			return ((CreateExtensionStatement) elem).getExpression();
		else if (elem instanceof Around)
			return ((Around) elem).getExpression();

		return null;
	}

	protected List<ISyntaxElement> getBody(ISyntaxElement exp) {
		// Hint: we accept all kinds of body elements here, because their
		// children could be OperationCalls
		ISyntaxElement[] body = new ISyntaxElement[0];
		if (exp instanceof IfExpression) {
			IfExpression op = (IfExpression) exp;
			body = new ISyntaxElement[] { op.getCondition(), op.getThenPart(), op.getElsePart() };
		} else if (exp instanceof OperationCall) {
			// Note: Check first for OperationCall because it extends
			// FeatureCall
			OperationCall op = (OperationCall) exp;
			List<ISyntaxElement> kids = new ArrayList<ISyntaxElement>();
			kids.add(op.getTarget());
			kids.add(new EvaluatedElementWrapper(op));
			kids.addAll(op.getParamsAsList());
			body = kids.toArray(new ISyntaxElement[0]);
		} else if (exp instanceof FeatureCall)
			body = new ISyntaxElement[] { ((FeatureCall) exp).getTarget(), new EvaluatedElementWrapper(exp) };
		else if (exp instanceof ExpressionExtensionStatement)
			body = new ISyntaxElement[] { ((ExpressionExtensionStatement) exp).getExpression() };
		else if (exp instanceof CreateExtensionStatement)
			body = new ISyntaxElement[] { ((CreateExtensionStatement) exp).getExpression() };
		else if (exp instanceof Around)
			body = new ISyntaxElement[] { ((Around) exp).getExpression() };
		else if (exp instanceof BooleanOperation) {
			BooleanOperation op = (BooleanOperation) exp;
			body = new ISyntaxElement[] { op.getLeft(), op.getRight() };
		} else if (exp instanceof SwitchExpression) {
			SwitchExpression op = (SwitchExpression) exp;
			body = new ISyntaxElement[] { op.getSwitchExpr(), op.getDefaultExpr() };
		} else if (exp instanceof ChainExpression) {
			ChainExpression op = (ChainExpression) exp;
			body = new ISyntaxElement[] { op.getFirst(), op.getNext() };
		} else if (exp instanceof LetExpression) {
			LetExpression op = (LetExpression) exp;
			body = new ISyntaxElement[] { op.getVarExpression(), op.getTargetExpression() };
		} else if (exp instanceof Cast) {
			Cast op = (Cast) exp;
			body = new ISyntaxElement[] { op.getTarget() };
		}
		List<ISyntaxElement> result = new ArrayList<ISyntaxElement>();
		for (ISyntaxElement element : body)
			if (element != null)
				result.add(element);
		return result;
	}

	protected boolean isValidReturnElement(final ISyntaxElement se) {
		return (se instanceof OperationCall || se instanceof ChainExpression);
	}

	// -------------------------------------------------------------------------

	private Image icon = null;

	public Image getIcon() {
		if (icon != null)
			return icon;
		IPath path = new Path("/icons/extensionfile.gif");
		InputStream is = null;
		try {
			is = FileLocator.openStream(XtendEditorPlugin.getDefault().getBundle(), path, false);
		} catch (IOException e) {
		}
		if (is != null) {
			final ImageData iData = new ImageData(is);
			icon = new Image(Display.getCurrent(), iData);
			return icon;
		}
		return null;
	}

}

Back to the top