我上周发布了这个并取得了进展,在那里我发现了 VSCode 的 JSON 支持通过扩展提供的包:
https://github.com/vscode-langservers/vscode-json-languageserver https://github.com/Microsoft/vscode-json-languageservice
和所有其他的。我正在尝试在电子(NodeJS)应用程序中重用它。我可以派生一个启动语言服务器的进程并对其进行初始化:
lspProcess = child_process.fork("node_modules/vscode-json-languageserver/out/jsonServerMain.js", [ "--node-ipc" ]);
function initialize() {
send("initialize", {
rootPath: process.cwd(),
processId: process.pid,
capabilities: {
textDocument: true
}
});
}
lspProcess.on('message', function (json) {
console.log(json);
});
我看到 console.log 触发并显示它似乎是正确的。我的想法是我只想根据LSP发送一个 textDocument/didChange 事件:
send('textDocument/didChange', {
textDocument: TextDocument.create('foo://bar/file.json', 'json', 0, JSON.stringify(data))
});
其中 data 是表示文件的 JSON 对象。
当我发送该消息和其他尝试时,我得到
error: {code: -32601, message: "Unhandled method textDocument/didChange"}
id: 2
jsonrpc: "2.0"
知道我在这里做错了什么吗?我的主要目标是允许通过我的 Electron 应用程序进行编辑,然后将更新的 JSON 发送到语言服务器以完成模式验证。
编辑:当我在 jsonServerMain.js 中实现 connection.onInitialized() 时,我什至看到未处理的方法被初始化。
EDIT2:更新,我想出了我在哪里出错了。initialized 和 textDocument/didChange 是通知,而不是请求。