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
|
package org.javacs;
import com.google.gson.Gson;
import java.util.*;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.javacs.lsp.*;
public class CompletionsBase {
protected static final Logger LOG = Logger.getLogger("main");
protected static final JavaLanguageServer server = LanguageServerFixture.getJavaLanguageServer();
protected Set<String> insertTemplate(String file, int row, int column) {
var items = items(file, row, column);
return items.stream().map(CompletionsBase::itemInsertTemplate).collect(Collectors.toSet());
}
static String itemInsertTemplate(CompletionItem i) {
var text = i.insertText;
if (text == null) text = i.label;
assert text != null : "Either insertText or label must be defined";
return text;
}
protected Set<String> insertText(String file, int row, int column) {
var items = items(file, row, column);
return items.stream().map(CompletionsBase::itemInsertText).collect(Collectors.toSet());
}
protected Set<String> detail(String file, int row, int column) {
var items = items(file, row, column);
var result = new HashSet<String>();
for (var i : items) {
i.data = new Gson().toJsonTree(i.data);
var resolved = resolve(i);
result.add(resolved.detail);
}
return result;
}
protected Map<String, Integer> insertCount(String file, int row, int column) {
var items = items(file, row, column);
var result = new HashMap<String, Integer>();
for (var each : items) {
var key = itemInsertText(each);
var count = result.getOrDefault(key, 0) + 1;
result.put(key, count);
}
return result;
}
static String itemInsertText(CompletionItem i) {
var text = i.insertText;
if (text == null) text = i.label;
assert text != null : "Either insertText or label must be defined";
if (text.endsWith("($0)")) text = text.substring(0, text.length() - "($0)".length());
return text;
}
protected Set<String> documentation(String file, int row, int column) {
var items = items(file, row, column);
return items.stream()
.flatMap(
i -> {
if (i.documentation != null) return Stream.of(i.documentation.value.trim());
else return Stream.empty();
})
.collect(Collectors.toSet());
}
protected List<? extends CompletionItem> items(String file, int row, int column) {
var uri = FindResource.uri(file);
var position =
new TextDocumentPositionParams(new TextDocumentIdentifier(uri), new Position(row - 1, column - 1));
return server.completion(position).get().items;
}
protected CompletionItem resolve(CompletionItem item) {
return server.resolveCompletionItem(item);
}
}
|