use of com.perl5.lang.perl.psi.impl.PerlUseStatementElement in project Perl5-IDEA by Camelcade.
the class PerlUseWarningsInspection method buildVisitor.
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
return new PerlVisitor() {
@Override
public void visitFile(@NotNull PsiFile file) {
if (!file.isWritable() || !file.isPhysical() || file.getVirtualFile() instanceof LightVirtualFile || InjectedLanguageManager.getInstance(file.getProject()).isInjectedFragment(file)) {
return;
}
FileType fileType = file.getFileType();
if (!(fileType instanceof PerlFileType) || !((PerlFileType) fileType).checkWarningsPragma()) {
return;
}
for (PerlUseStatementElement useStatement : PsiTreeUtil.findChildrenOfType(file, PerlUseStatementElement.class)) {
if (useStatement.getPackageProcessor() instanceof PerlWarningsProvider) {
return;
}
}
holder.registerProblem(file, PerlBundle.message("perl.inspection.use.warnings"), ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new PerlUsePackageQuickFix("warnings FATAL => 'all'"), new PerlUsePackageQuickFix("warnings"));
}
};
}
use of com.perl5.lang.perl.psi.impl.PerlUseStatementElement in project Perl5-IDEA by Camelcade.
the class PerlUnresolvedPackageFileInspection method buildVisitor.
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
return new PerlVisitor() {
@Override
public void visitUseStatement(@NotNull PerlUseStatementElement o) {
if (o.getNamespaceElement() != null) {
checkPackageFile(o.getNamespaceElement());
}
}
@Override
public void visitNoStatement(@NotNull PerlNoStatementElement o) {
if (o.getNamespaceElement() != null) {
checkPackageFile(o.getNamespaceElement());
}
}
@Override
public void visitRequireExpr(@NotNull PsiPerlRequireExpr o) {
if (o.getNamespaceElement() != null) {
checkPackageFile(o.getNamespaceElement());
}
}
public void checkPackageFile(PerlNamespaceElement o) {
List<PerlFileImpl> namespaceFiles = o.getNamespaceFiles();
String packageName = o.getCanonicalName();
if (namespaceFiles.isEmpty() && StringUtil.isNotEmpty(packageName)) {
List<LocalQuickFix> fixes = new ArrayList<>();
Project project = o.getProject();
Sdk perlSdk = PerlProjectManager.getSdk(project);
if (perlSdk != null) {
if (CpanminusAdapter.isAvailable(perlSdk)) {
fixes.add(new InstallPackageQuickfix(new CpanminusAdapter(perlSdk, project), packageName));
}
fixes.add(new InstallPackageQuickfix(new CpanAdapter(perlSdk, project), packageName));
}
registerProblem(holder, o, PerlBundle.message("perl.inspection.missing.package.file", packageName), fixes.toArray(LocalQuickFix.EMPTY_ARRAY));
}
}
};
}
use of com.perl5.lang.perl.psi.impl.PerlUseStatementElement in project Perl5-IDEA by Camelcade.
the class PerlStringCompletionUtil method fillWithUseParameters.
public static void fillWithUseParameters(@NotNull PerlCompletionProcessor completionProcessor) {
PsiElement baseElement = completionProcessor.getLeafElement();
PerlUseStatementElement useStatement = PsiTreeUtil.getParentOfType(baseElement, PerlUseStatementElement.class, true, PsiPerlStatement.class);
if (useStatement == null) {
return;
}
List<String> typedParameters = useStatement.getImportParameters();
Set<String> typedStringsSet = typedParameters == null ? Collections.emptySet() : new THashSet<>(typedParameters);
PerlPackageProcessor packageProcessor = useStatement.getPackageProcessor();
// fixme we should allow lookup elements customization by package processor
if (packageProcessor instanceof PerlPackageOptionsProvider) {
Map<String, String> options = ((PerlPackageOptionsProvider) packageProcessor).getOptions();
for (Map.Entry<String, String> option : options.entrySet()) {
String lookupString = option.getKey();
if (!typedStringsSet.contains(lookupString) && completionProcessor.matches(lookupString) && !completionProcessor.process(LookupElementBuilder.create(lookupString).withTypeText(option.getValue(), true).withIcon(PerlIcons.PERL_OPTION))) {
return;
}
}
options = ((PerlPackageOptionsProvider) packageProcessor).getOptionsBundles();
for (Map.Entry<String, String> option : options.entrySet()) {
String lookupString = option.getKey();
if (!typedStringsSet.contains(lookupString) && completionProcessor.matches(lookupString) && !completionProcessor.process(LookupElementBuilder.create(lookupString).withTypeText(option.getValue(), true).withIcon(PerlIcons.PERL_OPTIONS))) {
return;
}
}
}
if (packageProcessor instanceof PerlPackageParentsProvider && ((PerlPackageParentsProvider) packageProcessor).hasPackageFilesOptions() && !PerlPackageUtil.processPackageFilesForPsiElement(baseElement, (packageName, file) -> typedStringsSet.contains(packageName) || PerlPackageCompletionUtil.processPackageLookupElement(file, packageName, null, completionProcessor, false))) {
return;
}
Set<String> export = new HashSet<>();
Set<String> exportOk = new HashSet<>();
packageProcessor.addExports(useStatement, export, exportOk);
exportOk.removeAll(export);
for (String subName : export) {
if (!typedStringsSet.contains(subName) && completionProcessor.matches(subName) && !completionProcessor.process(LookupElementBuilder.create(subName).withIcon(PerlIcons.SUB_GUTTER_ICON).withTypeText("default", true))) {
return;
}
}
for (String subName : exportOk) {
if (!typedStringsSet.contains(subName) && completionProcessor.matches(subName) && !completionProcessor.process(LookupElementBuilder.create(subName).withIcon(PerlIcons.SUB_GUTTER_ICON).withTypeText("optional", true))) {
return;
}
}
}
use of com.perl5.lang.perl.psi.impl.PerlUseStatementElement in project Perl5-IDEA by Camelcade.
the class ExceptionClassProcessor method processExceptionElement.
private void processExceptionElement(@NotNull List<PsiElement> listElements, int currentIndex, @NotNull List<PerlDelegatingLightNamedElement<?>> result, @NotNull PerlUseStatementElement useStatementElement) {
PsiElement listElement = listElements.get(currentIndex);
if (!useStatementElement.isAcceptableIdentifierElement(listElement)) {
return;
}
String namespaceName = ElementManipulators.getValueText(listElement);
if (StringUtil.isEmpty(namespaceName)) {
return;
}
Map<String, PerlHashEntry> exceptionSettings = listElements.size() > currentIndex + 1 ? PerlHashUtil.collectHashMap(listElements.get(currentIndex + 1)) : Collections.emptyMap();
// Building fields
Set<PerlSubArgument> throwArguments = Collections.emptySet();
PerlHashEntry fieldsEntry = exceptionSettings.get("fields");
if (fieldsEntry != null && fieldsEntry.isComplete()) {
PsiElement fieldsContainer = fieldsEntry.getNonNullValueElement();
if (fieldsContainer instanceof PsiPerlAnonArray) {
fieldsContainer = ((PsiPerlAnonArray) fieldsContainer).getExpr();
}
List<PsiElement> elements = PerlArrayUtil.collectListElements(fieldsContainer);
if (!elements.isEmpty()) {
// Fields method
result.add(new PerlLightMethodDefinitionElement<>(useStatementElement, FIELDS_METHOD_NAME, LIGHT_METHOD_DEFINITION, fieldsEntry.keyElement, namespaceName, Collections.emptyList(), null));
// fields themselves
throwArguments = new LinkedHashSet<>();
for (PsiElement fieldElement : elements) {
if (useStatementElement.isAcceptableIdentifierElement(fieldElement)) {
String fieldName = PerlScalarUtil.getStringContent(fieldElement);
if (StringUtil.isNotEmpty(fieldName)) {
throwArguments.add(PerlSubArgument.mandatoryScalar(fieldName));
result.add(new PerlLightMethodDefinitionElement<>(useStatementElement, fieldName, LIGHT_METHOD_DEFINITION, fieldElement, namespaceName, Collections.emptyList(), null));
}
}
}
}
}
// making exception class
PerlHashEntry isaEntry = exceptionSettings.get("isa");
String parentClass = "Exception::Class::Base";
if (isaEntry != null && isaEntry.isComplete()) {
String manualIsa = isaEntry.getValueString();
if (manualIsa != null) {
parentClass = manualIsa;
}
}
result.add(new PerlLightExceptionClassDefinition(useStatementElement, namespaceName, LIGHT_NAMESPACE_DEFINITION, listElement, PerlMroType.DFS, Collections.singletonList(parentClass), PerlNamespaceAnnotations.tryToFindAnnotations(listElement, useStatementElement), Collections.emptyList(), Collections.emptyList(), Collections.emptyMap()));
// making alias
PerlHashEntry aliasEntry = exceptionSettings.get("alias");
if (aliasEntry != null && aliasEntry.isComplete()) {
if (useStatementElement.isAcceptableIdentifierElement(aliasEntry.valueElement)) {
String aliasName = aliasEntry.getValueString();
if (StringUtil.isNotEmpty(aliasName)) {
result.add(new PerlLightSubDefinitionElement<>(useStatementElement, aliasName, LIGHT_SUB_DEFINITION, aliasEntry.getNonNullValueElement(), PerlPackageUtil.getContextNamespaceName(useStatementElement), new ArrayList<>(throwArguments), PerlSubAnnotations.tryToFindAnnotations(aliasEntry.keyElement, aliasEntry.valueElement), UNKNOWN_VALUE_PROVIDER, null));
}
}
}
}
use of com.perl5.lang.perl.psi.impl.PerlUseStatementElement in project Perl5-IDEA by Camelcade.
the class PerlPackageProcessorBase method getImports.
@Override
@NotNull
public List<PerlExportDescriptor> getImports(@NotNull PerlUseStatementElement useStatement) {
String packageName = useStatement.getPackageName();
if (packageName == null) {
return Collections.emptyList();
}
PerlTimeLogger logger = PerlTimeLogger.create(LOG);
List<PerlExportDescriptor> result = new ArrayList<>();
List<String> importParameters = getImportParameters(useStatement);
Set<String> exportNames = new HashSet<>();
Set<String> exportOkNames = new HashSet<>();
addExports(useStatement, exportNames, exportOkNames);
if (importParameters == null) {
exportNames.forEach(name -> result.add(createDescriptor(packageName, name)));
} else {
importParameters.stream().filter(exportOkNames::contains).forEach(name -> result.add(createDescriptor(packageName, name)));
}
logger.debug("Collected imports for ", packageName);
return result;
}
Aggregations