【问题标题】:vscode extension. How can I get a file body by filenamevscode 扩展。如何通过文件名获取文件正文
【发布时间】:2019-02-13 07:52:46
【问题描述】:

我为专有语言编写了自己的扩展(使用 LSP - 语言服务器协议),并且这个扩展解析了源代码。当解析器采用import "Filename.ext" 之类的字符串(在c++ 中为#include "fileName.h")时,我必须找到此文件“Filename.ext”并获取此文件的正文进行解析。客户端有“workspace.findFiles(name);”但它在服务器端不起作用。请告诉我如何在服务器端按文件名获取文件。在客户端创建一个函数并导入到服务器端不起作用

【问题讨论】:

    标签: visual-studio-code vscode-extensions


    【解决方案1】:

    在服务器端:

    export function GetFileRequest(nameInter:string) {
        connection.sendRequest("getFile", nameInter).then( (body : string) => {
            if (body != undefined && body.length) pushImports(body);
        });
    }
    

    在客户端: 在 function activate(context: ExtensionContext) 之后 client.start();

        client.onReady().then(() => {
            client.onRequest("getFile", (nameInter : string) : Promise<string> => { return getFile(nameInter); } );
        });
    

    和客户端的异步功能:

    async function getFile(name):Promise<string>{
        let uri:Uri = undefined;
        let text:string = undefined;
        await workspace.findFiles(name, null, 1).then((value)=> {
            if (value.length) {
                uri=value[0];
            }
        });
        if (uri!=undefined) {
            let textDocument;
            await workspace.openTextDocument(uri).then((value)=>textDocument = value);
            text = textDocument.getText();
        }
        return text;
    }
    

    此代码将从文件返回一个文本到服务器端。

    【讨论】:

      猜你喜欢
      • 2016-11-27
      • 2012-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-14
      • 1970-01-01
      • 2012-07-23
      • 2017-12-22
      相关资源
      最近更新 更多