繁体   English   中英

vscode扩展。 如何按文件名获取文件正文

[英]vscode extension. How can I get a file body by filename

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

在服务器端:

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

在客户端:在client.start()之后的函数activate(context:ExtensionContext )中;

    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;
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM