我正在基于示例“语言服务器”(https://code.visualstudio.com/docs/extensions/example-language-server)进行语言扩展。
在服务器端,我需要知道 vscode 使用的当前文件夹,在客户端,它将通过以下方式检索:
import * as vscode from 'vscode';
[...]
let curFolder : string = vscode.workspace.rootPath;
[...]
但是如果我尝试在服务器端使用它,
- 我收到 TS 编译器错误:错误 TS2307:找不到模块“vscode”
- 一旦客户端启动(在客户端使用 F5),我就无法连接到服务器(在服务器中使用 F5)。
我的服务器和客户端 package.json 都指定:
"devDependencies": {
"typescript": "^1.8.9",
"vscode": "^0.11.12"
}
我的理解是服务器只通过 IConnection 对象与客户端通信,因此无法访问客户端维护的 vscode.* 数据。
我目前的工作是在服务器端使用它:
connection.sendRequest({ method: "getRootPath" })
.then( (rootPath : string) => {
[...]
和客户端的这段代码:
languageClient.onRequest({method: "getRootPath"}, (params : any) : string => {
return vscode.workspace.rootPath;
} );
有没有更好的方法来做到这一点?