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

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/woyebuzhidao321/article/details/131071724