use of org.ballerinalang.plugins.idea.psi.FullyQualifiedPackageNameNode 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.FullyQualifiedPackageNameNode in project ballerina by ballerina-lang.
the class BallerinaFoldingBuilder method buildImportFoldingRegion.
private void buildImportFoldingRegion(@NotNull List<FoldingDescriptor> descriptors, @NotNull PsiElement root) {
Collection<ImportDeclarationNode> importDeclarationNodes = PsiTreeUtil.findChildrenOfType(root, ImportDeclarationNode.class);
if (!importDeclarationNodes.isEmpty()) {
ImportDeclarationNode[] importDeclarationNodesArray = importDeclarationNodes.toArray(new ImportDeclarationNode[importDeclarationNodes.size()]);
ImportDeclarationNode firstImport = importDeclarationNodesArray[0];
ImportDeclarationNode lastImport = importDeclarationNodesArray[importDeclarationNodes.size() - 1];
FullyQualifiedPackageNameNode fullyQualifiedPackageNameNode = PsiTreeUtil.findChildOfType(firstImport, FullyQualifiedPackageNameNode.class);
if (fullyQualifiedPackageNameNode == null || fullyQualifiedPackageNameNode.getText().isEmpty()) {
return;
}
int startOffset = fullyQualifiedPackageNameNode.getTextRange().getStartOffset();
int endOffset = lastImport.getTextRange().getEndOffset();
descriptors.add(new NamedFoldingDescriptor(firstImport, startOffset, endOffset, null, "..."));
}
}
use of org.ballerinalang.plugins.idea.psi.FullyQualifiedPackageNameNode in project ballerina by ballerina-lang.
the class BallerinaRunConfigurationProducerBase method setupConfigurationFromContext.
@Override
protected boolean setupConfigurationFromContext(@NotNull T configuration, @NotNull ConfigurationContext context, Ref<PsiElement> sourceElement) {
// This method will be called with each configuration type. So we need to return true for correct
// configuration type after updating the configurations.
PsiFile file = getFileFromContext(context);
if (file == null) {
return false;
}
// Get the element. This will be an identifier element.
PsiElement element = sourceElement.get();
// Get the FunctionDefinitionNode parent from element (if exists).
FunctionDefinitionNode functionNode = PsiTreeUtil.getParentOfType(element, FunctionDefinitionNode.class);
// Get the ServiceDefinitionNode parent from element (if exists).
ServiceDefinitionNode serviceDefinitionNode = PsiTreeUtil.getParentOfType(element, ServiceDefinitionNode.class);
// Setup configuration for Ballerina test files.
if (file.getName().endsWith(BallerinaConstants.BALLERINA_TEST_FILE_SUFFIX) && functionNode != null && BallerinaRunUtil.isTestFunction(functionNode)) {
if (!(configuration instanceof BallerinaTestConfiguration)) {
return false;
}
PackageDeclarationNode packageDeclarationNode = PsiTreeUtil.findChildOfType(file, PackageDeclarationNode.class);
// Get the package path node. We need this to get the package path of the file.
FullyQualifiedPackageNameNode fullyQualifiedPackageNameNode = PsiTreeUtil.findChildOfType(packageDeclarationNode, FullyQualifiedPackageNameNode.class);
String packageInFile = "";
if (fullyQualifiedPackageNameNode != null) {
// Regardless of the OS, separator character will be "/".
packageInFile = fullyQualifiedPackageNameNode.getText().replaceAll("\\.", "/");
}
RunnerAndConfigurationSettings existingConfigurations = context.findExisting();
if (existingConfigurations != null) {
// Get the RunConfiguration.
RunConfiguration existingConfiguration = existingConfigurations.getConfiguration();
// Run configuration might be an application configuration. So we need to check the type.
if (existingConfiguration instanceof BallerinaTestConfiguration) {
// Set other configurations.
setTestConfigurations((BallerinaTestConfiguration) existingConfiguration, file, packageInFile);
return true;
}
return false;
} else {
// If an existing configuration is not found and the configuration provided is of correct type.
String configName = getConfigurationName(file);
// Set the config name. This will be the file name.
configuration.setName(configName);
// Set the file path.
configuration.setFilePath(file.getVirtualFile().getPath());
// Set the module.
Module module = context.getModule();
if (module != null) {
configuration.setModule(module);
}
// Set other configurations.
setTestConfigurations((BallerinaTestConfiguration) configuration, file, packageInFile);
return true;
}
}
// Get the declared package in the file if available.
String packageInFile = "";
boolean isPackageDeclared = false;
// Get the PackageDeclarationNode if available.
PackageDeclarationNode packageDeclarationNode = PsiTreeUtil.findChildOfType(file, PackageDeclarationNode.class);
if (packageDeclarationNode != null) {
isPackageDeclared = true;
}
// Get the package path node. We need this to get the package path of the file.
FullyQualifiedPackageNameNode fullyQualifiedPackageNameNode = PsiTreeUtil.findChildOfType(packageDeclarationNode, FullyQualifiedPackageNameNode.class);
if (fullyQualifiedPackageNameNode != null) {
// Regardless of the OS, separator character will be "/".
packageInFile = fullyQualifiedPackageNameNode.getText().replaceAll("\\.", "/");
}
// Get existing configuration if available.
RunnerAndConfigurationSettings existingConfigurations = context.findExisting();
if (existingConfigurations != null) {
// Get the RunConfiguration.
RunConfiguration existingConfiguration = existingConfigurations.getConfiguration();
// Run configuration might be an application configuration. So we need to check the type.
if (existingConfiguration instanceof BallerinaApplicationConfiguration) {
// Set other configurations.
setConfigurations((BallerinaApplicationConfiguration) existingConfiguration, file, functionNode, serviceDefinitionNode, packageInFile, isPackageDeclared);
return true;
}
} else if (configuration instanceof BallerinaApplicationConfiguration) {
// If an existing configuration is not found and the configuration provided is of correct type.
String configName = getConfigurationName(file);
// Set the config name. This will be the file name.
configuration.setName(configName);
// Set the file path.
configuration.setFilePath(file.getVirtualFile().getPath());
// Set the module.
Module module = context.getModule();
if (module != null) {
configuration.setModule(module);
}
// Set other configurations.
setConfigurations((BallerinaApplicationConfiguration) configuration, file, functionNode, serviceDefinitionNode, packageInFile, isPackageDeclared);
return true;
}
// Return false if the provided configuration type cannot be applied.
return false;
}
use of org.ballerinalang.plugins.idea.psi.FullyQualifiedPackageNameNode in project ballerina by ballerina-lang.
the class BallerinaCreateFileActionTest method doTemplateTest.
private static void doTemplateTest(@NotNull PsiDirectory dir, @NotNull String expectedPackage, @NotNull CustomFileTemplate template) {
PsiFile fileFromTemplate = CreateFileFromTemplateAction.createFileFromTemplate("test", template, dir, null, true);
BallerinaFile file = (BallerinaFile) fileFromTemplate;
assertNotNull(file);
PackageDeclarationNode packageDeclarationNode = PsiTreeUtil.findChildOfType(fileFromTemplate, PackageDeclarationNode.class);
// declaration available. Otherwise there will be a package declaration node.
if (!expectedPackage.isEmpty()) {
assertNotNull(packageDeclarationNode);
FullyQualifiedPackageNameNode fullyQualifiedPackageNameNode = PsiTreeUtil.getChildOfType(packageDeclarationNode, FullyQualifiedPackageNameNode.class);
assertNotNull(fullyQualifiedPackageNameNode);
assertEquals(expectedPackage, fullyQualifiedPackageNameNode.getText());
} else {
assertNull(packageDeclarationNode);
}
WriteCommandAction.runWriteCommandAction(dir.getProject(), file::delete);
}
use of org.ballerinalang.plugins.idea.psi.FullyQualifiedPackageNameNode in project ballerina by ballerina-lang.
the class BallerinaParserDefinition method createElement.
@NotNull
public PsiElement createElement(ASTNode node) {
IElementType elementType = node.getElementType();
if (elementType instanceof TokenIElementType) {
return new ANTLRPsiNode(node);
}
if (!(elementType instanceof RuleIElementType)) {
return new ANTLRPsiNode(node);
}
RuleIElementType ruleElType = (RuleIElementType) elementType;
switch(ruleElType.getRuleIndex()) {
case BallerinaParser.RULE_functionDefinition:
return new FunctionDefinitionNode(node);
case BallerinaParser.RULE_callableUnitBody:
return new CallableUnitBodyNode(node);
case BallerinaParser.RULE_nameReference:
return new NameReferenceNode(node);
case BallerinaParser.RULE_variableReference:
return new VariableReferenceNode(node);
case BallerinaParser.RULE_variableDefinitionStatement:
return new VariableDefinitionNode(node);
case BallerinaParser.RULE_parameter:
return new ParameterNode(node);
case BallerinaParser.RULE_resourceDefinition:
return new ResourceDefinitionNode(node);
case BallerinaParser.RULE_packageName:
return new PackageNameNode(node);
case BallerinaParser.RULE_fullyQualifiedPackageName:
return new FullyQualifiedPackageNameNode(node);
case BallerinaParser.RULE_expressionList:
return new ExpressionListNode(node);
case BallerinaParser.RULE_expression:
return new ExpressionNode(node);
case BallerinaParser.RULE_functionInvocation:
return new FunctionInvocationNode(node);
case BallerinaParser.RULE_compilationUnit:
return new CompilationUnitNode(node);
case BallerinaParser.RULE_packageDeclaration:
return new PackageDeclarationNode(node);
case BallerinaParser.RULE_annotationAttachment:
return new AnnotationAttachmentNode(node);
case BallerinaParser.RULE_serviceBody:
return new ServiceBodyNode(node);
case BallerinaParser.RULE_importDeclaration:
return new ImportDeclarationNode(node);
case BallerinaParser.RULE_statement:
return new StatementNode(node);
case BallerinaParser.RULE_typeName:
return new TypeNameNode(node);
case BallerinaParser.RULE_constantDefinition:
return new ConstantDefinitionNode(node);
case BallerinaParser.RULE_structDefinition:
return new StructDefinitionNode(node);
case BallerinaParser.RULE_simpleLiteral:
return new SimpleLiteralNode(node);
case BallerinaParser.RULE_packageAlias:
return new AliasNode(node);
case BallerinaParser.RULE_parameterList:
return new ParameterListNode(node);
case BallerinaParser.RULE_fieldDefinition:
return new FieldDefinitionNode(node);
case BallerinaParser.RULE_parameterTypeNameList:
return new ParameterTypeNameList(node);
case BallerinaParser.RULE_parameterTypeName:
return new ConnectorInitNode(node);
case BallerinaParser.RULE_serviceDefinition:
return new ServiceDefinitionNode(node);
case BallerinaParser.RULE_valueTypeName:
return new ValueTypeNameNode(node);
case BallerinaParser.RULE_annotationDefinition:
return new AnnotationAttributeValueNode(node);
case BallerinaParser.RULE_structBody:
return new StructBodyNode(node);
case BallerinaParser.RULE_returnParameter:
return new ReturnParameterNode(node);
case BallerinaParser.RULE_attachmentPoint:
return new AttachmentPointNode(node);
case BallerinaParser.RULE_definition:
return new DefinitionNode(node);
case BallerinaParser.RULE_ifElseStatement:
return new IfElseStatementNode(node);
case BallerinaParser.RULE_assignmentStatement:
return new AssignmentStatementNode(node);
case BallerinaParser.RULE_variableReferenceList:
return new VariableReferenceListNode(node);
case BallerinaParser.RULE_recordLiteral:
return new RecordLiteralNode(node);
case BallerinaParser.RULE_globalVariableDefinition:
return new GlobalVariableDefinitionNode(node);
case BallerinaParser.RULE_recordKeyValue:
return new RecordKeyValueNode(node);
case BallerinaParser.RULE_forkJoinStatement:
return new ForkJoinStatementNode(node);
case BallerinaParser.RULE_returnStatement:
return new ReturnStatementNode(node);
case BallerinaParser.RULE_throwStatement:
return new ThrowStatementNode(node);
case BallerinaParser.RULE_transformerDefinition:
return new TransformerDefinitionNode(node);
case BallerinaParser.RULE_workerReply:
return new WorkerReplyNode(node);
case BallerinaParser.RULE_triggerWorker:
return new TriggerWorkerNode(node);
case BallerinaParser.RULE_workerDeclaration:
return new WorkerDeclarationNode(node);
case BallerinaParser.RULE_workerBody:
return new WorkerBodyNode(node);
case BallerinaParser.RULE_joinConditions:
return new JoinConditionNode(node);
case BallerinaParser.RULE_field:
return new FieldNode(node);
case BallerinaParser.RULE_recordKey:
return new RecordKeyNode(node);
case BallerinaParser.RULE_recordValue:
return new RecordValueNode(node);
case BallerinaParser.RULE_functionReference:
return new FunctionReferenceNode(node);
case BallerinaParser.RULE_codeBlockBody:
return new CodeBlockBodyNode(node);
case BallerinaParser.RULE_nonEmptyCodeBlockBody:
return new NonEmptyCodeBlockBodyNode(node);
case BallerinaParser.RULE_tryCatchStatement:
return new TryCatchStatementNode(node);
case BallerinaParser.RULE_catchClause:
return new CatchClauseNode(node);
case BallerinaParser.RULE_codeBlockParameter:
return new CodeBlockParameterNode(node);
case BallerinaParser.RULE_foreachStatement:
return new ForEachStatementNode(node);
case BallerinaParser.RULE_joinClause:
return new JoinClauseNode(node);
case BallerinaParser.RULE_timeoutClause:
return new TimeoutClauseNode(node);
case BallerinaParser.RULE_xmlAttrib:
return new XmlAttribNode(node);
case BallerinaParser.RULE_namespaceDeclaration:
return new NamespaceDeclarationNode(node);
case BallerinaParser.RULE_stringTemplateLiteral:
return new StringTemplateLiteralNode(node);
case BallerinaParser.RULE_stringTemplateContent:
return new StringTemplateContentNode(node);
case BallerinaParser.RULE_typeConversion:
return new TypeConversionNode(node);
case BallerinaParser.RULE_xmlContent:
return new XmlContentNode(node);
case BallerinaParser.RULE_invocation:
return new InvocationNode(node);
case BallerinaParser.RULE_enumDefinition:
return new EnumDefinitionNode(node);
case BallerinaParser.RULE_enumField:
return new EnumFieldNode(node);
case BallerinaParser.RULE_builtInReferenceTypeName:
return new BuiltInReferenceTypeNameNode(node);
case BallerinaParser.RULE_referenceTypeName:
return new ReferenceTypeNameNode(node);
case BallerinaParser.RULE_functionTypeName:
return new FunctionTypeNameNode(node);
case BallerinaParser.RULE_lambdaFunction:
return new LambdaFunctionNode(node);
case BallerinaParser.RULE_endpointDeclaration:
return new EndpointDeclarationNode(node);
case BallerinaParser.RULE_anonStructTypeName:
return new AnonStructTypeNameNode(node);
case BallerinaParser.RULE_userDefineTypeName:
return new UserDefinedTypeName(node);
case BallerinaParser.RULE_anyIdentifierName:
return new AnyIdentifierNameNode(node);
case BallerinaParser.RULE_documentationAttachment:
return new DocumentationAttachmentNode(node);
case BallerinaParser.RULE_documentationTemplateInlineCode:
return new DocumentationTemplateInlineCodeNode(node);
case BallerinaParser.RULE_singleBackTickDocInlineCode:
return new SingleBackTickDocInlineCodeNode(node);
case BallerinaParser.RULE_doubleBackTickDocInlineCode:
return new DoubleBackTickInlineCodeNode(node);
case BallerinaParser.RULE_tripleBackTickDocInlineCode:
return new TripleBackTickInlineCodeNode(node);
case BallerinaParser.RULE_deprecatedAttachment:
return new DeprecatedAttachmentNode(node);
case BallerinaParser.RULE_deprecatedText:
return new DeprecatedTextNode(node);
case BallerinaParser.RULE_singleBackTickDeprecatedInlineCode:
return new SingleBackTickDeprecatedInlineCodeNode(node);
case BallerinaParser.RULE_doubleBackTickDeprecatedInlineCode:
return new DoubleBackTickDeprecatedInlineCodeNode(node);
case BallerinaParser.RULE_tripleBackTickDeprecatedInlineCode:
return new TripleBackTickDeprecatedInlineCodeNode(node);
case BallerinaParser.RULE_documentationTemplateAttributeDescription:
return new DocumentationTemplateAttributeDescriptionNode(node);
case BallerinaParser.RULE_formalParameterList:
return new FormalParameterListNode(node);
case BallerinaParser.RULE_integerLiteral:
return new IntegerLiteralNode(node);
case BallerinaParser.RULE_matchPatternClause:
return new MatchPatternClauseNode(node);
default:
return new ANTLRPsiNode(node);
}
}
Aggregations