自己编写了个vscode插件file-header
2018-09-17 本文已影响320人
gstorm
自己参考vsc的vscode-fileheader和vscode_extend-jsinfo扩展写了个插件
extension.ts:
'use strict';
// 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';
let vscode=require('vscode');
let wp = vscode.workspace;
let pyComment=[
'# @Author:${this.author}',
'# @Date:${this.createTime}',
'# @LastModifiedBy:${this.author}',
'# @Last Modified time:${this.updateTime}',
''
];
let tjComment=[
'/***',
' * @Author:${this.author}',
' * @Date:${this.createTime}',
' * @LastModifiedBy:${this.author}',
' * @Last Modified time:${this.updateTime}',
' */',
'',
''
];
export function activate(context:any) {
var config = wp.getConfiguration('fileheader');
console.log('Congratulations, your extension "file-header" is now active!');
console.log(config.Author);
// 添加文件头信息
let addFileHeader = vscode.commands.registerCommand('extension.addFileHeader', () => {
vscode.commands.executeCommand("workbench.action.files.save");
/**
* 判断文件类型,使用对应格式的header注释
*/
// let editor = vscode.editor || vscode.window.activeTextEditor;
let editor = vscode.editor || vscode.window.activeTextEditor;
// if(!editor){
// return;
// }
editor.edit(function(editBuilder:any){
let dm = editor.document;
let fileName = dm.fileName;
console.log(fileName);
let time = format(new Date());
console.log(time);
console.log(config.Author)
let data={
author:config.Author,
createTime:time,
LastModifiedBy:config.Author,
updateTime:time
};
let lineCount = dm.lineCount;
let fileCode = getFileCode(fileName);
switch (fileCode) {
case 1://Python文件
for(var i=0;i<lineCount;i++){
let lineText = dm.lineAt(i).text;
lineText = lineText.trim();
console.log('linetext',lineText);
//没有headComment信息,直接插入
if(lineText !== '' && !lineText.startsWith('# @')){
editBuilder.insert(new vscode.Position(0, 0), getFileInfo(pyComment, data));
break;
}
//如果已经有headComment信息,进行更新
if(lineText.startsWith('# @')){
editBuilder.replace(new vscode.Range(i+2,0,i+4,0), getFileInfo(pyComment.slice(2,pyComment.length), data));
break;
}
}
break;
case 2://js,ts文件
for(var j = 0; j < lineCount; j++){
let lineText = dm.lineAt(j).text;
lineText = lineText.trim();
console.log('linetext',lineText);
//没有headComment信息,直接插入
if(lineText !== '' && !lineText.startsWith('/***')){
let wse = new vscode.WorkspaceEdit();
wse.insert(dm.uri, new vscode.Position(0, 0), getFileInfo(tjComment, data));
wp.applyEdit(wse);
break;
}
//如果已经有headComment信息,进行更新
if(lineText.startsWith('/***')){
let wse = new vscode.WorkspaceEdit();
wse.replace(dm.uri, new vscode.Range(j+3,0,j+7,0), getFileInfo(tjComment.slice(3,tjComment.length), data));
wp.applyEdit(wse);
break;
}
}
default:
break;
}
});
});
//保存文件时更新文件头信息
let saveComment = vscode.commands.registerCommand('extension.addComment', () => {
vscode.window.showInformationMessage('Hello Wanggggg!');
});
context.subscriptions.push(addFileHeader);
context.subscriptions.push(saveComment);
}
export function deactivate() {
}
function format(d:Date){
let year = d.getFullYear();
let month = (d.getMonth()+1)<10?'0'+(d.getMonth()+1):(d.getMonth()+1);
let day = d.getDate()<10?'0'+d.getDate():d.getDate();
let time = new Date().toTimeString().substr(0,8);
return `${year}-${month}-${day} ${time}`;
}
function getFileInfo(m:string[],u:{}) {
let a = [];
a[0] = 'let j=[]';
for (let i = 0; i < m.length; i++) {
const element = m[i];
a.push(`j.push(\`${element}\`)`);
}
a.push(`return j.join('\\r\\n')`);
let s= a.join(';');
return new Function(s).apply(u);
}
function getFileCode(m:string){
const pyType = /.*\.py/;
const jsType = /.*\.js$/;
const tsType = /.*\.ts/;
let n:number=0;
if(pyType.exec(m)){
n=1;
}else if(jsType.exec(m)||tsType.exec(m)){
n=2;
}
return n;
}
package.json:
{
"name": "file-header",
"displayName": "file-header",
"description": "insert file head comment",
"version": "0.0.2",
"publisher": "wangzx",
"engines": {
"vscode": "^1.27.0"
},
"categories": [
"Other"
],
"activationEvents": [
"onCommand:extension.addFileHeader"
],
"main": "./out/extension",
"contributes": {
"configuration":{
"title": "file-header Configuration",
"properties": {
"fileheader.Author":{
"type":"string",
"default":"xxx",
"description":"文件的创建者与修改者,默认xxx,Author who created/updated the file"
}
}
},
"commands": [
{
"command": "extension.addFileHeader",
"title": "Hello World"
}
],
"keybindings": [
{
"command": "extension.addFileHeader",
"key": "ctrl+s"
},
{
"command": "extension.addFileHeader",
"key": "ctrl+shift+y"
}
]
},
"scripts": {
"vscode:prepublish": "npm run compile",
"compile": "tsc -p ./",
"watch": "tsc -watch -p ./",
"postinstall": "node ./node_modules/vscode/bin/install",
"test": "npm run compile && node ./node_modules/vscode/bin/test"
},
"devDependencies": {
"typescript": "^2.6.1",
"vscode": "^1.1.21",
"tslint": "^5.8.0",
"@types/node": "^8.10.25",
"@types/mocha": "^2.2.42"
}
}
使用快捷键ctrl+s
或ctrl+shift+y
启用。
不过安装后有个bug,修改配置fileheader.Author后,不能及时同步更新,需要重新启动才行,不知道为什么。
先记录下来。