Skip to main content
summaryrefslogtreecommitdiffstats
blob: 669d112fa3d116408e79cda63ac2ab22a731c89b (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
/*
 * generated by Xtext
 */
package org.eclipse.osee.framework.core.dsl.formatting;

import java.util.Arrays;
import java.util.List;
import java.util.Stack;
import org.eclipse.osee.framework.core.dsl.services.OseeDslGrammarAccess;
import org.eclipse.xtext.GrammarUtil;
import org.eclipse.xtext.Keyword;
import org.eclipse.xtext.formatting.IIndentationInformation;
import org.eclipse.xtext.formatting.impl.AbstractDeclarativeFormatter;
import org.eclipse.xtext.formatting.impl.FormattingConfig;

/**
 * This class contains custom formatting description. see :
 * http://www.eclipse.org/Xtext/documentation/latest/xtext.html#formatting on how and when to use it Also see
 * {@link org.eclipse.xtext.xtext.XtextFormattingTokenSerializer} as an example
 */
public class OseeDslFormatter extends AbstractDeclarativeFormatter implements IIndentationInformation {

   private final List<String> KEYWORDS = Arrays.asList(new String[] {
      "attribute",
      "sideAName",
      "sideAArtifactType",
      "sideBName",
      "sideBArtifactType",
      "defaultOrderType",
      "entryGuid",
      "multiplicity",
      "dataProvider",
      "min",
      "max",
      "taggerId",
      "enumType",
      "defaultValue",
      "entry",
      "guid",
      "uuid",
      "add",
      "remove",
      "inheritsAll",
      "description",
      "min",
      "max",
      "dataProvider",
      "defaultValue",
      "fileExtension",
      "mediaType",
      "taggerId",
      "accessContext",
      "ALLOW",
      "DENY",
      "AND",
      "OR"});

   private boolean isKeywordEntry(String current) {
      return KEYWORDS.contains(current);
   }

   @Override
   protected void configureFormatting(FormattingConfig c) {
      OseeDslGrammarAccess access = (OseeDslGrammarAccess) getGrammarAccess();

      c.setAutoLinewrap(120);
      //      c.setIndentationSpace("   ");

      Iterable<Keyword> keywords = GrammarUtil.containedKeywords(access.getGrammar());
      Stack<Keyword> openBraceStack = new Stack<>();

      for (Keyword currentKeyword : keywords) {
         String current = currentKeyword.getValue();
         if ("{".equals(current)) {
            openBraceStack.add(currentKeyword);
            c.setLinewrap().after(currentKeyword);
         } else if ("}".equals(current)) {
            c.setLinewrap().before(currentKeyword);
            c.setLinewrap(2).after(currentKeyword);
            if (!openBraceStack.isEmpty()) {
               c.setIndentation(openBraceStack.pop(), currentKeyword);
            }
         } else if (";".equals(current)) {
            c.setSpace("").before(currentKeyword);
            c.setLinewrap(1).after(currentKeyword);
         } else if (isKeywordEntry(current)) {
            c.setLinewrap().before(currentKeyword);
         }
      }
      c.setLinewrap(0, 1, 2).after(access.getImportRule());
      c.setLinewrap(0, 1, 2).before(access.getSL_COMMENTRule());
      c.setLinewrap(0, 1, 2).before(access.getML_COMMENTRule());
      c.setLinewrap(0, 1, 1).after(access.getML_COMMENTRule());
   }

   @Override
   public String getIndentString() {
      return "   ";
   }
}

Back to the top