Package com.puppetlabs.geppetto.common.stats

Examples of com.puppetlabs.geppetto.common.stats.IntegerCluster


          // without explicitly listing all that should not be set
          out.append("\n");
        }
        else {
          // prepare data, a cluster for padding calculation, and a treemap for sorting parameters
          IntegerCluster cluster = new IntegerCluster(20);
          TreeMap<String, List<String>> sortedParameters = Maps.newTreeMap();
          for(CatalogResourceParameter p : r.getParameters()) {
            cluster.add(p.getName().length());
            sortedParameters.put(p.getName(), p.getValue());
          }
          out.append(".with(\n");
          boolean firstItem = true;
View Full Code Here


  protected void markup(IDomNode node, final boolean breakAndAlign, final int clusterWidth, ITextFlow flow,
      ILayoutContext context) {

    Iterator<IDomNode> itor = node.treeIterator();
    Map<IDomNode, Integer> operatorNodes = Maps.newHashMap();
    IntegerCluster cluster = new IntegerCluster(clusterWidth);

    final LiteralHashElements access = grammarAccess.getLiteralHashAccess();
    final HashEntryElements hashAccess = grammarAccess.getHashEntryAccess();
    int previousKeyWidth = 0;
    while(itor.hasNext()) {
      IDomNode n = itor.next();
      EObject ge = n.getGrammarElement();
      if(ge == access.getLeftCurlyBracketKeyword_1()) {
        IDomNode nextLeaf = DomModelUtils.nextWhitespace(n);
        if(DomModelUtils.isWhitespace(nextLeaf) && breakAndAlign)
          nextLeaf.getStyles().add(StyleSet.withStyles(styles.oneLineBreak()));
      }
      else if(breakAndAlign) {
        if(ge == access.getCommaKeyword_2_1_0()) {
          IDomNode nextLeaf = DomModelUtils.nextWhitespace(n);
          if(DomModelUtils.isWhitespace(nextLeaf))
            nextLeaf.getStyles().add(StyleSet.withStyles(styles.oneLineBreak()));
        }
        else if(ge == hashAccess.getKeyLiteralNameOrStringParserRuleCall_0_0()) {
          DelegatingLayoutContext keyContext = new DelegatingLayoutContext(context);
          TextFlow keyFlow = new TextFlow(keyContext);
          ArrayList<IDomNode> children = Lists.newArrayList(n.getChildren());
          for(Iterator<IDomNode> subitor = children.iterator(); subitor.hasNext();) {
            IDomNode x = subitor.next();
            NodeType t = x.getNodeType();
            if(t == NodeType.ACTION || t == NodeType.WHITESPACE) {
              subitor.remove();
              continue;
            }
            break; // first non whitespace or action
          }
          feeder.sequence(children, keyFlow, context);
          previousKeyWidth = keyFlow.getWidthOfLastLine();
          cluster.add(previousKeyWidth);
          System.out.println("node= [" + keyFlow.getText() + "] width = " + previousKeyWidth);
        }
        else if(ge == hashAccess.getEqualsSignGreaterThanSignKeyword_1()) {
          operatorNodes.put(n, previousKeyWidth);
        }
View Full Code Here

    // used to collect the widths of each case's width of its values
    List<Integer> widths = Lists.newArrayList();
    // int maxLastLine = 0;
    boolean firstCaseSeen = false;
    List<IDomNode> colonNodes = Lists.newArrayList();
    IntegerCluster clusters = new IntegerCluster(20);
    boolean allCompactable = true; // until proven wrong
    for(IDomNode n : node.getChildren()) {
      if(n.getGrammarElement() == caseRuleCall) {
        if(!firstCaseSeen) {
          // finish measurement of the position the case will appear at
          //
          continuedFlow.appendBreak();
          continuedFlow.getIndentation();
          availableWidth = currentMaxWidth - (continuedFlow.getIndentation() + 1) *
              continuedFlow.getIndentSize();
        }
        // used to measure output of formatted case values
        // adjust its width to available width (and do not mark items consumed in the given context)
        DelegatingLayoutContext innerContext = new DelegatingLayoutContext(context, availableWidth);
        TextFlow measuredFlow = new TextFlow(innerContext);
        // visit all nodes in case until the colon is hit, and format the output to the measured flow
        IDomNode colonNode = feeder.sequence(n, measuredFlow, innerContext, caseColonPredicate);
        if(doCompaction && !n.getStyleClassifiers().contains(StatementStyle.COMPACTABLE))
          allCompactable = false;

        colonNodes.add(colonNode);

        // collect the width of the last case's values
        int lastLineWidth = measuredFlow.getWidthOfLastLine();
        if(!firstCaseSeen) {
          // the space before the first case triggers case expression indentation of 1, this must be adjusted
          lastLineWidth -= measuredFlow.getIndentSize();
        }
        clusters.add(lastLineWidth);
        widths.add(lastLineWidth);
        firstCaseSeen = true;

      }
      else if(!firstCaseSeen) {
View Full Code Here

    // used to collect the widths of each LHS width (typically just a variable name, but can be complex).
    List<Integer> widths = Lists.newArrayList();

    List<IDomNode> equalSignNodes = Lists.newArrayList();
    IntegerCluster clusters = new IntegerCluster(advice.clusterSize());

    boolean containsAppend = false;

    // while siblings are assignments, starting with the current one
    for(IDomNode aeNode = node; aeNode != null && includeInSequence(aeNode); aeNode = aeNode.getNextSibling()) {
      DelegatingLayoutContext innerContext = new DelegatingLayoutContext(context, availableWidth);
      TextFlow measuredFlow = new TextFlow(innerContext);

      // iterate to format the LHS expression and measure, and find the = sign (which gets padded)
      Iterator<IDomNode> itor = aeNode.treeIterator();
      while(itor.hasNext()) {
        IDomNode n = itor.next();
        // forward until assignment expression =, or +=
        if(!(n.getGrammarElement() == theAssignmentExpression || n.getGrammarElement() == theAppendExpression))
          continue;

        // Measure assignment expression
        feeder.sequence(n, measuredFlow, innerContext, new FirstLeafWithTextAndTheRest(), untilTheEnd);

        // forward to = or += sign
        while(itor.hasNext()) {
          n = itor.next();
          if(n.getGrammarElement() == theEqualSign || n.getGrammarElement() == thePlusEqualSign)
            break;
        }
        if(n.getGrammarElement() == thePlusEqualSign)
          containsAppend = true;

        if(!itor.hasNext())
          break; // WAT, nothing after the '=', give up on this assignment, try to align the others

        // If assignment node already has a width, it was processed by a preceding assignment
        // and we were done a long time ago...
        //
        WidthStyle widthStyle = n.getStyles().getStyle(WidthStyle.class, n);
        if(widthStyle != null)
          return false;

        equalSignNodes.add(n);

        // collect the width of the last selector entry's values
        int lastLineWidth = measuredFlow.getWidthOfLastLine();
        clusters.add(lastLineWidth);
        widths.add(lastLineWidth);
        break;
      }
    }
    markupWidths(equalSignNodes, widths, availableWidth, clusters, doAlignment, containsAppend);
View Full Code Here

    // used to collect the widths of each case's width of its values
    List<Integer> widths = Lists.newArrayList();
    // int maxLastLine = 0;
    boolean firstSelectoEntrySeen = false;
    List<IDomNode> fatCommaNodes = Lists.newArrayList();
    IntegerCluster clusters = new IntegerCluster(SELECTOR_EXPRESSION_CLUSTER_SIZE);
    for(IDomNode n : node.getChildren()) {
      if(n.getGrammarElement() instanceof RuleCall &&
          ((RuleCall) n.getGrammarElement()).getRule() == selectorEntryRule) {
        if(!firstSelectoEntrySeen) {
          // finish measurement of the position the selectorEntry will appear at
          //
          continuedFlow.appendBreak();
          continuedFlow.getIndentation();
          availableWidth = currentMaxWidth - (continuedFlow.getIndentation() + 1) *
              continuedFlow.getIndentSize();
        }
        // used to measure output of formatted selector entry values
        // adjust its width to available width (and do not mark items consumed in the given context)
        DelegatingLayoutContext innerContext = new DelegatingLayoutContext(context, availableWidth);
        TextFlow measuredFlow = new TextFlow(innerContext);
        // visit all nodes in case until the colon is hit, and format the output to the measured flow
        IDomNode fatCommaNode = feeder.sequence(n, measuredFlow, innerContext, caseFatCommaPredicate);

        fatCommaNodes.add(fatCommaNode);

        // collect the width of the last selector entry's values
        int lastLineWidth = measuredFlow.getWidthOfLastLine();
        if(!firstSelectoEntrySeen) {
          // the space before the first entry triggers selector expression indentation of 1, this must be adjusted
          lastLineWidth -= measuredFlow.getIndentSize();
        }
        clusters.add(lastLineWidth);
        widths.add(lastLineWidth);
        firstSelectoEntrySeen = true;

      }
      else if(!firstSelectoEntrySeen) {
View Full Code Here

   * @param grammarElement
   */
  public static void unifyWidthAndAlign(IDomNode dom, AbstractElement grammarElement, Alignment alignment,
      int clusterMax) {

    IntegerCluster cluster = new IntegerCluster(clusterMax);
    // int max = 0;
    Iterator<IDomNode> iterator = dom.treeIterator();
    List<IDomNode> affected = Lists.newArrayList();
    while(iterator.hasNext()) {
      IDomNode node = iterator.next();
      if(node.getGrammarElement() == grammarElement) {
        // node = DomModelUtils.firstTokenWithText(node);
        if(node.getText() == null)
          continue;
        int length = node.getText().length();
        cluster.add(length);
        //
        // max = Math.max(max, node.getText() == null
        // ? 0
        // : node.getText().length());
        affected.add(node);
      }
    }
    for(IDomNode node : affected)
      node.getStyles().add(StyleSet.withStyles(//
        new StyleFactory.WidthStyle(cluster.clusterMax(node.getText().length())), //
        new StyleFactory.AlignmentStyle(alignment)));
  }
View Full Code Here

  protected void markup(IDomNode node, final boolean breakAndAlign, final int clusterWidth) {

    Iterator<IDomNode> itor = node.treeIterator();
    Map<IDomNode, Integer> operatorNodes = Maps.newHashMap();
    IntegerCluster cluster = new IntegerCluster(clusterWidth);

    // With a little help From the grammar:
    // DefinitionArgumentList returns pp::DefinitionArgumentList : {pp::DefinitionArgumentList}
    // '('
    // (arguments += DefinitionArgument (',' arguments += DefinitionArgument)*)? ','?
    // ')'
    // ;
    //
    // DefinitionArgument returns pp::DefinitionArgument
    // : argName = UNION_VARIABLE_OR_NAME ((op = '=' | op = '=>') value = AssignmentExpression)?
    // ;
    final DefinitionArgumentListElements access = grammarAccess.getDefinitionArgumentListAccess();
    while(itor.hasNext()) {
      IDomNode n = itor.next();
      EObject ge = n.getGrammarElement();
      if(ge == access.getLeftParenthesisKeyword_1()) {
        IDomNode nextLeaf = DomModelUtils.nextLeaf(n);
        if(DomModelUtils.isWhitespace(nextLeaf))
          if(breakAndAlign)
            nextLeaf.getStyles().add(StyleSet.withStyles(styles.oneLineBreak(), styles.indent()));
          else
            nextLeaf.getStyles().add(StyleSet.withStyles(styles.indent()));
      }
      else if(ge == access.getRightParenthesisKeyword_4()) {
        IDomNode prevLeaf = DomModelUtils.previousLeaf(n);
        if(DomModelUtils.isWhitespace(prevLeaf))
          prevLeaf.getStyles().add(StyleSet.withStyles(styles.dedent()));
      }
      else if(breakAndAlign) {
        if(ge == access.getCommaKeyword_2_1_0()) {
          IDomNode nextLeaf = DomModelUtils.nextLeaf(n);
          if(DomModelUtils.isWhitespace(nextLeaf))
            nextLeaf.getStyles().add(StyleSet.withStyles(styles.oneLineBreak()));
        }
        // Break on endcomma does not look good - here is how it would be done though...
        // else if(ge == grammarAccess.getDefinitionArgumentListAccess().getEndCommaParserRuleCall_3()) {
        // IDomNode spaceAfterEndComma = DomModelUtils.nextLeaf(DomModelUtils.firstTokenWithText(n));
        // if(DomModelUtils.isWhitespace(spaceAfterEndComma))
        // spaceAfterEndComma.getStyles().add(StyleSet.withStyles(styles.oneLineBreak()));
        //
        // }
        else if(ge == grammarAccess.getDefinitionArgumentAccess().getOpEqualsSignGreaterThanSignKeyword_1_0_1_0() ||
            ge == grammarAccess.getDefinitionArgumentAccess().getOpEqualsSignKeyword_1_0_0_0()) {
          EObject semantic = n.getParent().getSemanticObject();
          if(semantic != null && semantic instanceof DefinitionArgument) {
            DefinitionArgument da = (DefinitionArgument) semantic;
            int length = da.getArgName() == null
                ? 0
                : da.getArgName().length();
            operatorNodes.put(n, length);
            cluster.add(length);
          }
        }
      }
    }
    if(breakAndAlign)
View Full Code Here

*/
public class TestFormatterUtils {

  @Test
  public void test_Cluster() {
    IntegerCluster cluster = new IntegerCluster(20);
    int[] values = new int[] { 13, 18, 13, 95, };
    int[] expectedClusterMax = new int[] { 18, 18, 18, 95, };
    for(int i = 0; i < values.length; i++)
      cluster.add(values[i]);

    assertEquals("Expected number of clusters", 2, cluster.getClusterCount());
    for(int i = 0; i < values.length; i++)
      assertEquals(
        "Expecte max of cluster is not correct for: " + i, expectedClusterMax[i], cluster.clusterMax(values[i]));

  }
View Full Code Here

  }

  @Test
  public void test_Cluster0() {
    IntegerCluster cluster = new IntegerCluster(20);
    int[] values = new int[] { 1, 2, 3, 4, 5, };
    int[] expectedClusterMax = new int[] { 5, 5, 5, 5, 5 };
    for(int i = 0; i < values.length; i++)
      cluster.add(values[i]);

    assertEquals("Expected number of clusters", 1, cluster.getClusterCount());
    for(int i = 0; i < values.length; i++)
      assertEquals(
        "Expecte max of cluster is not correct for: " + i, expectedClusterMax[i], cluster.clusterMax(values[i]));

  }
View Full Code Here

  }

  @Test
  public void test_Cluster2() {
    IntegerCluster cluster = new IntegerCluster(20);
    int[] values = new int[] { 1, 2, 3, 4, 5, 15, 27, 28, 50, 51, 52 };
    int[] expectedClusterMax = new int[] { 5, 5, 5, 5, 5, 28, 28, 28, 52, 52, 52 };
    for(int i = 0; i < values.length; i++)
      cluster.add(values[i]);

    assertEquals("Expected number of clusters", 3, cluster.getClusterCount());
    for(int i = 0; i < values.length; i++)
      assertEquals(
        "Expecte max of cluster is not correct for: " + i, expectedClusterMax[i], cluster.clusterMax(values[i]));

  }
View Full Code Here

TOP

Related Classes of com.puppetlabs.geppetto.common.stats.IntegerCluster

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.