Repository navigation
Expand file tree
/
Copy pathSignatures.java
More file actions
159 lines (137 loc) · 6.44 KB
/
Copy pathSignatures.java
File metadata and controls
159 lines (137 loc) · 6.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package org.javacs;
import com.sun.javadoc.ConstructorDoc;
import com.sun.javadoc.MethodDoc;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodInvocationTree;
import com.sun.source.tree.NewClassTree;
import com.sun.source.tree.Tree;
import com.sun.source.util.JavacTask;
import com.sun.source.util.SourcePositions;
import com.sun.source.util.TreePath;
import com.sun.source.util.Trees;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.lang.model.element.*;
import org.eclipse.lsp4j.ParameterInformation;
import org.eclipse.lsp4j.SignatureHelp;
import org.eclipse.lsp4j.SignatureInformation;
class Signatures {
static Optional<SignatureHelp> help(
FocusedResult compiled, int line, int column, Javadocs docs) {
long offset = compiled.compilationUnit.getLineMap().getPosition(line, column);
return compiled.cursor.flatMap(c -> new Signatures(c, offset, compiled.task, docs).get());
}
private final TreePath cursor;
private final long cursorOffset;
private final JavacTask task;
private final Javadocs docs;
private Signatures(TreePath cursor, long cursorOffset, JavacTask task, Javadocs docs) {
this.cursor = cursor;
this.cursorOffset = cursorOffset;
this.task = task;
this.docs = docs;
}
private Optional<SignatureHelp> get() {
if (cursor.getLeaf().getKind() == Tree.Kind.METHOD_INVOCATION)
return Optional.of(methodHelp((MethodInvocationTree) cursor.getLeaf()));
if (cursor.getLeaf().getKind() == Tree.Kind.NEW_CLASS)
return Optional.of(constructorHelp((NewClassTree) cursor.getLeaf()));
if (cursor.getParentPath().getLeaf().getKind() == Tree.Kind.METHOD_INVOCATION)
return Optional.of(methodHelp((MethodInvocationTree) cursor.getParentPath().getLeaf()));
if (cursor.getParentPath().getLeaf().getKind() == Tree.Kind.NEW_CLASS)
return Optional.of(constructorHelp((NewClassTree) cursor.getParentPath().getLeaf()));
else return Optional.empty();
}
private SignatureHelp constructorHelp(NewClassTree leaf) {
Trees trees = Trees.instance(task);
TreePath identifierPath =
TreePath.getPath(cursor.getCompilationUnit(), leaf.getIdentifier());
Element classElement = trees.getElement(identifierPath);
List<ExecutableElement> candidates =
classElement
.getEnclosedElements()
.stream()
.filter(member -> member.getKind() == ElementKind.CONSTRUCTOR)
.map(method -> (ExecutableElement) method)
.collect(Collectors.toList());
List<SignatureInformation> signatures =
candidates
.stream()
.map(member -> constructorInfo(member))
.collect(Collectors.toList());
int activeSignature = candidates.indexOf(classElement);
return new SignatureHelp(
signatures,
activeSignature < 0 ? null : activeSignature,
activeParameter(leaf.getArguments()));
}
private SignatureHelp methodHelp(MethodInvocationTree leaf) {
Trees trees = Trees.instance(task);
TreePath methodPath = TreePath.getPath(cursor.getCompilationUnit(), leaf.getMethodSelect());
Element methodElement = trees.getElement(methodPath);
Name name = methodElement.getSimpleName();
List<ExecutableElement> candidates =
methodElement
.getEnclosingElement()
.getEnclosedElements()
.stream()
.filter(
member ->
member.getKind() == ElementKind.METHOD
&& member.getSimpleName().equals(name))
.map(method -> (ExecutableElement) method)
.collect(Collectors.toList());
List<SignatureInformation> signatures =
candidates.stream().map(member -> methodInfo(member)).collect(Collectors.toList());
int activeSignature = candidates.indexOf(methodElement);
return new SignatureHelp(
signatures,
activeSignature < 0 ? null : activeSignature,
activeParameter(leaf.getArguments()));
}
private SignatureInformation constructorInfo(ExecutableElement method) {
Optional<ConstructorDoc> doc = docs.constructorDoc(docs.methodKey(method));
Optional<String> docText =
doc.flatMap(constructor -> Optional.ofNullable(constructor.commentText()))
.map(Javadocs::htmlToMarkdown)
.map(Javadocs::firstSentence);
return new SignatureInformation(
Hovers.methodSignature(method, false, true),
docText.orElse(null),
paramInfo(method));
}
private SignatureInformation methodInfo(ExecutableElement method) {
Optional<MethodDoc> doc = docs.methodDoc(docs.methodKey(method));
Optional<String> docText =
doc.flatMap(Javadocs::commentText)
.map(Javadocs::htmlToMarkdown)
.map(Javadocs::firstSentence);
return new SignatureInformation(
Hovers.methodSignature(method, true, true),
docText.orElse(null),
paramInfo(method));
}
private List<ParameterInformation> paramInfo(ExecutableElement method) {
List<ParameterInformation> params = new ArrayList<>();
int i = 0;
for (VariableElement var : method.getParameters()) {
boolean varargs = method.isVarArgs() && i == method.getParameters().size() - 1;
params.add(
new ParameterInformation(
Hovers.shortName(var, varargs), task.getElements().getDocComment(var)));
i++;
}
return params;
}
private Integer activeParameter(List<? extends ExpressionTree> arguments) {
SourcePositions pos = Trees.instance(task).getSourcePositions();
int i = 0;
for (ExpressionTree arg : arguments) {
if (pos.getEndPosition(cursor.getCompilationUnit(), arg) >= cursorOffset) return i;
i++;
}
return null;
}
}