Java Code Examples for com.intellij.psi.util.PsiTreeUtil#getChildOfType()

The following examples show how to use com.intellij.psi.util.PsiTreeUtil#getChildOfType() . 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: PsiAnnotationProxyUtils.java    From litho with Apache License 2.0 6 votes vote down vote up
private Object invoke(Method method, Object[] args) throws IllegalAccessException {
  Class<?> returnType = method.getReturnType();
  if (returnType.isEnum()) {
    PsiAnnotation currentAnnotation =
        AnnotationUtil.findAnnotationInHierarchy(
            mListOwner, Collections.singleton(mAnnotationClass.getCanonicalName()));
    PsiReferenceExpression declaredValue =
        (PsiReferenceExpression) currentAnnotation.findAttributeValue(method.getName());
    if (declaredValue == null) {
      return method.getDefaultValue();
    }
    PsiIdentifier identifier = PsiTreeUtil.getChildOfType(declaredValue, PsiIdentifier.class);
    return Enum.valueOf((Class<Enum>) returnType, identifier.getText());
  }

  try {
    if (args == null) {
      return method.invoke(mStubbed);
    }
    return method.invoke(mStubbed, args);
  } catch (InvocationTargetException e) {
    return method.getDefaultValue();
  }
}
 
Example 2
Source File: ReplaceTypeQuickFix.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@Override
public void invoke(@Nonnull Project project, Editor editor, PsiFile file) throws IncorrectOperationException
{
	DotNetType element = myPointer.getElement();
	if(element == null)
	{
		return;
	}

	CSharpFragmentFileImpl typeFragment = CSharpFragmentFactory.createTypeFragment(project, myTypeText, element);

	DotNetType newType = PsiTreeUtil.getChildOfType(typeFragment, DotNetType.class);
	if(newType == null)
	{
		return;
	}

	element.replace(newType);
}
 
Example 3
Source File: ServiceActionUtil.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
/**
 * fo<caret>o:
 *   class: foo
 *   arguments: []
 */
@Nullable
public static ServiceYamlContainer create(@NotNull YAMLKeyValue yamlServiceKeyValue) {
    YAMLMapping childOfType = PsiTreeUtil.getChildOfType(yamlServiceKeyValue, YAMLMapping.class);
    if(childOfType == null) {
        return null;
    }

    String serviceClass = null;

    YAMLKeyValue aClass = childOfType.getKeyValueByKey("class");
    if(aClass != null) {
        // service:
        //  class: Foobar
        YAMLValue value = aClass.getValue();
        if(value instanceof YAMLScalar) {
            serviceClass =  ((YAMLScalar) value).getTextValue();
        }
    } else {
        // Foobar:
        //  argument: ~
        String keyText = yamlServiceKeyValue.getKeyText();

        if(StringUtils.isNotBlank(keyText) && YamlHelper.isClassServiceId(keyText)) {
            serviceClass = keyText;
        }
    }

    if (StringUtils.isBlank(serviceClass)) {
        return null;
    }

    return new ServiceYamlContainer(yamlServiceKeyValue, childOfType.getKeyValueByKey("arguments"), serviceClass);
}
 
Example 4
Source File: CypherInvocation.java    From jetbrains-plugin-graph-database-support with Apache License 2.0 5 votes vote down vote up
default PsiElement argumentsToken() {
    if (this instanceof CypherFunctionInvocation) {
        return PsiTreeUtil.getChildOfType(this, CypherArgumentList.class);
    }
    if (this instanceof CypherProcedureInvocation) {
        return PsiTreeUtil.getChildOfType(this, CypherArgumentList.class);
    } else {
        return this;
    }

}
 
Example 5
Source File: SQFCodeBlockExpression.java    From arma-intellij-plugin with MIT License 5 votes vote down vote up
@NotNull
public SQFCodeBlock getBlock() {
	SQFCodeBlock block = PsiTreeUtil.getChildOfType(this, SQFCodeBlock.class);
	if (block == null) {
		throw new IllegalStateException("the block returned shouldn't be null");
	}
	return block;
}
 
Example 6
Source File: JavascriptTestContextProvider.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Nullable
private static PsiElement findTestSuiteReference(PsiElement element) {
  if (element instanceof JSVarStatement) {
    // variable assignment might be
    // testSuite = goog.require('goog.testing.testSuite')
    JSVariable variable = PsiTreeUtil.getChildOfType(element, JSVariable.class);
    if (variable != null && isImportingTestSuite(variable)) {
      return variable;
    }
  } else if (element instanceof JSExpressionStatement) {
    // expression statement might be
    // goog.require('goog.testing.testSuite')
    if (isImportingTestSuite(element)) {
      JSLiteralExpression literal =
          PsiTreeUtil.findChildOfType(element, JSLiteralExpression.class);
      // this should be 'goog.testing.testSuite'
      if (literal == null) {
        return null;
      }
      for (PsiReference reference : literal.getReferences()) {
        if (reference instanceof JSGclModuleReference) {
          // this should be testSuite, and should resolve to the function
          return reference.resolve();
        }
      }
    }
  }
  return null;
}
 
Example 7
Source File: ThriftTopLevelDeclarationImpl.java    From intellij-thrift with Apache License 2.0 5 votes vote down vote up
@Override
public List<ThriftDeclaration> findSubDeclarations() {
  ThriftDeclarationBody body = PsiTreeUtil.getChildOfType(this, ThriftDeclarationBody.class);
  if (body == null) {
    return Collections.emptyList();
  }
  return PsiTreeUtil.getChildrenOfTypeAsList(body, ThriftDeclaration.class);
}
 
Example 8
Source File: HaxePsiFieldImpl.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
@NotNull
@Override
public PsiType getType() {
  PsiType psiType = null;
  final HaxeTypeTag tag = PsiTreeUtil.getChildOfType(this, HaxeTypeTag.class);
  if (tag != null) {
    final HaxeTypeOrAnonymous toa = tag.getTypeOrAnonymous();
    final HaxeType type = (toa != null) ? toa.getType() : null;
    psiType = (type != null) ? type.getPsiType() : null;
  }
  return psiType != null ? psiType : HaxePsiTypeAdapter.DYNAMIC;
}
 
Example 9
Source File: HaskellQconImpl.java    From intellij-haskforce with Apache License 2.0 4 votes vote down vote up
@Override
@Nullable
public HaskellGconsym getGconsym() {
  return PsiTreeUtil.getChildOfType(this, HaskellGconsym.class);
}
 
Example 10
Source File: HaskellAtypeImpl.java    From intellij-haskforce with Apache License 2.0 4 votes vote down vote up
@Override
@Nullable
public HaskellVars getVars() {
  return PsiTreeUtil.getChildOfType(this, HaskellVars.class);
}
 
Example 11
Source File: HaskellOpImpl.java    From intellij-haskforce with Apache License 2.0 4 votes vote down vote up
@Override
@Nullable
public HaskellConop getConop() {
  return PsiTreeUtil.getChildOfType(this, HaskellConop.class);
}
 
Example 12
Source File: HaskellTypeeImpl.java    From intellij-haskforce with Apache License 2.0 4 votes vote down vote up
@Override
@Nullable
public HaskellQtyconop getQtyconop() {
  return PsiTreeUtil.getChildOfType(this, HaskellQtyconop.class);
}
 
Example 13
Source File: HaskellOqtyconImpl.java    From intellij-haskforce with Apache License 2.0 4 votes vote down vote up
@Override
@Nullable
public HaskellQtyconsym getQtyconsym() {
  return PsiTreeUtil.getChildOfType(this, HaskellQtyconsym.class);
}
 
Example 14
Source File: HaxeStatementUtils.java    From intellij-haxe with Apache License 2.0 4 votes vote down vote up
/**
 * Retrieve the (first) catch statement that belongs to the given element.
 */
public static HaxeCatchStatement getCatchStatement(PsiElement elem) {
  return PsiTreeUtil.getChildOfType(elem, HaxeCatchStatement.class);
}
 
Example 15
Source File: HaskellTypeeImpl.java    From intellij-haskforce with Apache License 2.0 4 votes vote down vote up
@Override
@Nullable
public HaskellVarop getVarop() {
  return PsiTreeUtil.getChildOfType(this, HaskellVarop.class);
}
 
Example 16
Source File: Utils.java    From silex-idea-plugin with MIT License 4 votes vote down vote up
private static Container findContainerForPimpleArrayAccess(ArrayAccessExpression arrayAccessElement, Boolean onlyParentContainers) {

        PsiElement children;
        PsiElement element = arrayAccessElement;
        while ((children = PsiTreeUtil.getChildOfType(element, ArrayAccessExpression.class)) != null) {
            element = children;
        }

        // check if var is pimple container
        Signature signature = new Signature();

        PsiElement signatureElement = PsiTreeUtil.getChildOfAnyType(element, Variable.class, FieldReference.class);
        if (signatureElement == null) {
            return null;
        }

        if (signatureElement instanceof Variable) {
            signature.set(((Variable) signatureElement).getSignature());
        }

        if (signatureElement instanceof FieldReference) {
            signature.set(((FieldReference) signatureElement).getSignature());
        }

        PhpIndex phpIndex = PhpIndex.getInstance(arrayAccessElement.getProject());

        ArrayList<String> parameters = new ArrayList<String>();
        if (!findPimpleContainer(phpIndex, signature.base, parameters)) {
            return null;
        }

        Container container = ContainerResolver.get(arrayAccessElement.getProject());

        // find proper base container from signature
        for (String parameter : parameters) {
            container = container.getContainers().get(getResolvedParameter(phpIndex, parameter));
            if (container == null)
                return null;
        }

        PsiElement lastElement = onlyParentContainers ? arrayAccessElement : arrayAccessElement.getParent();

        // find proper container
        while (!element.isEquivalentTo(lastElement) ) {

            ArrayIndex arrayIndex = ((ArrayAccessExpression)element).getIndex();
            if (arrayIndex == null) {
                return null;
            }

            PsiElement arrayIndexElement = arrayIndex.getValue();
            if (arrayIndexElement == null) {
                return null;
            }

            String containerName;

            if (arrayIndexElement instanceof StringLiteralExpression) {
                containerName = ((StringLiteralExpression) arrayIndexElement).getContents();
            }
            else if (arrayIndexElement instanceof MemberReference) {
                containerName = getResolvedParameter(phpIndex, ((MemberReference) arrayIndexElement).getSignature());
            }
            else return null;

            container = container.getContainers().get(containerName);
            if (container == null) {
                return null;
            }

            element = element.getParent();
        }

        return container;

    }
 
Example 17
Source File: PhpGlobalsTypeProvider.java    From idea-php-typo3-plugin with MIT License 4 votes vote down vote up
@Nullable
@Override
public PhpType getType(PsiElement psiElement) {
    if (DumbService.getInstance(psiElement.getProject()).isDumb() || !TYPO3CMSProjectSettings.isEnabled(psiElement)) {
        return null;
    }

    // $GLOBALS['TYPO3_DB']
    if (!(psiElement instanceof ArrayAccessExpression)) {
        return null;
    }

    VariableImpl variable = PsiTreeUtil.getChildOfType(psiElement, VariableImpl.class);
    if (variable == null || !variable.getName().equals("GLOBALS")) {
        return null;
    }

    ArrayIndex arrayIndex = PsiTreeUtil.getChildOfType(psiElement, ArrayIndex.class);
    if (arrayIndex == null) {
        return null;
    }

    StringLiteralExpression arrayIndexName = PsiTreeUtil.getChildOfType(arrayIndex, StringLiteralExpressionImpl.class);
    if (arrayIndexName == null) {
        return null;
    }

    switch (arrayIndexName.getContents()) {
        case "TYPO3_DB":
            return new PhpType().add("#C\\TYPO3\\CMS\\Core\\Database\\DatabaseConnection");
        case "TSFE":
            return new PhpType().add("#C\\TYPO3\\CMS\\Frontend\\Controller\\TypoScriptFrontendController");
        case "BE_USER":
            return new PhpType().add("#C\\TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication");
        case "LANG":
            return new PhpType().add("#C\\TYPO3\\CMS\\Lang\\LanguageService");
        case "TYPO3_REQUEST":
            return new PhpType().add("#C\\Psr\\Http\\Message\\ServerRequestInterface");
        default:
            return null;
    }
}
 
Example 18
Source File: HaskellBkindImpl.java    From intellij-haskforce with Apache License 2.0 4 votes vote down vote up
@Override
@Nullable
public HaskellBkind getBkind() {
  return PsiTreeUtil.getChildOfType(this, HaskellBkind.class);
}
 
Example 19
Source File: HaxeElementGenerator.java    From intellij-haxe with Apache License 2.0 4 votes vote down vote up
@Nullable
public static HaxePackageStatement createPackageStatementFromPath(Project myProject, String path) {
  final PsiFile dummyFile = createDummyFile(myProject, "package " + path + ";");
  return PsiTreeUtil.getChildOfType(dummyFile, HaxePackageStatement.class);
}
 
Example 20
Source File: HaskellTyconImpl.java    From intellij-haskforce with Apache License 2.0 4 votes vote down vote up
@Override
@Nullable
public HaskellConsym getConsym() {
  return PsiTreeUtil.getChildOfType(this, HaskellConsym.class);
}