/
extension.js
121 lines (103 loc) · 3.9 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* @Author: liumeng6
* @Date: 2017-11-27 19:38:38
* @Last Modified by: liumeng6
* @Last Modified time: 2017-12-04 20:17:09
*/
const vscode = require('vscode')
const indentString = require('indent-string')
const Mustache = require('mustache')
/**
* @Author: liumeng6
* @Date: 2017-11-29 19:49:22
* @Desc: 格式化时间
*/
Date.prototype.format = function (format) {
var o = {
"M+": this.getMonth() + 1, //month
"d+": this.getDate(), //day
"h+": this.getHours(), //hour
"m+": this.getMinutes(), //minute
"s+": this.getSeconds(), //second
"q+": Math.floor((this.getMonth() + 3) / 3), //quarter
"S": this.getMilliseconds() //millisecond
}
if (/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length))
}
for (var k in o) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length))
}
}
return format
}
/**
* @Author: liumeng6
* @Date: 2017-11-29 19:49:38
* @Desc: es6模版字符串引擎
*/
// function stp(strTmpl, data) {
// if (data !== null && data !== undefined) {
// return stp(strTmpl)(data)
// }
// strTmpl = strTmpl.replace(/\\/g, '\\\\')
// strTmpl = strTmpl.replace(/`/g, '\\`')
// return new Function("$", "with($){return `" + strTmpl + '`;}')
// }
/**
* @Author: liumeng6
* @Date: 2017-11-29 19:49:54
* @Desc: activate函数
*/
function activate(context) {
const config = vscode.workspace.getConfiguration('docBlocker')
console.log('Congratulations, your extension "docblocker" is now active!')
let disposable = vscode.commands.registerCommand('extension.docblocker', function () {
const editor = vscode.editor || vscode.window.activeTextEditor
const startLine = editor.selection.active.line
// const character = editor.selection.active.character
const time = new Date().format("yyyy-MM-dd hh:mm:ss")
const defaultTpl = config.defaultTpl
const data = {
author: config.Author,
createTime: time,
}
try {
// 当前文件所属的编程语言
const languageId = vscode.window.activeTextEditor.document.languageId
// 默认注释字符串
let textToInsert = Mustache.render(defaultTpl, data)
// 判断是否针对当前语言有特殊注释模版
if (config.languageTpl) {
config.languageTpl.map(langTpl => {
if (langTpl.language === languageId && langTpl.tpl) {
textToInsert = Mustache.render(langTpl.tpl, data)
}
})
}
let pos = new vscode.Position(startLine, 0)
const line = vscode.window.activeTextEditor.document.lineAt(editor.selection.start.line).text
const firstNonWhiteSpace = vscode.window.activeTextEditor.document.lineAt(editor.selection.start.line).firstNonWhitespaceCharacterIndex
let stringToIndent = ''
for (let i = 0; i < firstNonWhiteSpace; i++) {
if (line.charAt(i) == '\t') {
stringToIndent = stringToIndent + '\t';
}
else if (line.charAt(i) == ' ') {
stringToIndent = stringToIndent + ' ';
}
}
textToInsert = indentString(textToInsert, 1, {indent: stringToIndent})
editor.insertSnippet(new vscode.SnippetString(textToInsert), pos)
} catch (error) {
console.error(error)
}
})
context.subscriptions.push(disposable)
}
exports.activate = activate
// this method is called when your extension is deactivated
function deactivate() {
}
exports.deactivate = deactivate