Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8b8e04c15d6004042a47496b7ea572d1501dea46 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
 *                                                                            
 * 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)
				}
			}
		}
	}
}

Back to the top