如何为Protobuf生成的JavaScript文件设置VSCode智能感知?

5

通常情况下,我的VSCode安装在JavaScript项目中提供智能提示。然而,我遇到了一个问题,无法让VSCode显示/注册从协议缓冲区编译器 protoc 生成的其他JavaScript文件中导入的消息类型的智能提示。

下面是我面临的问题的截图。根据下面添加的protobuf文件,我期望在jeff对象上有setId(), setName(), setSalary()函数。然而,VSCode似乎无法识别Schema.Employee类型。

VSCode not providing intellisense

我是否能做些什么使智能提示能够使用从协议缓冲区生成的类型?

reprex 测试使用 VSCode 1.50.1 和 protoc 版本 libprotoc 3.13.0:

我使用以下命令编译了下面的protobuf文件:

protoc --js_out=import_style=commonjs,binary:. employees.proto

这将生成employees_pb.js

employees.proto

syntax = "proto3";

option optimize_for = SPEED;

message Employee {
    int32 id = 1;
    string name = 2;
    float salary = 3;
}

message Employees {
    repeated Employee employees = 1;
} 

index.js:

// import schema from compiled file
const Schema = require("./employees_pb");

//create a new instance of Employee
const jeff = new Schema.Employee();
jeff.setId(1001); // <-- Intellisense does not work here.
jeff.setName("Jeff");
jeff.setSalary(1001);

package.json:

{
  "name": "protobuff",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "google-protobuf": "^3.13.0"
  }
}
1个回答

3

在您的employees_pb文件中,您可以更改最后一行以获取完整的智能感知:

module.exports = {...goog, ...proto};
// goog.object.extend(exports, proto);

由于某些原因,导出扩展无法工作?我在这个主题上打开了一个问题。 链接

给我带来了好处,谢谢! - Ram

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接