blob: d529006636caa6a2a7d92e0f56049766511263fe [file] [log] [blame]
/**
* Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation
*
*
* This copyright notice shows up in the generated Java code
*/
package org.eclipse.osbp.utils.xtext;
import com.google.common.base.Objects;
import java.util.List;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.impl.EClassImpl;
import org.eclipse.xtext.AbstractRule;
import org.eclipse.xtext.Grammar;
import org.eclipse.xtext.GrammarUtil;
import org.eclipse.xtext.IGrammarAccess;
import org.eclipse.xtext.Keyword;
import org.eclipse.xtext.ParserRule;
import org.eclipse.xtext.RuleCall;
import org.eclipse.xtext.TerminalRule;
import org.eclipse.xtext.TypeRef;
import org.eclipse.xtext.formatting.impl.FormattingConfig;
import org.eclipse.xtext.impl.ParserRuleImpl;
import org.eclipse.xtext.util.Pair;
import org.eclipse.xtext.xbase.services.XtypeGrammarAccess;
@SuppressWarnings("all")
public class GenericFormatter {
/**
* <p>
* Variant of method when no import block is specified. Then only one empty line before each
* first level element of the given element name is inserted.
* </p>
*
* @example <u>ReportDSLFormatter</u>:<br>
* <code>
* ...FormattingConfig cfg<br>
* gf = new GenericFormatter()<br>
* gf.formatFirstLevelBlocks( cfg, grammar.grammar, "PageTemplates" )
* </code>
*
* @param config Dsl-specific FormattingConfig instance
* @param grammarRaw Dsl-specific grammar
* @param baseElementName Name of dsl-specific class of first level elements
*
* @return void
*
* @since 20160728, gu
* @changed yymmdd, name
*/
public void formatFirstLevelBlocks(final FormattingConfig config, final Grammar grammarRaw, final String baseElementName) {
this.formatFirstLevelBlocks(config, grammarRaw, baseElementName, null);
}
/**
* <p>
* Formats intersection between first level elements (= first level underneath package
* definition, i.e. import and following blocks).<br>
* Inserts one empty line between two blocks each.
* </p>
*
* @example <u>DatamartDSLFormatter</u>:<br>
* <code>
* ...FormattingConfig cfg<br>
* gf = new GenericFormatter()<br>
* gf.formatFirstLevelBlocks( cfg, grammar.grammar, "DatamartImport", "DatamartDefinition" )
* </code>
*
* @param config Dsl-specific FormattingConfig instance
* @param grammarRaw Dsl-specific grammar
* @param baseElementName Name of dsl-specific class of first level elements
* @param importClassName Name of dsl-specific import class
*
* @return void
*
* @since 20160728, gu
* @changed yymmdd, name
*/
public void formatFirstLevelBlocks(final FormattingConfig config, final Grammar grammarRaw, final String baseElementName, final String importClassName) {
AbstractRule _findRuleForName = GrammarUtil.findRuleForName(grammarRaw, baseElementName);
ParserRule baseElementDef = ((ParserRule) _findRuleForName);
boolean _notEquals = (!Objects.equal(importClassName, null));
if (_notEquals) {
AbstractRule _findRuleForName_1 = GrammarUtil.findRuleForName(grammarRaw, importClassName);
ParserRule importDef = ((ParserRule) _findRuleForName_1);
FormattingConfig.LinewrapLocator _setLinewrap = config.setLinewrap(2);
_setLinewrap.between(importDef, baseElementDef);
}
FormattingConfig.LinewrapLocator _setLinewrap_1 = config.setLinewrap(2);
_setLinewrap_1.before(baseElementDef);
}
/**
* Overloaded variant for those who can't provide a <i>XtypeGrammarAccess</i> instance.
*/
public void genericFormatting(final FormattingConfig config, final IGrammarAccess grammar) {
this.genericFormatting(config, grammar, null);
}
public void genericFormatting(final FormattingConfig config, final IGrammarAccess grammar, final XtypeGrammarAccess grammarAccess) {
config.setAutoLinewrap(400);
boolean _notEquals = (!Objects.equal(grammarAccess, null));
if (_notEquals) {
FormattingConfig.LinewrapLocator _setLinewrap = config.setLinewrap(0, 1, 2);
TerminalRule _sL_COMMENTRule = grammarAccess.getSL_COMMENTRule();
_setLinewrap.before(_sL_COMMENTRule);
FormattingConfig.LinewrapLocator _setLinewrap_1 = config.setLinewrap(0, 1, 1);
TerminalRule _sL_COMMENTRule_1 = grammarAccess.getSL_COMMENTRule();
_setLinewrap_1.after(_sL_COMMENTRule_1);
FormattingConfig.LinewrapLocator _setLinewrap_2 = config.setLinewrap(0, 1, 2);
TerminalRule _mL_COMMENTRule = grammarAccess.getML_COMMENTRule();
_setLinewrap_2.before(_mL_COMMENTRule);
FormattingConfig.LinewrapLocator _setLinewrap_3 = config.setLinewrap(1, 1, 2);
TerminalRule _mL_COMMENTRule_1 = grammarAccess.getML_COMMENTRule();
_setLinewrap_3.after(_mL_COMMENTRule_1);
}
List<Pair<Keyword, Keyword>> _findKeywordPairs = grammar.findKeywordPairs("{", "}");
for (final Pair<Keyword, Keyword> pair : _findKeywordPairs) {
{
FormattingConfig.SpaceLocator _setSpace = config.setSpace(" ");
Keyword _first = pair.getFirst();
_setSpace.before(_first);
Keyword _first_1 = pair.getFirst();
Keyword _second = pair.getSecond();
config.setIndentation(_first_1, _second);
FormattingConfig.LinewrapLocator _setLinewrap_4 = config.setLinewrap();
Keyword _first_2 = pair.getFirst();
_setLinewrap_4.after(_first_2);
FormattingConfig.LinewrapLocator _setLinewrap_5 = config.setLinewrap(1, 1, 2);
Keyword _second_1 = pair.getSecond();
_setLinewrap_5.around(_second_1);
}
}
List<Keyword> _findKeywords = grammar.findKeywords(".");
for (final Keyword kw : _findKeywords) {
{
FormattingConfig.NoSpaceLocator _setNoSpace = config.setNoSpace();
_setNoSpace.around(kw);
FormattingConfig.NoLinewrapLocator _setNoLinewrap = config.setNoLinewrap();
_setNoLinewrap.around(kw);
}
}
List<Keyword> _findKeywords_1 = grammar.findKeywords(";");
for (final Keyword kw_1 : _findKeywords_1) {
{
FormattingConfig.NoSpaceLocator _setNoSpace = config.setNoSpace();
_setNoSpace.before(kw_1);
FormattingConfig.LinewrapLocator _setLinewrap_4 = config.setLinewrap();
_setLinewrap_4.after(kw_1);
}
}
List<Keyword> _findKeywords_2 = grammar.findKeywords(",");
for (final Keyword kw_2 : _findKeywords_2) {
FormattingConfig.NoSpaceLocator _setNoSpace = config.setNoSpace();
_setNoSpace.before(kw_2);
}
List<Pair<Keyword, Keyword>> _findKeywordPairs_1 = grammar.findKeywordPairs("(", ")");
for (final Pair<Keyword, Keyword> pair_1 : _findKeywordPairs_1) {
{
FormattingConfig.NoSpaceLocator _setNoSpace_1 = config.setNoSpace();
Keyword _first = pair_1.getFirst();
_setNoSpace_1.before(_first);
FormattingConfig.SpaceLocator _setSpace = config.setSpace(" ");
Keyword _first_1 = pair_1.getFirst();
_setSpace.after(_first_1);
FormattingConfig.NoLinewrapLocator _setNoLinewrap = config.setNoLinewrap();
Keyword _first_2 = pair_1.getFirst();
_setNoLinewrap.around(_first_2);
FormattingConfig.SpaceLocator _setSpace_1 = config.setSpace(" ");
Keyword _second = pair_1.getSecond();
_setSpace_1.before(_second);
FormattingConfig.NoLinewrapLocator _setNoLinewrap_1 = config.setNoLinewrap();
Keyword _second_1 = pair_1.getSecond();
_setNoLinewrap_1.before(_second_1);
FormattingConfig.LinewrapLocator _setLinewrap_4 = config.setLinewrap(0, 0, 1);
Keyword _second_2 = pair_1.getSecond();
_setLinewrap_4.after(_second_2);
}
}
List<Pair<Keyword, Keyword>> _findKeywordPairs_2 = grammar.findKeywordPairs("[", "]");
for (final Pair<Keyword, Keyword> pair_2 : _findKeywordPairs_2) {
{
FormattingConfig.NoSpaceLocator _setNoSpace_1 = config.setNoSpace();
Keyword _first = pair_2.getFirst();
_setNoSpace_1.before(_first);
FormattingConfig.SpaceLocator _setSpace = config.setSpace(" ");
Keyword _first_1 = pair_2.getFirst();
_setSpace.after(_first_1);
FormattingConfig.NoLinewrapLocator _setNoLinewrap = config.setNoLinewrap();
Keyword _first_2 = pair_2.getFirst();
_setNoLinewrap.around(_first_2);
FormattingConfig.SpaceLocator _setSpace_1 = config.setSpace(" ");
Keyword _second = pair_2.getSecond();
_setSpace_1.before(_second);
FormattingConfig.NoSpaceLocator _setNoSpace_2 = config.setNoSpace();
Keyword _second_1 = pair_2.getSecond();
_setNoSpace_2.after(_second_1);
FormattingConfig.NoLinewrapLocator _setNoLinewrap_1 = config.setNoLinewrap();
Keyword _second_2 = pair_2.getSecond();
_setNoLinewrap_1.around(_second_2);
}
}
Grammar _grammar = grammar.getGrammar();
List<AbstractRule> allRules = GrammarUtil.allRules(_grammar);
int _size = allRules.size();
AbstractRule[] _newArrayOfSize = new AbstractRule[_size];
AbstractRule[] _array = allRules.<AbstractRule>toArray(_newArrayOfSize);
List<RuleCall> ruleCalls = grammar.findRuleCalls(_array);
for (final RuleCall ruleCall : ruleCalls) {
{
AbstractRule _rule = ruleCall.getRule();
String _name = _rule.getName();
boolean _equals = _name.equals("OperatorEnum");
if (_equals) {
FormattingConfig.NoLinewrapLocator _setNoLinewrap = config.setNoLinewrap();
_setNoLinewrap.around(ruleCall);
}
boolean _and = false;
AbstractRule _rule_1 = ruleCall.getRule();
if (!(_rule_1 instanceof ParserRuleImpl)) {
_and = false;
} else {
AbstractRule _rule_2 = ruleCall.getRule();
TypeRef _type = _rule_2.getType();
EClassifier _classifier = _type.getClassifier();
_and = (_classifier instanceof EClassImpl);
}
if (_and) {
AbstractRule _rule_3 = ruleCall.getRule();
String _name_1 = _rule_3.getName();
boolean _equals_1 = _name_1.equals("MessageDefaultFormat");
if (_equals_1) {
FormattingConfig.SpaceLocator _setSpace = config.setSpace(" ");
_setSpace.before(ruleCall);
}
boolean _and_1 = false;
boolean _and_2 = false;
boolean _or = false;
AbstractRule _rule_4 = ruleCall.getRule();
String _name_2 = _rule_4.getName();
boolean _startsWith = _name_2.startsWith("X");
boolean _not = (!_startsWith);
if (_not) {
_or = true;
} else {
AbstractRule _rule_5 = ruleCall.getRule();
String _name_3 = _rule_5.getName();
boolean _equals_2 = _name_3.equals("XImportSection");
_or = _equals_2;
}
if (!_or) {
_and_2 = false;
} else {
AbstractRule _rule_6 = ruleCall.getRule();
String _name_4 = _rule_6.getName();
boolean _contains = _name_4.contains("Jvm");
boolean _not_1 = (!_contains);
_and_2 = _not_1;
}
if (!_and_2) {
_and_1 = false;
} else {
AbstractRule _rule_7 = ruleCall.getRule();
String _name_5 = _rule_7.getName();
boolean _contains_1 = _name_5.contains("Expression");
boolean _not_2 = (!_contains_1);
_and_1 = _not_2;
}
if (_and_1) {
FormattingConfig.LinewrapLocator _setLinewrap_4 = config.setLinewrap(0, 1, 2);
AbstractRule _rule_8 = ruleCall.getRule();
_setLinewrap_4.before(_rule_8);
}
}
}
}
}
}