use of org.ballerinalang.plugins.idea.psi.IdentifierPSINode in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method getAllLocalVariablesInScope.
/**
* Returns all local variables in the provided scope.
*
* @param scope
* @param caretOffset
* @return
*/
@NotNull
public static List<IdentifierPSINode> getAllLocalVariablesInScope(@NotNull ScopeNode scope, int caretOffset) {
List<IdentifierPSINode> results = new LinkedList<>();
Collection<VariableDefinitionNode> variableDefinitionNodes = PsiTreeUtil.findChildrenOfType(scope, VariableDefinitionNode.class);
for (VariableDefinitionNode variableDefinitionNode : variableDefinitionNodes) {
if (isValidVariable(variableDefinitionNode, scope, caretOffset)) {
PsiElement identifier = variableDefinitionNode.getNameIdentifier();
if (identifier != null && identifier instanceof IdentifierPSINode) {
results.add(((IdentifierPSINode) identifier));
}
}
}
if (scope instanceof TransformerDefinitionNode) {
Collection<ExpressionVariableDefinitionStatementNode> nodes = PsiTreeUtil.findChildrenOfType(scope, ExpressionVariableDefinitionStatementNode.class);
for (ExpressionVariableDefinitionStatementNode node : nodes) {
ScopeNode closestScope = PsiTreeUtil.getParentOfType(node, TransformerDefinitionNode.class);
if (closestScope == null || !closestScope.equals(scope)) {
continue;
}
PsiElement identifier = node.getNameIdentifier();
if (identifier != null && identifier instanceof IdentifierPSINode) {
results.add(((IdentifierPSINode) identifier));
}
}
}
if (scope instanceof ForEachStatementNode) {
VariableReferenceListNode variableReferenceListNode = PsiTreeUtil.getChildOfType(scope, VariableReferenceListNode.class);
if (variableReferenceListNode != null) {
List<VariableReferenceNode> variableReferenceNodes = PsiTreeUtil.getChildrenOfTypeAsList(variableReferenceListNode, VariableReferenceNode.class);
for (VariableReferenceNode variableReferenceNode : variableReferenceNodes) {
if (variableReferenceNode != null) {
IdentifierPSINode identifier = PsiTreeUtil.findChildOfType(variableReferenceNode, IdentifierPSINode.class);
if (identifier != null) {
results.add(identifier);
}
}
}
}
}
Collection<AssignmentStatementNode> assignmentStatementNodes = PsiTreeUtil.findChildrenOfType(scope, AssignmentStatementNode.class);
for (AssignmentStatementNode assignmentStatementNode : assignmentStatementNodes) {
if (isValidVarAssignment(assignmentStatementNode, scope, caretOffset)) {
results.addAll(getVariablesFromVarAssignment(assignmentStatementNode));
}
}
return results;
}
use of org.ballerinalang.plugins.idea.psi.IdentifierPSINode in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method resolveReference.
@Nullable
public static <T extends PsiElement> PsiElement resolveReference(@NotNull Collection<T> definitionNodes, @NotNull IdentifierPSINode referenceIdentifier) {
for (T definitionNode : definitionNodes) {
IdentifierPSINode fieldName = PsiTreeUtil.getChildOfType(definitionNode, IdentifierPSINode.class);
if (fieldName == null) {
continue;
}
String text = fieldName.getText();
if (text.equals(referenceIdentifier.getText())) {
return fieldName;
}
}
return null;
}
use of org.ballerinalang.plugins.idea.psi.IdentifierPSINode in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method getVariablesFromVarAssignment.
@NotNull
public static List<IdentifierPSINode> getVariablesFromVarAssignment(@NotNull AssignmentStatementNode assignmentStatementNode) {
List<IdentifierPSINode> results = new LinkedList<>();
VariableReferenceListNode variableReferenceListNode = PsiTreeUtil.getChildOfType(assignmentStatementNode, VariableReferenceListNode.class);
if (variableReferenceListNode == null) {
return results;
}
VariableReferenceNode[] variableReferenceNodes = PsiTreeUtil.getChildrenOfType(variableReferenceListNode, VariableReferenceNode.class);
if (variableReferenceNodes == null) {
return results;
}
for (VariableReferenceNode variableReferenceNode : variableReferenceNodes) {
IdentifierPSINode identifier = PsiTreeUtil.findChildOfType(variableReferenceNode, IdentifierPSINode.class);
if (identifier != null && !"_".equals(identifier.getText())) {
results.add(identifier);
}
}
return results;
}
use of org.ballerinalang.plugins.idea.psi.IdentifierPSINode in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method getParameters.
/**
* Returns parameter for provided definition node.
*
* @param definitionNode
* @return
*/
@NotNull
public static List<IdentifierPSINode> getParameters(@NotNull PsiElement definitionNode) {
List<IdentifierPSINode> parameters = new LinkedList<>();
ParameterListNode parameterListNode = PsiTreeUtil.getChildOfType(definitionNode, ParameterListNode.class);
if (parameterListNode == null) {
return parameters;
}
// Get parameter nodes.
Collection<ParameterNode> parameterNodes = PsiTreeUtil.getChildrenOfTypeAsList(parameterListNode, ParameterNode.class);
for (ParameterNode parameterNode : parameterNodes) {
IdentifierPSINode identifier = PsiTreeUtil.getChildOfType(parameterNode, IdentifierPSINode.class);
if (identifier == null) {
continue;
}
parameters.add(identifier);
}
return parameters;
}
use of org.ballerinalang.plugins.idea.psi.IdentifierPSINode in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method getMatchingElementsFromAFile.
public static <T extends PsiElement> List<IdentifierPSINode> getMatchingElementsFromAFile(@NotNull PsiFile psiFile, @NotNull Class<T> clazz, boolean includePrivate) {
List<IdentifierPSINode> results = new ArrayList<>();
Collection<T> definitions = PsiTreeUtil.findChildrenOfType(psiFile, clazz);
for (T definition : definitions) {
PsiElement firstChild = definition.getFirstChild();
if (!includePrivate) {
if (firstChild instanceof LeafPsiElement) {
IElementType elementType = ((LeafPsiElement) firstChild).getElementType();
if (elementType != BallerinaTypes.PUBLIC) {
continue;
}
} else {
// is not present.
continue;
}
}
IdentifierPSINode identifier = PsiTreeUtil.getChildOfType(definition, IdentifierPSINode.class);
results.add(identifier);
}
return results;
}
Aggregations