28
28
import com .itsaky .androidide .lsp .api .IServerSettings ;
29
29
import com .itsaky .androidide .lsp .internal .model .CachedCompletion ;
30
30
import com .itsaky .androidide .lsp .java .compiler .CompileTask ;
31
+ import com .itsaky .androidide .lsp .java .compiler .CompletionInfo ;
32
+ import com .itsaky .androidide .lsp .java .compiler .JavaCompilerConfig ;
31
33
import com .itsaky .androidide .lsp .java .compiler .JavaCompilerService ;
32
34
import com .itsaky .androidide .lsp .java .compiler .SourceFileObject ;
33
35
import com .itsaky .androidide .lsp .java .compiler .SynchronizedTask ;
34
36
import com .itsaky .androidide .lsp .java .models .CompilationRequest ;
35
37
import com .itsaky .androidide .lsp .java .models .PartialReparseRequest ;
36
- import com .itsaky .androidide .lsp .java .parser .ts .TSJavaParser ;
37
- import com .itsaky .androidide .lsp .java .parser .ts .TSMethodPruner ;
38
38
import com .itsaky .androidide .lsp .java .providers .completion .IJavaCompletionProvider ;
39
39
import com .itsaky .androidide .lsp .java .providers .completion .IdentifierCompletionProvider ;
40
40
import com .itsaky .androidide .lsp .java .providers .completion .ImportCompletionProvider ;
50
50
import com .itsaky .androidide .lsp .models .CompletionResult ;
51
51
import com .itsaky .androidide .utils .DocumentUtils ;
52
52
import com .itsaky .androidide .utils .ILogger ;
53
- import com .itsaky .androidide .utils .StopWatch ;
54
- import com .itsaky .androidide .utils .VMUtils ;
55
53
56
54
import java .nio .file .Path ;
57
55
import java .time .Duration ;
@@ -156,23 +154,15 @@ private CompletionResult completeInternal(final @NonNull CompletionParams params
156
154
final var sourceObject = new SourceFileObject (file );
157
155
final var contentBuilder = new StringBuilder (sourceObject .getCharContent (true ));
158
156
159
- if (false && !VMUtils .isJvm ()) {
160
- // Cannot use tree sitter in tests
161
- // TODO(itsaky): Should we use the legacy method pruner for tests?
162
- final StopWatch watch = new StopWatch ("Prune method bodies" );
163
- final var parseResult = TSJavaParser .INSTANCE .parse (sourceObject );
164
- TSMethodPruner .INSTANCE .prune (contentBuilder , parseResult .getTree (), (int ) cursor );
165
- watch .log ();
166
- }
167
-
168
157
int endOfLine = endOfLine (contentBuilder , (int ) cursor );
169
158
contentBuilder .insert (endOfLine , ';' );
170
159
171
160
final StringBuilder contents ;
172
- if (compiler .compiler .currentContext != null ) {
161
+ final var context = compiler .compiler .currentContext ;
162
+ if (context != null ) {
173
163
abortIfCancelled ();
174
164
abortCompletionIfCancelled ();
175
- contents = new ASTFixer (compiler . compiler . currentContext ).fix (contentBuilder );
165
+ contents = new ASTFixer (context ).fix (contentBuilder );
176
166
} else {
177
167
contents = contentBuilder ;
178
168
}
@@ -182,7 +172,8 @@ private CompletionResult completeInternal(final @NonNull CompletionParams params
182
172
new PartialReparseRequest (cursor - params .requirePrefix ().length (), contentString );
183
173
abortIfCancelled ();
184
174
abortCompletionIfCancelled ();
185
- CompletionResult result = compileAndComplete (file , contentString , cursor , partialRequest );
175
+
176
+ CompletionResult result = compileAndComplete (contentString , params , partialRequest );
186
177
if (result == null ) {
187
178
result = CompletionResult .EMPTY ;
188
179
}
@@ -231,17 +222,24 @@ private int endOfLine(@NonNull CharSequence contents, int cursor) {
231
222
}
232
223
233
224
private CompletionResult compileAndComplete (
234
- Path file , String contents , final long cursor , PartialReparseRequest partialRequest ) {
235
- final Instant started = Instant .now ();
236
- final SourceFileObject source = new SourceFileObject (file , contents , Instant .now ());
237
- final String partial = partialIdentifier (contents , (int ) cursor );
238
- final boolean endsWithParen = endsWithParen (contents , (int ) cursor );
225
+ String contents , CompletionParams params , PartialReparseRequest partialRequest ) {
226
+ final long cursor = params .getPosition ().requireIndex ();
227
+ final var file = params .getFile ();
228
+ final var started = Instant .now ();
229
+ final var source = new SourceFileObject (file , contents , Instant .now ());
230
+ final var partial = partialIdentifier (contents , (int ) cursor );
231
+ final var endsWithParen = endsWithParen (contents , (int ) cursor );
239
232
240
233
abortIfCancelled ();
241
234
abortCompletionIfCancelled ();
242
235
243
236
final CompilationRequest request =
244
237
new CompilationRequest (Collections .singletonList (source ), partialRequest );
238
+ request .configureContext = ctx -> {
239
+ final var config = JavaCompilerConfig .instance (ctx );
240
+ config .setCompletionInfo (new CompletionInfo (params .getPosition ()));
241
+ };
242
+
245
243
SynchronizedTask synchronizedTask = compiler .compile (request );
246
244
return synchronizedTask .get (
247
245
task -> {
@@ -266,6 +264,13 @@ private CompletionResult compileAndComplete(
266
264
267
265
final var result = doComplete (file , contents , cursor , newPartial , endsWithParen , task , path );
268
266
new TopLevelSnippetsProvider ().complete (partial , task .root (), result );
267
+
268
+ // IMPORTANT: Unregister the completion info from the compiler configuration
269
+ if (task .task .getContext () != null ) {
270
+ final var compilerConfig = JavaCompilerConfig .instance (task .task .getContext ());
271
+ compilerConfig .setCompletionInfo (null );
272
+ }
273
+
269
274
return result ;
270
275
});
271
276
}
0 commit comments