TDLtxFormatter.java 10.6 KB
Newer Older
/*
 * generated by Xtext 2.25.0
 */
package org.etsi.mts.tdl.formatting2;

import java.util.List;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.Assignment;
import org.eclipse.xtext.formatting2.AbstractJavaFormatter;
import org.eclipse.xtext.formatting2.FormatterPreferenceKeys;
import org.eclipse.xtext.formatting2.IFormattableDocument;
import org.eclipse.xtext.formatting2.IHiddenRegionFormatter;
import org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion;
import org.eclipse.xtext.preferences.MapBasedPreferenceValues;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.etsi.mts.tdl.Element;
import org.etsi.mts.tdl.ElementImport;
import org.etsi.mts.tdl.PackageableElement;
import org.etsi.mts.tdl.ParameterMapping;
import org.etsi.mts.tdl.StructuredDataType;
import org.etsi.mts.tdl.EnumDataType;
import org.etsi.mts.tdl.StructuredDataInstance;
import org.etsi.mts.tdl.TestObjective;
import org.etsi.mts.tdl.TimeLabel;
import org.etsi.mts.tdl.tdlPackage;
import org.etsi.mts.tdl.TimeConstraint;
import org.etsi.mts.tdl.ComponentType;
import org.etsi.mts.tdl.Connection;
import org.etsi.mts.tdl.Annotation;
import org.etsi.mts.tdl.Extension;
import org.etsi.mts.tdl.DataElementMapping;
import org.etsi.mts.tdl.TestConfiguration;
import org.etsi.mts.tdl.TestDescription;
import org.etsi.mts.tdl.Behaviour;
import org.etsi.mts.tdl.ExceptionalBehaviour;
import org.etsi.mts.tdl.PeriodicBehaviour;
import org.etsi.mts.tdl.SimpleDataInstance;
import org.etsi.mts.tdl.Comment;
import org.etsi.mts.tdl.FormalParameter;
import org.etsi.mts.tdl.LocalExpression;
import org.etsi.mts.tdl.Member;
import org.etsi.mts.tdl.MemberAssignment;
import org.etsi.mts.tdl.Package;
import org.etsi.mts.tdl.services.TDLtxGrammarAccess;

import com.google.inject.Inject;

public class TDLtxFormatter extends AbstractJavaFormatter {
	@Inject
	private TDLtxGrammarAccess _grammarAccess;
	//NOTE: Backported from indentation-based syntax.. added some refinements
	//TODO: Clean-up, merge, validate, and supersede downstream
	protected void format(org.etsi.mts.tdl.Block e, IFormattableDocument doc) {
		for (EObject m : e.eContents()) {
			if (!(m instanceof Annotation) && !(m instanceof LocalExpression)) {
				//TODO: handle nested behaviour
				doc.prepend(m, p->p.newLine());
				doc.surround(m, p->p.indent());
				doc.append(m, p->p.newLine());
			} else if (m instanceof Annotation) {
				doc.append(m, p->p.newLine());
			} else {
			}
			doc.format(m);
	protected void format(org.etsi.mts.tdl.Element e, IFormattableDocument doc) {
		List<ISemanticRegion> open = this.textRegionExtensions.regionFor(e).ruleCallsTo(this._grammarAccess.getBEGINRule());
		List<ISemanticRegion> close = this.textRegionExtensions.regionFor(e).ruleCallsTo(this._grammarAccess.getENDRule());
		if (!open.isEmpty()) {
			doc.append(open.get(0), p->p.newLine());
			doc.interior(open.get(0), close.get(0), p->p.indent());
		}
//		doc.interior(e, p->p.indent());
		if (e instanceof Behaviour) {
//			List<ISemanticRegion> assignments = this.textRegionExtensions.regionFor(e).assignments(this._grammarAccess.findAssignments(this._grammarAccess.getTimeConstraintFragmentRule()).toArray(new Assignment[] {}));
//			for (var a : assignments) {
//				doc.prepend(a.getPreviousSemanticRegion(), p->p.newLine());
//				doc.surround(a.getPreviousSemanticRegion(), p->p.indent());
//				doc.append(a, p->p.newLine());
//			}
			List<ISemanticRegion> features = this.textRegionExtensions.regionFor(e).features(
					tdlPackage.Literals.BEHAVIOUR__TEST_OBJECTIVE
//					tdlPackage.Literals.ATOMIC_BEHAVIOUR__TIME_CONSTRAINT, 
//					tdlPackage.Literals.ATOMIC_BEHAVIOUR__TIME_LABEL 
					);
			for (ISemanticRegion f : features) {
				//TODO: this depends on keywords..
				doc.prepend(f.getPreviousSemanticRegion(), p->p.newLine());
				////doc.surround(f.getPreviousSemanticRegion(), p->p.indent());
				doc.append(f, p->p.newLine());
			}
		if (e instanceof Connection) {
			doc.prepend(e, p->p.newLine());
		}
		
		
		for (EObject m : e.eContents()) {
			if (m instanceof TimeLabel ||
					m instanceof TimeConstraint //||
//					m instanceof Comment
					) { 
				doc.append(m, p->p.newLine());
			} else if (
					m instanceof Annotation ||
					m instanceof Comment
			) {
				doc.prepend(m, p->p.newLine());
				doc.append(m, p->p.newLine());
			} else if (
					m instanceof Annotation ||
					m instanceof Comment || 
					m instanceof TestObjective || //?
					m instanceof ExceptionalBehaviour ||
					m instanceof PeriodicBehaviour
					) {
				doc.prepend(m, p->p.newLine());
				doc.surround(m, p->p.indent());
				doc.append(m, p->p.newLine());
			}
			doc.format(m);
	
	protected void format(org.etsi.mts.tdl.NamedElement e, IFormattableDocument doc) {
		doc.prepend(e, p->p.newLine());
//		doc.interior(e, p->p.indent());
//		doc.format(e);
//		doc.interior(e, p->p.indent());
		if (e instanceof StructuredDataType) {
//			var open = this.textRegionExtensions.regionFor(e).ruleCallsTo(this._grammarAccess.getBEGINRule());
//			var close = this.textRegionExtensions.regionFor(e).ruleCallsTo(this._grammarAccess.getENDRule());
//
//			doc.append(open.get(0), p->p.newLine());
//			doc.interior(open.get(0), close.get(0), p->p.indent());

			if (!((StructuredDataType) e).getMember().isEmpty()) {
//				doc.interior(
//						doc.append(this.textRegionExtensions.regionFor(e).keyword("("), r->r.newLine()), 
//						doc.prepend(this.textRegionExtensions.regionFor(e).keyword(")"), r->r.newLine()), 
//						r->r.indent());
			int size = ((StructuredDataType)e).getMember().size();
			for (Member m : ((StructuredDataType)e).getMember()) {
				doc.prepend(m, p->p.newLine());
				doc.surround(m, p->p.indent());
				if (i==size) {
					doc.append(m, p->p.newLine());
				}
				doc.format(m);
			}
		}

		if (e instanceof StructuredDataInstance) {
			int size = ((StructuredDataInstance)e).getMemberAssignment().size();
			int i = 0;
			for (MemberAssignment m : ((StructuredDataInstance)e).getMemberAssignment()) {
				doc.prepend(m, p->p.newLine());
				doc.surround(m, p->p.indent());
//				doc.append(m, p->p.newLine());
				if (i==size) {
					doc.append(m, p->p.newLine());
				}
				doc.format(m);
			}
		}

		if (e instanceof EnumDataType) {
			int size = ((EnumDataType)e).getValue().size();
			for (SimpleDataInstance m : ((EnumDataType)e).getValue()) {
				i++;
				doc.prepend(m, p->p.newLine());
				doc.surround(m, p->p.indent());
//				doc.append(m, p->p.newLine());
				if (i==size) {
					doc.append(m, p->p.newLine());
				}
				doc.format(m);
			}
		}
		
		if (e instanceof ComponentType ||
				e instanceof TestConfiguration ||
				e instanceof TestDescription) {
			int i = 0;
			for (EObject m : e.eContents()) {
				i++;
				if (!(m instanceof Annotation) && 
						!(m instanceof Extension) &&
						!(m instanceof FormalParameter)) {
					doc.prepend(m, p->p.newLine());
					doc.surround(m, p->p.indent());
					//doc.append(m, p->p.newLine());
					doc.format(m);
				}
				if (i==e.eContents().size()) {
					doc.append(m, p->p.newLine());
				}
			}
		}

		if (e instanceof DataElementMapping) {
			for (ParameterMapping m : ((DataElementMapping)e).getParameterMapping()) {
				doc.prepend(m, p->p.newLine());
				doc.surround(m, p->p.indent());
//				doc.append(m, p->p.newLine());
				doc.format(m);
			}
		}
		
		//Test Objective Only
		if (e instanceof TestObjective) {
//			ISemanticRegion dKeyword = this.textRegionExtensions.regionFor(e).keyword("Description:");
//			doc.prepend(dKeyword, p->p.newLine());
//			doc.surround(dKeyword, p->p.indent());
//			ISemanticRegion rKeyword = this.textRegionExtensions.regionFor(e).keyword("References:");
//			doc.prepend(rKeyword, p->p.newLine());
//			doc.surround(rKeyword, p->p.indent());
			
			List<ISemanticRegion> features = this.textRegionExtensions.regionFor(e).features(
					tdlPackage.Literals.TEST_OBJECTIVE__DESCRIPTION,
					tdlPackage.Literals.TEST_OBJECTIVE__OBJECTIVE_URI
//					tdlPackage.Literals.ATOMIC_BEHAVIOUR__TIME_CONSTRAINT, 
//					tdlPackage.Literals.ATOMIC_BEHAVIOUR__TIME_LABEL 
					);
			for (ISemanticRegion f : features) {
				//TODO: this depends on keywords..
				doc.prepend(f.getPreviousSemanticRegion(), p->p.newLine());
				doc.surround(f.getPreviousSemanticRegion(), p->p.indent());
				doc.append(f, p->p.newLine());
			}

		}
		//All
		ISemanticRegion withKeyword = this.textRegionExtensions.regionFor(e).keyword("with");
		if (withKeyword!=null) {
			doc.surround(withKeyword, p->p.newLine());
			doc.append(withKeyword, p->p.indent());
//			doc.interior(
//					withKeyword.getNextHiddenRegion().getNextSemanticRegion(), 
//					this.textRegionExtensions.nextHiddenRegion(e).getNextSemanticRegion(), 
//					p->p.indent()
//					);
		}
		//doc.prepend(withKeyword, p->p.indent());
		//withKeyword.getNextSemanticRegion();
		//		var rbs = this.textRegionExtensions.regionFor(e).ruleCallTo(this._grammarAccess.());
			
		for (Comment c : e.getComment()) {
			if (!(e instanceof Member)) {
				doc.surround(c, p->p.indent());
			}
			doc.append(c, p->p.newLine());
			doc.format(c);
		}
		
		for (Annotation a : e.getAnnotation()) {
			doc.append(a, p->p.newLine());
			doc.format(a);
		}
		
	protected void format(org.etsi.mts.tdl.TestDescription e, IFormattableDocument doc) {
		for (EObject m : e.eContents()) {
			doc.format(m);
		}
	}
	
	protected void format(org.etsi.mts.tdl.Package _package, IFormattableDocument doc) {
		// TODO: format HiddenRegions around keywords, attributes, cross references, etc.
		if (getPreferences() instanceof MapBasedPreferenceValues) {
			MapBasedPreferenceValues preferences = (MapBasedPreferenceValues) getPreferences();
			preferences.put(FormatterPreferenceKeys.indentation, "    ");
		}
		//TODO: repeated formatting doesn't work very well..
		Package e = _package;
		for (Annotation a : e.getAnnotation()) {
			doc.append(a, p->p.newLine());
			doc.append(a, p->p.noIndentation());
			doc.format(a);
		for (Comment c : e.getComment()) {
			doc.append(c, p->p.newLine());
			doc.append(c, p->p.noIndentation());
			doc.format(c);
		doc.interior(_package, p->p.indent());

		List<ISemanticRegion> dots = this.textRegionExtensions.allRegionsFor(e).keywords(".", "@", ",", "::");
		for (ISemanticRegion dot : dots) {
			doc.surround(dot, p -> p.noSpace());
		}
		for (ElementImport elementImport : _package.getImport()) {
			doc.surround(elementImport, p -> p.setNewLines(1));
		for (PackageableElement packageableElement : _package.getPackagedElement()) {
			doc.surround(packageableElement, p -> p.setNewLines(1));
			doc.format(packageableElement);
		}


		for (org.etsi.mts.tdl.Package __package : _package.getNestedPackage()) {
			doc.format(__package);
		}
	}