Java Code Examples for com.intellij.lang.ASTNode#getTextLength()

The following examples show how to use com.intellij.lang.ASTNode#getTextLength() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BashWordImpl.java    From BashSupport with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isWrapped() {
    if (isWrapped == null) {
        synchronized (stateLock) {
            if (isWrapped == null) {
                boolean newIsWrapped = false;
                if (getTextLength() >= 2) {
                    ASTNode firstChildNode = getNode().getFirstChildNode();
                    if (firstChildNode != null && firstChildNode.getTextLength() >= 2) {
                        String text = firstChildNode.getText();

                        newIsWrapped = (text.startsWith("$'") || text.startsWith("'")) && text.endsWith("'");
                    }
                }

                isWrapped = newIsWrapped;
            }
        }
    }

    return isWrapped;
}
 
Example 2
Source File: LowLevelSearchUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static TreeElement findNextLeafElementAt(ASTNode scopeNode, TreeElement last, int offset) {
  int offsetR = offset;
  if (last != null) {
    offsetR -= last.getStartOffset() - scopeNode.getStartOffset() + last.getTextLength();
    while (offsetR >= 0) {
      TreeElement next = last.getTreeNext();
      if (next == null) {
        last = last.getTreeParent();
        continue;
      }
      int length = next.getTextLength();
      offsetR -= length;
      last = next;
    }
    scopeNode = last;
    offsetR += scopeNode.getTextLength();
  }
  return (LeafElement)scopeNode.findLeafElementAt(offsetR);
}
 
Example 3
Source File: FormatterUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
private static ASTNode getWsCandidate(@Nullable ASTNode node) {
  if (node == null) return null;
  ASTNode treePrev = node.getTreePrev();
  if (treePrev != null) {
    if (treePrev.getElementType() == TokenType.WHITE_SPACE) {
      return treePrev;
    }
    else if (treePrev.getTextLength() == 0) {
      return getWsCandidate(treePrev);
    }
    else {
      return node;
    }
  }
  final ASTNode treeParent = node.getTreeParent();

  if (treeParent == null || treeParent.getTreeParent() == null) {
    return node;
  }
  else {
    return getWsCandidate(treeParent);
  }
}
 
Example 4
Source File: IndentationFoldingBuilder.java    From consulo with Apache License 2.0 6 votes vote down vote up
@RequiredReadAction
private void collectDescriptors(@Nonnull final ASTNode node, @Nonnull final List<FoldingDescriptor> descriptors) {
  final Queue<ASTNode> toProcess = new LinkedList<ASTNode>();
  toProcess.add(node);
  while (!toProcess.isEmpty()) {
    final ASTNode current = toProcess.remove();
    if (current.getTreeParent() != null
        && current.getTextLength() > 1
        && myTokenSet.contains(current.getElementType()))
    {
      descriptors.add(new FoldingDescriptor(current, current.getTextRange()));
    }
    for (ASTNode child = current.getFirstChildNode(); child != null; child = child.getTreeNext()) {
      toProcess.add(child);
    }
  }
}
 
Example 5
Source File: CsvBlock.java    From intellij-csv-validator with Apache License 2.0 5 votes vote down vote up
@Override
protected List<Block> buildChildren() {
    List<ASTNode> todoNodes = new ArrayList<>();
    List<Block> blocks = new ArrayList<>();
    todoNodes.add(getNode().getFirstChildNode());
    CsvBlockField currentField = null;
    while (todoNodes.size() > 0) {
        ASTNode node = todoNodes.remove(todoNodes.size() - 1);
        if (node == null) {
            continue;
        }

        IElementType elementType = node.getElementType();
        todoNodes.add(node.getTreeNext());
        if (elementType == CsvTypes.RECORD) {
            todoNodes.add(node.getFirstChildNode());
        } else if (elementType == CsvTypes.FIELD) {
            currentField = new CsvBlockField(node, myFormattingInfo);
            if (currentField.getTextLength() > 0) {
                blocks.add(currentField);
            }
        } else if (elementType == CsvTypes.COMMA || elementType == CsvTypes.CRLF) {
            blocks.add(new CsvBlockElement(node, myFormattingInfo, currentField));
        } else if (elementType != TokenType.WHITE_SPACE && node.getTextLength() > 0) {
            blocks.add(new CsvDummyBlock(node, myFormattingInfo));
        }
    }
    validateBlocks(blocks);
    return blocks;
}
 
Example 6
Source File: GraphQLFoldingBuilder.java    From js-graphql-intellij-plugin with MIT License 5 votes vote down vote up
private static void buildFolding(ASTNode node, List<FoldingDescriptor> list) {
    boolean isBlock = GraphQLBlock.INDENT_PARENTS.contains(node.getElementType());
    if(!isBlock && GraphQLElementTypes.QUOTED_STRING.equals(node.getElementType())) {
        // triple quoted multi-line strings should support folding
        ASTNode quote = node.findChildByType(GraphQLElementTypes.OPEN_QUOTE);
        isBlock = quote != null && quote.getTextLength() == 3;
    }
    if (isBlock && !node.getTextRange().isEmpty()) {
        final TextRange range = node.getTextRange();
        list.add(new FoldingDescriptor(node, range));
    }
    for (ASTNode child : node.getChildren(null)) {
        buildFolding(child, list);
    }
}
 
Example 7
Source File: BashStringImpl.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
@Override
public String createEquallyWrappedString(String newContent) {
    ASTNode node = getNode();
    ASTNode firstChild = node.getFirstChildNode();
    ASTNode lastChild = node.getLastChildNode();

    StringBuilder result = new StringBuilder(firstChild.getTextLength() + newContent.length() + lastChild.getTextLength());
    return result.append(firstChild.getText()).append(newContent).append(lastChild.getText()).toString();
}
 
Example 8
Source File: LattePsiImplUtil.java    From intellij-latte with MIT License 5 votes vote down vote up
public static int getMacroNameLength(LatteMacroTag element) {
	ASTNode nameNode = getMacroNameNode(element);
	if (nameNode != null) {
		return nameNode.getTextLength();
	}
	return createMacroName(element).length();
}
 
Example 9
Source File: PostprocessReformattingAspect.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static boolean hasRaiseableEdgeChild(ASTNode node) {
  ASTNode first = node.getFirstChildNode();
  while (first != null && first.getTextLength() == 0) first = first.getTreeNext();

  ASTNode last = node.getLastChildNode();
  while (last != null && last.getTextLength() == 0) last = last.getTreePrev();

  return first == null || last == null || isRaiseable(first) || isRaiseable(last);
}
 
Example 10
Source File: CSharpDisabledBlock.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@RequiredReadAction
@Nonnull
@Override
public TextRange getTextRange()
{
	ASTNode first = myNodes.get(0);
	ASTNode last = myNodes.get(myNodes.size() - 1);
	return new TextRange(first.getStartOffset(), last.getStartOffset() + last.getTextLength());
}
 
Example 11
Source File: BlockSupportImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void reportInconsistentLength(PsiFile file, CharSequence newFileText, ASTNode node, int start, int end) {
  String message = "Index out of bounds: type=" + node.getElementType() + "; file=" + file + "; file.class=" + file.getClass() + "; start=" + start + "; end=" + end + "; length=" + node.getTextLength();
  String newTextBefore = newFileText.subSequence(0, start).toString();
  String oldTextBefore = file.getText().subSequence(0, start).toString();
  if (oldTextBefore.equals(newTextBefore)) {
    message += "; oldTextBefore==newTextBefore";
  }
  LOG.error(message, new Attachment(file.getName() + "_oldNodeText.txt", node.getText()), new Attachment(file.getName() + "_oldFileText.txt", file.getText()),
            new Attachment(file.getName() + "_newFileText.txt", newFileText.toString()));
}
 
Example 12
Source File: LeafBlockWrapper.java    From consulo with Apache License 2.0 4 votes vote down vote up
public boolean isEndOfCodeBlock() {
  ASTNode node = getNode();
  return node != null && node.getTextLength() == 1 && node.getChars().charAt(0) == '}';
}
 
Example 13
Source File: FormatterUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static boolean isWhitespaceOrEmpty(@Nullable ASTNode node) {
  if (node == null) return false;
  IElementType type = node.getElementType();
  return type == TokenType.WHITE_SPACE || (type != TokenType.ERROR_ELEMENT && node.getTextLength() == 0);
}
 
Example 14
Source File: ASTStructure.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public int getEndOffset(@Nonnull ASTNode node) {
  return node.getStartOffset() + node.getTextLength();
}
 
Example 15
Source File: BlockSupportImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
/**
 * Find ast node that could be reparsed incrementally
 *
 * @return Pair (target reparseable node, new replacement node)
 * or {@code null} if can't parse incrementally.
 */
@Nullable
public static Couple<ASTNode> findReparseableRoots(@Nonnull PsiFileImpl file, @Nonnull FileASTNode oldFileNode, @Nonnull TextRange changedPsiRange, @Nonnull CharSequence newFileText) {
  final FileElement fileElement = (FileElement)oldFileNode;
  final CharTable charTable = fileElement.getCharTable();
  int lengthShift = newFileText.length() - fileElement.getTextLength();

  if (fileElement.getElementType() instanceof ITemplateDataElementType || isTooDeep(file)) {
    // unable to perform incremental reparse for template data in JSP, or in exceptionally deep trees
    return null;
  }

  final ASTNode leafAtStart = fileElement.findLeafElementAt(Math.max(0, changedPsiRange.getStartOffset() - 1));
  final ASTNode leafAtEnd = fileElement.findLeafElementAt(Math.min(changedPsiRange.getEndOffset(), fileElement.getTextLength() - 1));
  ASTNode node = leafAtStart != null && leafAtEnd != null ? TreeUtil.findCommonParent(leafAtStart, leafAtEnd) : fileElement;
  Language baseLanguage = file.getViewProvider().getBaseLanguage();

  while (node != null && !(node instanceof FileElement)) {
    IElementType elementType = node.getElementType();
    if (elementType instanceof IReparseableElementTypeBase || elementType instanceof IReparseableLeafElementType) {
      final TextRange textRange = node.getTextRange();

      if (textRange.getLength() + lengthShift > 0 && (baseLanguage.isKindOf(elementType.getLanguage()) || !TreeUtil.containsOuterLanguageElements(node))) {
        final int start = textRange.getStartOffset();
        final int end = start + textRange.getLength() + lengthShift;
        if (end > newFileText.length()) {
          reportInconsistentLength(file, newFileText, node, start, end);
          break;
        }

        CharSequence newTextStr = newFileText.subSequence(start, end);

        ASTNode newNode;
        if (elementType instanceof IReparseableElementTypeBase) {
          newNode = tryReparseNode((IReparseableElementTypeBase)elementType, node, newTextStr, file.getManager(), baseLanguage, charTable);
        }
        else {
          newNode = tryReparseLeaf((IReparseableLeafElementType)elementType, node, newTextStr);
        }

        if (newNode != null) {
          if (newNode.getTextLength() != newTextStr.length()) {
            String details = ApplicationManager.getApplication().isInternal() ? "text=" + newTextStr + "; treeText=" + newNode.getText() + ";" : "";
            LOG.error("Inconsistent reparse: " + details + " type=" + elementType);
          }

          return Couple.of(node, newNode);
        }
      }
    }
    node = node.getTreeParent();
  }
  return null;
}
 
Example 16
Source File: LattePsiImplUtil.java    From intellij-latte with MIT License 4 votes vote down vote up
private static boolean matchPsiElement(ASTNode element, @NotNull String text) {
	return element.getTextLength() == text.length() && element.getText().equals(text);
}
 
Example 17
Source File: CypherBlock.java    From jetbrains-plugin-graph-database-support with Apache License 2.0 4 votes vote down vote up
private static boolean isWhitespaceOrEmpty(ASTNode node) {
    return node.getElementType() == TokenType.WHITE_SPACE || node.getTextLength() == 0;
}