Ignore:
Timestamp:
11/02/10 16:39:02 (14 years ago)
Author:
zeiss
Message:
 
File:
1 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 
Note: See TracChangeset for help on using the changeset viewer.