Changeset 28 in default


Ignore:
Timestamp:
11/02/10 16:39:02 (14 years ago)
Author:
zeiss
Message:
 
Location:
trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/ui/formatter
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/ui/formatter/EbnfFormatterVisitor.java

    r27 r28  
    22 
    33import java.util.ArrayList; 
     4import java.util.Stack; 
    45 
    56import org.eclipse.emf.ecore.EObject; 
     
    4849        private String formattedTextNoWhitespaces; 
    4950        private String originalTextNoWhitespaces; 
     51        private int newLineOffsetCounter = 0; 
     52        private int rightHandSideRuleOffset = 0; 
     53        private Stack<Integer> ruleSpacingStack = new Stack<Integer>(); 
    5054         
    5155        public EbnfFormatterVisitor(EObject rootNode, FormatterConfig config) { 
     
    263267        private void text(String str) { 
    264268                buf.append(str); 
     269                newLineOffsetCounter += str.length(); 
    265270        } 
    266271 
    267272        private void space() { 
    268273                buf.append(" "); 
     274                newLineOffsetCounter++; 
     275        } 
     276         
     277        private void spaces(int count) { 
     278                for (int i=0; i < count; i++) { 
     279                        buf.append(" "); 
     280                } 
    269281        } 
    270282 
     
    350362        protected void visitBefore(GroupedSequence node) { 
    351363                text("("); 
     364                ruleSpacingStack.push(newLineOffsetCounter); 
    352365        } 
    353366 
    354367        protected void visitAfter(GroupedSequence node) { 
    355368                text(")"); 
     369                ruleSpacingStack.pop(); 
    356370        } 
    357371 
     
    378392        protected void visitBefore(OptionalSequence node) { 
    379393                text("["); 
     394                ruleSpacingStack.push(newLineOffsetCounter); 
    380395        } 
    381396 
    382397        protected void visitAfter(OptionalSequence node) { 
    383398                text("]"); 
     399                ruleSpacingStack.pop(); 
    384400        } 
    385401 
    386402        protected void visitBefore(RepeatedSequence node) { 
    387403                text("{"); 
     404                ruleSpacingStack.push(newLineOffsetCounter); 
    388405        } 
    389406 
     
    392409                if (node.isMorethanonce()) 
    393410                        text("+"); 
     411                ruleSpacingStack.pop(); 
    394412        } 
    395413 
     
    400418                lastWasSectionHeading=false; 
    401419 
     420                newLineOffsetCounter = 0; 
     421 
    402422                if (node.getRulenumber() > 0) 
    403423                        text(node.getRulenumber() + ". "); 
    404424                 
    405425                text(node.getName() + " ::= "); 
     426                 
     427                rightHandSideRuleOffset = newLineOffsetCounter; 
     428                ruleSpacingStack.push(newLineOffsetCounter); 
    406429        } 
    407430 
     
    409432                text(";"); 
    410433                newLine(); 
     434                ruleSpacingStack.pop(); 
    411435        } 
    412436 
     
    443467 
    444468        protected void visitAfter(SingleDefinition node) { 
    445                 if (!isLastElement()) 
     469                if (!isLastElement()) { 
    446470                        text(" | "); 
    447                  
     471                        if (config.isNewLineAfterAlternative()) { 
     472                                if (config.isPreventNewLineAfterAlternativeOnLessThanThreeElements()) { 
     473                                        DefinitionList definitionList = (DefinitionList) node.eContainer(); 
     474                                        if (definitionList.eContents().size() > 2) { 
     475                                                newLine(); 
     476                                                spaces(ruleSpacingStack.peek()); 
     477                                        } 
     478                                } else { 
     479                                        newLine(); 
     480                                        spaces(ruleSpacingStack.peek()); 
     481                                } 
     482                        } 
     483                } 
    448484        } 
    449485 
  • trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/ui/formatter/FormatterConfig.java

    r9 r28  
    22 
    33public class FormatterConfig { 
    4         private boolean useSpaces = true; // false=tabs, true=whitespaces 
    5         private int lineWrap = 80; // line in which the output should be wrapped 
    6         private boolean emptyLineBetweenRules = false; 
    7         private boolean emptyLineAfterMultiLineRule = false; 
    8         private boolean emptyLineBeforeSection = true; 
    9         private boolean emptyLineAfterSection = true; 
     4        private boolean newLineAfterAlternative = true; 
     5        private boolean preventNewLineAfterAlternativeOnLessThanThreeElements = true; 
    106 
    11         public boolean isUseSpaces() { 
    12                 return useSpaces; 
     7        public boolean isNewLineAfterAlternative() { 
     8                return newLineAfterAlternative; 
    139        } 
    1410 
    15         public void setUseSpaces(boolean useSpaces) { 
    16                 this.useSpaces = useSpaces; 
     11        public void setNewLineAfterAlternative(boolean newLineAfterAlternative) { 
     12                this.newLineAfterAlternative = newLineAfterAlternative; 
    1713        } 
    1814 
    19         public int getLineWrap() { 
    20                 return lineWrap; 
     15        public boolean isPreventNewLineAfterAlternativeOnLessThanThreeElements() { 
     16                return preventNewLineAfterAlternativeOnLessThanThreeElements; 
    2117        } 
    2218 
    23         public void setLineWrap(int lineWrap) { 
    24                 this.lineWrap = lineWrap; 
    25         } 
    26  
    27         public boolean isEmptyLineBetweenRules() { 
    28                 return emptyLineBetweenRules; 
    29         } 
    30  
    31         public void setEmptyLineBetweenRules(boolean emptyLineBetweenRules) { 
    32                 this.emptyLineBetweenRules = emptyLineBetweenRules; 
    33         } 
    34  
    35         public boolean isEmptyLineAfterMultiLineRule() { 
    36                 return emptyLineAfterMultiLineRule; 
    37         } 
    38  
    39         public void setEmptyLineAfterMultiLineRule( 
    40                         boolean emptyLineAfterMultiLineRule) { 
    41                 this.emptyLineAfterMultiLineRule = emptyLineAfterMultiLineRule; 
    42         } 
    43  
    44         public boolean isEmptyLineBeforeSection() { 
    45                 return emptyLineBeforeSection; 
    46         } 
    47  
    48         public void setEmptyLineBeforeSection(boolean emptyLineBeforeSection) { 
    49                 this.emptyLineBeforeSection = emptyLineBeforeSection; 
    50         } 
    51  
    52         public boolean isEmptyLineAfterSection() { 
    53                 return emptyLineAfterSection; 
    54         } 
    55  
    56         public void setEmptyLineAfterSection(boolean emptyLineAfterSection) { 
    57                 this.emptyLineAfterSection = emptyLineAfterSection; 
     19        public void setPreventNewLineAfterAlternativeOnLessThanThreeElements( 
     20                        boolean preventNewLineAfterAlternativeOnLessThanThreeElements) { 
     21                this.preventNewLineAfterAlternativeOnLessThanThreeElements = preventNewLineAfterAlternativeOnLessThanThreeElements; 
    5822        } 
    5923 
Note: See TracChangeset for help on using the changeset viewer.