public String getTypeName() {
   LinkedListTree type = ASTUtils.findChildByType(ast, AS3Parser.TYPE_SPEC);
   if (type == null) {
     return null;
   }
   final LinkedListTree typeAST = type.getFirstChild();
   if (typeAST.getType() == AS3Parser.STAR) {
     return "*";
   }
   return ASTUtils.identText(typeAST);
 }
  public List<ASType> getOutOfPackageTypes() {
    List<ASType> types = new ArrayList<ASType>();

    for (LinkedListTree type : ASTUtils.findChildrenByType(ast, AS3Parser.CLASS_DEF)) {
      types.add(new ASTASClassType(type));
    }

    for (LinkedListTree type : ASTUtils.findChildrenByType(ast, AS3Parser.INTERFACE_DEF)) {
      types.add(new ASTASInterfaceType(type));
    }

    return types;
  }
Exemplo n.º 3
0
 public static ASBinaryExpression.Op opFromType(int type) {
   ASBinaryExpression.Op op = (ASBinaryExpression.Op) OPERATORS_BY_TYPE.get(new Integer(type));
   if (op == null) {
     throw new IllegalArgumentException("No operator for token-type " + ASTUtils.tokenName(type));
   }
   return op;
 }
  public List<ASMethod> getOutOfPackageFunctions() {
    List<ASMethod> functions = new ArrayList<ASMethod>();

    for (LinkedListTree functionAST : ASTUtils.findChildrenByType(ast, AS3Parser.METHOD_DEF)) {
      functions.add(new ASTASMethod(functionAST));
    }

    return functions;
  }
  public List<ASField> getOutOfPackageFields() {
    List<ASField> fields = new ArrayList<ASField>();

    for (LinkedListTree fieldAST : ASTUtils.findChildrenByType(ast, AS3Parser.VAR_DEF)) {
      fields.addAll(new ASTASField(fieldAST).getSubFields());
    }

    return fields;
  }
Exemplo n.º 6
0
 @Override
 public long getEndPosition() {
   if (doc != null) {
     OffsetRange range = ASTUtils.getRangeFull(node.getNode(), doc);
     LOG.log(Level.FINEST, "getEndPosition(), end: {0}", range.getEnd());
     return (long) range.getEnd();
   }
   return 0;
 }
Exemplo n.º 7
0
 /**
  * @param argumentNode the ARGUMENTS AST node
  * @param args list of ASExpression objects to be used as the new argument list
  */
 public static void overwriteArgsWithExpressionList(LinkedListTree argumentNode, List args) {
   // get rid of any old arguments,
   ASTUtils.deleteAllChildren(argumentNode);
   // add the new arguments,
   for (Iterator i = args.iterator(); i.hasNext(); ) {
     ASTExpression arg = (ASTExpression) i.next();
     argumentNode.addChildWithTokens(arg.getAST());
     if (i.hasNext()) {
       argumentNode.appendToken(TokenBuilder.newComma());
       argumentNode.appendToken(TokenBuilder.newSpace());
     }
   }
 }
  public List<ASNamespaceDeclaration> getOutOfPackageNamespaces() {
    List<ASNamespaceDeclaration> namespaces = new ArrayList<ASNamespaceDeclaration>();

    List<LinkedListTree> namespacesAST = ASTUtils.findChildrenByType(ast, AS3Parser.NAMESPACE_DEF);
    for (LinkedListTree namespaceAST : namespacesAST) {
      final LinkedListTree child = namespaceAST.getFirstChild();
      if (child != null) {
        namespaces.add(new ASTASNamespaceDeclaration(namespaceAST));
      }
    }

    return namespaces;
  }
  public List<Statement> getOutOfPackageStatements() {
    List<Statement> statements = new ArrayList<Statement>();

    List<LinkedListTree> statementsAST =
        ASTUtils.findChildrenByType(ast, AS3Parser.OUT_OF_FUNCTION_STMT);
    for (LinkedListTree statementAST : statementsAST) {
      final LinkedListTree child = statementAST.getFirstChild();
      if (child != null) {
        statements.add(StatementBuilder.build(child));
      }
    }

    return statements;
  }
Exemplo n.º 10
0
  private void addFolds(
      BaseDocument doc,
      List<? extends ASTElement> elements,
      Map<String, List<OffsetRange>> folds,
      List<OffsetRange> codeblocks)
      throws BadLocationException {
    for (ASTElement element : elements) {
      ElementKind kind = element.getKind();
      switch (kind) {
        case FIELD:
        case METHOD:
        case CONSTRUCTOR:
        case CLASS:
        case MODULE:
          ASTNode node = element.getNode();
          OffsetRange range = ASTUtils.getRangeFull(node, doc);

          // beware of synthetic elements
          if ((kind == ElementKind.METHOD && !((MethodNode) node).isSynthetic())
              || (kind == ElementKind.CONSTRUCTOR && !((ConstructorNode) node).isSynthetic())
              || (kind == ElementKind.FIELD
                  && ((FieldNode) node).getInitialExpression() instanceof ClosureExpression)
              // Only make nested classes/modules foldable, similar to what the java editor is doing
              || (range.getStart() > Utilities.getRowStart(doc, range.getStart()))
                  && kind != ElementKind.FIELD) {

            int start = range.getStart();
            // Start the fold at the END of the line behind last non-whitespace, remove curly brace,
            // if any
            start = Utilities.getRowLastNonWhite(doc, start);
            if (start >= 0 && doc.getChars(start, 1)[0] != '{') {
              start++;
            }
            int end = range.getEnd();
            if (start != (-1) && end != (-1) && start < end && end <= doc.getLength()) {
              range = new OffsetRange(start, end);
              codeblocks.add(range);
            }
          }
          break;
      }

      List<? extends ASTElement> children = element.getChildren();

      if (children != null && children.size() > 0) {
        addFolds(doc, children, folds, codeblocks);
      }
    }
  }
Exemplo n.º 11
0
  @Override
  public List<? extends StructureItem> scan(ParserResult info) {
    GroovyParserResult result = ASTUtils.getParseResult(info);

    AnalysisResult ar = result.getStructure();
    List<? extends ASTElement> elements = ar.getElements();
    List<StructureItem> itemList = new ArrayList<StructureItem>(elements.size());

    for (ASTElement e : elements) {
      if (isVisible(e)) {
        itemList.add(new GroovyStructureItem(e, info));
      }
    }

    return itemList;
  }
 public String getExpressionString() {
   if (hasExpr()) {
     return null;
   }
   return ASTUtils.stringifyNode(ast.getFirstChild());
 }
Exemplo n.º 13
0
 @Override
 public ASTAnnotation getASTAnnotation(Class<? extends Annotation> annotation) {
   return ASTUtils.getInstance().getAnnotation(annotation, getAnnotations());
 }
 private LinkedListTree getPkgNode() {
   return ASTUtils.findChildByType(ast, AS3Parser.PACKAGE);
 }
Exemplo n.º 15
0
  @Override
  public Map<String, List<OffsetRange>> folds(ParserResult info) {
    ASTNode root = ASTUtils.getRoot(info);

    if (root == null) {
      return Collections.emptyMap();
    }

    GroovyParserResult rpr = ASTUtils.getParseResult(info);
    AnalysisResult analysisResult = rpr.getStructure();

    Map<String, List<OffsetRange>> folds = new HashMap<String, List<OffsetRange>>();
    List<OffsetRange> codefolds = new ArrayList<OffsetRange>();
    folds.put("codeblocks", codefolds); // NOI18N

    final BaseDocument doc = LexUtilities.getDocument(rpr, false);
    if (doc == null) {
      return Collections.emptyMap();
    }

    final OffsetRange[] importsRange = new OffsetRange[1];
    final List<OffsetRange> commentsRanges = new ArrayList<OffsetRange>();

    doc.render(
        new Runnable() {
          @Override
          public void run() {
            TokenSequence<?> ts = LexUtilities.getGroovyTokenSequence(doc, 1);

            int importStart = 0;
            int importEnd = 0;

            boolean startSet = false;

            while (ts != null && ts.isValid() && ts.moveNext()) {
              Token t = ts.token();
              if (t.id() == GroovyTokenId.LITERAL_import) {
                int offset = ts.offset();
                if (!startSet) {
                  importStart = offset;
                  startSet = true;
                }
                importEnd = offset;
              } else if (t.id() == GroovyTokenId.BLOCK_COMMENT) {
                // does this Block comment (GSF_BLOCK_COMMENT) span
                // multiple lines? E.g. includes \n ?
                StringBuffer sb = new StringBuffer(t.text());

                if (sb.indexOf("\n") != -1) {
                  int offset = ts.offset();
                  commentsRanges.add(new OffsetRange(offset, offset + t.length()));
                }
              }
            }
            try {
              importEnd = Utilities.getRowEnd(doc, importEnd);
              importsRange[0] = new OffsetRange(importStart, importEnd);
            } catch (BadLocationException ble) {
              Exceptions.printStackTrace(ble);
            }
          }
        });

    if (!commentsRanges.isEmpty()) {
      folds.put("comments", commentsRanges); // NOI18N
    }

    try {
      if (importsRange[0] != null
          && Utilities.getRowCount(doc, importsRange[0].getStart(), importsRange[0].getEnd()) > 1) {
        folds.put("imports", Collections.singletonList(importsRange[0])); // NOI18N
      }
      addFolds(doc, analysisResult.getElements(), folds, codefolds);
    } catch (BadLocationException ex) {
      Exceptions.printStackTrace(ex);
    }

    return folds;
  }
Exemplo n.º 16
0
  private void scan(
      GroovyParserResult result,
      ASTNode node,
      AstPath path,
      String in,
      Set<String> includes,
      ASTElement parent) {
    if (node instanceof AnnotatedNode && !((AnnotatedNode) node).hasNoRealSourcePosition()) {

      if (node instanceof ClassNode) {
        ASTClass co = new ASTClass(result, node);
        co.setFqn(((ClassNode) node).getName());

        if (parent != null) {
          parent.addChild(co);
        } else {
          structure.add(co);
        }

        parent = co;
      } else if (node instanceof FieldNode) {
        if (parent instanceof ASTClass) {
          // We don't have unique declarations, only assignments (possibly many)
          // so stash these in a map and extract unique fields when we're done
          Set<FieldNode> assignments = fields.get(parent);

          if (assignments == null) {
            assignments = new HashSet<FieldNode>();
            fields.put((ASTClass) parent, assignments);
          }

          assignments.add((FieldNode) node);
        }
      } else if (node instanceof MethodNode) {
        ASTMethod co = new ASTMethod(result, node);
        methods.add(co);
        co.setIn(in);

        // TODO - don't add this to the top level! Make a nested list
        if (parent != null) {
          parent.addChild(co);
        } else {
          structure.add(co);
        }
      } else if (node instanceof PropertyNode) {
        Set<PropertyNode> declarations = properties.get(parent);

        if (declarations == null) {
          declarations = new HashSet<PropertyNode>();
          properties.put((ASTClass) parent, declarations);
        }

        declarations.add((PropertyNode) node);
      }
    }

    @SuppressWarnings("unchecked")
    List<ASTNode> list = ASTUtils.children(node);

    for (ASTNode child : list) {
      path.descend(child);
      scan(result, child, path, in, includes, parent);
      path.ascend();
    }
  }
Exemplo n.º 17
0
  private AnalysisResult scan(GroovyParserResult result) {
    AnalysisResult analysisResult = new AnalysisResult();

    ASTNode root = ASTUtils.getRoot(result);

    if (root == null) {
      return analysisResult;
    }

    structure = new ArrayList<ASTElement>();
    fields = new HashMap<ASTClass, Set<FieldNode>>();
    methods = new ArrayList<ASTMethod>();
    properties = new HashMap<ASTClass, Set<PropertyNode>>();

    AstPath path = new AstPath();
    path.descend(root);
    // TODO: I should pass in a "default" context here to stash methods etc. outside of modules and
    // classes
    scan(result, root, path, null, null, null);
    path.ascend();

    // Process fields
    Map<String, FieldNode> names = new HashMap<String, FieldNode>();

    for (ASTClass clz : fields.keySet()) {
      Set<FieldNode> assignments = fields.get(clz);

      // Find unique variables
      if (assignments != null) {
        for (FieldNode assignment : assignments) {
          names.put(assignment.getName(), assignment);
        }

        // Add unique fields
        for (FieldNode field : names.values()) {
          ASTField co = new ASTField(result, field);
          // co.setIn(AstUtilities.getClassOrModuleName(clz));
          co.setIn(clz.getFqn());

          // Make sure I don't already have an entry for this field as an
          // attr_accessor or writer
          String fieldName = field.getName();

          boolean found = false;

          Set<PropertyNode> nodes = properties.get(clz);
          if (nodes != null) {
            for (PropertyNode node : nodes) {
              if (fieldName.equals(node.getName())) {
                found = true;
                break;
              }
            }
          }

          if (found) {
            co.setProperty(true);
          }
          clz.addChild(co);
        }

        names.clear();
      }
    }

    analysisResult.setElements(structure);

    return analysisResult;
  }
 private String importText(LinkedListTree imp) {
   return ASTUtils.identStarText(imp.getFirstChild());
 }
 private LinkedListTree block() {
   return ASTUtils.findChildByType(ast, AS3Parser.BLOCK);
 }
Exemplo n.º 20
0
 /**
  * Finds the first child of the given AST node of type ARGUMENTS
  *
  * @return the matching AST node, or null if none is found
  */
 public static LinkedListTree findArgs(LinkedListTree parent) {
   return ASTUtils.findChildByType(parent, AS3Parser.ARGUMENTS);
 }
Exemplo n.º 21
0
 @Override
 public boolean implementsFrom(ASTType type) {
   return ASTUtils.getInstance().inherits(this, type, true, false);
 }