/
PresentationCompiler.java
333 lines (283 loc) Β· 11.1 KB
/
PresentationCompiler.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package scala.meta.pc;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.CompletionItem;
import org.eclipse.lsp4j.CompletionList;
import org.eclipse.lsp4j.DocumentHighlight;
import org.eclipse.lsp4j.SignatureHelp;
import org.eclipse.lsp4j.TextEdit;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.ExecuteCommandParams;
import org.eclipse.lsp4j.DocumentSymbol;
import org.eclipse.lsp4j.DocumentRangeFormattingParams;
import org.eclipse.lsp4j.DocumentOnTypeFormattingParams;
import org.eclipse.lsp4j.InlayHint;
import org.eclipse.lsp4j.SelectionRange;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import java.net.URI;
import java.nio.file.Path;
import java.util.Collections;
import java.util.concurrent.CompletableFuture;
import java.util.List;
import java.util.LinkedList;
import java.util.Optional;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;
/**
* The public API of the presentation compiler.
*
* This API should remain
*/
public abstract class PresentationCompiler {
// ==============================
// Language Server Protocol APIs.
// ==============================
/**
* Returns token informations from presentation compiler.
*
*/
public CompletableFuture<List<Node>> semanticTokens(VirtualFileParams params) {
return CompletableFuture.completedFuture(Collections.emptyList());
}
/**
* Returns code completions for the given source position.
*
* The returned completion items are incomplete meaning they may not contain
* available information such as documentation or the detail signature may have
* `x$1` parameter names for Java methods. It's recommended to call
* {@link #completionItemResolve(CompletionItem, String)} before displaying the
* `detail` and `documentation` fields to the user.
*
* @implNote supports cancellation.
*/
public abstract CompletableFuture<CompletionList> complete(OffsetParams params);
/**
* Returns a fully resolved completion item with defined fields such as
* `documentation` and `details` populated.
*
* @implNote does not support cancellation.
*/
public abstract CompletableFuture<CompletionItem> completionItemResolve(CompletionItem item, String symbol);
/**
* Returns the parameter hints at the given source position.
*
* @implNote supports cancellation.
*/
public abstract CompletableFuture<SignatureHelp> signatureHelp(OffsetParams params);
/**
* Returns the type of the expression at the given position along with the
* symbol of the referenced symbol.
*/
public abstract CompletableFuture<Optional<HoverSignature>> hover(OffsetParams params);
/**
* Checks if the symbol at given position can be renamed using presentation
* compiler. Returns Some(range) if symbol is defined inside a method or the
* file is a worksheet, None otherwise
*/
public abstract CompletableFuture<Optional<Range>> prepareRename(OffsetParams params);
/**
* Renames the symbol at given position and all of its occurrences to `name`. If
* symbol can't be renamed using presentation compiler (prepareRename returns
* None), returns empty list.
*/
public abstract CompletableFuture<List<TextEdit>> rename(OffsetParams params, String name);
/**
* Returns the definition of the symbol at the given position.
*/
public abstract CompletableFuture<DefinitionResult> definition(OffsetParams params);
/**
* Returns location of the expression's type definition at the given position.
*/
public abstract CompletableFuture<DefinitionResult> typeDefinition(OffsetParams params);
/**
* Returns the occurrences of the symbol under the current position in the
* entire file.
*/
public abstract CompletableFuture<java.util.List<DocumentHighlight>> documentHighlight(OffsetParams params);
/**
* Return decoded and pretty printed TASTy content for .scala or .tasty file.
*
*/
public abstract CompletableFuture<String> getTasty(URI targetUri, boolean isHttpEnabled);
/**
* Return the necessary imports for a symbol at the given position.
*/
public abstract CompletableFuture<List<AutoImportsResult>> autoImports(String name, OffsetParams params,
Boolean isExtension);
/**
* Return the missing implements and imports for the symbol at the given
* position.
*/
public abstract CompletableFuture<List<TextEdit>> implementAbstractMembers(OffsetParams params);
/**
* Return the missing implements and imports for the symbol at the given
* position.
*/
public abstract CompletableFuture<List<TextEdit>> insertInferredType(OffsetParams params);
/**
* Return the text edits for inlining a value.
*/
public CompletableFuture<List<TextEdit>> inlineValue(OffsetParams params) {
return CompletableFuture.supplyAsync(() -> {
throw new DisplayableException("Inline value is not available in this version of Scala");
});
}
/**
* Extract method in selected range
*
* @param range range to extract from
* @param extractionPos position in file to extract to
*/
public abstract CompletableFuture<List<TextEdit>> extractMethod(RangeParams range, OffsetParams extractionPos);
/**
* Return named arguments for the apply method that encloses the given position.
* May fail with a DisplayableException.
*/
public abstract CompletableFuture<List<TextEdit>> convertToNamedArguments(OffsetParams params,
List<Integer> argIndices);
/**
* The text contents of the given file changed.
*/
public abstract CompletableFuture<List<Diagnostic>> didChange(VirtualFileParams params);
/**
* Returns decorations for missing type annotations, inferred type parameters, implicit parameters and conversions.
*/
public CompletableFuture<List<InlayHint>> inlayHints(InlayHintsParams params) {
return CompletableFuture.completedFuture(Collections.emptyList());
}
/**
* Returns decorations for missing type adnotations, inferred type parameters, implicit parameters and conversions.
*/
public CompletableFuture<List<SyntheticDecoration>> syntheticDecorations(SyntheticDecorationsParams params) {
return CompletableFuture.completedFuture(Collections.emptyList());
}
public CompletableFuture<Optional<PcSymbolInformation>> info(String symbol) {
return CompletableFuture.completedFuture(Optional.empty());
}
/**
* File was closed.
*/
public abstract void didClose(URI uri);
/**
* Returns the Protobuf byte array representation of a SemanticDB
* <code>TextDocument</code> for the given source.
*/
public abstract CompletableFuture<byte[]> semanticdbTextDocument(URI filename, String code);
/**
* Returns the Protobuf byte array representation of a SemanticDB
* <code>TextDocument</code> for the given source.
*/
public CompletableFuture<byte[]> semanticdbTextDocument(VirtualFileParams params) {
return semanticdbTextDocument(params.uri(), params.text());
}
/**
* Return the selections ranges for the given positions.
*/
public abstract CompletableFuture<List<SelectionRange>> selectionRange(List<OffsetParams> params);
// =================================
// Configuration and lifecycle APIs.
// =================================
/**
* Clean up resources and shutdown the presentation compiler.
*
* It is necessary to call this method in order to for example stop the
* presentation compiler thread. If this method is not called, then the JVM may
* not shut exit cleanly.
*
* This presentation compiler instance should no longer be used after calling
* this method.
*/
public abstract void shutdown();
/**
* Clean the symbol table and other mutable state in the compiler.
*/
public abstract void restart();
/**
* Set logger level for reports.
*/
public PresentationCompiler withReportsLoggerLevel(String level) {
return this;
};
/**
* Set build target name.
*/
public PresentationCompiler withBuildTargetName(String buildTargetName) {
return this;
};
/**
* Provide a SymbolSearch to extract docstrings, java parameter names and Scala
* default parameter values.
*/
public abstract PresentationCompiler withSearch(SymbolSearch search);
/**
* Provide a custom executor service to run asynchronous cancellation or
* requests.
*/
public abstract PresentationCompiler withExecutorService(ExecutorService executorService);
/**
* Provide a custom scheduled executor service to schedule `Thread.stop()` for
* unresponsive compiler instances.
*/
public abstract PresentationCompiler withScheduledExecutorService(
ScheduledExecutorService scheduledExecutorService);
/**
* Provide custom configuration for features like signature help and
* completions.
*/
public abstract PresentationCompiler withConfiguration(PresentationCompilerConfig config);
/**
* Provide workspace root for features like ammonite script $file completions.
*/
public abstract PresentationCompiler withWorkspace(Path workspace);
/**
* Construct a new presentation compiler with the given parameters.
*
* @param buildTargetIdentifier the build target containing this source file.
* This is needed for
* {@link #completionItemResolve(CompletionItem, String)}.
* @param classpath the classpath of this build target.
* @param options the compiler flags for the new compiler.
* Important, it is recommended to disable all
* compiler plugins excluding
* org.scalamacros:paradise, kind-projector and
* better-monadic-for.
*/
public abstract PresentationCompiler newInstance(String buildTargetIdentifier, List<Path> classpath,
List<String> options);
// =============================
// Intentionally missing methods
// =============================
// Metals uses diagnostics from the build. It is not on the roadmap to publish
// diagnostics
// from the presentation compiler because they are unreliable, especially for
// long-running
// compiler instances.
// def diagnostics(): List[Diagnostics]
// The presentation compiler does not have enough information to implement it
// standalone.
// def definition(params: OffsetParams): List[Location]
// The presentation compiler does not have enough information to implement it
// standalone.
// def references(params: OffsetParams): List[Location]
// ==============================================
// Internal methods - not intended for public use
// ==============================================
public abstract List<String> diagnosticsForDebuggingPurposes();
/**
* Returns false if the presentation compiler has not been used since the last
* reset.
*
* NOTE(olafur) This method was added for testing purposes. It's critical that
* we correctly reset the presentation compiler when build compilation complete
* to prevent the presentatin compiler from returning stale results. It's
* difficult to reliably test that a compiler is not returning stale results so
* we test instead against the implementation detail that the compiler is `null`
* when it has been reset.
*/
public abstract boolean isLoaded();
/**
* Scala version for the current presentation compiler
*/
public abstract String scalaVersion();
}