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

The following examples show how to use com.intellij.lang.ASTNode#findChildByType() . 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
@Override
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
    if (element instanceof KeywordsContainer) {
        KeywordsContainer container = (KeywordsContainer) element;
        for (PsiElement psiElement : container.keywords()) {
            setHighlighting(psiElement, holder, ProtoSyntaxHighlighter.KEYWORD);
        }
    }
    if (element instanceof EnumConstantNode) {
        ASTNode node = element.getNode();
        ASTNode name = node.findChildByType(ProtoParserDefinition.rule(ProtoParser.RULE_enumFieldName));
        if (name != null) {
            setHighlighting(name.getPsi(), holder, ProtoSyntaxHighlighter.ENUM_CONSTANT);
        }
    }
}
 
Example 2
@Override
@RequiredWriteAction
public void invoke(@Nonnull Project project, Editor editor, PsiFile file) throws IncorrectOperationException
{
	DotNetVariable element = myPointer.getElement();
	if(element == null)
	{
		return;
	}
	PsiDocumentManager.getInstance(project).commitAllDocuments();

	DotNetExpression initializer = element.getInitializer();
	if(initializer == null)
	{
		return;
	}

	initializer.delete();

	ASTNode node = element.getNode();
	ASTNode childByType = node.findChildByType(CSharpTokenSets.EQ);
	if(childByType != null)
	{
		node.removeChild(childByType);
	}
}
 
Example 3
Source Project: intellij-latte   File: LattePsiImplUtil.java    License: MIT License 5 votes vote down vote up
private static @Nullable ASTNode getMacroNameNode(LatteMacroTag element) {
	ASTNode elementNode = element.getNode();
	ASTNode nameNode = elementNode.findChildByType(T_MACRO_NAME);
	if (nameNode != null) {
		return nameNode;
	}
	return elementNode.findChildByType(T_MACRO_SHORTNAME);
}
 
Example 4
Source Project: BashSupport   File: BashVarDefImpl.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns either a assignment word element or a array assignment word.
 *
 * @return The element which represents the left part of the assignment.
 */
@NotNull
public PsiElement findAssignmentWord() {
    if (assignmentWord == null) {
        //no other lock is used in the callees, it's safe to synchronize around the whole calculation
        synchronized (stateLock) {
            if (assignmentWord == null) {
                PsiElement element = findChildByType(accepted);

                PsiElement newAssignmentWord;
                if (element != null) {
                    newAssignmentWord = element;
                } else {
                    //if null we probably represent a single var without assignment, i.e. the var node is nested inside of
                    //a parsed var
                    PsiElement firstChild = getFirstChild();
                    ASTNode childNode = firstChild != null ? firstChild.getNode() : null;

                    ASTNode node = childNode != null ? childNode.findChildByType(accepted) : null;
                    newAssignmentWord = (node != null) ? node.getPsi() : firstChild;
                }

                assignmentWord = newAssignmentWord;
            }
        }
    }

    return assignmentWord;
}
 
Example 5
Source Project: consulo-csharp   File: CS0145.java    License: Apache License 2.0 5 votes vote down vote up
@Override
@RequiredUIAccess
public void invoke(@Nonnull Project project, Editor editor, PsiFile file) throws IncorrectOperationException
{
	DotNetVariable element = myVariablePointer.getElement();
	if(element == null)
	{
		return;
	}

	PsiDocumentManager.getInstance(project).commitAllDocuments();

	String defaultValueForType = MethodGenerateUtil.getDefaultValueForType(element.toTypeRef(false), element);

	if(defaultValueForType == null)
	{
		return;
	}

	DotNetExpression expression = CSharpFileFactory.createExpression(project, defaultValueForType);

	PsiElement nameIdentifier = element.getNameIdentifier();
	if(nameIdentifier == null)
	{
		return;
	}

	ASTNode variableNode = element.getNode();

	ASTNode semicolon = variableNode.findChildByType(CSharpTokens.SEMICOLON);

	variableNode.addLeaf(CSharpTokens.WHITE_SPACE, " ", semicolon);
	variableNode.addLeaf(CSharpTokens.EQ, "=", semicolon);
	variableNode.addLeaf(CSharpTokens.WHITE_SPACE, " ", semicolon);
	ASTNode node = expression.getNode();
	CodeEditUtil.setOldIndentation((TreeElement) node, 0);
	variableNode.addChild(node, semicolon);

	editor.getCaretModel().moveToOffset(element.getTextRange().getEndOffset());
}
 
Example 6
@RequiredReadAction
@Override
public boolean process(@Nonnull Editor editor, @Nonnull PsiFile psiFile)
{
	PsiElement statementAtCaret = getStatementAtCaret(editor, psiFile);

	DotNetStatement statement = PsiTreeUtil.getParentOfType(statementAtCaret, DotNetStatement.class);
	if(statement == null)
	{
		return false;
	}

	if(statement instanceof CSharpBlockStatementImpl)
	{
		return false;
	}

	if(statement instanceof CSharpStatementAsStatementOwner)
	{
		DotNetStatement childStatement = ((CSharpStatementAsStatementOwner) statement).getChildStatement();

		if(childStatement == null)
		{
			insertStringAtEndWithReformat("{}", statement, editor, 1, true);
			return false;
		}
	}
	else
	{
		ASTNode node = statement.getNode();
		ASTNode semicolonNode = node.findChildByType(CSharpTokens.SEMICOLON);
		if(semicolonNode != null)
		{
			return false;
		}

		insertStringAtEndWithReformat(";", statement, editor, 1, true);
	}
	return true;
}
 
Example 7
@Override
public Collection<PsiElement> keywords() {
    ASTNode node = getNode();
    List<PsiElement> result = new ArrayList<>();
    result.addAll(Util.findKeywords(getNode()));
    ASTNode fieldModifier = node.findChildByType(R_FIELD_MODIFIER);
    if (fieldModifier != null) {
        result.addAll(Util.findKeywords(fieldModifier));
    }
    return result;
}
 
Example 8
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 9
public JSExpression getCollection() {
    final ASTNode myNode = getNode();
    final ASTNode secondExpression = myNode.findChildByType(
            JSElementTypes.EXPRESSIONS, myNode.findChildByType(JSTokenTypes.IN_KEYWORD)
    );
    return secondExpression != null ? (JSExpression) secondExpression.getPsi() : null;
}
 
Example 10
public JSExpression getCollection() {
    final ASTNode myNode = getNode();
    final ASTNode secondExpression = myNode.findChildByType(
            JSElementTypes.EXPRESSIONS, myNode.findChildByType(JSTokenTypes.IN_KEYWORD)
    );
    return secondExpression != null ? (JSExpression) secondExpression.getPsi() : null;
}
 
Example 11
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 12
@Nullable
@Override
public PsiElement getNameIdentifier() {
	final PsiElement stringElement = this.findChildByType(JSGraphQLEndpointTokenTypes.QUOTED_STRING);
	if(stringElement != null) {
		final ASTNode string = stringElement.getNode().findChildByType(JSGraphQLEndpointTokenTypes.STRING);
		if(string != null) {
			final ASTNode stringBody = string.findChildByType(JSGraphQLEndpointTokenTypes.STRING_BODY);
			if (stringBody != null) {
				return stringBody.getPsi();
			}
		}
	}
	return null;
}
 
Example 13
public ASTNode getConstantNameNode() {
    ASTNode node = getNode();
    return node.findChildByType(rule(RULE_enumFieldName));
}
 
Example 14
public ASTNode getConstantValueNode() {
    ASTNode node = getNode();
    return node.findChildByType(rule(RULE_enumFieldValue));
}
 
Example 15
@Override
public ASTNode getFieldNameNode() {
    ASTNode node = getNode();
    return node.findChildByType(R_FIELD_NAME);
}
 
Example 16
@Override
public ASTNode getTagNode() {
    ASTNode node = getNode();
    return node.findChildByType(R_TAG);
}
 
Example 17
@Override
public ASTNode getFieldNameNode() {
    ASTNode node = getNode();
    return node.findChildByType(R_GROUP_NAME);
}
 
Example 18
@Override
public ASTNode getTagNode() {
    ASTNode node = getNode();
    return node.findChildByType(R_TAG);
}
 
Example 19
@Override
public ASTNode getFieldLabelNode() {
    ASTNode node = getNode();
    return node.findChildByType(R_FIELD_MODIFIER);
}
 
Example 20
@Nullable
@Override
public String generateDoc(PsiElement element, @Nullable PsiElement originalElement) {
    boolean process = false;
    for ( Class supported: SUPPORTED_ELEMENT_TYPES ) {
        if ( supported.isInstance(element) ) {
            process = true;
            break;
        }
    }
    if ( !process ) {
        return null;
    }
    PsiFile file = null;
    if ( element instanceof PsiDirectory ) {
        // let's see whether we can map the directory to a package; if so, change the
        // element to the package and continue
        PsiPackage pkg = JavaDirectoryService.getInstance().getPackage((PsiDirectory)element);
        if ( pkg != null ) {
            element = pkg;
        }
        else {
            return null;
        }
    }
    if ( element instanceof PsiPackage ) {
        for ( PsiDirectory dir : ((PsiPackage)element).getDirectories() ) {
            PsiFile info = dir.findFile(PsiPackage.PACKAGE_INFO_FILE);
            if ( info != null ) {
                ASTNode node = info.getNode();
                if ( node != null ) {
                    ASTNode docCommentNode = node.findChildByType(JavaDocElementType.DOC_COMMENT);
                    if ( docCommentNode != null ) {
                        // the default implementation will now use this file
                        // we're going to take over below, if Markdown is enabled in
                        // the corresponding module
                        // see JavaDocInfoGenerator.generatePackageJavaDoc()
                        file = info;
                        break;
                    }
                }
            }
            if ( dir.findFile("package.html") != null ) {
                // leave that to the default
                return null;
            }
        }
    }
    else {
        if ( JavaLanguage.INSTANCE.equals(element.getLanguage()) ) {
            element = element.getNavigationElement();
            if ( element.getContainingFile() != null ) {
                file = element.getContainingFile();
            }
        }
    }
    if ( file != null ) {
        DocCommentProcessor processor = new DocCommentProcessor(file);
        if ( processor.isEnabled() ) {
            String docHtml;
            if ( element instanceof PsiMethod ) {
                docHtml = super.generateDoc(PsiProxy.forMethod((PsiMethod)element), originalElement);
            }
            else if ( element instanceof PsiParameter ) {
                docHtml = super.generateDoc(PsiProxy.forParameter((PsiParameter)element), originalElement);
            }
            else {
                MarkdownJavaDocInfoGenerator javaDocInfoGenerator = new MarkdownJavaDocInfoGenerator(element.getProject(), element, processor);
                List<String> docURLs = getExternalJavaDocUrl(element);
                String text = javaDocInfoGenerator.generateDocInfo(docURLs);
                Plugin.print("Intermediate HTML output", text);
                docHtml = JavaDocExternalFilter.filterInternalDocInfo(text);
            }
            docHtml = extendCss(docHtml);
            Plugin.print("Final HTML output", docHtml);
            return docHtml;
        }
        else {
            return null;
        }
    }
    else {
        return null;
    }
}