| /** |
| * |
| * Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), 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: |
| * Florian Pirchner - Initial implementation |
| * |
| * This copyright notice shows up in the generated Java code |
| * |
| */ |
| package org.eclipse.osbp.xtext.oxtype.formatting |
| |
| import org.eclipse.emf.ecore.impl.EClassImpl |
| import org.eclipse.osbp.xtext.oxtype.services.OXtypeGrammarAccess |
| 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 |
| |
| class GenericFormatter { |
| |
| /** |
| * <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) { |
| var ParserRule baseElementDef = GrammarUtil.findRuleForName(grammarRaw, baseElementName) as ParserRule |
| config.setLinewrap(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, OXtypeGrammarAccess grammarAccess) { |
| config.setAutoLinewrap(400) |
| |
| // import statements |
| config.setLinewrap(1, 1, 2).before(grammarAccess.XImportDeclarationRule) |
| |
| 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) |
| } |
| } |
| } |
| } |
| } |