如何从语言服务器端检索rootPath(或任何其他客户端信息)?

时间:2016-05-10 18:24:02

标签: typescript visual-studio-code vscode-extensions language-server-protocol

我正在根据示例“语言服务器”(https://code.visualstudio.com/docs/extensions/example-language-server)开展语言扩展。

在服务器端,我需要知道vscode使用的当前文件夹,在客户端,它将通过以下方式检索:

import * as vscode from 'vscode';
[...]
let curFolder : string =  vscode.workspace.rootPath;
[...]

但如果我尝试在服务器端使用它,

  1. 我收到TS编译错误:错误TS2307:找不到模块'vscode'
  2. 启动客户端后(在客户端使用F5),我无法连接到服务器(在服务器中使用F5)。
  3. 我的服务器和客户端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;
    } );
    

    有没有更好的方法呢?

1 个答案:

答案 0 :(得分:0)

实际上非常简单。 rootPath在初始化时提供给语言服务器,参数为“connection.onInitialize”。

    EndpointImpl jaxWsEndpoint = (EndpointImpl) Endpoint.publish(endPointAddress, httpWebService);