use of com.perl5.lang.perl.psi.PerlMethodDefinition 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;
}
Aggregations