Skip to content

Commit fcdba8f

Browse files
committed
Add support for namespace aliasing and prefix import
1 parent 60eb57c commit fcdba8f

4 files changed

Lines changed: 455 additions & 66 deletions

File tree

src/definition/resolve.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,25 @@ impl Backend {
351351
name
352352
}
353353
} else {
354+
// The name contains `\` — check if the first segment
355+
// is a use-map alias (e.g. `OA\Endpoint` where
356+
// `use Swagger\OpenAPI as OA;` maps `OA` →
357+
// `Swagger\OpenAPI`). Expand it to the FQN.
358+
let first_segment = name.split('\\').next().unwrap_or(name);
359+
if let Some(fqn_prefix) = file_use_map.get(first_segment) {
360+
let rest = &name[first_segment.len()..];
361+
let expanded = format!("{}{}", fqn_prefix, rest);
362+
if let Some(cls) = self.find_or_load_class(&expanded) {
363+
return Some(cls);
364+
}
365+
}
366+
// Also try prefixing with the current namespace.
367+
if let Some(ref ns) = file_namespace {
368+
let ns_qualified = format!("{}\\{}", ns, name);
369+
if let Some(cls) = self.find_or_load_class(&ns_qualified) {
370+
return Some(cls);
371+
}
372+
}
354373
name
355374
};
356375
self.find_or_load_class(resolved_name)

src/server.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,25 @@ impl LanguageServer for Backend {
293293
name
294294
}
295295
} else {
296+
// The name contains `\` — check if the first segment
297+
// is a use-map alias (e.g. `OA\Endpoint` where
298+
// `use Swagger\OpenAPI as OA;` maps `OA` →
299+
// `Swagger\OpenAPI`). Expand it to the FQN.
300+
let first_segment = name.split('\\').next().unwrap_or(name);
301+
if let Some(fqn_prefix) = file_use_map.get(first_segment) {
302+
let rest = &name[first_segment.len()..];
303+
let expanded = format!("{}{}", fqn_prefix, rest);
304+
if let Some(cls) = self.find_or_load_class(&expanded) {
305+
return Some(cls);
306+
}
307+
}
308+
// Also try prefixing with the current namespace.
309+
if let Some(ref ns) = file_namespace {
310+
let ns_qualified = format!("{}\\{}", ns, name);
311+
if let Some(cls) = self.find_or_load_class(&ns_qualified) {
312+
return Some(cls);
313+
}
314+
}
296315
name
297316
};
298317

tests/completion_mixins.rs

Lines changed: 65 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2155,26 +2155,26 @@ async fn test_completion_inherited_mixin_static_method_via_double_colon() {
21552155

21562156
let uri = Url::parse("file:///inherited_mixin_static.php").unwrap();
21572157
let text = concat!(
2158-
"<?php\n", // 0
2159-
"class Builder {\n", // 1
2160-
" /**\n", // 2
2161-
" * @return static\n", // 3
2162-
" */\n", // 4
2163-
" public static function query(): self {\n", // 5
2164-
" return new static();\n", // 6
2165-
" }\n", // 7
2166-
"}\n", // 8
2167-
"\n", // 9
2168-
"/**\n", // 10
2169-
" * @mixin Builder\n", // 11
2170-
" */\n", // 12
2171-
"abstract class Model {\n", // 13
2172-
"}\n", // 14
2173-
"\n", // 15
2174-
"class User extends Model {\n", // 16
2175-
"}\n", // 17
2176-
"\n", // 18
2177-
"$query = User::\n", // 19
2158+
"<?php\n", // 0
2159+
"class Builder {\n", // 1
2160+
" /**\n", // 2
2161+
" * @return static\n", // 3
2162+
" */\n", // 4
2163+
" public static function query(): self {\n", // 5
2164+
" return new static();\n", // 6
2165+
" }\n", // 7
2166+
"}\n", // 8
2167+
"\n", // 9
2168+
"/**\n", // 10
2169+
" * @mixin Builder\n", // 11
2170+
" */\n", // 12
2171+
"abstract class Model {\n", // 13
2172+
"}\n", // 14
2173+
"\n", // 15
2174+
"class User extends Model {\n", // 16
2175+
"}\n", // 17
2176+
"\n", // 18
2177+
"$query = User::\n", // 19
21782178
);
21792179

21802180
backend
@@ -2230,31 +2230,31 @@ async fn test_completion_inherited_mixin_instance_method_via_arrow() {
22302230

22312231
let uri = Url::parse("file:///inherited_mixin_instance.php").unwrap();
22322232
let text = concat!(
2233-
"<?php\n", // 0
2234-
"class Builder {\n", // 1
2235-
" public function where(): self {\n", // 2
2236-
" return $this;\n", // 3
2237-
" }\n", // 4
2238-
" public function get(): array {\n", // 5
2239-
" return [];\n", // 6
2240-
" }\n", // 7
2241-
"}\n", // 8
2242-
"\n", // 9
2243-
"/**\n", // 10
2244-
" * @mixin Builder\n", // 11
2245-
" */\n", // 12
2246-
"abstract class Model {\n", // 13
2247-
" public function save(): void {}\n", // 14
2248-
"}\n", // 15
2249-
"\n", // 16
2250-
"class User extends Model {\n", // 17
2251-
" public function getName(): string {\n", // 18
2252-
" return '';\n", // 19
2253-
" }\n", // 20
2254-
"}\n", // 21
2255-
"\n", // 22
2256-
"$user = new User();\n", // 23
2257-
"$user->\n", // 24
2233+
"<?php\n", // 0
2234+
"class Builder {\n", // 1
2235+
" public function where(): self {\n", // 2
2236+
" return $this;\n", // 3
2237+
" }\n", // 4
2238+
" public function get(): array {\n", // 5
2239+
" return [];\n", // 6
2240+
" }\n", // 7
2241+
"}\n", // 8
2242+
"\n", // 9
2243+
"/**\n", // 10
2244+
" * @mixin Builder\n", // 11
2245+
" */\n", // 12
2246+
"abstract class Model {\n", // 13
2247+
" public function save(): void {}\n", // 14
2248+
"}\n", // 15
2249+
"\n", // 16
2250+
"class User extends Model {\n", // 17
2251+
" public function getName(): string {\n", // 18
2252+
" return '';\n", // 19
2253+
" }\n", // 20
2254+
"}\n", // 21
2255+
"\n", // 22
2256+
"$user = new User();\n", // 23
2257+
"$user->\n", // 24
22582258
);
22592259

22602260
backend
@@ -2325,26 +2325,26 @@ async fn test_goto_definition_inherited_mixin_static_method() {
23252325

23262326
let uri = Url::parse("file:///inherited_mixin_goto.php").unwrap();
23272327
let text = concat!(
2328-
"<?php\n", // 0
2329-
"class Builder {\n", // 1
2330-
" /**\n", // 2
2331-
" * @return static\n", // 3
2332-
" */\n", // 4
2333-
" public static function query(): self {\n", // 5
2334-
" return new static();\n", // 6
2335-
" }\n", // 7
2336-
"}\n", // 8
2337-
"\n", // 9
2338-
"/**\n", // 10
2339-
" * @mixin Builder\n", // 11
2340-
" */\n", // 12
2341-
"abstract class Model {\n", // 13
2342-
"}\n", // 14
2343-
"\n", // 15
2344-
"class User extends Model {\n", // 16
2345-
"}\n", // 17
2346-
"\n", // 18
2347-
"User::query();\n", // 19
2328+
"<?php\n", // 0
2329+
"class Builder {\n", // 1
2330+
" /**\n", // 2
2331+
" * @return static\n", // 3
2332+
" */\n", // 4
2333+
" public static function query(): self {\n", // 5
2334+
" return new static();\n", // 6
2335+
" }\n", // 7
2336+
"}\n", // 8
2337+
"\n", // 9
2338+
"/**\n", // 10
2339+
" * @mixin Builder\n", // 11
2340+
" */\n", // 12
2341+
"abstract class Model {\n", // 13
2342+
"}\n", // 14
2343+
"\n", // 15
2344+
"class User extends Model {\n", // 16
2345+
"}\n", // 17
2346+
"\n", // 18
2347+
"User::query();\n", // 19
23482348
);
23492349

23502350
backend

0 commit comments

Comments
 (0)