Skip to content

Commit 0366dfe

Browse files
committed
fix(java): resolve a package-qualified static-import class by short name
`import static demo.Util.twice` records the import target package-qualified ("demo.Util"), but the class is registered under the project/directory QN ("<proj>.Util") when the `package` declaration and the file's directory differ (e.g. a flat layout). java_lookup_method("demo.Util", ...) then missed, the call fell through to the lsp_static_import_text fallback, and the precise lsp_static_import strategy was absent. On a miss, resolve the import's class by its short name against the registry and retry the method lookup, preferring an in-module match. Mirrors the C++ short-name type fallback. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent e6a46bd commit 0366dfe

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

‎internal/cbm/lsp/java_lsp.c‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1963,6 +1963,28 @@ static void resolve_method_call(JavaLSPContext *ctx, TSNode call) {
19631963
continue;
19641964
char *cls = cbm_arena_strndup(ctx->arena, target, (size_t)(last_dot - target));
19651965
const CBMRegisteredFunc *f = java_lookup_method(ctx, cls, mname, arity);
1966+
if (!f && ctx->registry) {
1967+
/* The import is written package-qualified ("demo.Util"), but the
1968+
* class is registered under the project/directory QN
1969+
* ("<proj>.Util") when the `package` declaration and the file's
1970+
* directory differ. Resolve the import's class by its short name
1971+
* against the registry and retry — preferring an in-module match.
1972+
* Mirrors the C++ short-name type fallback. */
1973+
const char *cls_dot = strrchr(cls, '.');
1974+
const char *cls_short = cls_dot ? cls_dot + 1 : cls;
1975+
size_t sl = strlen(cls_short);
1976+
for (int ti = 0; ti < ctx->registry->type_count && !f; ti++) {
1977+
const char *q = ctx->registry->types[ti].qualified_name;
1978+
if (!q) {
1979+
continue;
1980+
}
1981+
size_t ql = strlen(q);
1982+
if (ql > sl + 1 && q[ql - sl - 1] == '.' &&
1983+
strcmp(q + ql - sl, cls_short) == 0) {
1984+
f = java_lookup_method(ctx, q, mname, arity);
1985+
}
1986+
}
1987+
}
19661988
if (f) {
19671989
java_emit_resolved(ctx, f->qualified_name, "lsp_static_import", 0.92f);
19681990
return;

0 commit comments

Comments
 (0)