use of org.ballerinalang.plugins.idea.psi.IdentifierPSINode in project ballerina by ballerina-lang.
the class StatementReference method resolve.
@Nullable
@Override
public PsiElement resolve() {
IdentifierPSINode identifier = getElement();
PsiElement prevVisibleLeaf = PsiTreeUtil.prevVisibleLeaf(identifier);
if (prevVisibleLeaf != null && ":".equals(prevVisibleLeaf.getText())) {
PsiElement packageNameNode = PsiTreeUtil.prevVisibleLeaf(prevVisibleLeaf);
if (packageNameNode == null) {
return null;
}
PsiElement resolvedElement = BallerinaPsiImplUtil.resolvePackage(packageNameNode);
if (resolvedElement == null || !(resolvedElement instanceof PsiDirectory)) {
return null;
}
PsiDirectory containingPackage = (PsiDirectory) resolvedElement;
return BallerinaPsiImplUtil.resolveElementInPackage(containingPackage, identifier, true, true, true, true, true, true, false, false);
} else {
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 StatementReference method getVariants.
@NotNull
@Override
public Object[] getVariants() {
List<LookupElement> results = new LinkedList<>();
IdentifierPSINode identifier = getElement();
PsiElement prevVisibleLeaf = PsiTreeUtil.prevVisibleLeaf(identifier);
if (prevVisibleLeaf == null) {
return new LookupElement[0];
}
if (":".equals(prevVisibleLeaf.getText())) {
PsiElement packageNameNode = PsiTreeUtil.prevVisibleLeaf(prevVisibleLeaf);
if (packageNameNode == null || !(packageNameNode instanceof IdentifierPSINode)) {
return new LookupElement[0];
}
results.addAll(getVariantsFromPackage(packageNameNode));
} else if (".".equals(prevVisibleLeaf.getText())) {
// Todo - suggest length field
PsiElement previousField = PsiTreeUtil.prevVisibleLeaf(prevVisibleLeaf);
if (previousField == null) {
return new LookupElement[0];
}
PsiReference reference = previousField.findReferenceAt(0);
if (reference == null) {
return new Object[0];
}
PsiElement resolvedElement = reference.resolve();
if (resolvedElement == null) {
return new LookupElement[0];
}
// Todo - use util method
PsiElement resolvedElementParent = resolvedElement.getParent();
StructDefinitionNode structDefinitionNode = null;
// Resolve the corresponding resolvedElementParent to get the struct definition.
if (resolvedElementParent instanceof VariableDefinitionNode) {
structDefinitionNode = BallerinaPsiImplUtil.resolveStructFromDefinitionNode(((VariableDefinitionNode) resolvedElementParent));
} else if (resolvedElementParent instanceof FieldDefinitionNode) {
structDefinitionNode = BallerinaPsiImplUtil.resolveTypeNodeStruct((resolvedElementParent));
}
if (structDefinitionNode == null) {
return results.toArray(new LookupElement[results.size()]);
}
Collection<FieldDefinitionNode> fieldDefinitionNodes = PsiTreeUtil.findChildrenOfType(structDefinitionNode, FieldDefinitionNode.class);
List<LookupElement> fields = BallerinaCompletionUtils.createFieldLookupElements(fieldDefinitionNodes, (IdentifierPSINode) resolvedElement, PackageCompletionInsertHandler.INSTANCE_WITH_AUTO_POPUP);
results.addAll(fields);
} else {
results.addAll(getVariantsFromCurrentPackage());
}
return results.toArray(new LookupElement[results.size()]);
}
use of org.ballerinalang.plugins.idea.psi.IdentifierPSINode in project ballerina by ballerina-lang.
the class StatementReference method getVariantsFromCurrentPackage.
@NotNull
private List<LookupElement> getVariantsFromCurrentPackage() {
List<LookupElement> results = new LinkedList<>();
IdentifierPSINode identifier = getElement();
PsiFile containingFile = identifier.getContainingFile();
PsiFile originalFile = containingFile.getOriginalFile();
List<LookupElement> packages = BallerinaPsiImplUtil.getPackagesAsLookups(originalFile, true, PackageCompletionInsertHandler.INSTANCE_WITH_AUTO_POPUP, true, AutoImportInsertHandler.INSTANCE_WITH_AUTO_POPUP);
results.addAll(packages);
// 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));
}
PsiDirectory containingPackage = originalFile.getParent();
if (containingPackage != null) {
List<IdentifierPSINode> functions = BallerinaPsiImplUtil.getAllFunctionsFromPackage(containingPackage, true, true);
results.addAll(BallerinaCompletionUtils.createFunctionLookupElements(functions));
}
return results;
}
use of org.ballerinalang.plugins.idea.psi.IdentifierPSINode in project ballerina by ballerina-lang.
the class StructReference 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();
List<IdentifierPSINode> structs = BallerinaPsiImplUtil.getAllStructsFromPackage(containingPackage, true, true);
results.addAll(BallerinaCompletionUtils.createStructLookupElements(structs));
return results;
}
use of org.ballerinalang.plugins.idea.psi.IdentifierPSINode in project ballerina by ballerina-lang.
the class StructReference method getVariantsFromPackage.
@NotNull
private Collection<? extends LookupElement> getVariantsFromPackage(PackageNameNode packageNameNode) {
List<LookupElement> results = new LinkedList<>();
PsiElement resolvedElement = BallerinaPsiImplUtil.resolvePackage(packageNameNode);
if (resolvedElement == null || !(resolvedElement instanceof PsiDirectory)) {
return results;
}
PsiDirectory containingPackage = (PsiDirectory) resolvedElement;
List<IdentifierPSINode> structs = BallerinaPsiImplUtil.getAllStructsFromPackage(containingPackage, false, false);
results.addAll(BallerinaCompletionUtils.createStructLookupElements(structs));
return results;
}
Aggregations