Skip to content

Commit 5e129f4

Browse files
committed
new context menu, print all parameters command,..
- var from shell command output - update documentation
1 parent f7ee728 commit 5e129f4

6 files changed

Lines changed: 6635 additions & 119 deletions

File tree

.gitignore

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
node_modules/*
2-
.vscode/*
1+
node_modules
2+
.vscode/
3+
.vscode-test/
4+
*.vsix

README.md

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
This extension allow you to insert timestamp, copyright or any information to your file like comment below
44

55
/*
6-
* Created on Tue Feb 18 2020
6+
* Created on Tue Feb 18 2025
77
*
8-
* Copyright (c) 2020 - Your Company
8+
* Copyright (c) 2025 - Your Company
99
*/
1010

1111
## Features
1212

13-
- insert defined parameter like `date`, `time`, `datetime`, `day`, `month`, `month3`, `year`, `hour`, `minute`, `second`, `company`, `filename`
14-
- insert your own parameter and template
15-
- define multiple templates
13+
- insert defined variable like `date`, `time`, `datetime`, `day`, `month`, `month3`, `year`, `hour`, `minute`, `second`, `company`, `filename`, `workspacename`, `username`
14+
- insert your own variable and template
15+
- create variable from shell command output (experimental)
16+
- create multiple templates
1617

1718
## Install
1819

@@ -26,14 +27,18 @@ Execute it from `Command Pallete` (menu View - Command Pallete...) then type com
2627

2728
1. `FileHeaderComment: Insert Default Template at Cursor`
2829
2. `FileHeaderComment: Select from Available Templates`
30+
3. `FileHeaderCOmment: Print All Variables`
2931

30-
The second command will show your available templates defined in Settings
32+
These commands can also be accessed from the context menu.
3133

32-
If you want to set your own parameter and template (set from menu Preferences - User Settings), you can read explanation below
34+
The second command will display the available templates defined in Settings.
35+
You can see all available variables using command number 3.
36+
37+
If you want to set your own variables and templates (set from the Preferences - User Settings menu), you can read the explanation below
3338

3439
This is default configuration
3540

36-
```
41+
```json
3742
"fileHeaderComment.parameter":{
3843
"*":{
3944
"commentbegin": "/*",
@@ -53,21 +58,48 @@ This is default configuration
5358
}
5459
```
5560

56-
Define all custom variables/paramenters in asterisk `*` like
61+
Define all custom variables/parameters in asterisk `*` like
5762

63+
```json
64+
"fileHeaderComment.parameter":{
65+
"*":{
66+
"company": "Your Company"
67+
"myvar1": "My Variable 1",
68+
"myvar2": "My Variable 2"
69+
}
70+
}
5871
```
72+
73+
You can create/override variables for specific file language, ie python
74+
75+
```json
5976
"fileHeaderComment.parameter":{
6077
"*":{
6178
"company": "Your Company"
6279
"myvar1": "My Variable 1",
6380
"myvar2": "My Variable 2"
81+
},
82+
"python": {
83+
"commentbegin": "\"\"\"",
84+
"commentend": "\"\"\"",
85+
"commentprefix": ""
6486
}
6587
}
6688
```
6789

68-
Use your variable in template like (asterisk `*` will be default template)
90+
(Experimental) You can get shell command output as a variable like
6991

92+
```json
93+
"fileHeaderComment.parameter: {
94+
"myvar3": "cmd(ls -alh)"
95+
}"
7096
```
97+
my_command inside `cmd(my_command)` i.e. `ls -alh` in the example above will be executed in the current file directory. It only captures the last line output of 255 characters.
98+
99+
100+
Use your variable in template like (asterisk `*` will be default template)
101+
102+
```json
71103
"fileHeaderComment.template":{
72104
"*":[
73105
"${commentbegin}",
@@ -81,7 +113,7 @@ Use your variable in template like (asterisk `*` will be default template)
81113
```
82114
You can define multiple templates, for instance template for MIT License
83115

84-
```
116+
```json
85117
"fileHeaderComment.parameter":{
86118
"*":{
87119
"author": "Your Name",
@@ -117,33 +149,47 @@ You can define multiple templates, for instance template for MIT License
117149
```
118150
You can use your `mit` template above by calling it through `Command Pallete` and choose `FileHeaderComment: Select from Available Templates`.
119151

120-
You can use parameters below in your template
152+
You can use variables below in your template
121153

154+
- `company` : print "Your Company"
122155
- `date` : print current date
123-
- `time` : print current time
124156
- `time24h` : print current time in 24 hour format
125157
- `datetime`: print current date + time
126158
- `datetime24h` : print current date + time in 24 hour format
127-
- `company` : print "Your Company"
128159
- `day`: print day of the month
129-
- `month`: print current month
130-
- `month3`: first-3 letters of the month, e.g "Mar"
131-
- `year`: print current year
160+
- `filename`: print filename,
132161
- `hour`: print current hour (24h)
133162
- `minute`: print current minute
163+
- `month`: print current month
164+
- `month3`: first-3 letters of the month, e.g "Mar"
165+
- `now`: same as datetime
166+
- `now24h`: same as datetime24h
134167
- `second`: print current second
135-
- `filename`: print filename
168+
- `time` : print current time
169+
- `time24h`: print current time in 24 hour format
170+
- `username`: user name
171+
- `weeknumber` : print week number,
172+
- `workspacename`: workspace name,
173+
- `year`: print current year
174+
- `yearshort`: print 2 digit of current year
136175

137176

138177

139178
## Release Notes
140179
### 0.0.6
180+
- variable for specific file language
141181
- add month3 (thanks to @kfsone)
182+
- add workspacename (thanks to @ljahier)
183+
- add username (thanks to @mosayyeb-ebrahimi)
184+
- add context menu (thanks to @bwilliams-sequence)
185+
- custom variable from shell command output
186+
142187
### 0.0.5
143188
- fixing python comment style (thanks to @ronak1009)
189+
144190
### 0.0.4
145191
- support yaml, shellscript language (thanks to @waddyvic)
146-
- add day, month, hour, minute, second, filename parameter (thanks to @rcabg, @ternvein)
192+
- add day, month, hour, minute, second, filename variables (thanks to @rcabg, @ternvein)
147193

148194
### 0.0.3
149195
- fixing "unknown configuration setting" message in Settings (thanks to @isuda)
@@ -153,5 +199,4 @@ You can use parameters below in your template
153199
- bugfixes
154200

155201
### 0.0.1
156-
157202
- Initial release

command.js

Lines changed: 123 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,47 +19,50 @@
1919
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2020
*/
2121

22-
var vscode = require('vscode');
22+
var vscode = require('vscode'),
23+
os = require('os'),
24+
exec_sync = require("child_process").execSync
2325

24-
function insertFileHeaderComment(picked_template){
26+
// run shell command and get last line
27+
function run_shell_command(command){
28+
var output = ""
29+
try{
30+
output = exec_sync(command, {encoding: 'utf8', cwd: vscode.window.activeTextEditor.document.fileName.split("/").slice(0,-1).join("/")});
31+
if(output){
32+
// take the last line of the output, trim it, and limit to 255 characters
33+
output = (output.trim().split("\n").pop()+"").trim().substring(0, 255)
34+
}
35+
}catch(e){
36+
}
37+
return output
38+
}
39+
40+
//https://www.w3resource.com/javascript-exercises/javascript-date-exercise-24.php
41+
Date.prototype.ISO8601_week_number = function(){
42+
var tdt = new Date(this.valueOf()),
43+
dayn = (this.getDay() + 6) % 7;
44+
tdt.setDate(tdt.getDate() - dayn + 3);
45+
var firstThursday = tdt.valueOf();
46+
tdt.setMonth(0, 1);
47+
if (tdt.getDay() !== 4){
48+
tdt.setMonth(0, 1 + ((4 - tdt.getDay()) + 7) % 7);
49+
}
50+
return 1 + Math.ceil((firstThursday - tdt) / 604800000);
51+
}
52+
53+
function get_all_variables(is_all){
2554
var workspace = vscode.workspace,
2655
editor = vscode.window.activeTextEditor,
27-
// root = workspace.rootPath,
2856
prefix = 'fileHeaderComment',
2957
lang_id = editor.document.languageId,
30-
t_default = workspace.getConfiguration(prefix+".template").get('*'),
31-
// t_lang = workspace.getConfiguration(prefix+".template".get(lang_id),
32-
r_default = workspace.getConfiguration(prefix+".parameter").get('*'),
3358
r_lang = workspace.getConfiguration(prefix+".parameter").get(lang_id),
34-
template = [];
35-
if(picked_template){
36-
t_default = workspace.getConfiguration(prefix+".template").get(picked_template);
37-
var tmp_r_default = workspace.getConfiguration(prefix+".parameter").get(picked_template);
38-
if(tmp_r_default instanceof Object){
39-
Object.assign(r_default, tmp_r_default);
40-
}
41-
}
42-
43-
//remove feature template detection per language
44-
//this feature is replaced by "select from available templates"
45-
// if((t_lang instanceof Array)){
46-
// template = t_lang;
47-
// }else
48-
if(t_default instanceof Array){
49-
template = t_default;
50-
}else{
51-
template = [
52-
"${commentbegin}",
53-
"${commentprefix} Created on ${date}",
54-
"${commentprefix}",
55-
"${commentprefix} Copyright (c) ${year} ${company}",
56-
"${commentend}"
57-
];
58-
}
59+
r_default = workspace.getConfiguration(prefix+".parameter").get('*');
60+
5961
var date = new Date(),
6062
h = (date.getHours()+"").padStart(2, '0'),
6163
m = (date.getMinutes()+"").padStart(2, '0'),
6264
s = (date.getSeconds()+"").padStart(2, '0'),
65+
username = os.userInfo().username,
6366
replace = {
6467
'date': date.toDateString(),
6568
'time': date.toLocaleTimeString(),
@@ -70,12 +73,14 @@ function insertFileHeaderComment(picked_template){
7073
'year': date.getFullYear(),
7174
'company': 'Your Company',
7275
'filename': vscode.window.activeTextEditor.document.fileName.replace(/^.*[\\\/]/, ''),
73-
'workspaceName': vscode.workspace.name,
76+
'workspacename': vscode.workspace.name,
7477
'hour': h,
7578
'minute': m,
76-
'second': s
79+
'second': s,
80+
'weeknumber': date.ISO8601_week_number(),
81+
'yearshort': (date.getFullYear()+"").slice(-2),
82+
username,
7783
};
78-
7984
replace = Object.assign(replace, {
8085
'commentbegin': '/*',
8186
'commentprefix': ' *',
@@ -85,8 +90,8 @@ function insertFileHeaderComment(picked_template){
8590
});
8691
replace.now = replace.datetime;
8792
replace.now24h = replace.datetime24h;
88-
Object.assign(replace, r_default);
89-
93+
Object.assign(replace, JSON.parse(JSON.stringify(r_default)));
94+
9095
switch(lang_id){
9196
case "swift":
9297
replace.commentbegin = "/**";
@@ -102,6 +107,7 @@ function insertFileHeaderComment(picked_template){
102107
case "ruby":
103108
case "shellscript":
104109
case "yaml":
110+
case "cmake":
105111
case "python":
106112
Object.assign(replace, {
107113
'commentbegin': "#",
@@ -134,16 +140,70 @@ function insertFileHeaderComment(picked_template){
134140
}
135141
Object.assign(replace, r_lang);
136142

143+
for(var r_key in replace){
144+
var r_val = replace[r_key]+""
145+
// find cmd() syntax and replace it with the output (last line) of the command
146+
if(r_val && r_val.indexOf("cmd(") === 0){
147+
var shell_command = r_val.match(/^cmd\((.+?)\)$/);
148+
if(shell_command && shell_command[1]){
149+
replace[r_key] = run_shell_command(shell_command[1]);
150+
}
151+
}
152+
if(!is_all && Array.isArray(replace[r_key])){
153+
// take out if array
154+
delete replace[r_key];
155+
}
156+
}
157+
return JSON.parse(JSON.stringify(replace));
158+
}
159+
160+
function insertFileHeaderComment(picked_template){
161+
var workspace = vscode.workspace,
162+
editor = vscode.window.activeTextEditor,
163+
// root = workspace.rootPath,
164+
prefix = 'fileHeaderComment',
165+
lang_id = editor.document.languageId,
166+
t_default = workspace.getConfiguration(prefix+".template").get('*'),
167+
// t_lang = workspace.getConfiguration(prefix+".template".get(lang_id),
168+
r_default = workspace.getConfiguration(prefix+".parameter").get('*'),
169+
r_lang = workspace.getConfiguration(prefix+".parameter").get(lang_id),
170+
template = [];
171+
if(picked_template){
172+
t_default = workspace.getConfiguration(prefix+".template").get(picked_template);
173+
var tmp_r_default = workspace.getConfiguration(prefix+".parameter").get(picked_template);
174+
if(tmp_r_default instanceof Object){
175+
Object.assign(r_default, tmp_r_default);
176+
}
177+
}
178+
179+
//remove feature template detection per language
180+
//this feature is replaced by "select from available templates"
181+
// if((t_lang instanceof Array)){
182+
// template = t_lang;
183+
// }else
184+
if(t_default instanceof Array){
185+
template = t_default;
186+
}else{
187+
template = [
188+
"${commentbegin}",
189+
"${commentprefix} Created on ${date}",
190+
"${commentprefix}",
191+
"${commentprefix} Copyright (c) ${year} ${company}",
192+
"${commentend}"
193+
];
194+
}
195+
var replace = get_all_variables(true);
196+
137197
if(!editor)
138198
return;
139-
replace = JSON.parse(JSON.stringify(replace));
199+
140200
var s_template = template.join("\n"),
141201
escape = function(string) {
142202
return string.replace(/([.*+?^${}()|\[\]\/\\])/g, "\\$1");
143203
};
144-
for(var r in replace){
145-
var regexp = new RegExp(escape("${"+r+"}"), "gi"),
146-
replace_with = replace[r];
204+
for(var r_key in replace){
205+
var regexp = new RegExp(escape("${"+r_key+"}"), "gi"),
206+
replace_with = replace[r_key];
147207
if(replace_with.join){
148208
replace_with = replace_with.join("\n"+ replace.commentprefix);
149209
}else if(replace_with.replace){
@@ -186,3 +246,26 @@ function insertFileHeaderCommentOther(){
186246
});
187247
}
188248
exports.insertFileHeaderCommentOther = insertFileHeaderCommentOther;
249+
250+
exports.printAllParameters = function(){
251+
var editor = vscode.window.activeTextEditor;
252+
editor.edit(function(edit){
253+
var unordered = get_all_variables(),
254+
ordered = {};
255+
Object.keys(unordered).sort().forEach(function(key) {
256+
ordered[key] = unordered[key];
257+
});
258+
var output = []
259+
output.push(ordered['commentbegin']);
260+
output.push(ordered['commentprefix']+" Available parameters:");
261+
for(var key in ordered){
262+
if(['commentbegin', 'commentprefix', 'commentend'].indexOf(key) !== -1){
263+
continue;
264+
}
265+
output.push(ordered['commentprefix']+" "+key+": "+ordered[key]);
266+
}
267+
output.push(ordered['commentend']);
268+
// edit.insert(editor.selection.active, JSON.stringify(ordered, null, 2)+"\n");
269+
edit.insert(editor.selection.active, output.join("\n")+"\n");
270+
});
271+
};

0 commit comments

Comments
 (0)