【原创】 新写的 VSCode 插件,用于扩展 VSCode 文

2020-01-07  本文已影响0人  yanzf

ReadMe

PowerTools目的是扩展VSCode文本处理能力,提供转换大小写、base64编码解码、排序行,哈希文本、自定义扩展指令等功能。

插件使用&开发交流群:895768531

主要功能

自定义扩展指令

文本处理

行处理

其他功能

演示

功能列表截图

[图片上传失败...(image-852250-1578396407206)]

大小写转换

[图片上传失败...(image-167909-1578396407206)]

行排序

[图片上传失败...(image-d1749c-1578396407206)]

base64编码

[图片上传失败...(image-719c55-1578396407206)]

求和

[图片上传失败...(image-e96c1d-1578396407206)]

翻转行

[图片上传失败...(image-87684a-1578396407206)]

自定义扩展指令

插件支持用户通过加载外部js文件来达到扩展功能的目的。对于有自定义扩展需求的用户,可以参照以下步骤来实现。

编写js文件

在本地新建一个js文件,复制以下内容:

module.exports.helloWorld = function(context) {
    var vscode = context.require('vscode');
    vscode.window.showInformationMessage("hello world!");
}

这段代码的作用是导出一个名字为helloworld的自定义指令。

现在我们看看函数的实现,可以注意到函数有一个context参数,插件会在运行时把当前上下文对象注入到该参数。context暴露了以下方法供用户使用:

function require(module: string): any;

用于加载外部模块

var vscode = context.require('vscode');

PS: 对于大部分情况,vscode模块将会是最常使用的,相关vscode-api请点击这里查看。

function getEditor(): vscode.TextEditor;

获取当前要执行指令的编辑器

var editor = context.getEditor();

function getProcessSelection(): vscode.Range;

获取当前待处理区域。请注意以下两点:

var editor = context.getEditor();
var selection = context.getProcessSelection();
var text = editor.document.getText(selection);
context.print(text);

public async edit(changes: [vscode.Range, string][]): Promise<boolean>;

更新指定区域文本,支持一次更改多个,在一个事务操作中完成。
例如以下代码将每行第一个字符转成大写:

module.exports.toUpper = async function (context) {
    var vscode = context.require('vscode');
    var editor = context.getEditor();
    var changes = new Array();
    var document = editor.document;
    for (var index = 0; index < document.lineCount; index++) {
        var line = document.lineAt(index);
        if (line.isEmptyOrWhitespace) {
            continue;
        }
        var range = line.range.with(new vscode.Position(line.lineNumber, 0), new vscode.Position(line.lineNumber, 1));
        var changeText = document.getText(range).toUpperCase();
        changes.push([range, changeText]);
    }
    await context.edit(changes);
}

public isColumnBlock(): boolean;

判断当前选择内容是否列模式

 var columnBlock = context.isColumnBlock();
 context.print(columnBlock);

public print(): void;

打印文本

配置指令文件路径

1)通过文件菜单 -> 首选项 -> 设置 -> 扩展 -> PowerTools,然后点击在setting.json中编辑
2)在配置文件末尾加上以下节点:
"powertools.CommandFiles": []

操作演示:
[图片上传失败...(image-5f4220-1578396407206)]

Release Notes

1.0.1

支持用户自定义扩展指令

1.0.0

Initial release

Enjoy!

上一篇 下一篇

猜你喜欢

热点阅读