use of org.ballerinalang.plugins.idea.psi.IdentifierPSINode in project ballerina by ballerina-lang.
the class NameReference method resolveInPackage.
@Nullable
private PsiElement resolveInPackage(@NotNull PackageNameNode packageNameNode) {
PsiElement resolvedElement = BallerinaPsiImplUtil.resolvePackage(packageNameNode);
if (resolvedElement == null || !(resolvedElement instanceof PsiDirectory)) {
return null;
}
PsiDirectory psiDirectory = (PsiDirectory) resolvedElement;
IdentifierPSINode identifier = getElement();
return BallerinaPsiImplUtil.resolveElementInPackage(psiDirectory, identifier, true, true, true, true, true, true, false, false);
}
use of org.ballerinalang.plugins.idea.psi.IdentifierPSINode in project ballerina by ballerina-lang.
the class NameReference method getVariants.
@NotNull
@Override
public Object[] getVariants() {
List<LookupElement> results = new LinkedList<>();
IdentifierPSINode identifier = getElement();
PsiElement parent = identifier.getParent();
PackageNameNode packageNameNode = PsiTreeUtil.getChildOfType(parent, PackageNameNode.class);
if (packageNameNode == null) {
results.addAll(getVariantsFromCurrentPackage());
} else {
results.addAll(getVariantsFromPackage(packageNameNode));
}
return results.toArray(new LookupElement[results.size()]);
}
use of org.ballerinalang.plugins.idea.psi.IdentifierPSINode in project ballerina by ballerina-lang.
the class NameReference method resolveInCurrentPackage.
@Nullable
private PsiElement resolveInCurrentPackage() {
IdentifierPSINode identifier = getElement();
PsiFile containingFile = identifier.getContainingFile();
if (containingFile == null) {
return null;
}
PsiFile originalFile = containingFile.getOriginalFile();
PsiDirectory psiDirectory = originalFile.getParent();
if (psiDirectory == null) {
return null;
}
// If the next element is '(', that means we are at a function invocation node. So match any matching
// function first. If no matching function found, match with variables since this can be a lambda function.
// If the next element is not '(', we match variables first.
PsiElement nextVisibleLeaf = PsiTreeUtil.nextVisibleLeaf(identifier);
if (nextVisibleLeaf != null && "(".equals(nextVisibleLeaf.getText())) {
PsiElement element = BallerinaPsiImplUtil.resolveElementInPackage(psiDirectory, identifier, true, true, true, true, true, true, true, true);
if (element != null) {
return element;
}
return BallerinaPsiImplUtil.resolveElementInScope(identifier, true, true, true, true, true);
} else {
PsiElement elementInScope = BallerinaPsiImplUtil.resolveElementInScope(identifier, true, true, true, true, true);
if (elementInScope != null) {
return elementInScope;
}
PsiElement resolvedElement;
TypeNameNode typeNameNode = PsiTreeUtil.getParentOfType(identifier, TypeNameNode.class);
if (typeNameNode == null) {
resolvedElement = BallerinaPsiImplUtil.resolveElementInPackage(psiDirectory, identifier, true, true, true, true, true, true, true, true);
} else {
resolvedElement = BallerinaPsiImplUtil.resolveElementInPackage(psiDirectory, identifier, false, true, true, true, false, false, true, true);
}
if (resolvedElement != null) {
return resolvedElement;
}
return BallerinaPsiImplUtil.getNamespaceDefinition(identifier);
}
}
use of org.ballerinalang.plugins.idea.psi.IdentifierPSINode in project ballerina by ballerina-lang.
the class NameSpaceReference method getVariants.
@NotNull
@Override
public Object[] getVariants() {
List<LookupElement> results = new LinkedList<>();
IdentifierPSINode identifier = getElement();
PsiElement parent = identifier.getParent();
PackageNameNode packageNameNode = PsiTreeUtil.getChildOfType(parent, PackageNameNode.class);
if (packageNameNode == null) {
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);
results.addAll(BallerinaCompletionUtils.createNamespaceLookupElements(namespaces));
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));
}
}
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 multiResolve.
@NotNull
@Override
public ResolveResult[] multiResolve(boolean incompleteCode) {
IdentifierPSINode identifier = getElement();
if (identifier == null) {
return new ResolveResult[0];
}
AliasNode aliasNode = PsiTreeUtil.getParentOfType(identifier, AliasNode.class);
if (aliasNode != null) {
return new ResolveResult[0];
}
List<ResolveResult> results = new ArrayList<>();
ImportDeclarationNode importDeclarationNode = PsiTreeUtil.getParentOfType(identifier, ImportDeclarationNode.class);
if (importDeclarationNode != null) {
List<PsiDirectory> directories = BallerinaPsiImplUtil.resolveDirectory(identifier);
for (PsiDirectory directory : directories) {
results.add(new PsiElementResolveResult(directory));
}
// Return the results.
return results.toArray(new ResolveResult[results.size()]);
}
PackageDeclarationNode packageDeclarationNode = PsiTreeUtil.getParentOfType(identifier, PackageDeclarationNode.class);
if (packageDeclarationNode != null) {
// If this is a package declaration, resolve the directory.
List<PsiDirectory> directories = BallerinaPsiImplUtil.resolveDirectory(identifier);
for (PsiDirectory directory : directories) {
results.add(new PsiElementResolveResult(directory));
}
// Return the results.
return results.toArray(new ResolveResult[results.size()]);
}
PsiFile containingFile = identifier.getContainingFile();
if (containingFile == null) {
return new ResolveResult[0];
}
List<PsiElement> importedPackages = BallerinaPsiImplUtil.getImportedPackagesInCurrentFile(containingFile);
for (PsiElement importedPackage : importedPackages) {
String packageName = importedPackage.getText();
if (packageName == null || packageName.isEmpty()) {
continue;
}
if (packageName.equals(identifier.getText())) {
PsiReference reference = importedPackage.findReferenceAt(0);
if (reference != null) {
PsiElement resolvedElement = reference.resolve();
if (resolvedElement != null) {
results.add(new PsiElementResolveResult(resolvedElement));
}
}
}
}
importedPackages = BallerinaPsiImplUtil.getPackagesImportedAsAliasInCurrentFile(containingFile);
for (PsiElement importedPackage : importedPackages) {
String packageName = importedPackage.getText();
if (packageName == null || packageName.isEmpty()) {
continue;
}
if (packageName.equals(identifier.getText())) {
IdentifierPSINode nameNode = PsiTreeUtil.findChildOfType(importedPackage, IdentifierPSINode.class);
if (nameNode != null) {
results.add(new PsiElementResolveResult(nameNode));
}
}
}
return results.toArray(new ResolveResult[results.size()]);
}
Aggregations