public static String getImportText( Set<String> packages, Map<Declaration, String> imports, String delim) { StringBuilder sb = new StringBuilder(); for (String p : packages) { if (p.isEmpty()) { // can't import from default package continue; } sb.append("import ").append(p).append(" {").append(delim); boolean first = true; for (Map.Entry<Declaration, String> e : imports.entrySet()) { Declaration d = e.getKey(); String pn = d.getUnit().getPackage().getQualifiedNameString(); if (pn.equals(p)) { if (!first) { sb.append(",").append(delim); } sb.append(getDefaultIndent()); String name = d.getName(); String alias = e.getValue(); if (!name.equals(alias)) { sb.append(alias).append("="); } sb.append(name); first = false; } } sb.append(delim).append("}").append(delim); } return sb.toString(); }
private boolean isSharedDeclarationUninitialized() { return (declaration.isShared() || declaration.getOtherInstanceAccess()) && !declaration.isFormal() && !isNativeHeader(declaration) && !isLate() && !specified.definitely; }
@Override public void visit(Tree.BaseMemberOrTypeExpression that) { Declaration d = that.getDeclaration(); if (d != null && d.equals(dec)) { detected = true; } }
@Override public void visit(Tree.ClassBody that) { if (that.getScope() == declaration.getContainer()) { Tree.Statement les = getLastExecutableStatement(that); Tree.Declaration lc = getLastConstructor(that); declarationSection = les == null; lastExecutableStatement = les; lastConstructor = lc; super.visit(that); declarationSection = false; lastExecutableStatement = null; lastConstructor = null; if (!declaration.isAnonymous()) { if (isSharedDeclarationUninitialized()) { getDeclaration(that) .addError( "must be definitely specified by class initializer: " + message(declaration) + " is shared", 1401); } } } else { super.visit(that); } }
public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException, OperationCanceledException { if (!newName.matches("^[a-zA-Z_]\\w*$")) { return createErrorStatus("Not a legal Ceylon identifier"); } else if (escaping_.get_().isKeyword(newName)) { return createErrorStatus("'" + newName + "' is a Ceylon keyword"); } else { int ch = newName.codePointAt(0); if (declaration instanceof TypedDeclaration) { if (!Character.isLowerCase(ch) && ch != '_') { return createErrorStatus("Not an initial lowercase identifier"); } } else if (declaration instanceof TypeDeclaration) { if (!Character.isUpperCase(ch)) { return createErrorStatus("Not an initial uppercase identifier"); } } } Declaration existing = declaration .getContainer() .getMemberOrParameter(declaration.getUnit(), newName, null, false); if (null != existing && !existing.equals(declaration)) { return createWarningStatus( "An existing declaration named '" + newName + "' already exists in the same scope"); } return new RefactoringStatus(); }
Declaration getDisplayedDeclaration(CeylonHierarchyNode node) { Declaration declaration = node.getDeclaration(); if (declaration != null && isShowingRefinements() && declaration.isClassOrInterfaceMember()) { declaration = (ClassOrInterface) declaration.getContainer(); } return declaration; }
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())); } }
@Override public void visit(Tree.InitializerParameter that) { super.visit(that); Declaration d = that.getParameterModel().getModel(); if (d != null && d.equals(dec)) { result = that.getSpecifierExpression(); } }
static String getDescription(Declaration dec) { String desc = "'" + dec.getName() + "'"; Scope container = dec.getContainer(); if (container instanceof TypeDeclaration) { TypeDeclaration td = (TypeDeclaration) container; desc += " in '" + td.getName() + "'"; } return desc; }
private boolean hidesToplevel(Declaration dec) { for (Declaration d : unit.getDeclarations()) { String n = d.getName(); if (d.isToplevel() && n != null && dec.getName().equals(n)) { return true; } } return false; }
public static boolean canMoveDeclaration(CeylonEditor editor) { Node node = editor.getSelectedNode(); if (node instanceof Tree.Declaration) { Declaration d = ((Tree.Declaration) node).getDeclarationModel(); return d != null && d.isToplevel(); } else { return false; } }
@Override public Object[] getChildren(Object element) { if (element instanceof CeylonOutlineNode) { if (mode) { boolean includeParameters = !CeylonPlugin.getPreferences().getBoolean(PARAMS_IN_OUTLINES); CeylonOutlineNode node = (CeylonOutlineNode) element; CompilationUnit rootNode = getEditor().getParseController().getLastCompilationUnit(); Node treeNode = Nodes.findNode(rootNode, node.getStartOffset(), node.getEndOffset()); TypeDeclaration td; if (treeNode instanceof ClassOrInterface) { ClassOrInterface ci = (ClassOrInterface) treeNode; td = ci.getDeclarationModel(); } else if (treeNode instanceof ObjectDefinition) { ObjectDefinition od = (ObjectDefinition) treeNode; td = od.getDeclarationModel().getTypeDeclaration(); } else { return super.getChildren(element); } List<Declaration> list = new ArrayList<Declaration>(); String filter = getFilterText().getText(); for (int i = 0; i < filter.length(); i++) { char ch = filter.charAt(i); if (ch == '*' || i > 0 && Character.isUpperCase(ch)) { filter = filter.substring(0, i); break; } } Collection<DeclarationWithProximity> members = td.getMatchingMemberDeclarations(rootNode.getUnit(), td, filter, 0, null).values(); for (DeclarationWithProximity dwp : members) { for (Declaration dec : overloads(dwp.getDeclaration())) { if (!(dec instanceof TypeParameter)) { if (includeParameters || !dec.isParameter()) { list.add(dec); } } } } if (!lexicalSortingAction.isChecked()) { Collections.sort( list, new Comparator<Declaration>() { public int compare(Declaration x, Declaration y) { String xn = x.getContainer().getQualifiedNameString(); String yn = y.getContainer().getQualifiedNameString(); return xn.compareTo(yn); } }); } return list.toArray(); } else { return super.getChildren(element); } } else { return null; } }
@Override public void visit(Tree.DocLink that) { super.visit(that); // TODO: copy/paste from EnterAliasRefactoring Declaration base = that.getBase(); if (base != null && dec.equals(base)) { Region region = DocLinks.nameRegion(that, 0); change.addEdit(new ReplaceEdit(region.getOffset(), region.getLength(), dec.getName())); } }
private void importAllMembers( TypeDeclaration importedType, Set<String> ignoredMembers, ImportList til) { for (Declaration dec : importedType.getMembers()) { if (dec.isShared() && (dec.isStaticallyImportable() || isConstructor(dec)) && isResolvable(dec) && !ignoredMembers.contains(dec.getName())) { addWildcardImport(til, dec, importedType); } } }
private void importAllMembers( Package importedPackage, Set<String> ignoredMembers, ImportList il) { for (Declaration dec : importedPackage.getMembers()) { if (dec.isShared() && isResolvable(dec) && !ignoredMembers.contains(dec.getName()) && !isNonimportable(importedPackage, dec.getName())) { addWildcardImport(il, dec); } } }
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() + "'"); } } }
@Override public boolean select(Viewer viewer, Object parentElement, Object element) { if (element instanceof CeylonOutlineNode) { CeylonOutlineNode node = (CeylonOutlineNode) element; return node.isShared(); } else if (element instanceof Declaration) { Declaration dec = (Declaration) element; return dec.isShared(); } else { return true; } }
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; }
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; }
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; }
@Override public IHyperlink[] detectHyperlinks( ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) { if (controller == null || controller.getLastCompilationUnit() == null) { return null; } else { Node node = findNode( controller.getLastCompilationUnit(), controller.getTokens(), region.getOffset(), region.getOffset() + region.getLength()); if (node == null) { return null; } else { Node id = getIdentifyingNode(node); if (id == null) { return null; } else { Referenceable referenceable = getReferencedModel(node); Backends supportedBackends = supportedBackends(); if (referenceable instanceof Declaration) { Declaration dec = (Declaration) referenceable; if (dec.isNative()) { if (supportedBackends.none()) { return null; } else { referenceable = resolveNative(referenceable, dec, supportedBackends); } } else { if (!supportedBackends.none()) { return null; } } } else { // Module or package descriptors if (!supportedBackends.none()) { return null; } } Node r = getReferencedNode(referenceable); if (r == null) { return null; } else { return new IHyperlink[] {new CeylonNodeLink(r, id)}; } } } } }
@Override public void visit(Tree.DocLink that) { Declaration base = that.getBase(); if (base != null) { if (base.equals(declaration)) { count++; } else { List<Declaration> qualified = that.getQualified(); if (qualified != null) { if (qualified.contains(declaration)) { count++; } } } } }
@Override public boolean getEnabled() { return declaration instanceof Declaration && declaration.getName() != null && project != null && (inSameProject(declaration) || inSameUnit()); }
@Override protected boolean isReference(Declaration ref) { return super.isReference(ref) || // include refinements of the selected // declaration that we're renaming ref != null && ref.refines((Declaration) getDeclaration()); }
private void checkVariable(Tree.Term term, Node node) { if (isEffectivelyBaseMemberExpression(term)) { // Note: other cases handled in ExpressionVisitor Tree.StaticMemberOrTypeExpression mte = (Tree.StaticMemberOrTypeExpression) term; Declaration member = mte.getDeclaration(); if (member == declaration) { if ((declaration.isFormal() || declaration.isDefault()) && !isForwardReferenceable()) { term.addError( "member is formal or default and may not be assigned here: '" + member.getName() + "'"); } else if (!isVariable() && !isLate()) { if (member instanceof Value) { if (node instanceof Tree.AssignOp) { term.addError( "value is not a variable and may not be assigned here: '" + member.getName() + "'", 803); } else { term.addError("value is not a variable: '" + member.getName() + "'", 800); } } else { term.addError("not a variable value: '" + member.getName() + "'"); } } } } }
private void addWildcardImport(ImportList il, Declaration dec) { if (!hidesToplevel(dec)) { Import i = new Import(); i.setAlias(dec.getName()); i.setDeclaration(dec); i.setWildcardImport(true); addWildcardImport(il, dec, i); } }
private void renameSourceFile(CompositeChange change) { String unitPath = declaration.getUnit().getFullPath(); IPath oldPath = project.getFullPath().append(unitPath); String newFileName = getNewName() + ".ceylon"; IPath newPath = oldPath.removeFirstSegments(1).removeLastSegments(1).append(newFileName); if (!project.getFile(newPath).exists()) { change.add(new RenameResourceChange(oldPath, newFileName)); } }
public RenameRefactoring(IEditorPart editor) { super(editor); boolean identifiesDeclaration = node instanceof Tree.DocLink || getIdentifyingNode(node) instanceof Tree.Identifier; if (rootNode != null && identifiesDeclaration) { Referenceable refDec = getReferencedExplicitDeclaration(node, rootNode); if (refDec instanceof Declaration) { Declaration dec = (Declaration) refDec; declaration = dec.getRefinedDeclaration(); newName = declaration.getName(); String filename = declaration.getUnit().getFilename(); renameFile = (declaration.getName() + ".ceylon").equals(filename); } else { declaration = null; } } else { declaration = null; } }
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))); } }
@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()); }