vscode二次开发之如何扩展插件主体里的vscode对象

vscode插件里给我们提供了大量的对象、方法供我们使用
在这里插入图片描述
但其中的对象、方法如果不满足我们的需求的话,就需要对其进行扩展,这里我详细介绍下如何对其进行扩展。

这里我扩展的是文件的路径

首先定义类型

src/vscode-dts/vscode.d.ts

export namespace trash {
    export function getTrashRootPath(): Thenable<string>;
}

定义主线类方法

src/vs/workbench/api/browser/mainThreadTrash.ts

/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import { INativeEnvironmentService } from 'vs/platform/environment/common/environment';
import { MainContext, MainThreadTrashShape } from 'vs/workbench/api/common/extHost.protocol';

import { extHostNamedCustomer, IExtHostContext } from '../../services/extensions/common/extHostCustomers';
import { Disposable } from 'vs/base/common/lifecycle';

@extHostNamedCustomer(MainContext.MainThreadTrash)
export class MainThreadTrash extends Disposable implements MainThreadTrashShape {
    // private readonly _proxy: ExtHostTrashShape;

    constructor(
        extHostContext: IExtHostContext,
        @INativeEnvironmentService private readonly environmentMainService: INativeEnvironmentService
    ) {
        super();
        // this._proxy = extHostContext.getProxy(ExtHostContext.ExtHostTrash);
    }

    $getTrashRoot(): Promise<string> {
        return Promise.resolve(this.environmentMainService.trashContent.fsPath);
    }
}

src/vs/workbench/api/common/extHost.protocol.ts

接口

export interface MainThreadTrashShape extends IDisposable {
    $getTrashRoot(): Promise<string>;
}

export interface ExtHostTrashShape {
    $getTrashRoot(): Promise<string>;
}

统一注册主线类方法

src/vs/workbench/api/browser/extensionHost.contribution.ts

import './mainThreadTrash';

插件线程方法

src/vs/workbench/api/common/extHostTrash.ts

/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import { IMainContext, MainThreadTrashShape, MainContext, ExtHostTrashShape } from 'vs/workbench/api/common/extHost.protocol';

export class ExtHostTrash implements ExtHostTrashShape {

    private _proxy: MainThreadTrashShape;

    constructor(mainContext: IMainContext) {
        this._proxy = mainContext.getProxy(MainContext.MainThreadTrash);
    }

    $getTrashRoot(): Promise<string> {
        return this._proxy.$getTrashRoot();
    }
}

这里添加到插件扩展

src/vs/workbench/api/common/extHost.api.impl.ts
179

const extHostTrash = rpcProtocol.set(ExtHostContext.ExtHostTrash, new ExtHostTrash(rpcProtocol));

259

const trash: typeof vscode.trash = {
    async getTrashRootPath() {
        return extHostTrash.$getTrashRoot();
    }
};

1145

return <typeof vscode>{
    version: initData.version,
    trash,
    // namespaces
    ...
}          

插件里使用

vscode.trash.getTrashRootPath().then(data => {
		console.log(123,vscode.Uri.file(data) )
	})

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值