blob: 175ab69ae5c1b6743ca931a9125a9648283e376c [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 org.eclipse.emf.ecore.impl.EClassImpl
import org.eclipse.xtext.Grammar
import org.eclipse.xtext.GrammarUtil
import org.eclipse.xtext.IGrammarAccess
import org.eclipse.xtext.ParserRule
import org.eclipse.xtext.formatting.impl.FormattingConfig
import org.eclipse.xtext.impl.ParserRuleImpl
import org.eclipse.xtext.xbase.services.XtypeGrammarAccess
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
*/
def formatFirstLevelBlocks ( FormattingConfig config, Grammar grammarRaw, String baseElementName ) {
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
*/
def formatFirstLevelBlocks ( FormattingConfig config, Grammar grammarRaw, String baseElementName, String importClassName ) {
var ParserRule baseElementDef = GrammarUtil.findRuleForName( grammarRaw, baseElementName ) as ParserRule
if ( importClassName != null ) {
var ParserRule importDef = GrammarUtil.findRuleForName( grammarRaw, importClassName ) as ParserRule
config.setLinewrap( 2 ).between( importDef, baseElementDef )
}
config.setLinewrap( 2 ).before( baseElementDef )
// config.setLinewrap( 1, 2, 2 ).before( baseElementDef )
}
/**
* Overloaded variant for those who can't provide a <i>XtypeGrammarAccess</i> instance.
*/
def genericFormatting ( FormattingConfig config, IGrammarAccess grammar ) {
genericFormatting( config, grammar, null )
}
def genericFormatting ( FormattingConfig config, IGrammarAccess grammar, XtypeGrammarAccess grammarAccess ) {
config.setAutoLinewrap(400)
if ( grammarAccess != null ) {
// It's usually a good idea to activate the following three statements.
// They will add and preserve newlines around comments
// c.setLinewrap( 0, 1, 2 ).before( SL_COMMENTRule )
// c.setLinewrap( 0, 1, 2 ).before( ML_COMMENTRule )
// c.setLinewrap( 0, 1, 1 ).after ( ML_COMMENTRule )
config.setLinewrap( 0, 1, 2 ).before( grammarAccess.SL_COMMENTRule )
config.setLinewrap( 0, 1, 1 ).after ( grammarAccess.SL_COMMENTRule )
config.setLinewrap( 0, 1, 2 ).before( grammarAccess.ML_COMMENTRule )
config.setLinewrap( 1, 1, 2 ).after ( grammarAccess.ML_COMMENTRule )
}
for(pair : grammar.findKeywordPairs("{", "}")) {
// a space before the first '{'
config.setSpace(" ").before(pair.getFirst()) //$NON-NLS-1$
// indentation between
config.setIndentation(pair.first, pair.second)
config.setLinewrap().after(pair.first)
// config.setLinewrap( 1, 1, 2 ).after(pair.first)
config.setLinewrap( 1, 1, 2 ).around(pair.second)
// config.setLinewrap(1,1,1).after(pair.first) 1 for all is default!
// config.setLinewrap(1,1,1).around(pair.second)
}
for(kw : grammar.findKeywords(".")) {
config.setNoSpace().around(kw)
config.setNoLinewrap().around(kw)
}
for (kw : grammar.findKeywords(";")) {
config.setNoSpace().before(kw)
config.setLinewrap().after(kw)
}
for (kw : grammar.findKeywords(",")) {
config.setNoSpace().before(kw)
}
for ( pair : grammar.findKeywordPairs( "(", ")" ) ) {
config.setNoSpace().before(pair.first)
config.setSpace(" ").after(pair.first)
config.setNoLinewrap().around(pair.first)
config.setSpace( " " ).before(pair.second)
// config.setNoSpace().before(pair.second)
// config.setNoLinewrap().around(pair.second)
config.setNoLinewrap().before(pair.second)
config.setLinewrap( 0, 0, 1 ).after( pair.second )
// config.setNoLinewrap().bounds( pair.first, pair.second )
// config.setNoLinewrap().range( pair.first, pair.second )
}
for(pair : grammar.findKeywordPairs("[", "]")) {
config.setNoSpace().before(pair.first)
config.setSpace(" ").after(pair.first)
// config.setNoSpace().around(pair.first)
config.setNoLinewrap().around(pair.first)
config.setSpace( " " ).before(pair.second)
config.setNoSpace().after(pair.second)
// config.setNoSpace().around(pair.second)
config.setNoLinewrap().around(pair.second)
}
var allRules = GrammarUtil.allRules(grammar.grammar)
// linewrap before all rules
var ruleCalls = grammar.findRuleCalls(allRules.toArray(newArrayOfSize(allRules.size)))
for(ruleCall : ruleCalls) {
// No newline before and after comparison operators (e.g. '=', '<', 'like', etc.)
// (Currently only used for Datamart-DSL!)
if ( ruleCall.rule.name.equals("OperatorEnum") ) {
config.setNoLinewrap().around( ruleCall )
}
if (ruleCall.rule instanceof ParserRuleImpl &&
ruleCall.rule.type.classifier instanceof EClassImpl ) {
if ( ruleCall.rule.name.equals( "MessageDefaultFormat" ) ) {
config.setSpace( " " ).before( ruleCall )
// config.setNoLinewrap().before( ruleCall )
// config.setLinewrap ().before( ruleCall )
}
// if ( ruleCall.rule.name.contains("Operator") ) {
// config.setNoLinewrap().around( ruleCall )
// }
if ( ( !ruleCall.rule.name.startsWith("X") || ruleCall.rule.name.equals("XImportSection") ) &&
// if ( !ruleCall.rule.name.startsWith("X") &&
!ruleCall.rule.name.contains("Jvm") &&
// !ruleCall.rule.name.contains("Addition") &&
// !ruleCall.rule.name.contains("Operand") &&
// !ruleCall.rule.name.contains("Operator") &&
// !ruleCall.rule.name.contains("Property") &&
!ruleCall.rule.name.contains("Expression") ) {
config.setLinewrap( 0, 1, 2 ).before(ruleCall.rule)
// config.setLinewrap(1,1,1).around(ruleCall.rule)
}
}
}
}
}