Skip to content

Commit

Permalink
feat: implement abstract methods will now print proper generic types
Browse files Browse the repository at this point in the history
  • Loading branch information
tyron12233 committed Dec 28, 2021
1 parent 8e10c26 commit 69374ce
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
import org.openjdk.tools.javac.api.JavacTaskImpl;
import org.openjdk.tools.javac.code.Type;
import org.openjdk.tools.javac.tree.JCTree;

import com.tyron.builder.model.SourceFileObject;
import com.tyron.common.util.StringSearch;
import com.tyron.completion.java.CompileTask;
import com.tyron.completion.java.JavaCompilerService;
import com.tyron.completion.java.ParseTask;
import com.tyron.completion.java.rewrite.EditHelper;
import com.tyron.completion.java.util.ActionUtil;
import com.tyron.completion.java.util.ElementUtil;
import com.tyron.completion.model.CompletionItem;
import com.tyron.completion.model.CompletionList;
Expand Down Expand Up @@ -529,16 +531,20 @@ public List<CompletionItem> addAnonymous(CompileTask task, TreePath path, String
if (StringSearch.matchesPartialName(classElement.getSimpleName().toString(),
partial)) {
CompletionItem item = new CompletionItem();
item.iconKind = DrawableKind.Interface;
item.label = classElement.getSimpleName().toString() + " {...}";
item.commitText = "" + classElement.getSimpleName() + "() {\n\t//TODO:\n}";
item.cursorOffset = item.commitText.length();
item.detail = "";

StringBuilder sb = new StringBuilder();
if (classElement instanceof TypeElement) {
TypeElement typeElement = (TypeElement) classElement;
// import the class
item.action = CompletionItem.Kind.IMPORT;
item.data = ((TypeElement) classElement).getQualifiedName().toString();
item.data = typeElement.getQualifiedName().toString();
item.iconKind = DrawableKind.Interface;
item.label = classElement.getSimpleName().toString() + " {...}";
item.commitText = "" + classElement.getSimpleName() + "() {\n" +
"\t// TODO\n" +
"}";
item.cursorOffset = item.commitText.length();
item.detail = "";
}
items.add(item);
}
Expand Down Expand Up @@ -568,7 +574,8 @@ private void addStaticImports(CompileTask task, CompilationUnitTree root, String
methods.clear();
putMethod((ExecutableElement) member, methods);
for (List<ExecutableElement> overloads : methods.values()) {
list.items.addAll(method(task, overloads, endsWithParen, false, (DeclaredType) type.asType()));
list.items.addAll(method(task, overloads, endsWithParen, false,
(DeclaredType) type.asType()));
}
} else {
list.items.add(item(member));
Expand Down Expand Up @@ -837,7 +844,8 @@ private List<CompletionItem> overridableMethod(CompileTask task, TreePath parent

Element enclosingElement = element.getEnclosingElement();
if (!types.isAssignable(type, enclosingElement.asType())) {
items.addAll(method(task, Collections.singletonList(element), endsWithParen, false, type));
items.addAll(method(task, Collections.singletonList(element), endsWithParen,
false, type));
continue;
}

Expand All @@ -854,8 +862,9 @@ private List<CompletionItem> overridableMethod(CompileTask task, TreePath parent
return items;
}

private List<CompletionItem> method(CompileTask task, List<ExecutableElement> overloads, boolean endsWithParen,
boolean methodRef, DeclaredType type) {
private List<CompletionItem> method(CompileTask task, List<ExecutableElement> overloads,
boolean endsWithParen, boolean methodRef,
DeclaredType type) {
checkCanceled();
List<CompletionItem> items = new ArrayList<>();
Types types = task.task.getTypes();
Expand All @@ -866,23 +875,24 @@ private List<CompletionItem> method(CompileTask task, List<ExecutableElement> ov
return items;
}

private CompletionItem method(ExecutableElement first, boolean endsWithParen, boolean methodRef, ExecutableType type) {
private CompletionItem method(ExecutableElement first, boolean endsWithParen,
boolean methodRef, ExecutableType type) {
CompletionItem item = new CompletionItem();
item.label = getMethodLabel(first, type) + getThrowsType(first);
item.commitText = first.getSimpleName().toString() + ((methodRef || endsWithParen) ?
"" : "()");
item.commitText = first.getSimpleName().toString() + ((methodRef || endsWithParen) ? "" :
"()");
item.detail = simpleType(type.getReturnType());
item.iconKind = DrawableKind.Method;
item.cursorOffset = item.commitText.length();
if (first.getParameters() != null && !first.getParameters().isEmpty()) {
item.cursorOffset = item.commitText.length() - ((methodRef || endsWithParen) ? 0
: 1);
item.cursorOffset = item.commitText.length() - ((methodRef || endsWithParen) ? 0 : 1);
}
return item;
}

private List<CompletionItem> method(CompileTask task, List<ExecutableElement> overloads, boolean endsWithParen,
boolean methodRef, ExecutableType type) {
private List<CompletionItem> method(CompileTask task, List<ExecutableElement> overloads,
boolean endsWithParen, boolean methodRef,
ExecutableType type) {
checkCanceled();
List<CompletionItem> items = new ArrayList<>();
for (ExecutableElement first : overloads) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.openjdk.javax.tools.JavaFileObject;
import org.openjdk.source.tree.ClassTree;
import org.openjdk.source.tree.MethodTree;
import org.openjdk.source.util.TreePath;
import org.openjdk.source.util.Trees;
import org.openjdk.tools.javac.util.JCDiagnostic;

Expand Down Expand Up @@ -87,7 +88,6 @@ public Map<Path, TextEdit[]> rewrite(CompilerProvider compiler) {
Types types = task.task.getTypes();
Trees trees = Trees.instance(task.task);
TypeElement thisClass = elements.getTypeElement(mClassName);
DeclaredType thisType = (DeclaredType) thisClass.asType();
ClassTree thisTree = trees.getTree(thisClass);
if (mPosition != 0) {
thisTree = new FindTypeDeclarationAt(task.task).scan(task.root(), mPosition);
Expand All @@ -96,6 +96,10 @@ public Map<Path, TextEdit[]> rewrite(CompilerProvider compiler) {
thisTree = new FindNewTypeDeclarationAt(task.task, task.root()).scan(task.root(),
mPosition);
}
TreePath path = trees.getPath(task.root(), thisTree);
Element element = trees.getElement(path);
DeclaredType thisType = (DeclaredType) element.asType();

int indent = EditHelper.indent(task.task, task.root(), thisTree) + 4;

for (Element member : elements.getAllMembers(thisClass)) {
Expand Down

0 comments on commit 69374ce

Please sign in to comment.