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

The following examples show how to use com.intellij.lang.ASTNode#getStartOffset() . 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: BuildFileFoldingBuilder.java    From intellij with Apache License 2.0 6 votes vote down vote up
private static void foldFunctionCall(List<FoldingDescriptor> descriptors, ASTNode node) {
  IElementType type = node.getElementType();
  ASTNode listNode =
      type == BuildElementTypes.FUNCALL_EXPRESSION
          ? node.findChildByType(BuildElementTypes.ARGUMENT_LIST)
          : node;
  if (listNode == null) {
    return;
  }
  ASTNode lParen = listNode.findChildByType(BuildToken.fromKind(TokenKind.LPAREN));
  ASTNode rParen = listNode.findChildByType(BuildToken.fromKind(TokenKind.RPAREN));
  if (lParen == null || rParen == null) {
    return;
  }
  int start = lParen.getStartOffset() + 1;
  int end = rParen.getTextRange().getEndOffset() - 1;
  descriptors.add(new FoldingDescriptor(node, range(start, end)));
}
 
Example 2
Source File: HaxeIndentProcessor.java    From intellij-haxe with Apache License 2.0 6 votes vote down vote up
private static boolean isAtFirstColumn(ASTNode node) {
  PsiElement element = node.getPsi();
  if (null == element) {
    return false;
  }
  PsiFile file = element.getContainingFile();
  Project project = element.getProject();
  if (null == file || null == project) {
    return false;
  }
  Document doc = PsiDocumentManager.getInstance(project).getDocument(file);
  if (null == doc) {
    return false;
  }
  int line = doc.getLineNumber(node.getStartOffset());
  int lineStart = doc.getLineStartOffset(line);
  return node.getStartOffset() == lineStart;
}
 
Example 3
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 4
Source File: RemoveUnusedAtFixBase.java    From bamboo-soy with Apache License 2.0 5 votes vote down vote up
@NotNull
private static TextRange computeRangeToDelete(PsiElement element, ASTNode atParamNode,
    PsiElement nextMeaningfulNode) {
  if (nextMeaningfulNode != null) {
    return new TextRange(atParamNode.getStartOffset(),
        nextMeaningfulNode.getNode().getStartOffset());
  }
  PsiElement prevMeaningfulNode = PsiTreeUtil
      .skipSiblingsBackward(element, PsiWhiteSpace.class);
  return new TextRange(
      prevMeaningfulNode != null ? prevMeaningfulNode.getTextRange().getEndOffset() :
          atParamNode.getStartOffset(), atParamNode.getTextRange().getEndOffset());
}
 
Example 5
Source File: BuildFileFoldingBuilder.java    From intellij with Apache License 2.0 5 votes vote down vote up
private static void foldFunctionDefinition(List<FoldingDescriptor> descriptors, ASTNode node) {
  ASTNode colon = node.findChildByType(BuildToken.fromKind(TokenKind.COLON));
  if (colon == null) {
    return;
  }
  ASTNode stmtList = node.findChildByType(BuildElementTypes.STATEMENT_LIST);
  if (stmtList == null) {
    return;
  }
  int start = colon.getStartOffset() + 1;
  int end = endOfList(stmtList);
  descriptors.add(new FoldingDescriptor(node, range(start, end)));
}
 
Example 6
Source File: BashFoldingBuilder.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
private static int getStartOffset(ASTNode node) {
    for (ASTNode prev = node.getTreePrev(); prev != null; prev = prev.getTreePrev()) {
        IElementType elementType = prev.getElementType();
        if (startLogicalBlockTokens.contains(elementType)) {
            return prev.getStartOffset();
        }
    }
    return node.getStartOffset();
}
 
Example 7
Source File: GaugeFoldingBuilder.java    From Intellij-Plugin with Apache License 2.0 5 votes vote down vote up
protected void addNode(List<FoldingDescriptor> descriptors, ASTNode node, ASTNode heading) {
    if (heading == null) return;
    String text = node.getText().endsWith("\n") ? node.getText() : node.getText() + "\n";
    int startOffset = node.getStartOffset() + getLength(heading);
    int endOffset = node.getStartOffset() + text.lastIndexOf("\n");
    if (text.endsWith("\n\n")) endOffset--;
    if (endOffset <= startOffset) return;
    TextRange textRange = new TextRange(startOffset, endOffset);
    descriptors.add(new FoldingDescriptor(node, textRange));
}
 
Example 8
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 9
Source File: HaxeFoldingBuilder.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
private static TextRange buildImportsFoldingTextRange(ASTNode firstNode, ASTNode lastNode) {
  ASTNode nodeStartFrom = UsefulPsiTreeUtil.getNextSiblingSkipWhiteSpacesAndComments(firstNode.getFirstChildNode());
  if (nodeStartFrom == null) {
    nodeStartFrom = firstNode;
  }
  return new TextRange(nodeStartFrom.getStartOffset(), lastNode.getTextRange().getEndOffset());
}
 
Example 10
Source File: NamedBuildElement.java    From intellij with Apache License 2.0 4 votes vote down vote up
@Override
public int getTextOffset() {
  final ASTNode name = getNameNode();
  return name != null ? name.getStartOffset() : super.getTextOffset();
}
 
Example 11
Source File: BashFunctionDefImpl.java    From BashSupport with Apache License 2.0 4 votes vote down vote up
public int getTextOffset() {
    final ASTNode name = getNameSymbol().getNode();
    return name != null ? name.getStartOffset() : super.getTextOffset();
}
 
Example 12
Source File: ASTStructure.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public int getStartOffset(@Nonnull ASTNode node) {
  return node.getStartOffset();
}
 
Example 13
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 14
Source File: CodeEditUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static boolean needToForceReformat(final ASTNode parent, final ASTNode first, final ASTNode last) {
  return parent == null ||
         first.getStartOffset() != parent.getStartOffset() ||
         parent.getText().trim().length() == getTrimmedTextLength(first, last) && needToForceReformat(parent.getTreeParent(), parent, parent);
}
 
Example 15
Source File: EnterAfterUnmatchedBraceHandler.java    From consulo with Apache License 2.0 4 votes vote down vote up
/**
 * Current handler inserts closing curly brace (right brace) if necessary. There is a possible case that it should be located
 * more than one line forward.
 * <p/>
 * <b>Example</b>
 * <pre>
 *     if (test1()) {
 *     } else {<caret> if (test2()) {
 *         foo();
 *     }
 * </pre>
 * <p/>
 * We want to get this after the processing:
 * <pre>
 *     if (test1()) {
 *     } else {
 *         if (test2()) {
 *             foo();
 *         }
 *     }
 * </pre>
 * I.e. closing brace should be inserted two lines below current caret line. Hence, we need to calculate correct offset
 * to use for brace inserting. This method is responsible for that.
 * <p/>
 * In essence it inspects PSI structure and finds PSE elements with the max length that starts at caret offset. End offset
 * of that element is used as an insertion point.
 *
 * @param file   target PSI file
 * @param text   text from the given file
 * @param offset target offset where line feed will be inserted
 * @return pair of (element, offset). The element is the '}' owner, if applicable; the offset is the position for inserting closing brace
 */
protected Pair<PsiElement, Integer> calculateOffsetToInsertClosingBrace(@Nonnull PsiFile file, @Nonnull CharSequence text, final int offset) {
  PsiElement element = PsiUtilCore.getElementAtOffset(file, offset);
  ASTNode node = element.getNode();
  if (node != null && node.getElementType() == TokenType.WHITE_SPACE) {
    return Pair.create(null, CharArrayUtil.shiftForwardUntil(text, offset, "\n"));
  }
  for (PsiElement parent = element.getParent(); parent != null; parent = parent.getParent()) {
    ASTNode parentNode = parent.getNode();
    if (parentNode == null || parentNode.getStartOffset() != offset) {
      break;
    }
    element = parent;
  }
  if (element.getTextOffset() != offset) {
    return Pair.create(null, CharArrayUtil.shiftForwardUntil(text, offset, "\n"));
  }
  return Pair.create(element, calculateOffsetToInsertClosingBraceInsideElement(element));
}