Ejemplo n.º 1
0
 @Nullable
 public static PsiElement skipSiblingsForwardByPredicate(
     @Nullable PsiElement element, Predicate<PsiElement> elementsToSkip) {
   if (element == null) return null;
   for (PsiElement e = element.getNextSibling(); e != null; e = e.getNextSibling()) {
     if (elementsToSkip.apply(e)) continue;
     return e;
   }
   return null;
 }
Ejemplo n.º 2
0
 private static PsiElement getNextElement(PsiElement element) {
   PsiElement sibling = element.getNextSibling();
   if (sibling != null) return sibling;
   element = element.getParent();
   if (element != null) return getNextElement(element);
   return null;
 }
Ejemplo n.º 3
0
 public static <D> void visitChildren(
     @NotNull JetElement element, @NotNull JetTreeVisitor<D> visitor, D data) {
   PsiElement child = element.getFirstChild();
   while (child != null) {
     if (child instanceof JetElement) {
       ((JetElement) child).accept(visitor, data);
     }
     child = child.getNextSibling();
   }
 }
 private static boolean isLastInRuleOrFree(PsiElement element) {
   PsiElement parent =
       PsiTreeUtil.getParentOfType(
           element, BnfRule.class, GeneratedParserUtilBase.DummyBlock.class);
   if (parent instanceof GeneratedParserUtilBase.DummyBlock) return true;
   if (!(parent instanceof BnfRule)) return false;
   for (PsiElement cur = element, next = cur.getNextSibling();
       next == null || next instanceof PsiComment || next instanceof PsiWhiteSpace;
       cur = next, next = cur.getNextSibling()) {
     if (next == null) {
       PsiElement curParent = cur.getParent();
       while (next == null && curParent != parent) {
         next = curParent.getNextSibling();
         curParent = curParent.getParent();
       }
       if (curParent == parent) return true;
       next = PsiTreeUtil.getDeepestFirst(next);
     }
   }
   return false;
 }
Ejemplo n.º 5
0
  @Override
  public String suggestUniqueVariableName(String baseName, PsiElement place, boolean lookForward) {
    int index = 0;
    PsiElement scope =
        PsiTreeUtil.getNonStrictParentOfType(
            place, PsiStatement.class, PsiCodeBlock.class, PsiMethod.class);
    NextName:
    while (true) {
      String name = baseName;
      if (index > 0) {
        name += index;
      }
      index++;
      if (PsiUtil.isVariableNameUnique(name, place)) {
        if (lookForward) {
          final String name1 = name;
          PsiElement run = scope;
          while (run != null) {
            class CancelException extends RuntimeException {}
            try {
              run.accept(
                  new JavaRecursiveElementWalkingVisitor() {
                    @Override
                    public void visitAnonymousClass(final PsiAnonymousClass aClass) {}

                    @Override
                    public void visitVariable(PsiVariable variable) {
                      if (name1.equals(variable.getName())) {
                        throw new CancelException();
                      }
                    }
                  });
            } catch (CancelException e) {
              continue NextName;
            }
            run = run.getNextSibling();
            if (scope instanceof PsiMethod) { // do not check next member for param name conflict
              break;
            }
          }
        }
        return name;
      }
    }
  }
Ejemplo n.º 6
0
  // Delete given element and all the elements separating it from the neighboring elements of the
  // same class
  public static void deleteElementWithDelimiters(@NotNull PsiElement element) {
    PsiElement paramBefore = PsiTreeUtil.getPrevSiblingOfType(element, element.getClass());

    PsiElement from;
    PsiElement to;
    if (paramBefore != null) {
      from = paramBefore.getNextSibling();
      to = element;
    } else {
      PsiElement paramAfter = PsiTreeUtil.getNextSiblingOfType(element, element.getClass());

      from = element;
      to = paramAfter != null ? paramAfter.getPrevSibling() : element;
    }

    PsiElement parent = element.getParent();

    parent.deleteChildRange(from, to);
  }
Ejemplo n.º 7
0
  public XmlAttribute setAttribute(String qname, String value) throws IncorrectOperationException {
    final XmlAttribute attribute = getAttribute(qname);

    if (attribute != null) {
      if (value == null) {
        deleteChildInternal(attribute.getNode());
        return null;
      }
      attribute.setValue(value);
      return attribute;
    } else if (value == null) {
      return null;
    } else {
      PsiElement xmlAttribute =
          add(XmlElementFactory.getInstance(getProject()).createXmlAttribute(qname, value));
      while (!(xmlAttribute instanceof XmlAttribute)) xmlAttribute = xmlAttribute.getNextSibling();
      return (XmlAttribute) xmlAttribute;
    }
  }
  private void buildFlowForClosure(final GrClosableBlock closure) {
    for (GrParameter parameter : closure.getAllParameters()) {
      if (myPolicy.isVariableInitialized(parameter)) {
        addNode(new ReadWriteVariableInstruction(parameter.getName(), parameter, WRITE));
      }
    }

    addNode(new ReadWriteVariableInstruction("owner", closure.getLBrace(), WRITE));

    PsiElement child = closure.getFirstChild();
    while (child != null) {
      if (child instanceof GroovyPsiElement) {
        ((GroovyPsiElement) child).accept(this);
      }
      child = child.getNextSibling();
    }

    final GrStatement[] statements = closure.getStatements();
    if (statements.length > 0) {
      handlePossibleReturn(statements[statements.length - 1]);
    }
  }
  private static boolean shouldSkipLine(final PsiFile file, Document doc, int line) {
    final int start = doc.getLineStartOffset(line);
    final int end = doc.getLineEndOffset(line);
    final int _start = CharArrayUtil.shiftForward(doc.getCharsSequence(), start, " \n\t");
    if (_start >= end) {
      return true;
    }

    TextRange alreadyChecked = null;
    for (PsiElement elem = file.findElementAt(_start);
        elem != null
            && elem.getTextOffset() <= end
            && (alreadyChecked == null || !alreadyChecked.contains(elem.getTextRange()));
        elem = elem.getNextSibling()) {
      for (PsiElement _elem = elem; _elem.getTextOffset() >= _start; _elem = _elem.getParent()) {
        alreadyChecked = _elem.getTextRange();

        if (_elem instanceof PsiDeclarationStatement) {
          final PsiElement[] declared = ((PsiDeclarationStatement) _elem).getDeclaredElements();
          for (PsiElement declaredElement : declared) {
            if (declaredElement instanceof PsiVariable) {
              return false;
            }
          }
        }

        if (_elem instanceof PsiJavaCodeReferenceElement) {
          final PsiElement resolved = ((PsiJavaCodeReferenceElement) _elem).resolve();
          if (resolved instanceof PsiVariable) {
            return false;
          }
        }
      }
    }
    return true;
  }