Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9066c5a6576d9f46d468c6c3a6d93b3826f1db33 (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
/*******************************************************************************
 * 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.BaseConverterService
import org.eclipse.etrice.core.common.formatting2.BaseFormatter
import org.eclipse.etrice.core.common.formatting2.CCStringReplacer
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.xtext.formatting2.IFormattableDocument
import org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion
import org.eclipse.etrice.core.fsm.fSM.RefinedTransition
import org.eclipse.etrice.core.fsm.fSM.TransitionBase

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 !== null && 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)]
		eContents.forEach[format] // format children
	}
	
	def dispatch void format(State it, extension IFormattableDocument document) {
		regionFor.keywords('entry', 'exit', 'do', 'subgraph').forEach[prependDefaultNewLines(document) append[oneSpace]]
		eContents.forEach[format] // format children
	}
	
	def dispatch void format(TransitionBase it, extension IFormattableDocument document) {
		regionFor.keywords('action', 'guard', 'cond', 'triggers').forEach[prependDefaultNewLines(document) append[oneSpace]]	
		if(it instanceof TriggeredTransition) {
			triggers.head.prepend[newLine]
			triggers.tail.forEach[prepend[oneSpace]]
		}
		eContents.forEach[format] // format children
	}
	
	def dispatch void format(Trigger it, extension IFormattableDocument document) {
		regionFor.keywordPairs('<', '>').forEach[interior[noSpace]]
		eContents.forEach[format] // format children
	}
	
	def dispatch void format(ProtocolSemantics it, extension IFormattableDocument document) {
		rules.forEach[prependDefaultNewLines(document)]
		eContents.forEach[format] // format children
	}
	
	@Inject
	BaseConverterService converterService
	
	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 CCStringReplacer(document, ccRegion, converterService.CC_StringConverter))
		} else {
			detailcode.regionFor.assignments(detailCodeAccess.linesAssignment_1_1).forEach[prepend[newLine]]
		}
	}
}

Back to the top