我正在使用的编辑器扩展程序也遇到了类似的问题。
我强烈建议您侦听VSCode API之外的文件更改。这样,您的大多数代码都不会绑定到特定的编辑器。使用VSCode API createFileSystemWatcher
的困难也更大,因为它无法捕获所有更改。
激活后,使用文件监视程序库运行子进程。我建议使用chokidar-与VSCode在内部使用的库相同,但可以更广泛地访问API。
// get your workspace uri & root uri
const workspaceRoots = vscode.workspace.workspaceFolders
if (!workspaceRoots || !workspaceRoots.length) {
throw new Error('No workspace root path')
}
const workspaceRoot: vscode.WorkspaceFolder = workspaceRoots[0]
const rootUri = vscode.workspace.getWorkspaceFolder(workspaceUri);
// the action you want to trigger
const command = vscode.commands.registerCommand('YOUR_CUSTOM_COMMAND',yourCustomFunction)
// file watcher
const fsWatcher = chokidar.watch('watcher_name',{
cwd: rootUri.uri.path,interval: 1000,})
// listen to any add/update/delete fs events
fsWatcher.on('change',(path,event) => {
vscode.commands.executeCommand('YOUR_CUSTOM_COMMAND')
})
任何文件更改都会触发您选择的操作。
,
如果VS Code扩展可以实现您所追求的一切,那么最简单的方法就是让扩展由外部驱动。 VS Code扩展运行在node.js环境中,因此它们可以start a http server,listen on a named pipe或使用几乎任何其他机制与外界通信。
您还可以查看VS Code对测试扩展的支持,因为测试也仅使用标准的VS Code扩展api:https://code.visualstudio.com/api/working-with-extensions/testing-extension
为了开发VS Code本身,有一些automation scripts模拟用户操作。如果确实需要,我相信您可以在用例中采用这些脚本,但是我不能说这将需要多少工作。如果用户界面更改,这些脚本也会中断。如果可能的话,最好走扩展路线
本文链接:https://www.f2er.com/3045732.html