private static ClassOrInterface findClassContainer(Tree.CompilationUnit cu, Node node) { FindContainerVisitor fcv = new FindContainerVisitor(node); fcv.visit(cu); Tree.Declaration declaration = fcv.getDeclaration(); if (declaration == null || declaration == node) return null; if (declaration instanceof Tree.ClassOrInterface) return (ClassOrInterface) declaration.getDeclarationModel(); if (declaration instanceof Tree.MethodDefinition) return findClassContainer(declaration.getDeclarationModel()); if (declaration instanceof Tree.ObjectDefinition) return findClassContainer(declaration.getDeclarationModel()); return null; }
@Override public void visit(Tree.Declaration that) { boolean oe = endsInBreakReturnThrow; Tree.Continue olc = lastContinue; lastContinue = null; endsInBreakReturnThrow = false; if (that.getDeclarationModel() == declaration) { inLoop = false; beginDisabledSpecificationScope(); declare(); super.visit(that); endDisabledSpecificationScope(false); inLoop = false; } else { boolean l = inLoop; inLoop = false; Scope scope = that.getScope(); boolean constructor = scope instanceof Constructor; boolean c = false; if (!constructor) { c = beginDisabledSpecificationScope(); } boolean d = beginDeclarationScope(); SpecificationState as = beginSpecificationScope(); super.visit(that); if (!constructor) { endDisabledSpecificationScope(c); } endDeclarationScope(d); endSpecificationScope(as); inLoop = l; } endsInBreakReturnThrow = oe; lastContinue = olc; }
@Override public void visit(Tree.Declaration that) { super.visit(that); if (that.getDeclarationModel().equals(declaration)) { refs++; } }
public static void refactorDocLinks( Tree.Declaration node, final String targetPackage, Tree.CompilationUnit cu, final TextChange tc) { final Declaration dec = node.getDeclarationModel(); cu.visit( new Visitor() { @Override public void visit(DocLink that) { super.visit(that); if (that.getBase() != null && that.getBase().equals(dec)) { boolean inTargetPackage = that.getUnit().getPackage().getQualifiedNameString().equals(targetPackage); if (that.getPkg() == null) { if (!inTargetPackage) { Region region = DocLinks.nameRegion(that, 0); tc.addEdit(new InsertEdit(region.getOffset(), targetPackage + "::")); } } else { Region region = DocLinks.packageRegion(that); if (inTargetPackage) { tc.addEdit(new DeleteEdit(region.getOffset(), region.getLength() + 2)); } else { tc.addEdit( new ReplaceEdit(region.getOffset(), region.getLength(), targetPackage)); } } } } }); }
private Tree.Declaration getDeclaration(Tree.ClassBody that) { for (Tree.Statement s : that.getStatements()) { if (s instanceof Tree.Declaration) { Tree.Declaration d = (Tree.Declaration) s; if (d.getDeclarationModel() == declaration) { return d; } } } return null; }
@Override public void visit(Tree.Declaration that) { super.visit(that); if (result == null || !that.equals(result)) { Declaration d = that.getDeclarationModel(); if (d.isShared()) { problem = "a shared declaration"; } else { if (hasOuterRefs(d, scope, statements)) { problem = "a declaration used elsewhere"; } } } }
public static boolean isUnsharedUsedLocally( Tree.Declaration node, IFile originalFile, String originalPackage, String targetPackage) { Declaration dec = node.getDeclarationModel(); if (!dec.isShared() && !originalPackage.equals(targetPackage)) { for (PhasedUnit pu : getAllUnits(originalFile.getProject())) { Tree.CompilationUnit cu = pu.getCompilationUnit(); String pn = cu.getUnit().getPackage().getNameAsString(); if (pn.equals(originalPackage) && isUsedInUnit(cu, dec)) { return true; } } } return false; }
public static void refactorImports( Tree.Declaration node, final String originalPackage, final String targetPackage, Tree.CompilationUnit cu, final TextChange tc) { final Declaration dec = node.getDeclarationModel(); String pn = cu.getUnit().getPackage().getNameAsString(); boolean inOriginalPackage = pn.equals(originalPackage); boolean inNewPackage = pn.equals(targetPackage); boolean foundOriginal = removeImport(originalPackage, dec, cu, tc, Collections.<String>emptySet()); if (foundOriginal && !inNewPackage || inOriginalPackage && !inNewPackage && isUsedInUnit(cu, dec)) { if (!targetPackage.isEmpty()) { addImport(targetPackage, dec, cu, tc); } } }
@NotNull @Override public String getDescriptiveName(@NotNull PsiElement element) { if (element instanceof CeylonPsi.AttributeDeclarationPsi) { return ((CeylonPsi.AttributeDeclarationPsi) element) .getCeylonNode() .getDeclarationModel() .getQualifiedNameString(); } else if (element instanceof CeylonPsi.ClassOrInterfacePsi) { Tree.Declaration ceylonNode = ((CeylonPsi.ClassOrInterfacePsi) element).getCeylonNode(); // if (ceylonNode == null) { // // perhaps a stub // return ((CeylonPsi.ClassOrInterfacePsi) element).getQualifiedName(); // } Declaration model = ceylonNode.getDeclarationModel(); return model == null ? ceylonNode.getIdentifier().getText() : model.getQualifiedNameString(); } else if (element instanceof CeylonPsi.AnyMethodPsi) { Function model = ((CeylonPsi.AnyMethodPsi) element).getCeylonNode().getDeclarationModel(); return model == null ? ((CeylonPsi.AnyMethodPsi) element).getCeylonNode().getIdentifier().getText() : model.getQualifiedNameString(); } else if (element instanceof CeylonPsi.ParameterDeclarationPsi) { return ((CeylonPsi.ParameterDeclarationPsi) element) .getCeylonNode() .getTypedDeclaration() .getIdentifier() .getText(); } else if (element instanceof CeylonPsi.TypeParameterDeclarationPsi) { return ((CeylonPsi.TypeParameterDeclarationPsi) element) .getCeylonNode() .getIdentifier() .getText(); } else if (element instanceof CeylonFile) { return ((CeylonFile) element).getName(); } else if (element instanceof CeylonPsi.ObjectDefinitionPsi) { return ((CeylonPsi.ObjectDefinitionPsi) element).getCeylonNode().getIdentifier().getText(); } throw new UnsupportedOperationException( "Descriptive name not implemented for " + element.getClass()); }
private void extractStatementsInFile(TextChange tfc) throws CoreException { tfc.setEdit(new MultiTextEdit()); IDocument doc = tfc.getCurrentDocument(null); Tree.Body body = (Tree.Body) node; Integer start = statements.get(0).getStartIndex(); int length = statements.get(statements.size() - 1).getStopIndex() - start + 1; FindContainerVisitor fsv = new FindContainerVisitor(body); rootNode.visit(fsv); Tree.Declaration decNode = fsv.getDeclaration(); /*if (decNode instanceof Tree.Declaration) { Tree.AnnotationList anns = ((Tree.Declaration) decNode).getAnnotationList(); if (anns!=null && !anns.getAnnotations().isEmpty()) { decNode = anns.getAnnotations().get(0); } }*/ Declaration dec = decNode.getDeclarationModel(); FindLocalReferencesVisitor flrv = new FindLocalReferencesVisitor(dec); for (Statement s : statements) { s.visit(flrv); } List<TypeDeclaration> localTypes = new ArrayList<TypeDeclaration>(); List<Tree.BaseMemberExpression> localRefs = new ArrayList<Tree.BaseMemberExpression>(); for (Tree.BaseMemberExpression bme : flrv.getLocalReferences()) { if (result == null || !bme.getDeclaration().equals(result.getDeclarationModel())) { FindOuterReferencesVisitor v = new FindOuterReferencesVisitor(bme.getDeclaration()); for (Statement s : body.getStatements()) { if (!statements.contains(s)) { s.visit(v); } } if (v.refs > 0) { addLocalType(dec, bme.getTypeModel(), localTypes, new ArrayList<ProducedType>()); localRefs.add(bme); } } } String params = ""; String args = ""; Set<Declaration> done = new HashSet<Declaration>(); boolean nonempty = false; for (Tree.BaseMemberExpression bme : localRefs) { if (done.add(bme.getDeclaration())) { params += bme.getTypeModel().getProducedTypeName() + " " + bme.getIdentifier().getText() + ", "; args += bme.getIdentifier().getText() + ", "; nonempty = true; } } if (nonempty) { params = params.substring(0, params.length() - 2); args = args.substring(0, args.length() - 2); } String indent = "\n" + getIndent(decNode, doc); String extraIndent = indent + getDefaultIndent(); String typeParams = ""; String constraints = ""; if (!localTypes.isEmpty()) { for (TypeDeclaration t : localTypes) { typeParams += t.getName() + ", "; if (!t.getSatisfiedTypes().isEmpty()) { constraints += extraIndent + getDefaultIndent() + "given " + t.getName() + " satisfies "; for (ProducedType pt : t.getSatisfiedTypes()) { constraints += pt.getProducedTypeName() + "&"; } constraints = constraints.substring(0, constraints.length() - 1); } } typeParams = "<" + typeParams.substring(0, typeParams.length() - 2) + ">"; } String content = result == null ? "void" : result.getDeclarationModel().getType().getProducedTypeName(); content += " " + newName + typeParams + "(" + params + ")" + constraints + " {"; for (Statement s : statements) { content += extraIndent + toString(s); } if (result != null) { content += extraIndent + "return " + result.getDeclarationModel().getName() + ";"; } content += indent + "}" + indent + indent; String invocation = newName + "(" + args + ");"; if (result != null) { String modifs; if (result.getDeclarationModel().isShared()) { modifs = "shared " + result.getDeclarationModel().getType().getProducedTypeName() + " "; } else { modifs = "value "; } invocation = modifs + result.getDeclarationModel().getName() + "=" + invocation; } tfc.addEdit(new InsertEdit(decNode.getStartIndex(), content)); tfc.addEdit(new ReplaceEdit(start, length, invocation)); }
private void extractExpressionInFile(TextChange tfc) throws CoreException { tfc.setEdit(new MultiTextEdit()); IDocument doc = tfc.getCurrentDocument(null); Tree.Term term = (Tree.Term) node; Integer start = term.getStartIndex(); int length = term.getStopIndex() - start + 1; String exp = toString(term); FindContainerVisitor fsv = new FindContainerVisitor(term); rootNode.visit(fsv); Tree.Declaration decNode = fsv.getDeclaration(); /*if (decNode instanceof Tree.Declaration) { Tree.AnnotationList anns = ((Tree.Declaration) decNode).getAnnotationList(); if (anns!=null && !anns.getAnnotations().isEmpty()) { decNode = anns.getAnnotations().get(0); } }*/ Declaration dec = decNode.getDeclarationModel(); FindLocalReferencesVisitor flrv = new FindLocalReferencesVisitor(dec); term.visit(flrv); List<TypeDeclaration> localTypes = new ArrayList<TypeDeclaration>(); for (Tree.BaseMemberExpression bme : flrv.getLocalReferences()) { addLocalType(dec, bme.getTypeModel(), localTypes, new ArrayList<ProducedType>()); } String params = ""; String args = ""; if (!flrv.getLocalReferences().isEmpty()) { for (Tree.BaseMemberExpression bme : flrv.getLocalReferences()) { params += bme.getTypeModel().getProducedTypeName() + " " + bme.getIdentifier().getText() + ", "; args += bme.getIdentifier().getText() + ", "; } params = params.substring(0, params.length() - 2); args = args.substring(0, args.length() - 2); } String indent = "\n" + getIndent(decNode, doc); String extraIndent = indent + getDefaultIndent(); String typeParams = ""; String constraints = ""; if (!localTypes.isEmpty()) { for (TypeDeclaration t : localTypes) { typeParams += t.getName() + ", "; if (!t.getSatisfiedTypes().isEmpty()) { constraints += extraIndent + getDefaultIndent() + "given " + t.getName() + " satisfies "; for (ProducedType pt : t.getSatisfiedTypes()) { constraints += pt.getProducedTypeName() + "&"; } constraints = constraints.substring(0, constraints.length() - 1); } } typeParams = "<" + typeParams.substring(0, typeParams.length() - 2) + ">"; } String type; String ending; if ("Void".equals(term.getTypeModel().getProducedTypeName())) { type = "void"; ending = ""; } else { type = explicitType || dec.isToplevel() ? term.getTypeModel().getProducedTypeName() : "function"; ending = "return "; } tfc.addEdit( new InsertEdit( decNode.getStartIndex(), type + " " + newName + typeParams + "(" + params + ")" + constraints + " {" + extraIndent + ending + exp + ";" + indent + "}" + indent + indent)); tfc.addEdit(new ReplaceEdit(start, length, newName + "(" + args + ")")); }
public static boolean defaultParameterMethodOnSelf(Tree.Declaration decl) { return defaultParameterMethodOnSelf(decl.getDeclarationModel()); }
public static boolean defaultParameterMethodStatic(Tree.Declaration decl) { // Only top-level methods and top-level class initializers // have static default value methods return defaultParameterMethodStatic(decl.getDeclarationModel()); }
public static boolean defaultParameterMethodTakesThis(Tree.Declaration decl) { return defaultParameterMethodTakesThis(decl.getDeclarationModel()); }
@Override public void visit(Tree.Declaration that) { if (!that.getDeclarationModel().equals(dec)) { super.visit(that); } }