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

The following examples show how to use com.intellij.lang.ASTNode#getText() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
@NotNull
@Override
public String getType(@NotNull PsiElement element) {
    if(element instanceof JSGraphQLEndpointInputValueDefinitionIdentifier) {
        return "argument";
    }
    if(element instanceof JSGraphQLEndpointPropertyPsiElement) {
        return "field";
    }
    final JSGraphQLEndpointNamedTypeDefinition definition = PsiTreeUtil.getParentOfType(element, JSGraphQLEndpointNamedTypeDefinition.class);
    if(definition != null) {
        // if it's a definition, use the keyword, e.g. 'type', 'interface' etc.
        final ASTNode keyword = definition.getNode().findChildByType(JSGraphQLEndpointTokenTypesSets.KEYWORDS);
        if(keyword != null) {
            return keyword.getText();
        }
    }
    return "element";
}
 
Example 2
@Override
public Result charTyped(char c, final Project project, final @NotNull Editor editor, @NotNull final PsiFile editedFile) {
    if ((editedFile.getLanguage() instanceof XQueryLanguage) && c == '/') {
        PsiDocumentManager.getInstance(project).commitAllDocuments();
        PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
        if (file == null) return Result.CONTINUE;
        FileViewProvider provider = file.getViewProvider();
        final int offset = editor.getCaretModel().getOffset();
        PsiElement element = provider.findElementAt(offset - 1, XQueryLanguage.class);
        if (element == null) return Result.CONTINUE;
        if (!(element.getLanguage() instanceof XQueryLanguage)) return Result.CONTINUE;
        ASTNode prevLeaf = element.getNode();
        if (prevLeaf == null) return Result.CONTINUE;
        final String prevLeafText = prevLeaf.getText();
        if (isStartOfEndOfTag(prevLeaf, prevLeafText)) {
            XQueryXmlFullTag tag = PsiTreeUtil.getParentOfType(element, XQueryXmlFullTag.class);
            if (tag != null) {
                XQueryXmlTagName tagName = tag.getXmlTagNameList().get(0);
                if (hasNoClosingTagName(prevLeaf, tag, tagName)) {
                    finishClosingTag(editor, tagName);
                    return Result.STOP;
                }
            }
        }
        if (!"/".equals(prevLeafText.trim())) return Result.CONTINUE;
        prevLeaf = getPreviousNonWhiteSpaceLeaf(prevLeaf);
        if (prevLeaf == null) return Result.CONTINUE;
        if (PsiTreeUtil.getParentOfType(element, XQueryDirAttributeValue.class) != null) return Result.CONTINUE;
        if (prevLeaf.getElementType() == XQueryTypes.ELEMENTCONTENTCHAR) return Result.CONTINUE;
        XQueryEnclosedExpression parentEnclosedExpression = PsiTreeUtil.getParentOfType(element, XQueryEnclosedExpression.class, true, XQueryXmlFullTag.class);
        XQueryXmlFullTag fullTag = getParentFullTag(prevLeaf);
        if (isInEnclosedExpressionNestedInXmlTag(parentEnclosedExpression, fullTag)) return Result.CONTINUE;
        if (isInUnclosedXmlTag(fullTag)) {
            closeEmptyTag(editor);
            return Result.STOP;
        }
    }
    return Result.CONTINUE;
}
 
Example 3
@Override
public String getFieldName() {
    ASTNode nameNode = getFieldNameNode();
    if (nameNode != null) {
        return nameNode.getText();
    }
    return "";
}
 
Example 4
public static String getValue(@NotNull LombokConfigProperty element) {
  ASTNode valueNode = element.getNode().findChildByType(LombokConfigTypes.VALUE);
  if (valueNode != null) {
    return valueNode.getText();
  } else {
    return null;
  }
}
 
Example 5
Source Project: consulo   File: ASTShallowComparator.java    License: Apache License 2.0 5 votes vote down vote up
private ThreeState textMatches(ASTNode oldNode, ASTNode newNode) {
  myIndicator.checkCanceled();
  String oldText = TreeUtil.isCollapsedChameleon(oldNode) ? oldNode.getText() : null;
  String newText = TreeUtil.isCollapsedChameleon(newNode) ? newNode.getText() : null;
  if (oldText != null && newText != null) return oldText.equals(newText) ? ThreeState.YES : ThreeState.UNSURE;

  if (oldText != null) {
    return compareTreeToText((TreeElement)newNode, oldText) ? ThreeState.YES : ThreeState.UNSURE;
  }
  if (newText != null) {
    return compareTreeToText((TreeElement)oldNode, newText) ? ThreeState.YES : ThreeState.UNSURE;
  }

  if (oldNode instanceof ForeignLeafPsiElement) {
    return newNode instanceof ForeignLeafPsiElement && oldNode.getText().equals(newNode.getText()) ? ThreeState.YES : ThreeState.NO;
  }

  if (newNode instanceof ForeignLeafPsiElement) return ThreeState.NO;

  if (oldNode instanceof LeafElement) {
    return ((LeafElement)oldNode).textMatches(newNode.getText()) ? ThreeState.YES : ThreeState.NO;
  }
  if (newNode instanceof LeafElement) {
    return ((LeafElement)newNode).textMatches(oldNode.getText()) ? ThreeState.YES : ThreeState.NO;
  }

  if (oldNode instanceof PsiErrorElement && newNode instanceof PsiErrorElement) {
    final PsiErrorElement e1 = (PsiErrorElement)oldNode;
    final PsiErrorElement e2 = (PsiErrorElement)newNode;
    if (!Comparing.equal(e1.getErrorDescription(), e2.getErrorDescription())) return ThreeState.NO;
  }

  return ThreeState.UNSURE;
}
 
Example 6
@Override
public String getText() {
  ASTNode node = getNode();
  if (null != node) {
    return node.getText();
  }
  return "";
}
 
Example 7
Source Project: intellij-haxe   File: HXMLPsiImplUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static String getValue(HXMLLib lib) {
  ASTNode node = lib.getNode().findChildByType(HXMLTypes.VALUE);

  if (node != null) {
    return node.getText();
  }
  else {
    return null;
  }
}
 
Example 8
Source Project: intellij-haxe   File: HXMLPsiImplUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static String getValue(HXMLProperty property) {
  ASTNode node = property.getNode().findChildByType(HXMLTypes.VALUE);

  if (node != null) {
    return node.getText();
  }
  else {
    return null;
  }
}
 
Example 9
public static String getKey(@NotNull LombokConfigCleaner element) {
  ASTNode keyNode = element.getNode().findChildByType(LombokConfigTypes.KEY);
  if (keyNode != null) {
    return keyNode.getText();
  } else {
    return null;
  }
}
 
Example 10
Source Project: intellij-haxe   File: HaxeAstUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static boolean isNumber(@Nullable ASTNode t) {
  if (t != null) {
    String text = t.getText();
    // Optimization: If it doesn't start with '+', '-', '.', or digit, it's not a number (ignoring 'Nan' and Infinity).
    // Using the Float class to detect numbers is fairly expensive.  Before this optimization, about 2/3 of the
    // time we spent evaluating a conditional expression was spent in isFloat().
    char firstChar = text.charAt(0);
    if (Character.isDigit(firstChar) || '.' == firstChar || '-' == firstChar || '+' == firstChar) {
      return isInteger(text) || isFloat(text);
    }
  }
  return false;
}
 
Example 11
@Override
public String getDeclaration() {
	final StringBuilder sb = new StringBuilder();
	for (ASTNode child : this.getParent().getNode().getChildren(null)) {
		final String text = child.getText();
		if(text.startsWith("{")) {
			// don't include field definitions
			break;
		}
		sb.append(text);
	}
	return sb.toString();
}
 
Example 12
public PsiCodeSnippetBody(@Nonnull final ASTNode node) {
  super(node);
  text = node.getText();
}
 
Example 13
@Nullable
@Override
public String getPresentableText() {
	ASTNode node = element.getNode();
	return node.getText();
}
 
Example 14
Source Project: consulo   File: SyntaxTraverser.java    License: Apache License 2.0 4 votes vote down vote up
@Nonnull
@Override
public CharSequence textOf(@Nonnull ASTNode node) {
  return node.getText();
}
 
Example 15
Source Project: consulo   File: CompositeFoldingBuilder.java    License: Apache License 2.0 4 votes vote down vote up
@RequiredReadAction
@Override
public String getPlaceholderText(@Nonnull ASTNode node) {
  final FoldingBuilder builder = node.getUserData(FOLDING_BUILDER);
  return builder == null ? node.getText() : builder.getPlaceholderText(node);
}
 
Example 16
static private String getOperator(PsiElement element, TokenSet set) {
  ASTNode operatorNode = element.getNode().findChildByType(set);
  if (operatorNode == null) return "";
  return operatorNode.getText();
}
 
Example 17
public PsiTopicLevel(@Nonnull final ASTNode node) {
  super(node);
  final String text = node.getText();
  this.level = ModelUtils.calcCharsOnStart('#', text);
}
 
Example 18
Source Project: consulo   File: BlockSupportImpl.java    License: 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 19
Source Project: consulo   File: CodeEditUtil.java    License: Apache License 2.0 4 votes vote down vote up
private static void markWhitespaceForReformat(final ASTNode right) {
  final String text = right.getText();
  final LeafElement merged = ASTFactory.whitespace(text);
  right.getTreeParent().replaceChild(right, merged);
}
 
Example 20
Source Project: intellij   File: FuncallExpression.java    License: Apache License 2.0 4 votes vote down vote up
/** The name of the function being called. */
@Nullable
public String getFunctionName() {
  ASTNode node = getFunctionNameNode();
  return node != null ? node.getText() : null;
}