use of org.antlr.jetbrains.adaptor.psi.IdentifierDefSubtree in project ballerina by ballerina-lang.
the class WrongPackageStatementInspection method checkFile.
@Override
@Nullable
public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull InspectionManager manager, boolean isOnTheFly) {
// does not work in tests since CodeInsightTestCase copies file into temporary location
if (ApplicationManager.getApplication().isUnitTestMode()) {
return new ProblemDescriptor[0];
}
if (!(file instanceof BallerinaFile)) {
return new ProblemDescriptor[0];
}
Module module = ModuleUtil.findModuleForFile(file.getVirtualFile(), file.getProject());
boolean isBallerinaModule = BallerinaSdkService.getInstance(file.getProject()).isBallerinaModule(module);
if (!isBallerinaModule) {
return new ProblemDescriptor[0];
}
BallerinaFile ballerinaFile = (BallerinaFile) file;
PsiDirectory directory = ballerinaFile.getContainingDirectory();
if (directory == null) {
return new ProblemDescriptor[0];
}
List<ProblemDescriptor> problemDescriptors = new LinkedList<>();
String packageName = BallerinaUtil.suggestPackageNameForDirectory(directory);
PackageDeclarationNode packageDeclarationNode = PsiTreeUtil.findChildOfType(file, PackageDeclarationNode.class);
Collection<DefinitionNode> definitionNodes = PsiTreeUtil.findChildrenOfType(file, DefinitionNode.class);
for (DefinitionNode definitionNode : definitionNodes) {
PsiElement firstChild = definitionNode.getFirstChild();
if (firstChild == null || !(firstChild instanceof IdentifierDefSubtree)) {
return new ProblemDescriptor[0];
}
PsiElement nameIdentifier = ((IdentifierDefSubtree) firstChild).getNameIdentifier();
if (nameIdentifier == null) {
return new ProblemDescriptor[0];
}
if (!Comparing.strEqual(packageName, "", true) && packageDeclarationNode == null) {
String description = "Missing package statement: '" + packageName + "'";
ProblemDescriptor problemDescriptor = manager.createProblemDescriptor(nameIdentifier, description, new AdjustPackageNameFix(nameIdentifier, packageName), ProblemHighlightType.GENERIC_ERROR_OR_WARNING, isOnTheFly);
problemDescriptors.add(problemDescriptor);
}
}
if (!problemDescriptors.isEmpty()) {
return problemDescriptors.toArray(new ProblemDescriptor[problemDescriptors.size()]);
}
if (packageDeclarationNode == null) {
return new ProblemDescriptor[0];
}
FullyQualifiedPackageNameNode fullyQualifiedPackageNameNode = PsiTreeUtil.findChildOfType(packageDeclarationNode, FullyQualifiedPackageNameNode.class);
if (fullyQualifiedPackageNameNode == null || fullyQualifiedPackageNameNode.getText().isEmpty()) {
return new ProblemDescriptor[0];
}
Collection<PackageNameNode> packageNames = PsiTreeUtil.findChildrenOfType(packageDeclarationNode, PackageNameNode.class);
if (packageNames.isEmpty()) {
return new ProblemDescriptor[0];
}
LinkedList<PackageNameNode> packageNameNodes = new LinkedList<>();
packageNameNodes.addAll(packageNames);
PackageNameNode lastElement = packageNameNodes.getLast();
if (lastElement == null) {
return new ProblemDescriptor[0];
}
List<LocalQuickFix> availableFixes = new ArrayList<>();
availableFixes.add(new AdjustPackageNameFix(fullyQualifiedPackageNameNode, packageName));
PsiElement packageNameIdentifier = lastElement.getNameIdentifier();
if (packageNameIdentifier == null) {
return getProblemDescriptors(manager, isOnTheFly, packageName, availableFixes, fullyQualifiedPackageNameNode, lastElement);
}
PsiReference reference = packageNameIdentifier.getReference();
if (reference == null) {
return getProblemDescriptors(manager, isOnTheFly, packageName, availableFixes, fullyQualifiedPackageNameNode, lastElement);
}
PsiElement resolvedElement = reference.resolve();
if (!(resolvedElement instanceof PsiDirectory)) {
return getProblemDescriptors(manager, isOnTheFly, packageName, availableFixes, fullyQualifiedPackageNameNode, lastElement);
}
String containingDirectoryPackageName = BallerinaUtil.suggestPackageNameForDirectory(((PsiDirectory) resolvedElement));
if (!Comparing.equal(packageName, containingDirectoryPackageName, true)) {
if (!availableFixes.isEmpty()) {
return getProblemDescriptors(manager, isOnTheFly, packageName, availableFixes, fullyQualifiedPackageNameNode, lastElement);
}
}
return new ProblemDescriptor[0];
}
Aggregations