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
|
/*******************************************************************************
* Copyright (c) 2011 protos software gmbh (http://www.protos.de).
* 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:
* Juergen Haug (initial contribution)
*
*******************************************************************************/
package org.eclipse.etrice.core.fsm.formatting2
import com.google.inject.Inject
import org.eclipse.emf.ecore.EObject
import org.eclipse.etrice.core.common.converter.CCStringIndentation
import org.eclipse.etrice.core.common.converter.CC_StringConverter
import org.eclipse.etrice.core.common.formatting2.BaseFormatter
import org.eclipse.etrice.core.fsm.fSM.DetailCode
import org.eclipse.etrice.core.fsm.fSM.ProtocolSemantics
import org.eclipse.etrice.core.fsm.fSM.State
import org.eclipse.etrice.core.fsm.fSM.StateGraph
import org.eclipse.etrice.core.fsm.fSM.Transition
import org.eclipse.etrice.core.fsm.fSM.Trigger
import org.eclipse.etrice.core.fsm.fSM.TriggeredTransition
import org.eclipse.etrice.core.fsm.services.FSMGrammarAccess
import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor
import org.eclipse.xtext.formatting2.IFormattableDocument
import org.eclipse.xtext.formatting2.ITextReplacerContext
import org.eclipse.xtext.formatting2.internal.AbstractTextReplacer
import org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion
class FSMFormatter extends BaseFormatter {
@Inject extension FSMGrammarAccess
override void formatAllByKeywords(EObject it, extension IFormattableDocument document) {
super.formatAllByKeywords(it, document)
allRegionsFor.keywords('->', 'extends', '=', 'or', '|').forEach[surround[oneSpace]]
}
protected def prependDefaultNewLines(EObject it, extension IFormattableDocument document) {
if(previousHiddenRegion.lineCount > 2) prepend[newLines = 2] else prepend[newLine]
}
protected def prependDefaultNewLines(ISemanticRegion it, extension IFormattableDocument document) {
if(previousHiddenRegion.lineCount > 2) prepend[newLines = 2] else prepend[newLine]
}
def dispatch void format(StateGraph it, extension IFormattableDocument document) {
eContents.forEach[prependDefaultNewLines(document)]
}
def dispatch void format(State it, extension IFormattableDocument document) {
regionFor.keywords('entry', 'exit', 'do', 'subgraph').forEach[prependDefaultNewLines(document) append[oneSpace]]
}
def dispatch void format(Transition it, extension IFormattableDocument document) {
regionFor.keywords('action', 'guard', 'cond').forEach[prependDefaultNewLines(document) append[oneSpace]]
}
def dispatch void format(TriggeredTransition it, extension IFormattableDocument document) {
regionFor.keywords('action', 'guard', 'cond', 'triggers').forEach[prependDefaultNewLines(document) append[oneSpace]]
triggers.head.prepend[newLine]
triggers.tail.forEach[prepend[oneSpace]]
}
def dispatch void format(Trigger it, extension IFormattableDocument document) {
regionFor.keywordPairs('<', '>').forEach[interior[noSpace]]
}
def dispatch void format(ProtocolSemantics it, extension IFormattableDocument document) {
rules.forEach[prependDefaultNewLines(document)]
}
@FinalFieldsConstructor
static class DetailCodeReplacer extends AbstractTextReplacer {
override createReplacements(ITextReplacerContext context) {
if (region.multiline) {
val ccIndent = new CCStringIndentation(CC_StringConverter.stripDelim(region.text.trim))
val endIndent = if(ccIndent.ignoreLast) context.indentationString else ''
val replacement = ccIndent.replaceEditorIndentation(context.getIndentationString(context.indentation + 1)) + endIndent
context => [
addReplacement(region.replaceWith(CC_StringConverter.DELIM + replacement + CC_StringConverter.DELIM))
]
}
context
}
}
def dispatch void format(DetailCode detailcode, extension IFormattableDocument document) {
val ccRegion = detailcode.regionFor.assignment(detailCodeAccess.linesAssignment_0_1)
if(ccRegion !== null) {
detailcode.prepend[oneSpace]
if(detailcode.multiline) document.addReplacer(new DetailCodeReplacer(document, ccRegion))
} else {
detailcode.regionFor.assignments(detailCodeAccess.linesAssignment_1_1).forEach[prepend[newLine]]
}
}
}
|