原创 艾逗笔 2021-03-19 17:32
本文从实际需求出发,写了一个能帮助开发者日志导数据的小插件。
let editor = vscode.window.activeTextEditor;
let selection = editor?.selection;
let text = editor?.document.getText(selection);
内容替换插件的核心功能是把编辑器选择的 sql 查询结果转换成能保存到 csv 文件的内容。我们分析后发现,sql 查询结果通过大量的“|”来做内容的分割,而一个 csv 文件,用文本框打开后是用“,”分割单元格的。所以我们要做的是把“|”替换成“,”,并且要去掉表头和表尾的分割线。sql resultcsv content要做内容替换,比较简单的方式就是用正则表达式。打开 https://regex101.com/,粘贴我们要替换的内容,通过调试找到一个正确的正则表达式:/\|((.*)\|){1,}/g 可以用来做多行匹配。匹配后获得的每一行数据都是这种格式| 6f9fa41be49979251202a5cf23d500da | 1 | 2021-03-19 12:01:52 |我们只需要把“|”替换成“,”,再去除首尾的“,”就行了。最后写出来的转换逻辑是这样:let editor = vscode.window.activeTextEditor;
let selection = editor?.selection;
let text = editor?.document.getText(selection);
let arr = text?.match(/\|((.*)\|){1,}/g);
let newArr: string[] = [];
arr?.forEach(function (item, i) {
let str = item.replace(/\s*\|\s*/g, ",");
str = str.replace(/^\,/, "").replace(/\,$/, "");
newArr.push(str);
});
let content = newArr.join("\n");
保存文件完成内容转换后,下一步我们就可以写保存文件的逻辑了。vscode 已经封装了相关的 api,我们只需要通过几行代码即可完成调用。// 打开文件保存对话框
let uri = await vscode.window.showSaveDialog({
filters: {
csv: ["csv"],
},
});
if (uri == undefined) {
vscode.window.showErrorMessage("invalid save path");
return;
}
// 保存文件
let writeData = Buffer.from(content, "utf8");
vscode.workspace.fs.writeFile(uri, writeData);
完整逻辑最后再加上必要的错误提示,和保存后打开文件的操作,这个插件的核心逻辑就写完了。src/extension.ts// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from "vscode";
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {
// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
console.log('Congratulations, your extension "sqlresult" is now active!');
// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
let disposable = vscode.commands.registerCommand(
"sqlresult.tocsv",
async () => {
let editor = vscode.window.activeTextEditor;
let selection = editor?.selection;
let text = editor?.document.getText(selection);
let arr = text?.match(/\|((.*)\|){1,}/g);
let newArr: string[] = [];
arr?.forEach(function (item, i) {
let str = item.replace(/\s*\|\s*/g, ",");
str = str.replace(/^\,/, "").replace(/\,$/, "");
newArr.push(str);
});
let content = newArr.join("\n");
if (content == "") {
vscode.window.showErrorMessage("invalid content");
return;
}
let uri = await vscode.window.showSaveDialog({
filters: {
csv: ["csv"],
},
});
if (uri == undefined) {
vscode.window.showErrorMessage("invalid save path");
return;
}
let writeData = Buffer.from(content, "utf8");
vscode.workspace.fs.writeFile(uri, writeData);
// Display a message box to the user
let openFile = "Open File";
vscode.window
.showInformationMessage("file saved", openFile)
.then((value) => {
if (value === openFile) {
vscode.window.showTextDocument(uri!, {});
}
});
}
);
context.subscriptions.push(disposable);
}
// this method is called when your extension is deactivated
export function deactivate() {}
插件使用在 vscode 扩展中心搜索sqlresult并安装插件安装完插件,复制 sql 查询结果到编辑器,全选内容,执行插件保存 csv 文件的命令:打开保存后的 csv 文件,内容看起来非常工整。最后把 csv 文件发给运营同事,就可以收工了。总结本文从实际需求出发,写了一个能帮助开发者日志导数据的小插件。vscode 是一个“真香”神器,丰富的插件给了开发者很多想象的空间,强烈安利一波。插件源码奉上,欢迎赐教:https://github.com/idoubi/sqlresult