Newer
Older
/*
* generated by Xtext 2.25.0
*/
package org.etsi.mts.tdl.formatting2;
import java.util.List;
import org.eclipse.xtext.Assignment;
import org.eclipse.xtext.Keyword;
import org.eclipse.xtext.TerminalRule;
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.ParameterBinding;
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.DataElementUse;
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.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.structuredobjectives.EventOccurrence;
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.StructuredTestObjective;
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());
}
if (e instanceof EventOccurrence) {
doc.prepend(e, p->p.newLine());
doc.surround(e, p->p.indent());
doc.append(e, p->p.newLine());
//TODO: expand custom formatting - when then, event occurrence arguments, etc.
return;
}
if (e instanceof ExpectedBehaviour) {
}
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 EventOccurrence) {
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);
}
}
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
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);
}
List<ISemanticRegion> close = this.textRegionExtensions.regionFor(e).ruleCallsTo(this._grammarAccess.getENDRule());
if (!close.isEmpty()) {
doc.prepend(close.get(0), p->p.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());
// 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());
}
if (e instanceof StructuredTestObjective) {
InitialConditions ic = ((StructuredTestObjective) e).getInitialConditions();
doc.prepend(ic, p->p.newLine());
doc.surround(ic, p->p.indent());
doc.append(ic, p->p.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.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.format(fc);
}
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
}
//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 (Annotation a : e.getAnnotation()) {
doc.surround(a, p->p.newLine());
// doc.append(a, p->p.noIndentation());
doc.format(a);
// doc.prepend(a, p->p.newLine());
}
for (TestObjective a : e.getTestObjective()) {
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);
}
for (EObject m : e.eContents()) {
doc.format(m);
}
}
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
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.append(m, p->p.newLine());
doc.format(m);
}
if (e instanceof DataElementUse) {
for (DataUse m : ((DataElementUse) e).getItem()) {
doc.prepend(m, p->p.newLine());
doc.surround(m, p->p.indent());
// doc.append(m, p->p.newLine());
doc.format(m);
}
}
List<ISemanticRegion> closeParen = this.textRegionExtensions.regionFor(e).ruleCallsTo(this._grammarAccess.getRParenRule());
if (!closeParen.isEmpty()) {
doc.prepend(closeParen.get(0), p->p.newLine());
}
List<ISemanticRegion> closeItems = this.textRegionExtensions.regionFor(e).keywords("]");
if (!closeItems.isEmpty()) {
doc.prepend(closeItems.get(0), p->p.newLine());
}
}
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));
doc.format(elementImport);
}
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);
}
}