-
Notifications
You must be signed in to change notification settings - Fork 0
/
DartProposalComputer.java
147 lines (123 loc) · 5.48 KB
/
DartProposalComputer.java
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
package at.bestsolution.dart.editor.services.complete;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.PreDestroy;
import javax.inject.Inject;
import org.eclipse.fx.code.editor.Input;
import org.eclipse.fx.code.editor.LocalFile;
import org.eclipse.fx.code.editor.services.CompletionProposal;
import org.eclipse.fx.code.editor.services.ContextInformation;
import org.eclipse.fx.code.editor.services.EditingContext;
import org.eclipse.fx.code.editor.services.ProposalComputer;
import org.eclipse.fx.code.editor.services.URIProvider;
import org.eclipse.fx.core.log.Log;
import org.eclipse.fx.core.log.Logger;
import org.eclipse.jface.text.IDocument;
import at.bestsolution.dart.server.api.DartServer;
import at.bestsolution.dart.server.api.Registration;
import at.bestsolution.dart.server.api.model.CompletionGetSuggestionsResult;
import at.bestsolution.dart.server.api.model.CompletionResultsNotification;
import at.bestsolution.dart.server.api.model.CompletionSuggestion;
import at.bestsolution.dart.server.api.model.CompletionSuggestionKind;
import at.bestsolution.dart.server.api.model.ElementKind;
import at.bestsolution.dart.server.api.services.ServiceCompletion;
@SuppressWarnings("restriction")
public class DartProposalComputer implements ProposalComputer {
private final Input<?> input;
private final EditingContext editingContext;
private String requestId;
private ServiceCompletion completionService;
private Registration proposalRegistration;
private CompletableFuture<List<CompletionProposal>> future;
private List<CompletionProposal> completions = new ArrayList<>();
private Logger logger;
@Inject
public DartProposalComputer(IDocument document, Input<?> input, EditingContext editingContext, DartServer server, @Log Logger logger) {
this.logger = logger;
this.logger.debug("Creating proposal computer");
this.input = input;
this.editingContext = editingContext;
completionService = server.getService(ServiceCompletion.class);
proposalRegistration = completionService.results(this::handleHandleResults);
}
private synchronized void handleHandleResults(CompletionResultsNotification notification) {
System.err.println("handleResult("+notification.getId()+")");
if( requestId != null && requestId.equals(notification.getId()) ) {
completions.addAll(Stream.of(notification.getResults()).map( e -> mapToCompletion(notification, e)).collect(Collectors.toList()));
if( notification.getIsLast() ) {
List<CompletionProposal> tmp = completions;
completions = new ArrayList<>();
if( ! future.isDone() ) {
Collections.sort(tmp, new Comparator<CompletionProposal>() {
@Override
public int compare(CompletionProposal o1, CompletionProposal o2) {
return ((DartCompletionProposal)o1).compareTo((DartCompletionProposal) o2);
}
});
// for (CompletionProposal p : tmp) {
// System.err.println(" * " + p.getLabel() + " -> " + p.getContextInformation());
// }
future.complete(tmp);
} else {
System.err.println("Received informations after the last item has been sent");
}
}
}
else {
System.err.println("dropping result for request " + notification.getId());
}
}
private ContextInformation createContextInformation(CompletionResultsNotification notification, CompletionSuggestion proposal) {
// System.err.println(" - " + proposal.getCompletion());
// System.err.println("kind: " + proposal.getKind());
// System.err.println(proposal.getHasNamedParameters());
// System.err.println(proposal.getElement());
if (proposal.getElement() != null && proposal.getElement().getKind() == ElementKind.METHOD) {
return new ContextInformation.BaseContextInformation(0, proposal.getElement().getParameters());
}
if (proposal.getKind() == CompletionSuggestionKind.ARGUMENT_LIST) {
String paramString = "";
for (int i = 0; i < proposal.getParameterTypes().length; i++) {
paramString += proposal.getParameterTypes()[i] + " " + proposal.getParameterNames()[i];
if (i < proposal.getParameterTypes().length - 1) {
paramString +=", ";
}
}
return new ContextInformation.BaseContextInformation(0, paramString);
}
return null;
}
private DartCompletionProposal mapToCompletion(CompletionResultsNotification notification, CompletionSuggestion proposal) {
// System.err.println("notification " + notification.getReplacementOffset() + " - " + notification.getReplacementLength());
// System.err.println("mapping " + proposal);
// System.err.println(" el: " + proposal.getElement());
//
// System.err.println(" " + proposal.getSelectionOffset() + " - " + proposal.getSelectionLength());
return new DartCompletionProposal(notification, proposal, createContextInformation(notification, proposal));
}
@Override
public CompletableFuture<List<CompletionProposal>> compute() {
System.err.println("compute " + this);
LocalFile p = (LocalFile) this.input;
System.err.println(p.getPath());
Path file = p.getPath().toAbsolutePath();
CompletionGetSuggestionsResult result = completionService.getSuggestions(file.toString(), editingContext.getCaretOffset());
requestId = result.getId();
future = new CompletableFuture<>();
return future;
}
@PreDestroy
void cleanup() {
if( proposalRegistration != null ) {
proposalRegistration.dispose();
}
}
}