use of org.ballerinalang.plugins.idea.psi.IdentifierPSINode 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.ballerinalang.plugins.idea.psi.IdentifierPSINode in project ballerina by ballerina-lang.
the class NameReference method getVariantsFromPackage.
@NotNull
private List<LookupElement> getVariantsFromPackage(@NotNull PackageNameNode packageNameNode) {
List<LookupElement> results = new LinkedList<>();
PsiElement resolvedElement = BallerinaPsiImplUtil.resolvePackage(packageNameNode);
if (resolvedElement == null || !(resolvedElement instanceof PsiDirectory)) {
return results;
}
PsiDirectory containingPackage = (PsiDirectory) resolvedElement;
AnnotationAttachmentNode attachmentNode = PsiTreeUtil.getParentOfType(packageNameNode, AnnotationAttachmentNode.class);
if (attachmentNode != null) {
List<IdentifierPSINode> constants = BallerinaPsiImplUtil.getAllConstantsFromPackage(containingPackage, false, false);
results.addAll(BallerinaCompletionUtils.createConstantLookupElements(constants));
} else {
// Todo - use a util method
List<IdentifierPSINode> functions = BallerinaPsiImplUtil.getAllFunctionsFromPackage(containingPackage, false, false);
results.addAll(BallerinaCompletionUtils.createFunctionLookupElements(functions));
List<IdentifierPSINode> connectors = BallerinaPsiImplUtil.getAllConnectorsFromPackage(containingPackage, false, false);
results.addAll(BallerinaCompletionUtils.createConnectorLookupElements(connectors, AddSpaceInsertHandler.INSTANCE));
List<IdentifierPSINode> structs = BallerinaPsiImplUtil.getAllStructsFromPackage(containingPackage, false, false);
results.addAll(BallerinaCompletionUtils.createStructLookupElements(structs));
List<IdentifierPSINode> enums = BallerinaPsiImplUtil.getAllEnumsFromPackage(containingPackage, true, false);
results.addAll(BallerinaCompletionUtils.createEnumLookupElements(enums, null));
List<IdentifierPSINode> globalVariables = BallerinaPsiImplUtil.getAllGlobalVariablesFromPackage(containingPackage, false, false);
results.addAll(BallerinaCompletionUtils.createGlobalVariableLookupElements(globalVariables));
List<IdentifierPSINode> constants = BallerinaPsiImplUtil.getAllConstantsFromPackage(containingPackage, false, false);
results.addAll(BallerinaCompletionUtils.createConstantLookupElements(constants));
}
return results;
}
use of org.ballerinalang.plugins.idea.psi.IdentifierPSINode 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.ballerinalang.plugins.idea.psi.IdentifierPSINode in project ballerina by ballerina-lang.
the class PackageNameReference method getVariants.
@NotNull
@Override
public Object[] getVariants() {
IdentifierPSINode identifier = getElement();
FullyQualifiedPackageNameNode fullyQualifiedPackageNameNode = PsiTreeUtil.getParentOfType(identifier, FullyQualifiedPackageNameNode.class);
if (fullyQualifiedPackageNameNode != null) {
return new LookupElement[0];
}
List<LookupElement> results = new ArrayList<>();
PsiFile containingFile = identifier.getContainingFile();
PsiFile originalFile = containingFile.getOriginalFile();
// We don't need to add ':' at the end of the package name in SourceNotationNode.
InsertHandler<LookupElement> importedPackagesInsertHandler = PackageCompletionInsertHandler.INSTANCE_WITH_AUTO_POPUP;
InsertHandler<LookupElement> unImportedPackagesInsertHandler = AutoImportInsertHandler.INSTANCE_WITH_AUTO_POPUP;
SourceNotationNode sourceNotationNode = PsiTreeUtil.getParentOfType(identifier, SourceNotationNode.class);
if (sourceNotationNode != null) {
importedPackagesInsertHandler = null;
unImportedPackagesInsertHandler = AutoImportInsertHandler.INSTANCE;
}
List<LookupElement> packages = BallerinaPsiImplUtil.getPackagesAsLookups(originalFile, true, importedPackagesInsertHandler, true, unImportedPackagesInsertHandler);
results.addAll(packages);
return results.toArray(new LookupElement[results.size()]);
}
use of org.ballerinalang.plugins.idea.psi.IdentifierPSINode 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;
}
Aggregations