diff --git a/java-completion/build.gradle b/java-completion/build.gradle index 2438722a3..4ecc8afeb 100644 --- a/java-completion/build.gradle +++ b/java-completion/build.gradle @@ -36,8 +36,12 @@ dependencies { androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' - implementation 'androidx.annotation:annotation:1.3.0' + + // used for code generation + implementation 'com.github.javaparser:javaparser-core:3.23.1' + + // fuzzy search implementation 'me.xdrop:fuzzywuzzy:1.3.1' implementation project(path: ':common') implementation project(path: ':completion-api') diff --git a/java-completion/src/main/java/com/tyron/completion/java/rewrite/EditHelper.java b/java-completion/src/main/java/com/tyron/completion/java/rewrite/EditHelper.java index 465641c6b..f5c3bf6c2 100644 --- a/java-completion/src/main/java/com/tyron/completion/java/rewrite/EditHelper.java +++ b/java-completion/src/main/java/com/tyron/completion/java/rewrite/EditHelper.java @@ -2,6 +2,10 @@ import androidx.annotation.NonNull; +import com.github.javaparser.ast.Modifier.Keyword; +import com.github.javaparser.ast.NodeList; +import com.github.javaparser.ast.body.MethodDeclaration; +import com.github.javaparser.ast.body.Parameter; import com.tyron.completion.java.util.ActionUtil; import com.tyron.completion.model.Position; import com.tyron.completion.model.Range; @@ -54,25 +58,26 @@ public TextEdit removeTree(CompilationUnitTree root, Tree remove) { } /** - * Prints a given method into a String, adds {@code throws UnsupportedOperationException} to the method body - * if the source is null, it will get the parameter names from the class file which will be {@code arg1, arg2, arg3} - * @param method method to print + * Prints a given method into a String, adds {@code throws UnsupportedOperationException} to + * the method body + * if the source is null, it will get the parameter names from the class file which will be + * {@code arg1, arg2, arg3} + * + * @param method method to print * @param parameterizedType type parameters of this method - * @param source the source method, in which the parameter names are fetched + * @param source the source method, in which the parameter names are fetched * @return a string that represents the method */ - public static String printMethod(ExecutableElement method, ExecutableType parameterizedType, MethodTree source) { + public static String printMethod(ExecutableElement method, ExecutableType parameterizedType, + MethodTree source) { StringBuilder buf = new StringBuilder(); - buf.append("@Override\n"); - if (method.getModifiers().contains(Modifier.PUBLIC)) { - buf.append("public "); - } - if (method.getModifiers().contains(Modifier.PROTECTED)) { - buf.append("protected "); - } - - buf.append(EditHelper.printType(parameterizedType.getReturnType())).append(" "); + MethodDeclaration methodDeclaration = new MethodDeclaration(); + methodDeclaration.addAndGetAnnotation(Override.class); + methodDeclaration.setModifiers(method.getModifiers().stream() + .map(it -> Keyword.valueOf(it.toString())) + .toArray(Keyword[]::new)); + methodDeclaration.setType(EditHelper.printType(parameterizedType.getReturnType())); buf.append(method.getSimpleName()).append("("); if (source == null) { buf.append(printParameters(parameterizedType, method)); @@ -85,7 +90,8 @@ public static String printMethod(ExecutableElement method, ExecutableType parame return buf.toString(); } - public static String printMethod(ExecutableElement method, ExecutableType parameterizedType, ExecutableElement source) { + public static String printMethod(ExecutableElement method, ExecutableType parameterizedType, + ExecutableElement source) { StringBuilder buf = new StringBuilder(); buf.append("@Override\n"); if (method.getModifiers().contains(Modifier.PUBLIC)) { @@ -117,8 +123,7 @@ public static String printMethod(ExecutableElement method, ExecutableType parame public static String printThrows(@NonNull List thrownTypes) { StringBuilder types = new StringBuilder(); for (TypeMirror m : thrownTypes) { - types.append((types.length() == 0) ? "" : ", ") - .append(printType(m)); + types.append((types.length() == 0) ? "" : ", ").append(printType(m)); } return "throws " + types; } @@ -133,11 +138,9 @@ public static String printBody(ExecutableElement method, MethodTree source) { } else { String names; if (source != null) { - names = source.getParameters().stream().map(VariableTree::getName) - .map(Name::toString).collect(Collectors.joining(", ")); + names = source.getParameters().stream().map(VariableTree::getName).map(Name::toString).collect(Collectors.joining(", ")); } else { - names = method.getParameters().stream().map(VariableElement::getSimpleName) - .map(Name::toString).collect(Collectors.joining(", ")); + names = method.getParameters().stream().map(VariableElement::getSimpleName).map(Name::toString).collect(Collectors.joining(", ")); } body = "super." + method.getSimpleName() + "(" + names + ");"; } @@ -146,13 +149,16 @@ public static String printBody(ExecutableElement method, MethodTree source) { } switch (returnType.getKind()) { - case VOID: return ""; + case VOID: + return ""; case SHORT: case CHAR: case FLOAT: case BYTE: - case INT: return "return 0;"; - case BOOLEAN: return "return false;"; + case INT: + return "return 0;"; + case BOOLEAN: + return "return false;"; default: return "return null;"; } @@ -168,11 +174,9 @@ public static String printBody(ExecutableElement method, ExecutableElement sourc } else { String names; if (source != null) { - names = source.getParameters().stream().map(VariableElement::getSimpleName) - .map(Name::toString).collect(Collectors.joining(", ")); + names = source.getParameters().stream().map(VariableElement::getSimpleName).map(Name::toString).collect(Collectors.joining(", ")); } else { - names = method.getParameters().stream().map(VariableElement::getSimpleName) - .map(Name::toString).collect(Collectors.joining(", ")); + names = method.getParameters().stream().map(VariableElement::getSimpleName).map(Name::toString).collect(Collectors.joining(", ")); } body = "super." + method.getSimpleName() + "(" + names + ");"; } @@ -181,13 +185,16 @@ public static String printBody(ExecutableElement method, ExecutableElement sourc } switch (returnType.getKind()) { - case VOID: return ""; + case VOID: + return ""; case SHORT: case CHAR: case FLOAT: case BYTE: - case INT: return "return 0;"; - case BOOLEAN: return "return false;"; + case INT: + return "return 0;"; + case BOOLEAN: + return "return false;"; default: return "return null;"; } @@ -195,6 +202,7 @@ public static String printBody(ExecutableElement method, ExecutableElement sourc /** * Prints parameters given the source method that contains parameter names + * * @param method element from the .class file * @param source element from the .java file * @return Formatted string that represents the methods parameters with proper names @@ -213,6 +221,7 @@ public static String printParameters(ExecutableType method, MethodTree source) { /** * Prints parameters with the default names eg. {@code arg0, arg1} * this is used when the source file of the class isn't found + * * @param method element to print * @param source the class file of the method * @return Formatted String that represents the parameters of this method @@ -313,7 +322,8 @@ public static Position insertAfter(JavacTask task, CompilationUnitTree root, Tre return new Position(line, 0); } - public static Position insertAtEndOfClass(JavacTask task, CompilationUnitTree root, ClassTree leaf) { + public static Position insertAtEndOfClass(JavacTask task, CompilationUnitTree root, + ClassTree leaf) { SourcePositions pos = Trees.instance(task).getSourcePositions(); LineMap lines = root.getLineMap(); long end = pos.getEndPosition(root, leaf);