use of org.antlr.jetbrains.adaptor.psi.ScopeNode in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method getAllEndpointsInResolvableScope.
@NotNull
public static List<IdentifierPSINode> getAllEndpointsInResolvableScope(@NotNull ScopeNode scope, int caretOffset) {
List<IdentifierPSINode> results = new LinkedList<>();
if (scope instanceof VariableContainer || scope instanceof CodeBlockScope || scope instanceof TopLevelDefinition || scope instanceof LowerLevelDefinition) {
results.addAll(getAllEndpointsInScope(scope, caretOffset));
ScopeNode context = scope.getContext();
if (context != null) {
results.addAll(getAllEndpointsInResolvableScope(context, caretOffset));
}
}
return results;
}
use of org.antlr.jetbrains.adaptor.psi.ScopeNode in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method resolveElementInScope.
@Nullable
public static PsiElement resolveElementInScope(@NotNull IdentifierPSINode identifier, boolean matchLocalVariables, boolean matchParameters, boolean matchGlobalVariables, boolean matchConstants, boolean matchEndpoint) {
ScopeNode scope = PsiTreeUtil.getParentOfType(identifier, CodeBlockScope.class, VariableContainer.class, TopLevelDefinition.class, LowerLevelDefinition.class);
if (scope == null) {
return null;
}
int caretOffset = identifier.getStartOffset();
if (matchLocalVariables) {
List<IdentifierPSINode> variables = BallerinaPsiImplUtil.getAllLocalVariablesInResolvableScope(scope, caretOffset);
PsiElement element = getMatchingElement(identifier, variables);
if (element != null) {
return element;
}
}
if (matchEndpoint) {
List<IdentifierPSINode> endpoints = BallerinaPsiImplUtil.getAllEndpointsInResolvableScope(scope, caretOffset);
PsiElement element = getMatchingElement(identifier, endpoints);
if (element != null) {
return element;
}
}
if (matchParameters) {
List<IdentifierPSINode> parameters = BallerinaPsiImplUtil.getAllParametersInResolvableScope(scope, caretOffset);
PsiElement element = getMatchingElement(identifier, parameters);
if (element != null) {
return element;
}
}
if (matchGlobalVariables) {
List<IdentifierPSINode> globalVariables = BallerinaPsiImplUtil.getAllGlobalVariablesInResolvableScope(scope);
PsiElement element = getMatchingElement(identifier, globalVariables);
if (element != null) {
return element;
}
}
if (matchConstants) {
List<IdentifierPSINode> constants = BallerinaPsiImplUtil.getAllConstantsInResolvableScope(scope);
PsiElement element = getMatchingElement(identifier, constants);
if (element != null) {
return element;
}
}
return null;
}
use of org.antlr.jetbrains.adaptor.psi.ScopeNode in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method getAllXmlNamespacesInResolvableScope.
@NotNull
public static List<PsiElement> getAllXmlNamespacesInResolvableScope(@NotNull ScopeNode scope, int caretOffset) {
List<PsiElement> results = new LinkedList<>();
if (scope instanceof VariableContainer || scope instanceof CodeBlockScope || scope instanceof BallerinaFile) {
results.addAll(getAllXmlNamespacesInScope(scope, caretOffset));
ScopeNode context = scope.getContext();
if (context != null) {
results.addAll(getAllXmlNamespacesInResolvableScope(context, caretOffset));
}
} else if (scope instanceof ParameterContainer || scope instanceof TopLevelDefinition || scope instanceof LowerLevelDefinition) {
ScopeNode context = scope.getContext();
if (context != null) {
results.addAll(getAllXmlNamespacesInResolvableScope(context, caretOffset));
}
}
if (scope instanceof BallerinaFile) {
PsiFile originalFile = ((BallerinaFile) scope).getOriginalFile();
PsiDirectory containingPackage = originalFile.getParent();
if (containingPackage == null) {
return results;
}
PsiFile[] files = containingPackage.getFiles();
for (PsiFile file : files) {
// Do't check the current file again.
if (file.equals(originalFile)) {
continue;
}
Collection<NamespaceDeclarationNode> namespaceDeclarationNodes = PsiTreeUtil.findChildrenOfType(file, NamespaceDeclarationNode.class);
for (NamespaceDeclarationNode namespaceDeclarationNode : namespaceDeclarationNodes) {
PsiElement identifier = namespaceDeclarationNode.getNameIdentifier();
if (identifier != null) {
results.add(identifier);
}
}
}
}
return results;
}
use of org.antlr.jetbrains.adaptor.psi.ScopeNode in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method getAllParametersInScope.
@NotNull
private static List<IdentifierPSINode> getAllParametersInScope(@NotNull ScopeNode scope, int caretOffset) {
List<IdentifierPSINode> results = new LinkedList<>();
Collection<ParameterNode> parameterNodes = PsiTreeUtil.findChildrenOfType(scope, ParameterNode.class);
for (ParameterNode parameter : parameterNodes) {
ScopeNode parentScope = PsiTreeUtil.getParentOfType(parameter, ScopeNode.class);
if (!scope.equals(parentScope)) {
continue;
}
PsiElement identifier = parameter.getNameIdentifier();
if (identifier != null && identifier instanceof IdentifierPSINode) {
results.add(((IdentifierPSINode) identifier));
}
}
Collection<CodeBlockParameterNode> codeBlockParameterNodes = PsiTreeUtil.findChildrenOfType(scope, CodeBlockParameterNode.class);
for (CodeBlockParameterNode parameter : codeBlockParameterNodes) {
PsiElement elementAtCaret = scope.getContainingFile().findElementAt(caretOffset);
if (elementAtCaret == null) {
return results;
}
if (parameter.getTextRange().getEndOffset() >= caretOffset) {
return results;
}
ScopeNode closestScope = PsiTreeUtil.getParentOfType(parameter, ScopeNode.class);
if (closestScope == null || !closestScope.equals(scope)) {
continue;
}
PsiElement identifier = parameter.getNameIdentifier();
if (identifier != null && identifier instanceof IdentifierPSINode) {
results.add(((IdentifierPSINode) identifier));
}
}
return results;
}
use of org.antlr.jetbrains.adaptor.psi.ScopeNode in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method isRedeclaredVar.
/**
* Used to identify variables used in var assignment statements are redeclared variables or not.
*
* @param identifier an identifier
* @return {@code true} if the variable is declared before in a resolvable scope, {@code false} otherwise.
*/
public static boolean isRedeclaredVar(@NotNull IdentifierPSINode identifier) {
ScopeNode scope = PsiTreeUtil.getParentOfType(identifier, CodeBlockScope.class, VariableContainer.class, TopLevelDefinition.class, LowerLevelDefinition.class);
if (scope != null) {
int caretOffset = identifier.getStartOffset();
List<IdentifierPSINode> variables = BallerinaPsiImplUtil.getAllLocalVariablesInResolvableScope(scope, caretOffset);
for (IdentifierPSINode variable : variables) {
if (variable != null && variable.getText().equals(identifier.getText())) {
return true;
}
}
List<IdentifierPSINode> parameters = BallerinaPsiImplUtil.getAllParametersInResolvableScope(scope, caretOffset);
for (IdentifierPSINode parameter : parameters) {
if (parameter != null && parameter.getText().equals(identifier.getText())) {
return true;
}
}
List<IdentifierPSINode> globalVariables = BallerinaPsiImplUtil.getAllGlobalVariablesInResolvableScope(scope);
for (IdentifierPSINode variable : globalVariables) {
if (variable != null && variable.getText().equals(identifier.getText())) {
return true;
}
}
List<IdentifierPSINode> constants = BallerinaPsiImplUtil.getAllConstantsInResolvableScope(scope);
for (IdentifierPSINode constant : constants) {
if (constant != null && constant.getText().equals(identifier.getText())) {
return true;
}
}
List<PsiElement> namespaces = BallerinaPsiImplUtil.getAllXmlNamespacesInResolvableScope(scope, caretOffset);
for (PsiElement namespace : namespaces) {
if (namespace != null && namespace.getText().equals(identifier.getText())) {
return true;
}
}
List<IdentifierPSINode> endpoints = BallerinaPsiImplUtil.getAllEndpointsInResolvableScope(scope, caretOffset);
for (IdentifierPSINode endpoint : endpoints) {
if (endpoint != null && endpoint.getText().equals(identifier.getText())) {
return true;
}
}
}
return false;
}
Aggregations