VSCode插件开发 浅谈workspace问题

判断当前是否存在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里面存储的内容就不过多介绍,可以查看笔者之前写的文章

    VSCode插件开发 更新设置列表中的配置项

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

VSCode插件开发系列之目录

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值