Skip to content

Commit 4aa1780

Browse files
committed
added stuff for v0.0.2
1 parent 0b637b5 commit 4aa1780

File tree

3 files changed

+153
-85
lines changed

3 files changed

+153
-85
lines changed

README.md

+34-4
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,47 @@ This extension contributes the following settings:
8686
]
8787
```
8888

89+
As you can already see, each header template object is of form:
90+
```
91+
{
92+
"<languageId>": {
93+
"headerBegin": <string value>,
94+
"headerPrefix": <string value>,
95+
"headerEnd": <string value>,
96+
"template": [
97+
<string values with parameters>
98+
]
99+
}
100+
}
101+
```
102+
103+
To use the parameters inside the `template`, use the `${<parameter name>}` notation.
104+
89105

90106
Topper has a few intrinsic template parameters:
91-
* `createdDate` - The date when the `Topper: Add header` command is invoked.
92-
* `lastModifiedDate` - The date when the `Topper: Add header` command is invoked. [This is a WIP, I'm trying to make it auto updatable with every save.]
107+
* `createdDate` - The date when the file was created, this is obtained from the underlying OS. If the file is `Untitled-x` or unsaved, the created time defaults to the time `Topper: Add Header` command was invoked.
108+
109+
110+
* `lastModifiedDate` - The date when the file was modified, this too is obtained from the underlying OS. If the file is `Untitled-x` or unsaved, the created time defaults to the time `Topper: Add Header` command was invoked.
111+
112+
93113
* `fileName` - The name of the file
114+
115+
94116
* `fileVersion` - The VSCode maintained fileVersion
95117

96118

97-
### 0.0.1
119+
## Changelog
98120

99-
Initial release of `Topper` extension. [Alpha]
121+
* ### 0.0.2 [Alpha]
122+
123+
* Added support for multi line strings as values for the custom template parameters.
124+
* Updated `lastModifiedDate` to be fetched from the underlying OS. This is the true last modified date.
125+
126+
127+
* ### 0.0.1 [Alpha]
128+
129+
Initial release of `Topper` extension. [Alpha]
100130

101131
### P.S
102132
In case of any suggestions or issues please email me at [**sidharth.mishra@sjsu.edu**](mailto:sidharth.mishra@sjsu.edu)

package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
"name": "topper",
33
"displayName": "topper",
44
"description": "A fileheader utility",
5-
"version": "0.0.1",
5+
"version": "0.0.2",
66
"publisher": "sidmishraw",
77
"engines": {
88
"vscode": "^1.13.0"
99
},
1010
"categories": [
11+
"Snippets",
1112
"Other"
1213
],
1314
"activationEvents": [
@@ -33,7 +34,7 @@
3334
"personalProfile": {
3435
"author": "bulbasaur",
3536
"website": "bulbasaur.github.bitbucket.yababbdadado.com",
36-
"copyright": "None",
37+
"copyright": "None \n None",
3738
"license": "None",
3839
"email": "ivysaur.bulbasaur@venosaur.com"
3940
}
@@ -62,6 +63,7 @@
6263
"${headerPrefix} @author ${author}",
6364
"${headerPrefix} @description ${description}",
6465
"${headerPrefix} @created ${createdDate}",
66+
"${headerPrefix} @copyright ${copyright}",
6567
"${headerPrefix} @last-modified ${lastModifiedDate}",
6668
"${headerEnd}"
6769
]

topper.js

+115-79
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,36 @@
1212

1313
// vscode import
1414
const vscode = require("vscode");
15+
const fs = require("fs");
16+
const path = require("path");
1517

1618
// for logging
1719
const LOGGING_MODE_DEBUG = "debug";
1820
const LOGGING_MODE_INFO = "info";
1921
const LOGGING_MODE_NONE = "none";
2022

23+
// topper specific constants
24+
const TOPPER = "topper";
25+
const TOPPER_CUSTOM_TEMPLATE_PARAMETERS = "customTemplateParameters";
26+
const TOPPER_HEADER_TEMPLATES = "headerTemplates";
27+
const TEMPLATE = "template";
28+
const DEFAULT_HEADER_TEMPLATE = "defaultCStyled";
29+
const HEADER_BEGIN = "headerBegin";
30+
const HEADER_PREFIX = "headerPrefix";
31+
const HEADER_END = "headerEnd";
32+
33+
// vscode apis
34+
const window = vscode.window;
35+
const workspace = vscode.workspace;
36+
2137
/**
2238
* Logging utility
2339
*
2440
* @param {*} message the message to be logged
2541
*/
2642
let loginfo = function (message) {
2743

28-
let logginMode = LOGGING_MODE_NONE;
44+
let logginMode = LOGGING_MODE_INFO;
2945

3046
switch (logginMode) {
3147

@@ -48,114 +64,132 @@
4864
}
4965
};
5066

51-
// vscode apis
52-
const window = vscode.window;
53-
const workspace = vscode.workspace;
54-
const editor = vscode.window.activeTextEditor;
67+
/**
68+
* Fetches the time related intrinsic parameters -- creation time and last modified times
69+
* from the underlying OS.
70+
*
71+
* @param {*} callback the callback function that is executed after computation of the intrinsic params
72+
*/
73+
function fetchTimeIntrinsicParams(filePath, intrinsicParams, callback) {
5574

56-
if (!workspace || !editor) {
75+
fs.stat(filePath, (error, stats) => {
5776

58-
loginfo(`Situation doesn't look all that well, not going to load the extension! Workspace: ${workspace}; Editor: ${editor}`);
77+
if (error) {
5978

60-
return;
61-
}
79+
loginfo(`ERROR:: ${JSON.stringify(error)}`);
6280

63-
// topper specific constants
64-
const TOPPER = "topper";
65-
const TOPPER_CUSTOM_TEMPLATE_PARAMETERS = "customTemplateParameters";
66-
const TOPPER_HEADER_TEMPLATES = "headerTemplates";
67-
const TEMPLATE = "template";
68-
const DEFAULT_HEADER_TEMPLATE = "defaultCStyled";
81+
intrinsicParams["createdDate"] = new Date();
82+
intrinsicParams["lastModifiedDate"] = new Date();
83+
} else {
6984

70-
// filename of the active document
71-
let filePath = editor.document.uri.fsPath;
72-
let fileName = filePath.split(vscode.Uri._slash)[filePath.split(vscode.Uri._slash).length - 1];
73-
let fileVersion = editor.document.version;
74-
let languageId = editor.document.languageId;
75-
76-
// topper intrinsic params
77-
const intrinsicParams = {
78-
"createdDate": null,
79-
"lastModifiedDate": null,
80-
"fileName": fileName,
81-
"fileVersion": fileVersion
82-
};
85+
loginfo(`Stats received from the OS: ${JSON.stringify(stats)}`);
8386

84-
// custom topper template parameters
85-
let topperTemplateParams = workspace.getConfiguration(TOPPER).get(TOPPER_CUSTOM_TEMPLATE_PARAMETERS);
87+
intrinsicParams["createdDate"] = stats.birthtime;
88+
intrinsicParams["lastModifiedDate"] = stats.mtime;
89+
}
8690

87-
// custom header templates that are language specific
88-
let topperTemplates = workspace.getConfiguration(TOPPER).get(TOPPER_HEADER_TEMPLATES);
91+
callback();
92+
});
93+
}
8994

90-
loginfo(`FileName: ${fileName}; FileVersion: ${fileVersion}; Language: ${languageId}`);
91-
loginfo(`TopperTemplateParams from configuration: ${JSON.stringify(topperTemplateParams)}`);
92-
loginfo(`TopperTemplates from configuration: ${JSON.stringify(topperTemplates)}`);
95+
/**
96+
* The command addTopHeader adds the configured header to the active file.
97+
*/
98+
topper.addTopHeader = function () {
9399

94-
// choose the header template depending on the languageId of the file
95-
let selectedHeaderTemplate = (function () {
100+
const editor = vscode.window.activeTextEditor;
96101

97-
let selectedTemplate = null;
102+
if (!workspace || !editor) {
98103

99-
topperTemplates.forEach((headerTemplate) => {
104+
loginfo(`Situation doesn't look all that well, not going to load the extension! Workspace: ${workspace}; Editor: ${editor}`);
100105

101-
let templateName = Object.getOwnPropertyNames(headerTemplate)[0];
106+
return;
107+
}
102108

103-
if (!templateName) {
109+
// filename of the active document
110+
let filePath = editor.document.uri.fsPath;
111+
let fileName = path.parse(filePath).base;
112+
let fileVersion = editor.document.version;
113+
let languageId = editor.document.languageId;
104114

105-
return;
106-
}
115+
// topper intrinsic params
116+
const intrinsicParams = {
117+
"createdDate": null,
118+
"lastModifiedDate": null,
119+
"fileName": fileName,
120+
"fileVersion": fileVersion
121+
};
107122

108-
if (templateName.toLowerCase() === languageId.toLowerCase()) {
123+
// custom topper template parameters
124+
let topperTemplateParams = workspace.getConfiguration(TOPPER).get(TOPPER_CUSTOM_TEMPLATE_PARAMETERS);
109125

110-
selectedTemplate = headerTemplate[languageId];
111-
}
112-
});
126+
// custom header templates that are language specific
127+
let topperTemplates = workspace.getConfiguration(TOPPER).get(TOPPER_HEADER_TEMPLATES);
113128

114-
if (!selectedTemplate) {
129+
loginfo(`FileName: ${fileName}; FileVersion: ${fileVersion}; Language: ${languageId}`);
130+
loginfo(`TopperTemplateParams from configuration: ${JSON.stringify(topperTemplateParams)}`);
131+
loginfo(`TopperTemplates from configuration: ${JSON.stringify(topperTemplates)}`);
115132

116-
selectedTemplate = topperTemplates[0][DEFAULT_HEADER_TEMPLATE];
117-
}
133+
// choose the header template depending on the languageId of the file
134+
let selectedHeaderTemplate = (function () {
118135

119-
return selectedTemplate;
120-
}());
136+
let selectedTemplate = null;
121137

122-
loginfo(`\n\nSelected Header Template:: ${JSON.stringify(selectedHeaderTemplate)}`);
138+
topperTemplates.forEach((headerTemplate) => {
123139

124-
/**
125-
* The command addTopHeader adds the configured header to the active file.
126-
*/
127-
topper.addTopHeader = function () {
140+
let templateName = Object.getOwnPropertyNames(headerTemplate)[0];
141+
142+
if (!templateName) {
128143

129-
intrinsicParams["createdDate"] = new Date();
130-
intrinsicParams["lastModifiedDate"] = new Date();
144+
return;
145+
}
131146

132-
let customProfiles = new Map();
133-
let customProfileNames = [];
147+
if (templateName.toLowerCase() === languageId.toLowerCase()) {
134148

135-
// create the list of all the profile names for the selection menu
136-
topperTemplateParams.forEach(function (templateParamObject) {
149+
selectedTemplate = headerTemplate[languageId];
150+
}
151+
});
137152

138-
for (let templateParamElement in templateParamObject) {
153+
if (!selectedTemplate) {
139154

140-
customProfiles.set(templateParamElement, templateParamObject[templateParamElement]);
141-
customProfileNames.push(templateParamElement);
155+
selectedTemplate = topperTemplates[0][DEFAULT_HEADER_TEMPLATE];
142156
}
143-
});
144157

145-
loginfo(`Profile Names: ${JSON.stringify(customProfileNames)}`);
158+
return selectedTemplate;
159+
}());
160+
161+
loginfo(`\n\nSelected Header Template:: ${JSON.stringify(selectedHeaderTemplate)}`);
162+
163+
fetchTimeIntrinsicParams(filePath, intrinsicParams, () => {
164+
165+
let customProfiles = new Map();
166+
let customProfileNames = [];
146167

147-
// display the info box for the next step
148-
window.showInformationMessage("Please select the profile name to apply").then(() => {
168+
// create the list of all the profile names for the selection menu
169+
topperTemplateParams.forEach(function (templateParamObject) {
149170

150-
// show the quickpick selection menu to select the profile
151-
window.showQuickPick(customProfileNames).then((selectedProfileName) => {
171+
for (let templateParamElement in templateParamObject) {
152172

153-
loginfo(`User selected profile: ${selectedProfileName}`);
154-
let selectedTemplateParams = customProfiles.get(selectedProfileName);
173+
customProfiles.set(templateParamElement, templateParamObject[templateParamElement]);
174+
customProfileNames.push(templateParamElement);
175+
}
176+
});
177+
178+
loginfo(`Profile Names: ${JSON.stringify(customProfileNames)}`);
179+
180+
// display the info box for the next step
181+
window.showInformationMessage("Please select the profile name to apply").then(() => {
155182

156-
loginfo(`Selected Params: ${JSON.stringify(selectedTemplateParams)}`);
183+
// show the quickpick selection menu to select the profile
184+
window.showQuickPick(customProfileNames).then((selectedProfileName) => {
157185

158-
makeHeaderString(selectedTemplateParams, publishHeaderString);
186+
loginfo(`User selected profile: ${selectedProfileName}`);
187+
let selectedTemplateParams = customProfiles.get(selectedProfileName);
188+
189+
loginfo(`Selected Params: ${JSON.stringify(selectedTemplateParams)}`);
190+
191+
makeHeaderString(editor, selectedHeaderTemplate, selectedTemplateParams, intrinsicParams);
192+
});
159193
});
160194
});
161195
};
@@ -166,7 +200,7 @@
166200
* @param {*} selectedTemplateParams the template parameters of the selected template profile
167201
* @param {*} callback the function to do after initial setup is done
168202
*/
169-
function makeHeaderString(selectedTemplateParams, callback) {
203+
function makeHeaderString(editor, selectedHeaderTemplate, selectedTemplateParams, intrinsicParams) {
170204

171205
let headerTemplateLines = selectedHeaderTemplate[TEMPLATE];
172206

@@ -201,6 +235,8 @@
201235
templateValue = intrinsicParams[templateName];
202236
}
203237

238+
templateValue = templateValue.toString().replace(/\n/g, `\n${selectedHeaderTemplate[HEADER_PREFIX]}`);
239+
204240
headerLine.push(templateValue);
205241
});
206242

@@ -209,15 +245,15 @@
209245

210246
loginfo(`Header String: ${headerLines.join("\n")}`);
211247

212-
callback(headerLines.join("\n"));
248+
publishHeaderString(editor, headerLines.join("\n"));
213249
}
214250

215251
/**
216252
* Publishes the headerString to the text editor. It writes the header to the active text editor at the topmost position.
217253
*
218254
* @param {*} headerString the header string made after replacing all the templates with their values.
219255
*/
220-
function publishHeaderString(headerString) {
256+
function publishHeaderString(editor, headerString) {
221257

222258
loginfo("Publishing to the document");
223259

0 commit comments

Comments
 (0)