/**
  * Return the body of the function that contains the given parameter, or {@code null} if no
  * function body could be found.
  *
  * @param node the parameter contained in the function whose body is to be returned
  * @return the body of the function that contains the given parameter
  */
 private FunctionBody getFunctionBody(FormalParameter node) {
   ASTNode parent = node.getParent();
   while (parent != null) {
     if (parent instanceof ConstructorDeclaration) {
       return ((ConstructorDeclaration) parent).getBody();
     } else if (parent instanceof FunctionExpression) {
       return ((FunctionExpression) parent).getBody();
     } else if (parent instanceof MethodDeclaration) {
       return ((MethodDeclaration) parent).getBody();
     }
     parent = parent.getParent();
   }
   return null;
 }
 private Void visitChildren(ElementHolder holder, ASTNode node) {
   ElementHolder previousBuilder = currentHolder;
   currentHolder = holder;
   try {
     node.visitChildren(this);
   } finally {
     currentHolder = previousBuilder;
   }
   return null;
 }
Exemple #3
0
 /**
  * Make the given holder be the current holder while visiting the children of the given node.
  *
  * @param holder the holder that will gather elements that are built while visiting the children
  * @param node the node whose children are to be visited
  */
 private void visitChildren(ElementHolder holder, ASTNode node) {
   if (node != null) {
     ElementHolder previousHolder = currentHolder;
     currentHolder = holder;
     try {
       node.visitChildren(this);
     } finally {
       currentHolder = previousHolder;
     }
   }
 }
  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);
    }
  }
Exemple #5
0
 /**
  * Safely visit the given node, printing the prefix before the node if it is non-{@code null}.
  *
  * @param prefix the prefix to be printed if there is a node to visit
  * @param node the node to be visited
  */
 private void visit(String prefix, ASTNode node) {
   if (node != null) {
     writer.print(prefix);
     node.accept(this);
   }
 }
Exemple #6
0
 /**
  * Safely visit the given node, printing the suffix after the node if it is non-{@code null}.
  *
  * @param suffix the suffix to be printed if there is a node to visit
  * @param node the node to be visited
  */
 private void visit(ASTNode node, String suffix) {
   if (node != null) {
     node.accept(this);
     writer.print(suffix);
   }
 }
Exemple #7
0
 /**
  * Safely visit the given node.
  *
  * @param node the node to be visited
  */
 private void visit(ASTNode node) {
   if (node != null) {
     node.accept(this);
   }
 }