Skip to content

feat(language-service): support cross-module resource references - #308

Open
chinokoyuki wants to merge 2 commits into
ohosvscode:nextfrom
chinokoyuki:feat/multi-module-resources
Open

feat(language-service): support cross-module resource references#308
chinokoyuki wants to merge 2 commits into
ohosvscode:nextfrom
chinokoyuki:feat/multi-module-resources

Conversation

@chinokoyuki

@chinokoyuki chinokoyuki commented Jul 9, 2026

Copy link
Copy Markdown

总结

鸿蒙具有多模块特性。当开发者在 oh-package.json5 中声明模块依赖后,可以通过 [moduleName].type.name 的语法引用其他模块中的资源(如 $r('[basic].color.xxx'))。

当前插件仅支持 app.xxx.xxxsys.xxx.xxx 两种引用方式,对跨模块引用格式会报错 Invalid resource scope,且无法提供补全、跳转和悬停预览。

此PR为issue #265 服务

改动内容

packages/language-service/src/classes/resource-provider.ts 中新增跨模块资源引用支持,覆盖以下 4 个语言服务能力:

1. 诊断(Diagnostics)

  • 识别 [module].type.name 格式,不再误报 Invalid resource scope
  • 模块不存在时提示 Module "xxx" not found,并列出可用模块名
  • 资源不存在时提示 Resource [module].type.name not found in module "xxx"

2. 跳转定义(Go-to-Definition)

  • 支持 $r('[basic].color.xxx') 跳转到目标模块中对应资源的定义位置

3. 自动补全(Completion)

  • 输入 [ 时自动列出项目中所有模块及其资源
  • 输入 [basic]. 时自动列出 basic 模块中的所有资源

4. 悬停预览(Hover)

  • 支持 [module].media.name 格式的媒体资源悬停预览

新增辅助方法

方法 说明
parseCrossModuleReference(value) 用正则解析 [module].type.name 格式
findModuleByName(product, name) 在当前项目中按模块名查找 Module
findReferencesInModule(module) 收集指定模块下所有 Product 的资源引用

改动文件

  • packages/language-service/src/classes/resource-provider.ts(+182 / -13)

测试方式

  1. 创建多模块鸿蒙项目(如 basic 模块 + phone 模块)
  2. phone 模块的 oh-package.json5 中声明 "basic": "file:../basic"
  3. phone 模块中使用 $r('[basic].string.xxx') 引用 basic 模块的资源
  4. 验证:无误报诊断、自动补全可用、跳转定义正确、悬停预览正常

@changeset-bot

changeset-bot Bot commented Jul 9, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 33a9b66

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@chinokoyuki
chinokoyuki marked this pull request as ready for review July 9, 2026 04:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant