use of org.antlr.jetbrains.adaptor.psi.ScopeNode in project ballerina by ballerina-lang.
the class NameReference method getVariantsFromCurrentPackage.
@NotNull
private List<LookupElement> getVariantsFromCurrentPackage() {
List<LookupElement> results = new LinkedList<>();
IdentifierPSINode identifier = getElement();
PsiFile containingFile = identifier.getContainingFile();
PsiFile originalFile = containingFile.getOriginalFile();
PsiDirectory containingPackage = originalFile.getParent();
AnnotationAttachmentNode attachmentNode = PsiTreeUtil.getParentOfType(identifier, AnnotationAttachmentNode.class);
if (attachmentNode != null && containingFile instanceof BallerinaFile) {
ScopeNode scope = (BallerinaFile) containingFile;
List<IdentifierPSINode> constants = BallerinaPsiImplUtil.getAllConstantsInResolvableScope(scope);
results.addAll(BallerinaCompletionUtils.createConstantLookupElements(constants));
} else if (containingPackage != null) {
List<LookupElement> packages = BallerinaPsiImplUtil.getPackagesAsLookups(originalFile, true, PackageCompletionInsertHandler.INSTANCE_WITH_AUTO_POPUP, true, AutoImportInsertHandler.INSTANCE_WITH_AUTO_POPUP);
results.addAll(packages);
PsiElement prevVisibleLeaf = PsiTreeUtil.prevVisibleLeaf(identifier);
ANTLRPsiNode definitionParent = PsiTreeUtil.getParentOfType(identifier, CallableUnitBodyNode.class, ServiceBodyNode.class, ResourceDefinitionNode.class, ConnectorBodyNode.class);
TypeNameNode typeNameNode = PsiTreeUtil.getParentOfType(identifier, TypeNameNode.class);
if ((definitionParent != null && !(definitionParent instanceof ResourceDefinitionNode)) || prevVisibleLeaf != null && (!";".equals(prevVisibleLeaf.getText()) && typeNameNode == null || prevVisibleLeaf.getText().matches("[{}]"))) {
List<IdentifierPSINode> functions = BallerinaPsiImplUtil.getAllFunctionsFromPackage(containingPackage, true, true);
results.addAll(BallerinaCompletionUtils.createFunctionLookupElements(functions));
// Todo - use a util method
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);
results.addAll(BallerinaCompletionUtils.createVariableLookupElements(variables));
List<IdentifierPSINode> parameters = BallerinaPsiImplUtil.getAllParametersInResolvableScope(scope, caretOffset);
results.addAll(BallerinaCompletionUtils.createParameterLookupElements(parameters));
List<IdentifierPSINode> globalVariables = BallerinaPsiImplUtil.getAllGlobalVariablesInResolvableScope(scope);
results.addAll(BallerinaCompletionUtils.createGlobalVariableLookupElements(globalVariables));
List<IdentifierPSINode> constants = BallerinaPsiImplUtil.getAllConstantsInResolvableScope(scope);
results.addAll(BallerinaCompletionUtils.createConstantLookupElements(constants));
List<PsiElement> namespaces = BallerinaPsiImplUtil.getAllXmlNamespacesInResolvableScope(scope, caretOffset);
results.addAll(BallerinaCompletionUtils.createNamespaceLookupElements(namespaces));
List<IdentifierPSINode> endpoints = BallerinaPsiImplUtil.getAllEndpointsInResolvableScope(scope, caretOffset);
results.addAll(BallerinaCompletionUtils.createEndpointLookupElements(endpoints));
} else {
ConstantDefinitionNode constantDefinitionNode = PsiTreeUtil.getParentOfType(identifier, ConstantDefinitionNode.class);
GlobalVariableDefinitionNode globalVariableDefinitionNode = PsiTreeUtil.getParentOfType(identifier, GlobalVariableDefinitionNode.class);
if (constantDefinitionNode != null || globalVariableDefinitionNode != null) {
scope = PsiTreeUtil.getParentOfType(constantDefinitionNode, BallerinaFile.class);
}
if (globalVariableDefinitionNode != null) {
scope = PsiTreeUtil.getParentOfType(globalVariableDefinitionNode, BallerinaFile.class);
}
if (scope != null) {
int caretOffset = identifier.getStartOffset();
List<IdentifierPSINode> globalVars = BallerinaPsiImplUtil.getAllGlobalVariablesInResolvableScope(scope, caretOffset);
results.addAll(BallerinaCompletionUtils.createGlobalVariableLookupElements(globalVars));
List<IdentifierPSINode> constants = BallerinaPsiImplUtil.getAllConstantsInResolvableScope(scope, caretOffset);
results.addAll(BallerinaCompletionUtils.createConstantLookupElements(constants));
}
}
}
List<IdentifierPSINode> connectors = BallerinaPsiImplUtil.getAllConnectorsFromPackage(containingPackage, true, true);
results.addAll(BallerinaCompletionUtils.createConnectorLookupElements(connectors, AddSpaceInsertHandler.INSTANCE));
List<IdentifierPSINode> structs = BallerinaPsiImplUtil.getAllStructsFromPackage(containingPackage, true, true);
results.addAll(BallerinaCompletionUtils.createStructLookupElements(structs));
List<IdentifierPSINode> enums = BallerinaPsiImplUtil.getAllEnumsFromPackage(containingPackage, true, true);
results.addAll(BallerinaCompletionUtils.createEnumLookupElements(enums, null));
return results;
}
// Try to get fields from an anonymous struct.
PsiElement structDefinitionNode = BallerinaPsiImplUtil.resolveAnonymousStruct(identifier);
if (structDefinitionNode == null || !(structDefinitionNode instanceof StructDefinitionNode)) {
return results;
}
IdentifierPSINode structNameNode = PsiTreeUtil.getChildOfType(structDefinitionNode, IdentifierPSINode.class);
if (structNameNode == null) {
return results;
}
Collection<FieldDefinitionNode> fieldDefinitionNodes = PsiTreeUtil.findChildrenOfType(structDefinitionNode, FieldDefinitionNode.class);
results = BallerinaCompletionUtils.createFieldLookupElements(fieldDefinitionNodes, structNameNode, PackageCompletionInsertHandler.INSTANCE_WITH_AUTO_POPUP);
return results;
}
use of org.antlr.jetbrains.adaptor.psi.ScopeNode in project ballerina by ballerina-lang.
the class NameSpaceReference method resolve.
@Nullable
@Override
public PsiElement resolve() {
IdentifierPSINode identifier = getElement();
ScopeNode scope = PsiTreeUtil.getParentOfType(identifier, CodeBlockScope.class, VariableContainer.class, TopLevelDefinition.class, LowerLevelDefinition.class);
if (scope != null) {
int caretOffset = identifier.getStartOffset();
List<PsiElement> namespaces = BallerinaPsiImplUtil.getAllXmlNamespacesInResolvableScope(scope, caretOffset);
for (PsiElement namespace : namespaces) {
if (namespace == null || namespace.getText().isEmpty()) {
continue;
}
if (namespace.getText().equals(identifier.getText())) {
return namespace;
}
}
}
return BallerinaPsiImplUtil.resolveElementInScope(identifier, true, true, true, true, true);
}
use of org.antlr.jetbrains.adaptor.psi.ScopeNode in project ballerina by ballerina-lang.
the class PackageNameReference method resolve.
@Nullable
@Override
public PsiElement resolve() {
ResolveResult[] resolveResults = multiResolve(false);
if (resolveResults.length != 0) {
return resolveResults.length == 1 ? resolveResults[0].getElement() : null;
}
IdentifierPSINode identifier = getElement();
ScopeNode scope = PsiTreeUtil.getParentOfType(identifier, CodeBlockScope.class, VariableContainer.class, TopLevelDefinition.class, LowerLevelDefinition.class);
if (scope == null) {
return null;
}
int caretOffset = identifier.getStartOffset();
List<PsiElement> namespaces = BallerinaPsiImplUtil.getAllXmlNamespacesInResolvableScope(scope, caretOffset);
for (PsiElement namespace : namespaces) {
if (namespace == null || namespace.getText().isEmpty()) {
continue;
}
if (namespace.getText().equals(identifier.getText())) {
return namespace;
}
}
return null;
}
use of org.antlr.jetbrains.adaptor.psi.ScopeNode in project ballerina by ballerina-lang.
the class WorkerReference method getVariants.
@NotNull
@Override
public Object[] getVariants() {
List<LookupElement> results = new LinkedList<>();
IdentifierPSINode identifier = getElement();
ScopeNode scopeNode = PsiTreeUtil.getParentOfType(identifier, ScopeNode.class);
if (scopeNode == null) {
return results.toArray(new LookupElement[results.size()]);
}
if (scopeNode instanceof JoinClauseNode) {
scopeNode = (ScopeNode) scopeNode.getParent();
}
List<WorkerDeclarationNode> workerDeclarations = BallerinaPsiImplUtil.getWorkerDeclarationsInScope(scopeNode);
results.addAll(BallerinaCompletionUtils.createWorkerLookupElements(workerDeclarations));
return results.toArray(new LookupElement[results.size()]);
}
use of org.antlr.jetbrains.adaptor.psi.ScopeNode in project ballerina by ballerina-lang.
the class WorkerReference method resolve.
@Nullable
@Override
public PsiElement resolve() {
IdentifierPSINode identifier = getElement();
ScopeNode scopeNode = PsiTreeUtil.getParentOfType(identifier, ScopeNode.class);
if (scopeNode == null) {
return null;
}
if (scopeNode instanceof JoinClauseNode) {
scopeNode = (ScopeNode) scopeNode.getParent();
}
List<WorkerDeclarationNode> workerDeclarations = BallerinaPsiImplUtil.getWorkerDeclarationsInScope(scopeNode);
for (WorkerDeclarationNode workerDeclaration : workerDeclarations) {
IdentifierPSINode workerName = PsiTreeUtil.getChildOfType(workerDeclaration, IdentifierPSINode.class);
if (workerName == null) {
continue;
}
if (identifier.getText().equals(workerName.getText())) {
return workerName;
}
}
return super.resolve();
}
Aggregations