public static void addChangeIdentifierCaseProposal(
      Node node, Collection<ICompletionProposal> proposals, IFile file) {
    Tree.Identifier identifier = null;

    if (node instanceof Tree.TypeDeclaration) {
      identifier = ((Tree.TypeDeclaration) node).getIdentifier();
    } else if (node instanceof Tree.TypeParameterDeclaration) {
      identifier = ((Tree.TypeParameterDeclaration) node).getIdentifier();
    } else if (node instanceof Tree.TypedDeclaration) {
      identifier = ((Tree.TypedDeclaration) node).getIdentifier();
    } else if (node instanceof Tree.ImportPath) {
      List<Identifier> importIdentifiers = ((Tree.ImportPath) node).getIdentifiers();
      for (Identifier importIdentifier : importIdentifiers) {
        if (importIdentifier.getText() != null
            && !importIdentifier.getText().isEmpty()
            && Character.isUpperCase(importIdentifier.getText().charAt(0))) {
          identifier = importIdentifier;
          break;
        }
      }
    }

    if (identifier != null && !identifier.getText().isEmpty()) {
      addProposal(identifier, proposals, file);
    }
  }
 static void addCreateProposals(
     Tree.CompilationUnit cu,
     Node node,
     Collection<ICompletionProposal> proposals,
     IProject project,
     IFile file) {
   Tree.MemberOrTypeExpression smte = (Tree.MemberOrTypeExpression) node;
   String brokenName = Nodes.getIdentifyingNode(node).getText();
   if (!brokenName.isEmpty()) {
     ValueFunctionDefinitionGenerator vfdg =
         ValueFunctionDefinitionGenerator.create(brokenName, smte, cu);
     if (vfdg != null) {
       if (smte instanceof Tree.BaseMemberExpression) {
         Tree.BaseMemberExpression bme = (Tree.BaseMemberExpression) smte;
         Tree.Identifier id = bme.getIdentifier();
         if (id.getToken().getType() != CeylonLexer.AIDENTIFIER) {
           addCreateParameterProposal(proposals, project, vfdg);
         }
       }
       addCreateProposals(cu, proposals, project, file, smte, vfdg);
     }
     ObjectClassDefinitionGenerator ocdg =
         ObjectClassDefinitionGenerator.create(brokenName, smte, cu);
     if (ocdg != null) {
       addCreateProposals(cu, proposals, project, file, smte, ocdg);
     }
   }
 }
 private static String toPath(Tree.Import ai) {
   String path = "";
   for (Tree.Identifier id : ai.getImportPath().getIdentifiers()) {
     path += "." + id.getText();
   }
   path = path.substring(1);
   return path;
 }
 private void addRemoval(Tree.Identifier id, Declaration d) {
   if (id != null
       && d != null
       && dec.equals(getAbstraction(d))
       && id.getText().equals(aid.getText())) {
     change.addEdit(new ReplaceEdit(id.getStartIndex(), id.getDistance(), dec.getName()));
   }
 }
Пример #5
0
 private void checkAliasCase(Tree.Alias alias, Declaration d) {
   if (alias != null) {
     Tree.Identifier id = alias.getIdentifier();
     int tt = id.getToken().getType();
     if (d instanceof TypeDeclaration && tt != CeylonLexer.UIDENTIFIER) {
       id.addError("imported type should have uppercase alias: '" + d.getName() + "'");
     } else if (d instanceof TypedDeclaration && tt != CeylonLexer.LIDENTIFIER) {
       id.addError("imported member should have lowercase alias: '" + d.getName() + "'");
     }
   }
 }
 protected void renameIdentifier(TextChange tfc, Tree.Identifier id, Tree.CompilationUnit root) {
   String name = declaration.getName();
   int loc = id.getText().indexOf(name);
   int start = id.getStartIndex();
   int len = id.getDistance();
   if (loc > 0) {
     tfc.addEdit(new ReplaceEdit(start + loc, name.length(), newName));
   } else {
     tfc.addEdit(new ReplaceEdit(start, len, escaping_.get_().toInitialLowercase(newName)));
   }
 }
 private static String name(Tree.Identifier id) {
   if (id == null) {
     return "<unknown>";
   } else {
     return id.getText();
   }
 }
 @Override
 public void visit(Tree.TypedDeclaration that) {
   super.visit(that);
   Tree.Identifier id = that.getIdentifier();
   if (id != null) {
     Type type = that.getType().getTypeModel();
     if (type != null) {
       TypeDeclaration td = type.getDeclaration();
       if ((td instanceof ClassOrInterface || td instanceof TypeParameter)
           && td.equals(declaration)) {
         String text = id.getText();
         String name = declaration.getName();
         if (text.equalsIgnoreCase(name) || text.endsWith(name)) {
           identifiers.add(id);
         }
       }
     }
   }
 }
 static void addRemoveAliasProposal(
     Tree.ImportMemberOrType imt,
     Collection<ICompletionProposal> proposals,
     IFile file,
     CeylonEditor editor) {
   if (imt != null) {
     Declaration dec = imt.getDeclarationModel();
     Tree.CompilationUnit upToDateAndTypechecked =
         editor.getParseController().getTypecheckedRootNode();
     if (dec != null && imt.getAlias() != null && upToDateAndTypechecked != null) {
       TextFileChange change = new TextFileChange("Remove Alias", file);
       change.setEdit(new MultiTextEdit());
       Tree.Identifier aid = imt.getAlias().getIdentifier();
       change.addEdit(
           new DeleteEdit(
               aid.getStartIndex(), imt.getIdentifier().getStartIndex() - aid.getStartIndex()));
       upToDateAndTypechecked.visit(new AliasRemovalVisitor(dec, change, aid));
       proposals.add(new RemoveAliasProposal(file, dec, change));
     }
   }
 }
Пример #10
0
 private String importMember(
     Tree.ImportMemberOrType member, Package importedPackage, ImportList il) {
   Tree.Identifier id = member.getIdentifier();
   if (id == null) {
     return null;
   }
   Import i = new Import();
   member.setImportModel(i);
   Tree.Alias alias = member.getAlias();
   String name = name(id);
   if (alias == null) {
     i.setAlias(name);
   } else {
     i.setAlias(name(alias.getIdentifier()));
   }
   if (isNonimportable(importedPackage, name)) {
     id.addError("root type may not be imported");
     return name;
   }
   Declaration d = importedPackage.getMember(name, null, false);
   if (d == null) {
     String correction = correct(importedPackage, unit, name);
     String message = correction == null ? "" : " (did you mean '" + correction + "'?)";
     id.addError("imported declaration not found: '" + name + "'" + message, 100);
     unit.getUnresolvedReferences().add(id);
   } else {
     if (!declaredInPackage(d, unit)) {
       if (!d.isShared()) {
         id.addError("imported declaration is not shared: '" + name + "'", 400);
       } else if (d.isPackageVisibility()) {
         id.addError("imported package private declaration is not visible: '" + name + "'");
       } else if (d.isProtectedVisibility()) {
         id.addError("imported protected declaration is not visible: '" + name + "'");
       }
     }
     i.setDeclaration(d);
     member.setDeclarationModel(d);
     if (il.hasImport(d)) {
       id.addError("already imported: '" + name + "'");
     } else if (!checkForHiddenToplevel(id, i, alias)) {
       addImport(member, il, i);
     }
     checkAliasCase(alias, d);
   }
   if (d != null) {
     importMembers(member, d);
   }
   return name;
 }
Пример #11
0
 private boolean checkForHiddenToplevel(Tree.Identifier id, Import i, Tree.Alias alias) {
   for (Declaration d : unit.getDeclarations()) {
     String n = d.getName();
     Declaration idec = i.getDeclaration();
     if (d.isToplevel()
         && n != null
         && i.getAlias().equals(n)
         && !idec.equals(d)
         &&
         // it is legal to import an object declaration
         // in the current package without providing an
         // alias:
         !isLegalAliasFreeImport(d, idec)) {
       if (alias == null) {
         id.addError("toplevel declaration with this name declared in this unit: '" + n + "'");
       } else {
         alias.addError("toplevel declaration with this name declared in this unit: '" + n + "'");
       }
       return true;
     }
   }
   return false;
 }
Пример #12
0
 private String importMember(Tree.ImportMemberOrType member, TypeDeclaration td, ImportList il) {
   Tree.Identifier id = member.getIdentifier();
   if (id == null) {
     return null;
   }
   Import i = new Import();
   member.setImportModel(i);
   Tree.Alias alias = member.getAlias();
   String name = name(id);
   if (alias == null) {
     i.setAlias(name);
   } else {
     i.setAlias(name(alias.getIdentifier()));
   }
   Declaration m = td.getMember(name, null, false);
   if (m == null) {
     String correction = correct(td, null, unit, name);
     String message = correction == null ? "" : " (did you mean '" + correction + "'?)";
     id.addError(
         "imported declaration not found: '" + name + "' of '" + td.getName() + "'" + message,
         100);
     unit.getUnresolvedReferences().add(id);
   } else {
     List<Declaration> members = m.getContainer().getMembers();
     for (Declaration d : members) {
       String dn = d.getName();
       if (dn != null && dn.equals(name) && !d.sameKind(m) && !d.isAnonymous()) {
         // crazy interop cases like isOpen() + open()
         id.addError("ambiguous member declaration: '" + name + "' of '" + td.getName() + "'");
         return null;
       }
     }
     if (!m.isShared()) {
       id.addError(
           "imported declaration is not shared: '" + name + "' of '" + td.getName() + "'", 400);
     } else if (!declaredInPackage(m, unit)) {
       if (m.isPackageVisibility()) {
         id.addError(
             "imported package private declaration is not visible: '"
                 + name
                 + "' of '"
                 + td.getName()
                 + "'");
       } else if (m.isProtectedVisibility()) {
         id.addError(
             "imported protected declaration is not visible: '"
                 + name
                 + "' of '"
                 + td.getName()
                 + "'");
       }
     }
     i.setTypeDeclaration(td);
     if (!m.isStaticallyImportable()
         && !isToplevelClassConstructor(td, m)
         && !isToplevelAnonymousClass(m.getContainer())) {
       if (alias == null) {
         member.addError("does not specify an alias");
       }
     }
     i.setDeclaration(m);
     member.setDeclarationModel(m);
     if (il.hasImport(m)) {
       id.addError("already imported: '" + name + "' of '" + td.getName() + "'");
     } else {
       if (m.isStaticallyImportable()
           || isToplevelClassConstructor(td, m)
           || isToplevelAnonymousClass(m.getContainer())) {
         if (!checkForHiddenToplevel(id, i, alias)) {
           addImport(member, il, i);
         }
       } else {
         addMemberImport(member, il, i);
       }
     }
     checkAliasCase(alias, m);
   }
   if (m != null) {
     importMembers(member, m);
   }
   // imtl.addError("member aliases may not have member aliases");
   return name;
 }
  public static String getInitialValueDescription(
      final Declaration dec, CeylonParseController cpc) {
    if (cpc != null) {
      Node refnode = getReferencedNode(dec);
      Tree.SpecifierOrInitializerExpression sie = null;
      String arrow = null;
      if (refnode instanceof Tree.AttributeDeclaration) {
        Tree.AttributeDeclaration ad = (Tree.AttributeDeclaration) refnode;
        sie = ad.getSpecifierOrInitializerExpression();
        arrow = " = ";
      } else if (refnode instanceof Tree.MethodDeclaration) {
        Tree.MethodDeclaration md = (Tree.MethodDeclaration) refnode;
        sie = md.getSpecifierExpression();
        arrow = " => ";
      }
      Tree.CompilationUnit lcu = cpc.getLastCompilationUnit();
      if (sie == null) {
        class FindInitializerVisitor extends Visitor {
          Tree.SpecifierOrInitializerExpression result;

          @Override
          public void visit(Tree.InitializerParameter that) {
            super.visit(that);
            Declaration d = that.getParameterModel().getModel();
            if (d != null && d.equals(dec)) {
              result = that.getSpecifierExpression();
            }
          }
        }
        FindInitializerVisitor fiv = new FindInitializerVisitor();
        fiv.visit(lcu);
        sie = fiv.result;
      }
      if (sie != null) {
        Tree.Expression e = sie.getExpression();
        if (e != null) {
          Tree.Term term = e.getTerm();
          if (term instanceof Tree.Literal) {
            String text = term.getToken().getText();
            if (text.length() < 20) {
              return arrow + text;
            }
          } else if (term instanceof Tree.BaseMemberOrTypeExpression) {
            Tree.BaseMemberOrTypeExpression bme = (Tree.BaseMemberOrTypeExpression) term;
            Tree.Identifier id = bme.getIdentifier();
            if (id != null && bme.getTypeArguments() == null) {
              return arrow + id.getText();
            }
          } else if (term != null) {
            Unit unit = lcu.getUnit();
            if (term.getUnit().equals(unit)) {
              String impl = Nodes.text(term, cpc.getTokens());
              if (impl.length() < 10) {
                return arrow + impl;
              }
            }
          }
          // don't have the token stream :-/
          // TODO: figure out where to get it from!
          return arrow + "...";
        }
      }
    }
    return "";
  }