use of org.ballerinalang.plugins.idea.psi.IdentifierPSINode in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method getAllActionsFromAConnector.
/**
* Returns all actions/native actions defined in the given ConnectorDefinitionNode.
*
* @param connectorDefinitionNode PsiElement which represent a Connector Definition
* @return List of all actions/native actions defined in the given ConnectorDefinitionNode.
*/
@NotNull
public static List<IdentifierPSINode> getAllActionsFromAConnector(@NotNull PsiElement connectorDefinitionNode) {
List<IdentifierPSINode> results = new ArrayList<>();
Collection<ActionDefinitionNode> actionDefinitionNodes = PsiTreeUtil.findChildrenOfType(connectorDefinitionNode, ActionDefinitionNode.class);
for (ActionDefinitionNode definitionNode : actionDefinitionNodes) {
IdentifierPSINode identifier = PsiTreeUtil.getChildOfType(definitionNode, IdentifierPSINode.class);
results.add(identifier);
}
return results;
}
use of org.ballerinalang.plugins.idea.psi.IdentifierPSINode in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method getErrorStruct.
/**
* Returns corresponding error struct for type casts and type conversions.
*
* @param assignmentStatementNode {@link AssignmentStatementNode} which we are currently checking
* @param referenceNode resolved variable reference node
* @param isTypeCast indicates whether the {@link AssignmentStatementNode} contains a type cast
* @param isTypeConversion indicates whether the {@link AssignmentStatementNode} contains a type conversion
* @return {@code TypeCastError} if the provided assignment statement is a type cast operation.
* {@code TypeConversionError} if the provided assignment statement is a type conversion operation.
*/
@Nullable
public static StructDefinitionNode getErrorStruct(@NotNull AssignmentStatementNode assignmentStatementNode, @NotNull IdentifierPSINode referenceNode, boolean isTypeCast, boolean isTypeConversion) {
List<IdentifierPSINode> variablesFromVarAssignment = BallerinaPsiImplUtil.getVariablesFromVarAssignment(assignmentStatementNode);
// might be unnecessary and identified at the semantic analyzer.
if (variablesFromVarAssignment.size() < 2) {
return null;
}
// Get the second value from the assignment statement.
IdentifierPSINode errorVariable = variablesFromVarAssignment.get(1);
// This should be the provided reference node. Since the error is returned as the second variable.
if (!errorVariable.equals(referenceNode)) {
return null;
}
// Now we need to find the error.bal file which contains the definition of these error structs.
Project project = referenceNode.getProject();
VirtualFile errorFile = BallerinaPsiImplUtil.findFileInSDK(project, referenceNode, "/ballerina/builtin/core/source.bal");
if (errorFile == null) {
return null;
}
// Find the file.
PsiFile psiFile = PsiManager.getInstance(project).findFile(errorFile);
if (psiFile == null) {
return null;
}
// Get all struct definitions in the file.
Collection<StructDefinitionNode> structDefinitionNodes = PsiTreeUtil.findChildrenOfType(psiFile, StructDefinitionNode.class);
// Iterate through all struct definitions.
for (StructDefinitionNode definitionNode : structDefinitionNodes) {
IdentifierPSINode nameNode = PsiTreeUtil.getChildOfType(definitionNode, IdentifierPSINode.class);
if (nameNode != null) {
// Check and return the matching definition.
if (isTypeCast && "TypeCastError".equals(nameNode.getText())) {
return definitionNode;
}
if (isTypeConversion && "TypeConversionError".equals(nameNode.getText())) {
return definitionNode;
}
}
}
return null;
}
use of org.ballerinalang.plugins.idea.psi.IdentifierPSINode in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method getAllEndpointsInScope.
@NotNull
private static List<IdentifierPSINode> getAllEndpointsInScope(@NotNull ScopeNode scope, int caretOffset) {
List<IdentifierPSINode> results = new LinkedList<>();
Collection<EndpointDeclarationNode> endpointDeclarationNodes = PsiTreeUtil.getChildrenOfTypeAsList(scope, EndpointDeclarationNode.class);
for (EndpointDeclarationNode endpointDeclarationNode : endpointDeclarationNodes) {
IdentifierPSINode identifier = PsiTreeUtil.getChildOfType(endpointDeclarationNode, IdentifierPSINode.class);
if (identifier != null) {
results.add((identifier));
}
}
return results;
}
use of org.ballerinalang.plugins.idea.psi.IdentifierPSINode in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method getAllLocalVariablesInResolvableScope.
/**
* Returns all local variables in provided scope and all parent contexts.
*
* @param scope
* @param caretOffset
* @return
*/
@NotNull
public static List<IdentifierPSINode> getAllLocalVariablesInResolvableScope(@NotNull ScopeNode scope, int caretOffset) {
List<IdentifierPSINode> results = new LinkedList<>();
if (scope instanceof VariableContainer || scope instanceof CodeBlockScope) {
results.addAll(getAllLocalVariablesInScope(scope, caretOffset));
ScopeNode context = scope.getContext();
if (context != null && !(scope instanceof RestrictedScope)) {
results.addAll(getAllLocalVariablesInResolvableScope(context, caretOffset));
}
} else if (scope instanceof ParameterContainer || scope instanceof LowerLevelDefinition) {
ScopeNode context = scope.getContext();
if (context != null) {
results.addAll(getAllLocalVariablesInResolvableScope(context, caretOffset));
}
}
return results;
}
use of org.ballerinalang.plugins.idea.psi.IdentifierPSINode in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method resolveAliasNode.
/**
* Resolves the given alias node to the corresponding directory.
*
* @param aliasNode an alias node
* @return {@link PsiDirectory} which is the definition of the alias node.
*/
@Nullable
public static PsiDirectory resolveAliasNode(@NotNull AliasNode aliasNode) {
ImportDeclarationNode importDeclarationNode = PsiTreeUtil.getParentOfType(aliasNode, ImportDeclarationNode.class);
FullyQualifiedPackageNameNode fullyQualifiedPackageNameNode = PsiTreeUtil.getChildOfType(importDeclarationNode, FullyQualifiedPackageNameNode.class);
if (fullyQualifiedPackageNameNode == null) {
return null;
}
PackageNameNode[] packageNameNodes = PsiTreeUtil.getChildrenOfType(fullyQualifiedPackageNameNode, PackageNameNode.class);
if (packageNameNodes == null) {
return null;
}
PackageNameNode lastElement = ArrayUtil.getLastElement(packageNameNodes);
if (lastElement == null) {
return null;
}
PsiElement packageName = lastElement.getNameIdentifier();
if (!(packageName instanceof IdentifierPSINode)) {
return null;
}
List<PsiDirectory> directories = BallerinaPsiImplUtil.resolveDirectory(((IdentifierPSINode) packageName));
if (directories.isEmpty()) {
return null;
}
return directories.get(0);
}
Aggregations