private static List<Tree.ImportMemberOrType> getUsedImportElements(
     List<Tree.Import> imports, List<Declaration> unused, boolean hasWildcard) {
   List<Tree.ImportMemberOrType> list = new ArrayList<Tree.ImportMemberOrType>();
   for (Tree.Import ti : imports) {
     for (Tree.ImportMemberOrType imt : ti.getImportMemberOrTypeList().getImportMemberOrTypes()) {
       Declaration dm = imt.getDeclarationModel();
       if (dm != null && !hasRealErrors(imt.getIdentifier()) && !hasRealErrors(imt)) {
         if (unused.contains(dm)) {
           if (imt.getImportMemberOrTypeList() != null) {
             for (Tree.ImportMemberOrType nimt :
                 imt.getImportMemberOrTypeList().getImportMemberOrTypes()) {
               Declaration ndm = nimt.getDeclarationModel();
               if (ndm != null && !hasRealErrors(nimt.getIdentifier()) && !hasRealErrors(nimt)) {
                 if (!unused.contains(ndm)) {
                   list.add(imt);
                   break;
                 }
               }
             }
           }
         } else {
           if (!hasWildcard || imt.getAlias() != null || imt.getImportMemberOrTypeList() != null) {
             list.add(imt);
           }
         }
       }
     }
   }
   return list;
 }
Example #2
0
 private void addImport(Tree.ImportMemberOrType member, ImportList il, Import i) {
   String alias = i.getAlias();
   if (alias != null) {
     Map<String, String> mods = unit.getModifiers();
     if (mods.containsKey(alias) && mods.get(alias).equals(alias)) {
       // spec says you can't hide a language modifier
       // unless the modifier itself has an alias
       // (this is perhaps a little heavy-handed)
       member.addError("import hides a language modifier: '" + alias + "'");
     } else {
       Import o = unit.getImport(alias);
       if (o == null) {
         unit.getImports().add(i);
         il.getImports().add(i);
       } else if (o.isWildcardImport()) {
         unit.getImports().remove(o);
         il.getImports().remove(o);
         unit.getImports().add(i);
         il.getImports().add(i);
       } else {
         member.addError("duplicate import alias: '" + alias + "'");
       }
     }
   }
 }
Example #3
0
 public static boolean removeImport(
     String originalPackage,
     final Declaration dec,
     Tree.CompilationUnit cu,
     TextChange tc,
     Set<String> packages) {
   boolean foundOriginal = false;
   Tree.ImportList il = cu.getImportList();
   for (Tree.Import imp : il.getImports()) {
     Referenceable model = imp.getImportPath().getModel();
     if (model != null) {
       if (model.getNameAsString().equals(originalPackage)) {
         Tree.ImportMemberOrTypeList imtl = imp.getImportMemberOrTypeList();
         if (imtl != null) {
           List<ImportMemberOrType> imts = imtl.getImportMemberOrTypes();
           for (int j = 0; j < imts.size(); j++) {
             Tree.ImportMemberOrType imt = imts.get(j);
             Declaration d = imt.getDeclarationModel();
             if (d != null && d.equals(dec)) {
               int offset;
               int length;
               if (j > 0) {
                 offset = getNodeEndOffset(imts.get(j - 1));
                 length = getNodeEndOffset(imt) - offset;
               } else if (j < imts.size() - 1) {
                 offset = getNodeStartOffset(imt);
                 length = getNodeStartOffset(imts.get(j + 1)) - offset;
               } else {
                 if (packages.contains(originalPackage)) {
                   // we're adding to this import statement,
                   // so don't delete the whole import
                   offset = getNodeStartOffset(imt);
                   length = getNodeLength(imt);
                 } else {
                   offset = getNodeStartOffset(imp);
                   length = getNodeLength(imp);
                 }
               }
               tc.addEdit(new DeleteEdit(offset, length));
               foundOriginal = true;
               break;
               // TODO: return the alias!
             }
           }
         }
         break;
       }
     }
   }
   return foundOriginal;
 }
 private static void appendImportElements(
     String packageName,
     List<Tree.ImportMemberOrType> elements,
     List<Declaration> unused,
     List<Declaration> proposed,
     boolean hasWildcard,
     StringBuilder builder) {
   for (Tree.ImportMemberOrType i : elements) {
     if (i.getDeclarationModel() != null
         && i.getIdentifier().getErrors().isEmpty()
         && i.getErrors().isEmpty()) {
       if (!i.getImportModel().getAlias().equals(i.getDeclarationModel().getName())) {
         builder.append(i.getImportModel().getAlias()).append("=");
       }
       builder.append(i.getDeclarationModel().getName());
       appendNestedImportElements(i, unused, builder);
       builder.append(", ");
     }
   }
   for (Declaration d : proposed) {
     if (d.getUnit().getPackage().getNameAsString().equals(packageName)) {
       builder.append(d.getName()).append(", ");
     }
   }
   if (hasWildcard) {
     builder.append("...");
   } else {
     builder.setLength(builder.length() - 2);
   }
 }
Example #5
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;
 }
Example #6
0
 private void importMembers(Tree.ImportMemberOrType member, Declaration d) {
   Tree.ImportMemberOrTypeList imtl = member.getImportMemberOrTypeList();
   if (imtl != null) {
     if (d instanceof Value) {
       Value v = (Value) d;
       TypeDeclaration td = v.getTypeDeclaration();
       if (td.isObjectClass()) {
         d = td;
       }
     }
     if (d instanceof TypeDeclaration) {
       Set<String> names = new HashSet<String>();
       ImportList til = imtl.getImportList();
       TypeDeclaration td = (TypeDeclaration) d;
       til.setImportedScope(td);
       List<Tree.ImportMemberOrType> imts = imtl.getImportMemberOrTypes();
       for (Tree.ImportMemberOrType imt : imts) {
         names.add(importMember(imt, td, til));
       }
       if (imtl.getImportWildcard() != null) {
         importAllMembers(td, names, til);
       } else if (imts.isEmpty()) {
         imtl.addError("empty import list", 1020);
       }
     } else {
       imtl.addError("member alias list must follow a type");
     }
   }
 }
Example #7
0
 private void addMemberImport(Tree.ImportMemberOrType member, ImportList il, Import i) {
   String alias = i.getAlias();
   if (alias != null) {
     if (il.getImport(alias) == null) {
       unit.getImports().add(i);
       il.getImports().add(i);
     } else {
       member.addError("duplicate member import alias: '" + alias + "'");
     }
   }
 }
 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));
     }
   }
 }
Example #9
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;
 }
 private static void appendNestedImportElements(
     Tree.ImportMemberOrType imt, List<Declaration> unused, StringBuilder builder) {
   if (imt.getImportMemberOrTypeList() != null) {
     builder.append(" { ");
     boolean found = false;
     for (Tree.ImportMemberOrType nimt :
         imt.getImportMemberOrTypeList().getImportMemberOrTypes()) {
       if (nimt.getDeclarationModel() != null
           && nimt.getIdentifier().getErrors().isEmpty()
           && nimt.getErrors().isEmpty()) {
         if (!unused.contains(nimt.getDeclarationModel())) {
           found = true;
           if (!nimt.getImportModel().getAlias().equals(nimt.getDeclarationModel().getName())) {
             builder.append(nimt.getImportModel().getAlias()).append("=");
           }
           builder.append(nimt.getDeclarationModel().getName()).append(", ");
         }
       }
     }
     if (found) builder.setLength(builder.length() - 2);
     builder.append(" }");
     if (!found) builder.setLength(builder.length() - 5);
   }
 }