use of com.perl5.lang.perl.psi.PerlSubElement in project Perl5-IDEA by Camelcade.
the class PerlDocumentationProvider method getCustomDocumentationElement.
@Nullable
@Override
public PsiElement getCustomDocumentationElement(@NotNull Editor editor, @NotNull PsiFile file, @Nullable PsiElement contextElement) {
if (contextElement == null || contextElement.getLanguage() != PerlLanguage.INSTANCE) {
return null;
}
IElementType elementType = contextElement.getNode().getElementType();
if (contextElement instanceof PerlVariable) {
// fixme try to search doc in package or declaration
return PerlDocUtil.getPerlVarDoc((PerlVariable) contextElement);
} else if (elementType == REGEX_MODIFIER) {
return PerlDocUtil.getRegexModifierDoc(contextElement);
} else if (elementType == REGEX_TOKEN) {
return PerlDocUtil.resolveDocLink("perlretut", contextElement);
} else if (elementType == VERSION_ELEMENT) {
return PerlDocUtil.resolveDocLink("perldata/\"Version Strings\"", contextElement);
} else if (isFunc(contextElement)) {
return PerlDocUtil.getPerlFuncDoc(contextElement);
} else if (isOp(contextElement)) {
return PerlDocUtil.getPerlOpDoc(contextElement);
} else if (contextElement instanceof PerlSubNameElement) {
String packageName = ((PerlSubNameElement) contextElement).getPackageName();
String subName = ((PerlSubNameElement) contextElement).getName();
if (StringUtil.isNotEmpty(subName)) {
PsiElement result = null;
// search by link
if (StringUtil.isNotEmpty(packageName) && !StringUtil.equals(PerlPackageUtil.MAIN_PACKAGE, packageName)) {
result = PerlDocUtil.resolveDocLink(packageName + "/" + ((PerlSubNameElement) contextElement).getName(), contextElement);
}
// not found or main::
if (result == null) {
PsiElement target = null;
if (contextElement.getParent() instanceof PerlSubElement) {
target = contextElement.getParent();
} else {
PsiReference reference = contextElement.getReference();
if (reference != null) {
target = reference.resolve();
}
}
if (target != null) {
PsiFile targetFile = target.getContainingFile();
if (targetFile != null) {
PsiFile podFile = targetFile.getViewProvider().getPsi(PodLanguage.INSTANCE);
if (podFile != null) {
result = PerlDocUtil.searchPodElement(targetFile, PodDocumentPattern.headingAndItemPattern(subName));
}
}
}
}
if (result != null) {
return result;
}
}
} else if (contextElement instanceof PerlNamespaceElement) {
String packageName = ((PerlNamespaceElement) contextElement).getCanonicalName();
if (StringUtil.equals(PerlPackageUtil.SUPER_PACKAGE, packageName)) {
return PerlDocUtil.resolveDocLink("perlobj/Inheritance", contextElement);
} else if (StringUtil.isNotEmpty(packageName)) {
return PerlDocUtil.resolveDocLink(packageName, contextElement);
}
}
return super.getCustomDocumentationElement(editor, file, contextElement);
}
use of com.perl5.lang.perl.psi.PerlSubElement in project Perl5-IDEA by Camelcade.
the class PerlPackageUtil method processParentClassesSubs.
public static void processParentClassesSubs(PerlNamespaceDefinitionElement childClass, Set<String> processedSubsNames, Set<PerlNamespaceDefinitionElement> recursionMap, Processor<PerlSubElement> processor) {
if (childClass == null || recursionMap.contains(childClass)) {
return;
}
recursionMap.add(childClass);
for (PerlNamespaceDefinitionElement parentNamespace : childClass.getParentNamespaceDefinitions()) {
for (PsiElement subDefinitionBase : collectNamespaceSubs(parentNamespace)) {
ProgressManager.checkCanceled();
String subName = ((PerlSubElement) subDefinitionBase).getSubName();
if (subDefinitionBase.isValid() && ((PerlSubElement) subDefinitionBase).isMethod() && !processedSubsNames.contains(subName)) {
processedSubsNames.add(subName);
processor.process(((PerlSubElement) subDefinitionBase));
}
}
processParentClassesSubs(parentNamespace, processedSubsNames, recursionMap, processor);
}
}
use of com.perl5.lang.perl.psi.PerlSubElement in project Perl5-IDEA by Camelcade.
the class PerlRenamePolyReferencedElementProcessor method prepareRenaming.
@Override
public void prepareRenaming(PsiElement element, String newName, Map<PsiElement, String> allRenames, SearchScope scope) {
final String currentBaseName = ((PsiNameIdentifierOwner) element).getName();
if (currentBaseName != null && StringUtil.isNotEmpty(newName)) {
boolean globScanned = element instanceof PerlGlobVariable;
for (PsiReference reference : ReferencesSearch.search(element, element.getUseScope()).findAll()) {
if (reference instanceof PsiPolyVariantReference) {
for (ResolveResult resolveResult : ((PsiPolyVariantReference) reference).multiResolve(false)) {
PsiElement resolveResultElement = resolveResult.getElement();
if (!allRenames.containsKey(resolveResultElement)) {
allRenames.put(resolveResultElement, newName);
if (!globScanned && resolveResultElement instanceof PerlGlobVariable) {
globScanned = true;
prepareRenaming(resolveResultElement, newName, allRenames, scope);
}
}
}
}
processDocReference(currentBaseName, newName, reference, allRenames);
}
if (element instanceof PerlSubElement && ((PerlSubElement) element).isMethod()) {
for (PerlSubElement overridingSub : PerlSubUtil.collectOverridingSubs((PerlSubElement) element)) {
allRenames.put(overridingSub, newName);
}
}
// following is the hack until #1730 is fixed
Set<PsiElement> allElements = new THashSet<>(allRenames.keySet());
allElements.stream().filter(e -> e instanceof PerlClassAccessorMethod).forEach(e -> {
PerlClassAccessorMethod pairedMethod = ((PerlClassAccessorMethod) e).getPairedMethod();
if (pairedMethod != null && allRenames.containsKey(e)) {
allRenames.remove(pairedMethod);
}
});
}
}
use of com.perl5.lang.perl.psi.PerlSubElement in project Perl5-IDEA by Camelcade.
the class PerlCodeGeneratorImpl method getOverrideCodeText.
@Nullable
@Override
public String getOverrideCodeText(PsiElement subBase) {
if (subBase instanceof PerlSubElement) {
PerlSubElement perlSubBase = (PerlSubElement) subBase;
StringBuilder code = new StringBuilder();
code.append("#@override\n");
PerlSubAnnotations annotations = perlSubBase.getAnnotations();
if (annotations != null) {
if (annotations.isDeprecated()) {
code.append("#@deprecated\n");
}
if (annotations.isAbstract()) {
code.append("#@abstract\n");
}
if (annotations.isMethod() || subBase instanceof PerlMethodDefinition) {
code.append("#@method\n");
}
if (StringUtil.isNotEmpty(annotations.getReturns())) {
code.append("#@returns ");
code.append(annotations.getReturns());
code.append("\n");
}
}
code.append("sub ");
code.append(perlSubBase.getSubName());
code.append("{\n");
List<String> superArgs = new ArrayList<>();
List<PerlSubArgument> arguments = Collections.emptyList();
if (perlSubBase instanceof PerlSubDefinitionElement) {
// noinspection unchecked
arguments = ((PerlSubDefinitionElement) perlSubBase).getSubArgumentsList();
if (!arguments.isEmpty()) {
boolean useShift = false;
for (PerlSubArgument argument : arguments) {
if (StringUtil.isNotEmpty(argument.getVariableClass())) {
useShift = true;
break;
}
}
if (useShift) {
for (PerlSubArgument argument : arguments) {
if (!argument.isEmpty()) {
code.append("my ");
code.append(argument.getVariableClass());
code.append(" ");
String superArg = argument.toStringShort();
superArgs.add(superArg);
code.append(superArg);
code.append(" = ");
}
code.append("shift;\n");
}
} else {
code.append("my ");
code.append('(');
boolean insertComma = false;
for (PerlSubArgument argument : arguments) {
if (insertComma) {
code.append(", ");
} else {
insertComma = true;
}
String superArg = argument.toStringShort();
superArgs.add(superArg);
code.append(superArg);
}
code.append(") = @_;\n");
}
} else {
code.append("my ($self) = @_;\n");
}
}
if (!superArgs.isEmpty()) {
superArgs.remove(0);
}
if (!arguments.isEmpty() && !arguments.get(0).isEmpty()) {
// noinspection StringConcatenationInsideStringBufferAppend
code.append(arguments.get(0).toStringShort() + "->SUPER::" + perlSubBase.getSubName() + "(" + StringUtil.join(superArgs, ", ") + ");\n");
}
code.append("}");
return code.toString();
}
return null;
}
use of com.perl5.lang.perl.psi.PerlSubElement in project Perl5-IDEA by Camelcade.
the class PerlSubReferenceSimple method getResolveResults.
@NotNull
public List<ResolveResult> getResolveResults(List<PsiElement> relatedItems) {
List<ResolveResult> result = new ArrayList<>();
resetFlags();
for (PsiElement element : relatedItems) {
if (!isAutoloaded() && element instanceof PerlIdentifierOwner && PerlSubUtil.SUB_AUTOLOAD.equals(((PerlIdentifierOwner) element).getName())) {
setAutoloaded();
}
if (!isConstant() && element instanceof PerlLightConstantDefinitionElement) {
setConstant();
}
if (!isDeclared() && element instanceof PerlSubDeclarationElement) {
setDeclared();
}
if (!isDefined() && element instanceof PerlSubDefinitionElement) {
setDefined();
}
if (!isXSub() && element instanceof PerlSubElement && ((PerlSubElement) element).isXSub()) {
setXSub();
}
if (!isAliased() && element instanceof PerlGlobVariable) {
setAliased();
}
result.add(new PsiElementResolveResult(element));
}
return result;
}
Aggregations