保存文件后,从TreeProvider打开虚拟文件并引用树项

时间:2018-12-20 04:28:05

标签: javascript typescript visual-studio-code vscode-extensions

我创建了一个基本的TreeProvider类,该类提供了从Web api访问类似于文件夹的结构的功能。 api中的每个项目都是JSON块,因此我正在使用VSCode编辑此JSON,然后将其提交回API。

该项目以“内存文件”系统类型打开,我使用vscode团队创建的示例文件系统进行此工作:https://github.com/Microsoft/vscode-extension-samples/tree/master/fsprovider-sample

我目前在哪里挣扎,如何引用已保存的商品ID?我想在打开文件时在文件本身上设置ID和APIConnection之类的属性,但是我不确定该怎么做,因为我使用内存中的uri在VSCode中打开文件:

// this command is executed whenever a tree item is "clicked"
export default async function(item :ArcGISItem, scope : any){
    // get the item data from the api using the TreeItem.id
    let data = await item.connection.getItem(item.id);
    const file = `memfs:/${item.id}.json`;
    const uri = Uri.parse(file);
    // create a virtual file using the filesystem api and the json data
    scope.fs.writeFile(uri, Buffer.from(data), { 
        create: true, overwrite: true 
    });
    // open the document and format it 
    workspace.openTextDocument(uri).then(doc => {
        window.showTextDocument(doc);
        commands.executeCommand('editor.action.format');
    }, (e: any) => console.warn(e));

}

好的,正如我所说,我现在需要检测这些重要文件的更改时间,然后将其提交回api。我将fs直接传递给TreeProvider,因此它知道如何监视文件更改:

fs.onDidChangeFile((e) => {
    console.log(e);
    // seems janky
    const id = e[0].uri.path.substring(1).split('.')[0];

     // then...
     // recursively this.getChildren() until child.id === id ??
     // const child = getChildrenrecursivelyById(id);

     // get content from file?
     // const jsonContent = fs.readFile(e[0].uri.external)

     // update item using the api (axios)
     // child.connection.updateItem(id, jsonContent);
});

这将导致包含1个项目的数组的日志:

Array[1]
0:Object
type:1
uri:Object
$mid:1
fsPath:"\328f052b2be24637b7305a08f877a443.json"
external:"memfs:/328f052b2be24637b7305a08f877a443.json"
path:"/328f052b2be24637b7305a08f877a443.json"
scheme:"memfs"

有什么想法我接下来可以做什么?

供参考,我完整的vscode插件位于: https://github.com/roemhildtg/vscode-arcgis-assistant

0 个答案:

没有答案