forked from zhaopengme/vscode-fileheader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
198 lines (176 loc) · 6.89 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
* @Author: mikey.zhaopeng
* @Date: 2016-07-29 15:57:29
* @Last Modified by: Eastegg
* @Last Modified time: 2018-02-5 18:05:49
*/
var vscode = require('vscode');
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;
}
function activate(context) {
var config = vscode.workspace.getConfiguration('fileheader');
console.log('"vscode-fileheader" is now active!');
var disposable = vscode.commands.registerCommand('extension.fileheader', function () {
var editor = vscode.editor || vscode.window.activeTextEditor;
/*
* @Author: huangyuan
* @Date: 2017-02-28 17:51:35
* @Last Modified by: huangyuan413026@163.com
* @Last Modified time: 2017-02-28 17:51:35
* @description: 在当前行插入,而非在首行插入
*/
var line = editor.selection.active.line;
editor.edit(function (editBuilder) {
var time = new Date().format("yyyy-MM-dd hh:mm:ss");
var data = {
author: config.Author,
email: config.Email,
lastModifiedBy: config.LastModifiedBy,
createTime: time,
updateTime: time
}
try {
var tpl = new template(config.tpl).render(data);;
editBuilder.insert(new vscode.Position(line, 0), tpl);
} catch (error) {
console.error(error);
}
});
});
context.subscriptions.push(disposable);
vscode.workspace.onDidSaveTextDocument(function (file) {
setTimeout(function () {
try {
var f = file;
var editor = vscode.editor || vscode.window.activeTextEditor;
var document = editor.document;
var isReturn = false;
var authorRange = null;
var authorText = null;
var lastTimeRange = null;
var lastTimeText = null;
var diff = -1;
var lineCount = document.lineCount;
var comment = false;
for (var i = 0; i < lineCount; i++) {
var linetAt = document.lineAt(i);
var line = linetAt.text;
line = line.trim();
if (line.startsWith("/*") && !line.endsWith("*/")) {//是否以 /* 开头
comment = true;//表示开始进入注释
} else if (comment) {
if (line.endsWith("*/")) {
comment = false;//结束注释
}
var range = linetAt.range;
if (line.indexOf('@Last\ Modified\ by') > -1) {//表示是修改人
authorRange = range;
authorText=' * @Last Modified by: ' + config.LastModifiedBy;
} else if (line.indexOf('@Last\ Modified\ time') > -1) {//最后修改时间
var time = line.replace('@Last\ Modified\ time:', '').replace('*', '');
var oldTime = new Date(time);
var curTime = new Date();
var diff = (curTime - oldTime) / 1000;
lastTimeRange = range;
lastTimeText=' * @Last Modified time: ' + curTime.format("yyyy-MM-dd hh:mm:ss");
}
if (!comment) {
break;//结束
}
}
}
if ((authorRange != null) && (lastTimeRange != null) && (diff > 20)) {
setTimeout(function () {
editor.edit(function (edit) {
edit.replace(authorRange, authorText);
edit.replace(lastTimeRange, lastTimeText);
});
document.save();
}, 200);
}
} catch (error) {
console.error(error);
}
}, 200);
});
}
function getConfiguration() {
return vscode.workspace.getConfiguration('mocha');
}
function getLineText(lineNum, editor) {
const document = editor.document;
if (lineNum >= document.lineCount) {
return '';
}
const start = new vscode.Position(lineNum, 0);
const lastLine = document.lineAt(lineNum);
const end = new vscode.Position(lineNum, lastLine.text.length);
const range = new vscode.Range(start, end);
var t = document.getText(range);
return t;
}
function replaceLineText(lineNum, text, editor) {
const document = editor.document;
if (lineNum >= document.lineCount) {
return '';
}
const start = new vscode.Position(lineNum, 0);
const lastLine = document.lineAt(lineNum);
const end = new vscode.Position(lineNum, lastLine.text.length);
const range = new vscode.Range(start, end);
editor.edit(function (edit) {
edit.replace(range, text);
});
}
/**
* template engine
*/
function template(tpl) {
var
fn,
match,
code = ['var r=[];\nvar _html = function (str) { return str.replace(/&/g, \'&\').replace(/"/g, \'"\').replace(/\'/g, \''\').replace(/</g, \'<\').replace(/>/g, \'>\'); };'],
re = /\{\s*([a-zA-Z\.\_0-9()]+)(\s*\|\s*safe)?\s*\}/m,
addLine = function (text) {
code.push('r.push(\'' + text.replace(/\'/g, '\\\'').replace(/\n/g, '\\n').replace(/\r/g, '\\r') + '\');');
};
while (match = re.exec(tpl)) {
if (match.index > 0) {
addLine(tpl.slice(0, match.index));
}
if (match[2]) {
code.push('r.push(String(this.' + match[1] + '));');
}
else {
code.push('r.push(_html(String(this.' + match[1] + ')));');
}
tpl = tpl.substring(match.index + match[0].length);
}
addLine(tpl);
code.push('return r.join(\'\');');
fn = new Function(code.join('\n'));
this.render = function (model) {
return fn.apply(model);
};
}
exports.activate = activate;
function deactivate() { }
exports.deactivate = deactivate;