Skip to content

Commit 9647f0d

Browse files
mickaelistriargrunber
authored andcommitted
Small fix to CompletionEngines and ExplectedTypes
Should improve a couple tests + Added a Timer utility class to help profiling
1 parent 6a85614 commit 9647f0d

File tree

3 files changed

+99
-27
lines changed

3 files changed

+99
-27
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025, Red Hat, Inc. and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*******************************************************************************/
11+
package org.eclipse.jdt.internal;
12+
13+
import java.time.Duration;
14+
import java.time.Instant;
15+
import java.util.Deque;
16+
import java.util.HashMap;
17+
import java.util.LinkedList;
18+
import java.util.Map;
19+
import java.util.Objects;
20+
import java.util.AbstractMap.SimpleEntry;
21+
import java.util.Map.Entry;
22+
import java.util.function.Supplier;
23+
24+
public class Timer {
25+
Map<String, Duration> durations = new HashMap<>();
26+
Deque<Map.Entry<String, Instant>> started = new LinkedList<>();
27+
public void reportDuration(String id, Runnable r) {
28+
Instant before = Instant.now();
29+
r.run();
30+
Duration d = Duration.between(before, Instant.now());
31+
System.err.println(
32+
id + " current:" + d.toMillis() + " total:" + durations.compute(id, (_, prev) -> prev == null ? d : prev.plus(d)).toMillis());
33+
}
34+
public <T> T reportDuration(String id, Supplier<T> r) {
35+
Instant before = Instant.now();
36+
T res = r.get();
37+
Duration d = Duration.between(before, Instant.now());
38+
System.err.println(
39+
id + " current:" + d.toMillis() + " total:" + durations.compute(id, (_, prev) -> prev == null ? d : prev.plus(d)).toMillis());
40+
return res;
41+
}
42+
public void start(String id) {
43+
started.push(new SimpleEntry<>(id, Instant.now()));
44+
}
45+
public void stopCurrent() {
46+
if (!started.isEmpty()) {
47+
Entry<String, Instant> entry = started.pop();
48+
String current = entry.getKey();
49+
Duration d = Duration.between(entry.getValue(), Instant.now());
50+
System.err.println(
51+
current + " current:" + d.toMillis() + " total:" + durations.compute(current, (_, prev) -> prev == null ? d : prev.plus(d)).toMillis());
52+
}
53+
}
54+
public void stopLast(String id) {
55+
Entry<String, Instant> e = this.started.reversed().stream().filter(entry -> Objects.equals(id, entry.getKey())).findFirst().orElse(null);
56+
if (e != null) {
57+
started.remove(e);
58+
String current = e.getKey();
59+
Duration d = Duration.between(e.getValue(), Instant.now());
60+
System.err.println(
61+
current + " current:" + d.toMillis() + " total:" + durations.compute(current, (_, prev) -> prev == null ? d : prev.plus(d)).toMillis());
62+
63+
}
64+
}
65+
}

org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/codeassist/DOMCompletionEngine.java

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@
9696
import org.eclipse.jdt.core.dom.IfStatement;
9797
import org.eclipse.jdt.core.dom.ImportDeclaration;
9898
import org.eclipse.jdt.core.dom.InfixExpression;
99-
import org.eclipse.jdt.core.dom.InfixExpression.Operator;
10099
import org.eclipse.jdt.core.dom.Initializer;
101100
import org.eclipse.jdt.core.dom.InstanceofExpression;
102101
import org.eclipse.jdt.core.dom.Javadoc;
@@ -108,7 +107,6 @@
108107
import org.eclipse.jdt.core.dom.MethodInvocation;
109108
import org.eclipse.jdt.core.dom.MethodRef;
110109
import org.eclipse.jdt.core.dom.Modifier;
111-
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
112110
import org.eclipse.jdt.core.dom.ModuleDeclaration;
113111
import org.eclipse.jdt.core.dom.Name;
114112
import org.eclipse.jdt.core.dom.NodeFinder;
@@ -149,6 +147,8 @@
149147
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
150148
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
151149
import org.eclipse.jdt.core.dom.WhileStatement;
150+
import org.eclipse.jdt.core.dom.InfixExpression.Operator;
151+
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
152152
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
153153
import org.eclipse.jdt.core.search.IJavaSearchConstants;
154154
import org.eclipse.jdt.core.search.IJavaSearchScope;
@@ -586,7 +586,6 @@ public void complete(org.eclipse.jdt.internal.compiler.env.ICompilationUnit sour
586586
}
587587
}
588588

589-
590589
try {
591590
Bindings defaultCompletionBindings = new Bindings();
592591
Bindings specificCompletionBindings = new Bindings();
@@ -1358,30 +1357,30 @@ public void complete(org.eclipse.jdt.internal.compiler.env.ICompilationUnit sour
13581357
if (context instanceof ClassInstanceCreation) {
13591358
if (this.expectedTypes.getExpectedTypes() != null && !this.expectedTypes.getExpectedTypes().isEmpty() && !this.expectedTypes.getExpectedTypes().get(0).isRecovered()) {
13601359
completeConstructor(this.expectedTypes.getExpectedTypes().get(0), context, this.javaProject);
1361-
} else {
1362-
if (!this.requestor.isIgnored(CompletionProposal.TYPE_REF) && !this.requestor.isIgnored(CompletionProposal.CONSTRUCTOR_INVOCATION)) {
1363-
String packageName = "";//$NON-NLS-1$
1364-
PackageDeclaration packageDecl = this.unit.getPackage();
1365-
if (packageDecl != null) {
1366-
packageName = packageDecl.getName().toString();
1367-
}
1368-
this.findTypes(this.prefix, IJavaSearchConstants.TYPE, packageName)
1369-
.filter(type -> {
1370-
try {
1371-
return !type.isAnnotation();
1372-
} catch (JavaModelException e) {
1373-
return true;
1374-
}
1375-
}) //
1376-
.flatMap(type -> {
1377-
if (this.prefix.isEmpty()) {
1378-
return Stream.of(toProposal(type));
1379-
} else {
1380-
return toConstructorProposals(type, this.toComplete, false).stream();
1381-
}
1382-
}) //
1383-
.forEach(this.requestor::accept);
1360+
} else if (this.toComplete == context) {
1361+
// completing empty args
1362+
} else if (!this.requestor.isIgnored(CompletionProposal.TYPE_REF) && !this.requestor.isIgnored(CompletionProposal.CONSTRUCTOR_INVOCATION)) {
1363+
String packageName = "";//$NON-NLS-1$
1364+
PackageDeclaration packageDecl = this.unit.getPackage();
1365+
if (packageDecl != null) {
1366+
packageName = packageDecl.getName().toString();
13841367
}
1368+
this.findTypes(this.prefix, IJavaSearchConstants.TYPE, packageName)
1369+
.filter(type -> {
1370+
try {
1371+
return !type.isAnnotation();
1372+
} catch (JavaModelException e) {
1373+
return true;
1374+
}
1375+
}) //
1376+
.flatMap(type -> {
1377+
if (this.prefix.isEmpty()) {
1378+
return Stream.of(toProposal(type));
1379+
} else {
1380+
return toConstructorProposals(type, this.toComplete, false).stream();
1381+
}
1382+
}) //
1383+
.forEach(this.requestor::accept);
13851384
}
13861385
suggestDefaultCompletions = false;
13871386
}

org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/codeassist/ExpectedTypes.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,13 @@ private void computeExpectedTypes(){
133133
return;
134134
}
135135
if (parent2 instanceof ClassInstanceCreation newObj && this.offset > newObj.getType().getStartPosition() + newObj.getType().getLength()) {
136-
// TODO find params
136+
ITypeBinding binding = newObj.resolveTypeBinding();
137+
if(binding != null) {
138+
computeExpectedTypesForAllocationExpression(
139+
binding,
140+
newObj.arguments(),
141+
newObj);
142+
}
137143
break;
138144
}
139145
if (parent2 instanceof CastExpression cast && this.offset > cast.getType().getStartPosition() + cast.getType().getLength()) {
@@ -596,6 +602,8 @@ private void computeExpectedTypesForAllocationExpression(
596602
if(expectedType != null) {
597603
this.expectedTypes.add(expectedType);
598604
}
605+
} else if (arguments.isEmpty() && parameters.length > 0 && parameters[0] != null) {
606+
this.expectedTypes.add(parameters[0]);
599607
}
600608
}
601609
}

0 commit comments

Comments
 (0)