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
|
package org.javacs;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.eclipse.lsp4j.CompletionItem;
import org.eclipse.lsp4j.CompletionParams;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.TextDocumentIdentifier;
public class CompletionsBase {
protected static final Logger LOG = Logger.getLogger("main");
protected Set<String> insertTemplate(String file, int row, int column) throws IOException {
var items = items(file, row, column);
return items.stream().map(CompletionsBase::itemInsertTemplate).collect(Collectors.toSet());
}
static String itemInsertTemplate(CompletionItem i) {
var text = i.getInsertText();
if (text == null) text = i.getLabel();
assert text != null : "Either insertText or label must be defined";
return text;
}
protected Set<String> insertText(String file, int row, int column) throws IOException {
var items = items(file, row, column);
return items.stream().map(CompletionsBase::itemInsertText).collect(Collectors.toSet());
}
protected Map<String, Integer> insertCount(String file, int row, int column) throws IOException {
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.getInsertText();
if (text == null) text = i.getLabel();
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) throws IOException {
var items = items(file, row, column);
return items.stream()
.flatMap(
i -> {
if (i.getDocumentation() != null)
return Stream.of(i.getDocumentation().getRight().getValue().trim());
else return Stream.empty();
})
.collect(Collectors.toSet());
}
protected static final JavaLanguageServer server = LanguageServerFixture.getJavaLanguageServer();
protected List<? extends CompletionItem> items(String file, int row, int column) {
var uri = FindResource.uri(file);
var position =
new CompletionParams(new TextDocumentIdentifier(uri.toString()), new Position(row - 1, column - 1));
try {
return server.getTextDocumentService().completion(position).get().getRight().getItems();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
}
}
|