Skip to content

Commit

Permalink
Completion for import module |;
Browse files Browse the repository at this point in the history
This is new Java 23 syntax.
This PR should make 6 new tests pass
(mostly from the java 23 completion suite)
This PR also incorporates
eclipse-jdt#3479,
I forget specifically why, but it's needed.

- Fixes to the javac AST converter
  - I did a hack in the converter to recover the following case,
    since javac doesn't handle it currently:

```java
import module

public class HelloWorld {
}
```

- Adjust module completion relevance based on if it's required by the current
  module

Signed-off-by: David Thompson <davthomp@redhat.com>
  • Loading branch information
datho7561 committed Jan 3, 2025
1 parent 58c84f6 commit d016155
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
import com.sun.tools.javac.tree.JCTree.JCMethodInvocation;
import com.sun.tools.javac.tree.JCTree.JCModifiers;
import com.sun.tools.javac.tree.JCTree.JCModuleDecl;
import com.sun.tools.javac.tree.JCTree.JCModuleImport;
import com.sun.tools.javac.tree.JCTree.JCNewArray;
import com.sun.tools.javac.tree.JCTree.JCNewClass;
import com.sun.tools.javac.tree.JCTree.JCOpens;
Expand Down Expand Up @@ -192,6 +193,9 @@ void populateCompilationUnit(CompilationUnit res, JCCompilationUnit javacCompila
res.setModule(convert(javacCompilationUnit.getModuleDecl()));
}
javacCompilationUnit.getImports().stream().filter(imp -> imp instanceof JCImport).map(jc -> convert((JCImport)jc)).forEach(res.imports()::add);
if (this.ast.apiLevel >= AST.JLS23_INTERNAL) {
javacCompilationUnit.getImports().stream().filter(imp -> imp instanceof JCModuleImport).map(jc -> convert((JCModuleImport)jc)).forEach(res.imports()::add);
}
javacCompilationUnit.getTypeDecls().stream()
.map(n -> convertBodyDeclaration(n, res))
.filter(Objects::nonNull)
Expand Down Expand Up @@ -394,6 +398,14 @@ private ImportDeclaration convert(JCImport javac) {
if (select.getIdentifier().contentEquals("*")) {
res.setOnDemand(true);
res.setName(toName(select.getExpression()));
} else if (this.ast.apiLevel >= AST.JLS23_INTERNAL && select.selected.toString().equals("module") && select.name.toString().equals("<error>")) {
// it's a broken module import
var moduleModifier = this.ast.newModifier(ModifierKeyword.MODULE_KEYWORD);
res.modifiers().add(moduleModifier);
Name name = new SimpleName(this.ast);
name.setSourceRange(res.getStartPosition() + res.getLength() + 1, 0);
res.setName(name);
res.setSourceRange(res.getStartPosition(), res.getLength() + 1);
} else {
res.setName(toName(select));
}
Expand Down Expand Up @@ -423,6 +435,21 @@ private ImportDeclaration convert(JCImport javac) {
}
return res;
}

private ImportDeclaration convert(JCModuleImport javac) {
ImportDeclaration res = this.ast.newImportDeclaration();
commonSettings(res, javac);
var moduleModifier = this.ast.newModifier(ModifierKeyword.MODULE_KEYWORD);
res.modifiers().add(moduleModifier);
if (javac.isStatic()) {
if( this.ast.apiLevel != AST.JLS2_INTERNAL) {
res.setStatic(true);
}
}
var select = javac.getQualifiedIdentifier();
res.setName(toName(select));
return res;
}

void commonSettings(ASTNode res, JCTree javac) {
if( javac != null ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,7 @@ yield switch (rootCauseCode) {
case "compiler.err.void.not.allowed.here" -> IProblem.ParameterMismatch;
case "compiler.err.abstract.cant.be.accessed.directly" -> IProblem.DirectInvocationOfAbstractMethod;
case "compiler.warn.annotation.method.not.found" -> IProblem.UndefinedAnnotationMember;
case "compiler.err.import.module.not.found" -> IProblem.UndefinedModule;
default -> {
ILog.get().error("Could not accurately convert diagnostic (" + diagnostic.getCode() + ")\n" + diagnostic);
if (diagnostic.getKind() == javax.tools.Diagnostic.Kind.ERROR && diagnostic.getCode().startsWith("compiler.err")) {
Expand Down
Loading

0 comments on commit d016155

Please sign in to comment.