@Override public Void visitMethodDeclaration(MethodDeclaration node) { ElementHolder holder = new ElementHolder(); boolean wasInFunction = inFunction; inFunction = true; try { visitChildren(holder, node); } finally { inFunction = wasInFunction; } boolean isStatic = node.isStatic(); Token property = node.getPropertyKeyword(); if (property == null) { SimpleIdentifier methodName = node.getName(); String nameOfMethod = methodName.getName(); if (nameOfMethod.equals(TokenType.MINUS.getLexeme()) && node.getParameters().getParameters().size() == 0) { nameOfMethod = "unary-"; } MethodElementImpl element = new MethodElementImpl(nameOfMethod, methodName.getOffset()); element.setAbstract(node.isAbstract()); element.setFunctions(holder.getFunctions()); element.setLabels(holder.getLabels()); element.setLocalVariables(holder.getLocalVariables()); element.setParameters(holder.getParameters()); element.setStatic(isStatic); currentHolder.addMethod(element); methodName.setStaticElement(element); } else { SimpleIdentifier propertyNameNode = node.getName(); String propertyName = propertyNameNode.getName(); FieldElementImpl field = (FieldElementImpl) currentHolder.getField(propertyName); if (field == null) { field = new FieldElementImpl(node.getName().getName()); field.setFinal(true); field.setStatic(isStatic); field.setSynthetic(true); currentHolder.addField(field); } if (matches(property, Keyword.GET)) { PropertyAccessorElementImpl getter = new PropertyAccessorElementImpl(propertyNameNode); getter.setFunctions(holder.getFunctions()); getter.setLabels(holder.getLabels()); getter.setLocalVariables(holder.getLocalVariables()); getter.setVariable(field); getter.setAbstract( node.getBody() instanceof EmptyFunctionBody && node.getExternalKeyword() == null); getter.setGetter(true); getter.setStatic(isStatic); field.setGetter(getter); currentHolder.addAccessor(getter); propertyNameNode.setStaticElement(getter); } else { PropertyAccessorElementImpl setter = new PropertyAccessorElementImpl(propertyNameNode); setter.setFunctions(holder.getFunctions()); setter.setLabels(holder.getLabels()); setter.setLocalVariables(holder.getLocalVariables()); setter.setParameters(holder.getParameters()); setter.setVariable(field); setter.setAbstract( node.getBody() instanceof EmptyFunctionBody && !matches(node.getExternalKeyword(), Keyword.EXTERNAL)); setter.setSetter(true); setter.setStatic(isStatic); field.setSetter(setter); field.setFinal(false); currentHolder.addAccessor(setter); propertyNameNode.setStaticElement(setter); } } holder.validate(); return null; }
public void start() { if (getActiveLinkedMode() != null) { // for safety; should already be handled in RenameDartElementAction fgActiveLinkedMode.startFullDialog(); return; } ISourceViewer viewer = fEditor.getViewer(); IDocument document = viewer.getDocument(); fOriginalSelection = viewer.getSelectedRange(); int offset = fOriginalSelection.x; try { fLinkedPositionGroup = new LinkedPositionGroup(); prepareElement(); if (fDartElement == null) { return; } if (viewer instanceof ITextViewerExtension6) { IUndoManager undoManager = ((ITextViewerExtension6) viewer).getUndoManager(); if (undoManager instanceof IUndoManagerExtension) { IUndoManagerExtension undoManagerExtension = (IUndoManagerExtension) undoManager; IUndoContext undoContext = undoManagerExtension.getUndoContext(); IOperationHistory operationHistory = OperationHistoryFactory.getOperationHistory(); fStartingUndoOperation = operationHistory.getUndoOperation(undoContext); } } fOriginalName = nameNode.getName(); final int pos = nameNode.getOffset(); final List<ASTNode> sameNodes = Lists.newArrayList(); nameNode .getRoot() .accept( new RecursiveASTVisitor<Void>() { @Override public Void visitSimpleIdentifier(SimpleIdentifier node) { Element element = node.getElement(); element = getCanonicalElement(element); if (Objects.equal(element, fDartElement)) { sameNodes.add(node); } return super.visitSimpleIdentifier(node); } }); // TODO: copied from LinkedNamesAssistProposal#apply(..): // sort for iteration order, starting with the node @ offset Collections.sort( sameNodes, new Comparator<ASTNode>() { @Override public int compare(ASTNode o1, ASTNode o2) { return rank(o1) - rank(o2); } /** * Returns the absolute rank of an <code>ASTNode</code>. Nodes preceding <code>pos * </code> are ranked last. * * @param node the node to compute the rank for * @return the rank of the node with respect to the invocation offset */ private int rank(ASTNode node) { int relativeRank = node.getOffset() + node.getLength() - pos; if (relativeRank < 0) { return Integer.MAX_VALUE + relativeRank; } else { return relativeRank; } } }); for (int i = 0; i < sameNodes.size(); i++) { ASTNode elem = sameNodes.get(i); LinkedPosition linkedPosition = new LinkedPosition(document, elem.getOffset(), elem.getLength(), i); if (i == 0) { fNamePosition = linkedPosition; } fLinkedPositionGroup.addPosition(linkedPosition); } fLinkedModeModel = new LinkedModeModel(); fLinkedModeModel.addGroup(fLinkedPositionGroup); fLinkedModeModel.forceInstall(); fLinkedModeModel.addLinkingListener(new EditorHighlightingSynchronizer(fEditor)); fLinkedModeModel.addLinkingListener(new EditorSynchronizer()); LinkedModeUI ui = new EditorLinkedModeUI(fLinkedModeModel, viewer); ui.setExitPosition(viewer, offset, 0, Integer.MAX_VALUE); ui.setExitPolicy(new ExitPolicy(document)); ui.enter(); viewer.setSelectedRange( fOriginalSelection.x, fOriginalSelection.y); // by default, full word is selected; restore original selection if (viewer instanceof IEditingSupportRegistry) { IEditingSupportRegistry registry = (IEditingSupportRegistry) viewer; registry.register(fFocusEditingSupport); } openSecondaryPopup(); // startAnimation(); fgActiveLinkedMode = this; } catch (BadLocationException e) { DartToolsPlugin.log(e); } }
@Override public Void visitFunctionDeclaration(FunctionDeclaration node) { FunctionExpression expression = node.getFunctionExpression(); if (expression != null) { ElementHolder holder = new ElementHolder(); boolean wasInFunction = inFunction; inFunction = true; try { visitChildren(holder, expression); } finally { inFunction = wasInFunction; } Token property = node.getPropertyKeyword(); if (property == null) { SimpleIdentifier functionName = node.getName(); FunctionElementImpl element = new FunctionElementImpl(functionName); element.setFunctions(holder.getFunctions()); element.setLabels(holder.getLabels()); element.setLocalVariables(holder.getLocalVariables()); element.setParameters(holder.getParameters()); if (inFunction) { Block enclosingBlock = node.getAncestor(Block.class); if (enclosingBlock != null) { int functionEnd = node.getOffset() + node.getLength(); int blockEnd = enclosingBlock.getOffset() + enclosingBlock.getLength(); element.setVisibleRange(functionEnd, blockEnd - functionEnd - 1); } } currentHolder.addFunction(element); expression.setElement(element); functionName.setStaticElement(element); } else { SimpleIdentifier propertyNameNode = node.getName(); if (propertyNameNode == null) { // TODO(brianwilkerson) Report this internal error. return null; } String propertyName = propertyNameNode.getName(); TopLevelVariableElementImpl variable = (TopLevelVariableElementImpl) currentHolder.getTopLevelVariable(propertyName); if (variable == null) { variable = new TopLevelVariableElementImpl(node.getName().getName()); variable.setFinal(true); variable.setSynthetic(true); currentHolder.addTopLevelVariable(variable); } if (matches(property, Keyword.GET)) { PropertyAccessorElementImpl getter = new PropertyAccessorElementImpl(propertyNameNode); getter.setFunctions(holder.getFunctions()); getter.setLabels(holder.getLabels()); getter.setLocalVariables(holder.getLocalVariables()); getter.setVariable(variable); getter.setGetter(true); getter.setStatic(true); variable.setGetter(getter); currentHolder.addAccessor(getter); expression.setElement(getter); propertyNameNode.setStaticElement(getter); } else { PropertyAccessorElementImpl setter = new PropertyAccessorElementImpl(propertyNameNode); setter.setFunctions(holder.getFunctions()); setter.setLabels(holder.getLabels()); setter.setLocalVariables(holder.getLocalVariables()); setter.setParameters(holder.getParameters()); setter.setVariable(variable); setter.setSetter(true); setter.setStatic(true); variable.setSetter(setter); variable.setFinal(false); currentHolder.addAccessor(setter); expression.setElement(setter); propertyNameNode.setStaticElement(setter); } } holder.validate(); } return null; }