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

import java.util.List;

import org.eclipse.xtext.formatting2.AbstractJavaFormatter;
import org.eclipse.xtext.formatting2.IFormattableDocument;
import org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion;
import org.eclipse.xtext.xbase.lib.Extension;
import org.etsi.mts.tdl.Annotation;
import org.etsi.mts.tdl.Comment;
import org.etsi.mts.tdl.ElementImport;
import org.etsi.mts.tdl.NamedElement;
import org.etsi.mts.tdl.Element;
import org.etsi.mts.tdl.MemberAssignment;
import org.etsi.mts.tdl.PackageableElement;
import org.etsi.mts.tdl.ParameterMapping;
import org.etsi.mts.tdl.TestObjective;
import org.etsi.mts.tdl.services.TDLtxGrammarAccess;

import com.google.inject.Inject;

public class TDLtxFormatter extends AbstractJavaFormatter {
	@Inject
	@Extension
	private TDLtxGrammarAccess _grammarAccess;

	//lazy short default formatting approach based on delimiters
	//TODO: it does not work with white-space delimiters
	protected void formatCustom(Element e, IFormattableDocument doc) {
		doc.prepend(e, p -> p.setNewLines(1));
		var rbs = this.textRegionExtensions.regionFor(e).ruleCallsTo(this._grammarAccess.getBEGINRule());
		var res = this.textRegionExtensions.regionFor(e).ruleCallsTo(this._grammarAccess.getENDRule());
Philip Makedonski's avatar
Philip Makedonski committed
//		System.out.println(res.size());
		int i = 0;
		for (var rs : rbs) {
			i++;
			var re = res.get(res.size() - i);
			doc.interior(rs, re, p -> p.indent());
		}

		for (var r : rbs) {
			doc.append(r, p -> p.setNewLines(1));
		}
		for (var r : res) {
			doc.surround(r, p -> p.setNewLines(1));
		}
	}
	
	protected void format(org.etsi.mts.tdl.NamedElement e, IFormattableDocument doc) {
//		doc.interior(e, p->p.indent());
		formatCustom(e, doc);

		for (Comment comment : e.getComment()) {
			doc.surround(comment, p -> p.setNewLines(1));
			doc.format(comment);
		}
		for (Annotation _annotation : e.getAnnotation()) {
Philip Makedonski's avatar
Philip Makedonski committed
//			this.textRegionExtensions.regionFor(e).keyword("@") 			
			doc.append(_annotation, p -> p.setNewLines(1));
Philip Makedonski's avatar
Philip Makedonski committed
//		doc.surround(this.textRegionExtensions.regionFor(e).keyword("@"), p->p.noSpace());
		for (var c : e.eContents()) {
			if (c instanceof NamedElement 
					|| c instanceof MemberAssignment
					|| c instanceof ParameterMapping
					|| c instanceof Comment
					) {
				doc.surround(c, p->p.setNewLines(1));
				formatCustom((Element)c, doc);
			}
		}
		if (e instanceof TestObjective) {
			//TODO: a bit of a hack
			for (var rs : this.textRegionExtensions.regionFor(e).ruleCallsTo(this._grammarAccess.getEStringRule())) {
				doc.append(rs, p -> p.setNewLines(1));
			}
		}

		
	}

	protected void format(org.etsi.mts.tdl.Package _package, IFormattableDocument doc) {
		// TODO: format HiddenRegions around keywords, attributes, cross references,
		// etc.
		org.etsi.mts.tdl.Package e = _package;
		formatCustom(e, doc);
		
		var dots = this.textRegionExtensions.allRegionsFor(e).keywords(".", "@");
		for (var dot : dots) {
			doc.surround(dot, p -> p.noSpace());
		}

		for (Comment comment : _package.getComment()) {
			doc.surround(comment, p -> p.setNewLines(1));
			doc.format(comment);
		}
		for (Annotation _annotation : _package.getAnnotation()) {
			doc.surround(_annotation, p -> p.setNewLines(1));
			doc.format(_annotation);
		}
		for (PackageableElement packageableElement : _package.getPackagedElement()) {
			doc.format(packageableElement);
		}
		for (ElementImport elementImport : _package.getImport()) {
			doc.surround(elementImport, p -> p.setNewLines(1));
			doc.format(elementImport);
		}
		for (org.etsi.mts.tdl.Package __package : _package.getNestedPackage()) {
			doc.format(__package);
		}
	}

	protected void format(Comment comment, IFormattableDocument doc) {
		// TODO: format HiddenRegions around keywords, attributes, cross references,
		// etc.
		doc.surround(comment, p -> p.setNewLines(1));
	}

	// TODO: implement for Annotation, ElementImport, AnnotationType, Extension,
	// TestObjective, DataResourceMapping, DataElementMapping, SimpleDataType,
	// SimpleDataInstance, StructuredDataType, StructuredDataInstance, Action,
	// Function, ComponentType, GateType, Time, TestConfiguration, TestDescription,
	// PredefinedFunction, ProcedureSignature, CollectionDataType,
	// CollectionDataInstance, ParameterMapping, Member, FormalParameter,
	// ProcedureParameter, MemberAssignment, ParameterBinding, MemberReference,
	// OmitValue, AnyValue, AnyValueOrOmit, DataInstanceUse, LiteralValueUse,
	// FormalParameterUse, FunctionCall, VariableUse, TimeLabelUse,
	// PredefinedFunctionCall, ComponentInstance, Variable, TimeLabel, GateInstance,
	// Timer, Connection, GateReference, BehaviourDescription, TimerStart,
	// TimerStop, TimeOut, Wait, Quiescence, PeriodicBehaviour,
	// AlternativeBehaviour, ParallelBehaviour, BoundedLoopBehaviour,
	// OptionalBehaviour, UnboundedLoopBehaviour, ConditionalBehaviour,
	// CompoundBehaviour, DefaultBehaviour, InterruptBehaviour, VerdictAssignment,
	// Assertion, Stop, Break, Assignment, InlineAction, ActionReference,
	// TestDescriptionReference, Interaction, Message, ProcedureCall,
	// TimeConstraint, Block, LocalExpression, ComponentInstanceBinding, Target,
	// ValueAssignment
}