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.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());
}
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
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);
}
}
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
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));
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);
}
}