Ejemplo n.º 1
0
 private static void addCreateProposals(
     Tree.CompilationUnit rootNode,
     Collection<ICompletionProposal> proposals,
     IProject project,
     IFile file,
     Tree.MemberOrTypeExpression smte,
     DefinitionGenerator dg) {
   if (smte instanceof Tree.QualifiedMemberOrTypeExpression) {
     addCreateMemberProposals(
         proposals,
         project,
         dg,
         (Tree.QualifiedMemberOrTypeExpression) smte,
         Nodes.findStatement(rootNode, smte));
   } else {
     if (!(dg.getNode() instanceof Tree.ExtendedTypeExpression)) {
       addCreateLocalProposals(proposals, project, dg);
       ClassOrInterface container = findClassContainer(rootNode, smte);
       if (container != null
           && container
               != smte
                   .getScope()) { // if the statement appears directly in an initializer, propose a
                                  // local, not a member
         do {
           addCreateMemberProposals(
               proposals,
               project,
               dg,
               container,
               // TODO: this is a little lame because
               //      it doesn't handle some cases
               //      of nesting
               Nodes.findStatement(rootNode, smte));
           if (container.getContainer() instanceof Declaration) {
             Declaration outerContainer = (Declaration) container.getContainer();
             container = findClassContainer(outerContainer);
           } else {
             break;
           }
         } while (container != null);
       }
     }
     addCreateToplevelProposals(proposals, project, dg);
     addCreateInNewUnitProposal(proposals, dg, file, rootNode);
   }
 }
 @Deprecated
 static void addRefineFormalMembersProposal(
     Collection<ICompletionProposal> proposals,
     Node n,
     Tree.CompilationUnit rootNode,
     boolean ambiguousError) {
   for (ICompletionProposal p : proposals) {
     if (p instanceof RefineFormalMembersProposal) {
       return;
     }
   }
   Node node;
   if (n instanceof Tree.ClassBody
       || n instanceof Tree.InterfaceBody
       || n instanceof Tree.ClassDefinition
       || n instanceof Tree.InterfaceDefinition
       || n instanceof Tree.ObjectDefinition
       || n instanceof Tree.ObjectExpression) {
     node = n;
   } else {
     FindBodyContainerVisitor v = new FindBodyContainerVisitor(n);
     v.visit(rootNode);
     node = v.getDeclaration();
   }
   if (node != null) {
     Scope scope = node.getScope();
     if (scope instanceof ClassOrInterface) {
       ClassOrInterface ci = (ClassOrInterface) scope;
       String name = ci.getName();
       if (name == null) {
         return;
       } else if (name.startsWith("anonymous#")) {
         name = "anonymous class";
       } else {
         name = "'" + name + "'";
       }
       String desc =
           ambiguousError
               ? "Refine inherited ambiguous and formal members of " + name
               : "Refine inherited formal members of " + name;
       proposals.add(new RefineFormalMembersProposal(node, rootNode, desc));
     }
   }
 }
  @Deprecated
  // replaced by RefineFormalMembersQuickFix.ceylon
  private void refineFormalMembers(IDocument document) throws ExecutionException {
    if (rootNode == null) return;
    TextChange change = new DocumentChange("Refine Members", document);
    change.setEdit(new MultiTextEdit());
    // TODO: copy/pasted from CeylonQuickFixAssistant
    Tree.Body body;
    int offset;
    if (node instanceof Tree.ClassDefinition) {
      ClassDefinition classDefinition = (Tree.ClassDefinition) node;
      body = classDefinition.getClassBody();
      offset = -1;
    } else if (node instanceof Tree.InterfaceDefinition) {
      Tree.InterfaceDefinition interfaceDefinition = (Tree.InterfaceDefinition) node;
      body = interfaceDefinition.getInterfaceBody();
      offset = -1;
    } else if (node instanceof Tree.ObjectDefinition) {
      Tree.ObjectDefinition objectDefinition = (Tree.ObjectDefinition) node;
      body = objectDefinition.getClassBody();
      offset = -1;
    } else if (node instanceof Tree.ObjectExpression) {
      Tree.ObjectExpression objectExpression = (Tree.ObjectExpression) node;
      body = objectExpression.getClassBody();
      offset = -1;
    } else if (node instanceof Tree.ClassBody || node instanceof Tree.InterfaceBody) {
      body = (Tree.Body) node;
      IEditorPart editor = getCurrentEditor();
      if (editor instanceof CeylonEditor) {
        CeylonEditor ce = (CeylonEditor) editor;
        offset = ce.getSelection().getOffset();
      } else {
        offset = -1;
      }
    } else {
      return;
    }
    if (body == null) {
      return;
    }
    boolean isInterface = body instanceof Tree.InterfaceBody;
    List<Tree.Statement> statements = body.getStatements();
    String indent;
    //        String bodyIndent = getIndent(body, document);
    String bodyIndent = utilJ2C().indents().getIndent(node, document);
    String delim = utilJ2C().indents().getDefaultLineDelimiter(document);
    if (statements.isEmpty()) {
      indent = delim + bodyIndent + utilJ2C().indents().getDefaultIndent();
      if (offset < 0) {
        offset = body.getStartIndex() + 1;
      }
    } else {
      Tree.Statement statement = statements.get(statements.size() - 1);
      indent = delim + utilJ2C().indents().getIndent(statement, document);
      if (offset < 0) {
        offset = statement.getEndIndex();
      }
    }
    StringBuilder result = new StringBuilder();
    Set<Declaration> already = new HashSet<Declaration>();
    ClassOrInterface ci = (ClassOrInterface) node.getScope();
    Unit unit = node.getUnit();
    Set<String> ambiguousNames = new HashSet<String>();
    // TODO: does not return unrefined overloaded
    //      versions of a method with one overlaad
    //      already refined
    Collection<DeclarationWithProximity> proposals =
        ci.getMatchingMemberDeclarations(unit, ci, "", 0).values();
    for (DeclarationWithProximity dwp : proposals) {
      Declaration dec = dwp.getDeclaration();
      for (Declaration d : overloads(dec)) {
        try {
          if (d.isFormal() && ci.isInheritedFromSupertype(d)) {
            appendRefinementText(isInterface, indent, result, ci, unit, d);
            importProposals().importSignatureTypes(d, rootNode, already);
            ambiguousNames.add(d.getName());
          }
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }
    for (TypeDeclaration superType : ci.getSupertypeDeclarations()) {
      for (Declaration m : superType.getMembers()) {
        try {
          if (m.getName() != null && m.isShared()) {
            Declaration r = ci.getMember(m.getName(), null, false);
            if ((r == null || !r.refines(m) && !r.getContainer().equals(ci))
                && ambiguousNames.add(m.getName())) {
              appendRefinementText(isInterface, indent, result, ci, unit, m);
              importProposals().importSignatureTypes(m, rootNode, already);
            }
          }
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }

    try {
      if (document.getChar(offset) == '}' && result.length() > 0) {
        result.append(delim).append(bodyIndent);
      }
    } catch (BadLocationException e) {
      e.printStackTrace();
    }
    importProposals().applyImports(change, already, rootNode, document);
    change.addEdit(new InsertEdit(offset, result.toString()));
    change.initializeValidationData(null);
    try {
      getWorkspace().run(new PerformChangeOperation(change), new NullProgressMonitor());
    } catch (CoreException ce) {
      ce.printStackTrace();
    }
  }