判断当前是否存在workspace
打开Settings
, 有时可以看到这里有一个Workspace
, 中文环境下叫做工作区
。
什么时候会出现这个Workspace
选项呢?
如果现在没有Workspace
显示,执行以下操作会出现Workspace
选项
File -> Open Folder...
,当有文件夹打开的时候File -> Open Workspace...
, 当打开一个Workspace的时候File -> Add Folder to Workspace...
, 当添加文件到workspace的时候File -> Save Workspace As...
, 把当前环境保存到一个workspace的时候
在VSCode插件开发中,如何通过代码的方式判断是否有Workspace
这个选项出现呢?可以通过vscode.workspace.workspaceFolders
的返回值来确定是否存在Workspace
,以下是引用源码中的一段内容
/**
* List of workspace folders or `undefined` when no folder is open.
* *Note* that the first entry corresponds to the value of `rootPath`.
*/
export const workspaceFolders: WorkspaceFolder[] | undefined;
以下内容是我封装的一段是否存在Workspace
的方法,亲测可行
function hasWorkspace() {
return vscode.workspace.workspaceFolders !== undefined;
}
Workspace中文件的内容与作用
目前笔者所接触的Workspace内容就这两类
{
"folders": [
{
"path": "C:\\"
}
],
"settings": {
"editor.wordWrap": "on"
}
}
folders
, 存放打开的文件夹settings
, 存储Settings中非默认的配置值
如何修改Workspace
文件中的内容
-
settings
关于settings里面存储的内容就不过多介绍,可以查看笔者之前写的文章
-
folders
这个目录中存放的是当前所打开的文件夹,可以打开多个,VSCode中的搜索功能搜索的就是这些添加到
workspace
中打开的文件如果修改它的内容呢,使用vscode的api.
/** * This method replaces `deleteCount` [workspace folders](#workspace.workspaceFolders) starting at index `start` * by an optional set of `workspaceFoldersToAdd` on the `vscode.workspace.workspaceFolders` array. This "splice" * behavior can be used to add, remove and change workspace folders in a single operation. * * If the first workspace folder is added, removed or changed, the currently executing extensions (including the * one that called this method) will be terminated and restarted so that the (deprecated) `rootPath` property is * updated to point to the first workspace folder. * * Use the [`onDidChangeWorkspaceFolders()`](#onDidChangeWorkspaceFolders) event to get notified when the * workspace folders have been updated. * * **Example:** adding a new workspace folder at the end of workspace folders * ```typescript * workspace.updateWorkspaceFolders(workspace.workspaceFolders ? workspace.workspaceFolders.length : 0, null, { uri: ...}); * ``` * * **Example:** removing the first workspace folder * ```typescript * workspace.updateWorkspaceFolders(0, 1); * ``` * * **Example:** replacing an existing workspace folder with a new one * ```typescript * workspace.updateWorkspaceFolders(0, 1, { uri: ...}); * ``` * * It is valid to remove an existing workspace folder and add it again with a different name * to rename that folder. * * **Note:** it is not valid to call [updateWorkspaceFolders()](#updateWorkspaceFolders) multiple times * without waiting for the [`onDidChangeWorkspaceFolders()`](#onDidChangeWorkspaceFolders) to fire. * * @param start the zero-based location in the list of currently opened [workspace folders](#WorkspaceFolder) * from which to start deleting workspace folders. * @param deleteCount the optional number of workspace folders to remove. * @param workspaceFoldersToAdd the optional variable set of workspace folders to add in place of the deleted ones. * Each workspace is identified with a mandatory URI and an optional name. * @return true if the operation was successfully started and false otherwise if arguments were used that would result * in invalid workspace folder state (e.g. 2 folders with the same URI). */ export function updateWorkspaceFolders(start: number, deleteCount: number | undefined | null, ...workspaceFoldersToAdd: { uri: Uri, name?: string }[]): boolean;
注意:当Workspace打开的文件从无到有
(0->1)
或者从有到无(1->0)
的时候,VSCode会重启
Workspace文件变化监听
- settings
这里以监听editor.wordWrap
为例vscode.workspace.onDidChangeConfiguration((e) => { if (e.affectsConfiguration("editor.wordWrap")) { // TODO } });
- folders
vscode.workspace.onDidChangeWorkspaceFolders(e=> { var add:{uri:vscode.Uri, name:string, index:number}[] = e.added; var remove:{uri:vscode.Uri, name:string, index:number}[] = e.removed; // TODO });