Search in sources :

Example 1 with PsiElement

use of com.intellij.psi.PsiElement in project buck by facebook.

the class BuckBuildUtil method getPropertyValue.

/**
   * Get the value of a property in a specific buck rule body.
   * TODO(#7908675): We should use Buck's own classes for it.
   */
public static String getPropertyValue(BuckRuleBody body, String name) {
    if (body == null) {
        return null;
    }
    PsiElement[] children = body.getChildren();
    for (PsiElement child : children) {
        if (BuckPsiUtils.testType(child, BuckTypes.PROPERTY)) {
            PsiElement lvalue = child.getFirstChild();
            PsiElement propertyName = lvalue.getFirstChild();
            if (propertyName != null && propertyName.getText().equals(name)) {
                BuckExpression expression = (BuckExpression) BuckPsiUtils.findChildWithType(child, BuckTypes.EXPRESSION);
                return expression != null ? BuckPsiUtils.getStringValueFromExpression(expression) : null;
            }
        }
    }
    return null;
}
Also used : BuckExpression(com.facebook.buck.intellij.ideabuck.lang.psi.BuckExpression) PsiElement(com.intellij.psi.PsiElement)

Example 2 with PsiElement

use of com.intellij.psi.PsiElement in project buck by facebook.

the class BuckBuildUtil method extractBuckTarget.

/**
   * Get the buck target from a buck file.
   * TODO(#7908675): We should use Buck's own classes for it.
   */
public static String extractBuckTarget(Project project, VirtualFile file) {
    BuckFile buckFile = (BuckFile) PsiManager.getInstance(project).findFile(file);
    if (buckFile == null) {
        return null;
    }
    PsiElement[] children = buckFile.getChildren();
    for (PsiElement child : children) {
        if (child.getNode().getElementType() == BuckTypes.RULE_BLOCK) {
            PsiElement ruleName = child.getFirstChild();
            // Find rule "project_config"
            if (ruleName != null && BuckPsiUtils.testType(ruleName, BuckTypes.RULE_NAME) && ruleName.getText().equals(PROJECT_CONFIG_RULE_NAME)) {
                // Find property "src_target"
                PsiElement bodyElement = BuckPsiUtils.findChildWithType(child, BuckTypes.RULE_BODY);
                return getPropertyValue((BuckRuleBody) bodyElement, SRC_TARGET_PROPERTY_NAME);
            }
        }
    }
    return null;
}
Also used : BuckFile(com.facebook.buck.intellij.ideabuck.lang.BuckFile) PsiElement(com.intellij.psi.PsiElement)

Example 3 with PsiElement

use of com.intellij.psi.PsiElement in project buck by facebook.

the class BuckFormattingModelBuilder method getRangeAffectingIndent.

@Nullable
@Override
public TextRange getRangeAffectingIndent(PsiFile file, int offset, ASTNode elementAtOffset) {
    final PsiElement element = elementAtOffset.getPsi();
    final PsiElement container = element.getParent();
    return container != null ? container.getTextRange() : null;
}
Also used : PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with PsiElement

use of com.intellij.psi.PsiElement in project buck by facebook.

the class DependenciesOptimizer method optimzeDeps.

public static void optimzeDeps(@NotNull PsiFile file) {
    final PropertyVisitor visitor = new PropertyVisitor();
    file.accept(new BuckVisitor() {

        @Override
        public void visitElement(PsiElement node) {
            node.acceptChildren(this);
            node.accept(visitor);
        }
    });
    // Commit modifications.
    final PsiDocumentManager manager = PsiDocumentManager.getInstance(file.getProject());
    manager.doPostponedOperationsAndUnblockDocument(manager.getDocument(file));
}
Also used : BuckVisitor(com.facebook.buck.intellij.ideabuck.lang.psi.BuckVisitor) PsiElement(com.intellij.psi.PsiElement) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 5 with PsiElement

use of com.intellij.psi.PsiElement in project intellij-community by JetBrains.

the class GroovyEditorTextProvider method getEditorText.

@Override
public TextWithImports getEditorText(PsiElement elementAtCaret) {
    String result = "";
    PsiElement element = findExpressionInner(elementAtCaret, true);
    if (element != null) {
        if (element instanceof GrReferenceExpression) {
            final GrReferenceExpression reference = (GrReferenceExpression) element;
            if (reference.getQualifier() == null) {
                final PsiElement resolved = reference.resolve();
                if (resolved instanceof PsiEnumConstant) {
                    final PsiEnumConstant enumConstant = (PsiEnumConstant) resolved;
                    final PsiClass enumClass = enumConstant.getContainingClass();
                    if (enumClass != null) {
                        result = enumClass.getName() + "." + enumConstant.getName();
                    }
                }
            }
        }
        if (result.isEmpty()) {
            result = element.getText();
        }
    }
    return new TextWithImportsImpl(CodeFragmentKind.EXPRESSION, result);
}
Also used : PsiEnumConstant(com.intellij.psi.PsiEnumConstant) PsiClass(com.intellij.psi.PsiClass) TextWithImportsImpl(com.intellij.debugger.engine.evaluation.TextWithImportsImpl) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Aggregations

PsiElement (com.intellij.psi.PsiElement)4067 NotNull (org.jetbrains.annotations.NotNull)744 Nullable (org.jetbrains.annotations.Nullable)644 PsiFile (com.intellij.psi.PsiFile)592 PsiReference (com.intellij.psi.PsiReference)330 Project (com.intellij.openapi.project.Project)304 ArrayList (java.util.ArrayList)277 TextRange (com.intellij.openapi.util.TextRange)263 VirtualFile (com.intellij.openapi.vfs.VirtualFile)236 IElementType (com.intellij.psi.tree.IElementType)159 BasePhpElementVisitor (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor)156 ASTNode (com.intellij.lang.ASTNode)152 XmlTag (com.intellij.psi.xml.XmlTag)136 PsiClass (com.intellij.psi.PsiClass)131 PsiDirectory (com.intellij.psi.PsiDirectory)128 Document (com.intellij.openapi.editor.Document)126 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)126 Editor (com.intellij.openapi.editor.Editor)121 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)121 PsiMethod (com.intellij.psi.PsiMethod)94