Commit ee4f4174 authored by Philip Makedonski's avatar Philip Makedonski
Browse files

* formatter fixes, improvements and refactorings

parent f4c6d17f
Loading
Loading
Loading
Loading
+153 −77
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@ package org.etsi.mts.tdl.formatting2;

import java.util.List;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.Assignment;
import org.eclipse.xtext.Keyword;
@@ -38,6 +39,7 @@ import org.etsi.mts.tdl.DataUse;
import org.etsi.mts.tdl.TestConfiguration;
import org.etsi.mts.tdl.TestDescription;
import org.etsi.mts.tdl.Behaviour;
import org.etsi.mts.tdl.Block;
import org.etsi.mts.tdl.ExceptionalBehaviour;
import org.etsi.mts.tdl.PeriodicBehaviour;
import org.etsi.mts.tdl.SimpleDataInstance;
@@ -46,12 +48,20 @@ 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.NamedElement;
import org.etsi.mts.tdl.Package;
import org.etsi.mts.tdl.structuredobjectives.EventOccurrence;
import org.etsi.mts.tdl.structuredobjectives.EventReference;
import org.etsi.mts.tdl.structuredobjectives.ExpectedBehaviour;
import org.etsi.mts.tdl.structuredobjectives.FinalConditions;
import org.etsi.mts.tdl.structuredobjectives.InitialConditions;
import org.etsi.mts.tdl.structuredobjectives.LiteralValue;
import org.etsi.mts.tdl.structuredobjectives.PICSReference;
import org.etsi.mts.tdl.structuredobjectives.StructuredTestObjective;
import org.etsi.mts.tdl.structuredobjectives.Value;
import org.etsi.mts.tdl.structuredobjectives.Content;
import org.etsi.mts.tdl.structuredobjectives.EntityReference;
import org.etsi.mts.tdl.structuredobjectives.StructuredObjectivesPackage;
import org.etsi.mts.tdl.services.TDLtxGrammarAccess;

import com.google.inject.Inject;
@@ -63,7 +73,7 @@ public class TDLtxFormatter extends AbstractJavaFormatter {
	//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) {
	protected void format(Block e, IFormattableDocument doc) {
		for (EObject m : e.eContents()) {
			if (!(m instanceof Annotation) && !(m instanceof LocalExpression)) {
				//TODO: handle nested behaviour
@@ -78,20 +88,60 @@ public class TDLtxFormatter extends AbstractJavaFormatter {
		}
	}

	protected void format(org.etsi.mts.tdl.Element e, IFormattableDocument doc) {
	protected void format(LiteralValue e, IFormattableDocument doc) {
//		doc.surround(e, indent());
//		doc.surround(e, newLine());
		List<ISemanticRegion> keywords = this.textRegionExtensions.regionFor(e).keywords(",");
		for (ISemanticRegion f : keywords) {
			doc.surround(f, indent());
		}

		for (EObject m : e.eContents()) {
			if ((m instanceof Content)) {
				doc.prepend(m, newLine());
			} else {
			}
			doc.format(m);
		}
	}

	protected void format(Content e, IFormattableDocument doc) {
		doc.surround(e, indent());
//		doc.surround(e, newLine());
		List<ISemanticRegion> keywords = this.textRegionExtensions.regionFor(e).keywords(",");
		for (ISemanticRegion f : keywords) {
			doc.surround(f, indent());
		}
		for (EObject m : e.eContents()) {
			if ((m instanceof Content)) {
				//TODO: works but possibly fragile
				doc.prepend(m, newLine());
				if (!((Content)m).getContent().isEmpty() ||
						(((Content)m).getValue()!=null &&
						!((LiteralValue) ((Content)m).getValue()).getContent().isEmpty())) {
					doc.append(m, newLine());
				}
			} else {
			}
			doc.format(m);
		}
	}

	
	protected void format(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.append(open.get(0), newLine());
			doc.interior(open.get(0), close.get(0), indent());
		}
//		doc.interior(e, p->p.indent());
//		doc.interior(e, 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());
//				doc.prepend(a.getPreviousSemanticRegion(), newLine());
//				doc.surround(a.getPreviousSemanticRegion(), indent);
//				doc.append(a, newLine());
//			}
			List<ISemanticRegion> features = this.textRegionExtensions.regionFor(e).features(
					tdlPackage.Literals.BEHAVIOUR__TEST_OBJECTIVE
@@ -100,21 +150,20 @@ public class TDLtxFormatter extends AbstractJavaFormatter {
					);
			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());
				doc.prepend(f.getPreviousSemanticRegion(), newLine());
				////doc.surround(f.getPreviousSemanticRegion(), indent);
				doc.append(f, newLine());
			}
		}
		if (e instanceof Connection) {
			doc.prepend(e, p->p.newLine());
			doc.prepend(e, newLine());
		}

		if (e instanceof EventOccurrence) {
			doc.prepend(e, p->p.newLine());
			doc.surround(e, p->p.indent());
			doc.append(e, p->p.newLine());
			doc.prepend(e, newLine());
			doc.surround(e, indent());
			doc.append(e, newLine());
			//TODO: expand custom formatting - when then, event occurrence arguments, etc.
			return;
		}

		if (e instanceof ExpectedBehaviour) {
@@ -126,17 +175,23 @@ public class TDLtxFormatter extends AbstractJavaFormatter {
					m instanceof TimeConstraint //||
//					m instanceof Comment
					) { 
				doc.append(m, p->p.newLine());
				doc.append(m, newLine());
			} else if (
					m instanceof Annotation ||
					m instanceof Comment
					
			) {
				doc.prepend(m, p->p.newLine());
				doc.append(m, p->p.newLine());
				if (!(e instanceof EntityReference || e instanceof EventReference )) {
					doc.prepend(m, newLine());
					doc.append(m, newLine());
				}  else {
					//doc.prepend(m, newLine());
				}
			} else if (m instanceof EntityReference) {
				doc.prepend(m, newLine());
			} else if (m instanceof EventOccurrence) {
				doc.prepend(m, p->p.newLine());
				doc.append(m, p->p.newLine());
				doc.prepend(m, newLine());
				doc.append(m, newLine());
			} else if (
					m instanceof Annotation ||
					m instanceof Comment || 
@@ -144,42 +199,52 @@ public class TDLtxFormatter extends AbstractJavaFormatter {
					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.prepend(m, newLine());
				doc.surround(m, indent());
				doc.append(m, newLine());
			}
			doc.format(m);
		}
	}

	public Procedure1<? super IHiddenRegionFormatter> indent() {
		Procedure1<? super IHiddenRegionFormatter> indent = p->p.indent();
		return indent;
	}

	public Procedure1<? super IHiddenRegionFormatter> newLine() {
		Procedure1<? super IHiddenRegionFormatter> newLine = p->p.newLine();
		return newLine;
	}

	
	protected void format(org.etsi.mts.tdl.NamedElement e, IFormattableDocument doc) {
		doc.prepend(e, p->p.newLine());
//		doc.interior(e, p->p.indent());
	protected void format(NamedElement e, IFormattableDocument doc) {
		doc.prepend(e, newLine());
//		doc.interior(e, indent());
//		doc.format(e);
//		doc.interior(e, p->p.indent());
//		doc.interior(e, 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());
//			doc.append(open.get(0), newLine());
//			doc.interior(open.get(0), close.get(0), 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());
//						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();
			int i = 0;
			for (Member m : ((StructuredDataType)e).getMember()) {
				i++;
				doc.prepend(m, p->p.newLine());
				doc.surround(m, p->p.indent());
				doc.prepend(m, newLine());
				doc.surround(m, indent());
				if (i==size) {
					doc.append(m, p->p.newLine());
					doc.append(m, newLine());
				}
				doc.format(m);
			}
@@ -190,11 +255,11 @@ public class TDLtxFormatter extends AbstractJavaFormatter {
			int i = 0;
			for (MemberAssignment m : ((StructuredDataInstance)e).getMemberAssignment()) {
				i++;
				doc.prepend(m, p->p.newLine());
				doc.surround(m, p->p.indent());
//				doc.append(m, p->p.newLine());
				doc.prepend(m, newLine());
				doc.surround(m, indent());
//				doc.append(m, newLine());
				if (i==size) {
					doc.append(m, p->p.newLine());
					doc.append(m, newLine());
				}
				doc.format(m);
			}
@@ -205,11 +270,11 @@ public class TDLtxFormatter extends AbstractJavaFormatter {
			int i = 0;
			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());
				doc.prepend(m, newLine());
				doc.surround(m, indent());
//				doc.append(m, newLine()());
				if (i==size) {
					doc.append(m, p->p.newLine());
					doc.append(m, newLine());
				}
				doc.format(m);
			}
@@ -225,40 +290,41 @@ public class TDLtxFormatter extends AbstractJavaFormatter {
				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.prepend(m, newLine());
					doc.surround(m, indent());
					//doc.append(m, newLine());
					doc.format(m);
				}
				if (i==e.eContents().size()) {
					doc.append(m, p->p.newLine());
					doc.append(m, 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.prepend(m, newLine());
				doc.surround(m, indent());
//				doc.append(m, newLine());
				doc.format(m);
			}
			
			List<ISemanticRegion> close = this.textRegionExtensions.regionFor(e).ruleCallsTo(this._grammarAccess.getENDRule());
			if (!close.isEmpty()) {
				doc.prepend(close.get(0), p->p.newLine());
				doc.prepend(close.get(0), newLine());
			}

		}
		
		//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());
//			doc.prepend(dKeyword, newLine());
//			doc.surround(dKeyword, indent());
//			ISemanticRegion rKeyword = this.textRegionExtensions.regionFor(e).keyword("References:");
//			doc.prepend(rKeyword, p->p.newLine());
//			doc.surround(rKeyword, p->p.indent());
//			doc.prepend(rKeyword, newLine());
//			doc.surround(rKeyword, indent());
			
			List<ISemanticRegion> features = this.textRegionExtensions.regionFor(e).features(
					tdlPackage.Literals.TEST_OBJECTIVE__DESCRIPTION,
@@ -268,55 +334,65 @@ public class TDLtxFormatter extends AbstractJavaFormatter {
					);
			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());
				doc.prepend(f.getPreviousSemanticRegion(), newLine());
				doc.surround(f.getPreviousSemanticRegion(), indent());
				doc.append(f, newLine());
			}

			if (e instanceof StructuredTestObjective) {
				//TODO: this depends on keywords..
				List<ISemanticRegion> keywords = this.textRegionExtensions.regionFor(e).keywords("Configuration:", "PICS:");
				for (ISemanticRegion f : keywords) {
					doc.prepend(f, newLine());
					doc.surround(f, indent());
				}
				
				InitialConditions ic = ((StructuredTestObjective) e).getInitialConditions();
				doc.prepend(ic, p->p.newLine());
				doc.surround(ic, p->p.indent());
				doc.append(ic, p->p.newLine());
				doc.prepend(ic, newLine());
				doc.surround(ic, indent());
				doc.append(ic, newLine());
				doc.format(ic);
				ExpectedBehaviour eb = ((StructuredTestObjective) e).getExpectedBehaviour();
				doc.prepend(eb, p->p.newLine());
				doc.surround(eb, p->p.indent());
				doc.append(eb, p->p.newLine());
				doc.prepend(eb, newLine());
				doc.surround(eb, indent());
				doc.append(eb, newLine());
				doc.format(eb);
				FinalConditions fc = ((StructuredTestObjective) e).getFinalConditions();
				doc.prepend(fc, p->p.newLine());
				doc.surround(fc, p->p.indent());
				doc.append(fc, p->p.newLine());
				doc.prepend(fc, newLine());
				doc.surround(fc, indent());
				doc.append(fc, newLine());
				doc.format(fc);
			}

		}
		//All
		//DONE: exclude initial conditions -> not triggered
		ISemanticRegion withKeyword = this.textRegionExtensions.regionFor(e).keyword("with");
		if (withKeyword!=null) {
			doc.surround(withKeyword, p->p.newLine());
			doc.append(withKeyword, p->p.indent());
			doc.surround(withKeyword, newLine());
			doc.append(withKeyword, indent());
//			doc.interior(
//					withKeyword.getNextHiddenRegion().getNextSemanticRegion(), 
//					this.textRegionExtensions.nextHiddenRegion(e).getNextSemanticRegion(), 
//					p->p.indent()
//					indent()
//					);
		}
		//doc.prepend(withKeyword, p->p.indent());
		//doc.prepend(withKeyword, 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.surround(c, indent());
			}
			if (!(e instanceof EntityReference)) {
				doc.append(c, newLine());
			}
			doc.append(c, p->p.newLine());
			doc.format(c);
		}
		
		for (Annotation a : e.getAnnotation()) {
			doc.append(a, p->p.newLine());
			doc.append(a, newLine());
			doc.format(a);
		}
		
@@ -347,8 +423,8 @@ public class TDLtxFormatter extends AbstractJavaFormatter {

	protected void format(org.etsi.mts.tdl.DataUse e, IFormattableDocument doc) {
		for (ParameterBinding m : e.getArgument()) {
			doc.prepend(m, p->p.newLine());
			doc.surround(m, p->p.indent());
			doc.prepend(m, newLine());
			doc.surround(m, indent());
//			doc.append(m, p->p.newLine());
			doc.format(m);
		}