|
12 | 12 |
|
13 | 13 | // vscode import
|
14 | 14 | const vscode = require("vscode");
|
| 15 | + const fs = require("fs"); |
| 16 | + const path = require("path"); |
15 | 17 |
|
16 | 18 | // for logging
|
17 | 19 | const LOGGING_MODE_DEBUG = "debug";
|
18 | 20 | const LOGGING_MODE_INFO = "info";
|
19 | 21 | const LOGGING_MODE_NONE = "none";
|
20 | 22 |
|
| 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 | + |
21 | 37 | /**
|
22 | 38 | * Logging utility
|
23 | 39 | *
|
24 | 40 | * @param {*} message the message to be logged
|
25 | 41 | */
|
26 | 42 | let loginfo = function (message) {
|
27 | 43 |
|
28 |
| - let logginMode = LOGGING_MODE_NONE; |
| 44 | + let logginMode = LOGGING_MODE_INFO; |
29 | 45 |
|
30 | 46 | switch (logginMode) {
|
31 | 47 |
|
|
48 | 64 | }
|
49 | 65 | };
|
50 | 66 |
|
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) { |
55 | 74 |
|
56 |
| - if (!workspace || !editor) { |
| 75 | + fs.stat(filePath, (error, stats) => { |
57 | 76 |
|
58 |
| - loginfo(`Situation doesn't look all that well, not going to load the extension! Workspace: ${workspace}; Editor: ${editor}`); |
| 77 | + if (error) { |
59 | 78 |
|
60 |
| - return; |
61 |
| - } |
| 79 | + loginfo(`ERROR:: ${JSON.stringify(error)}`); |
62 | 80 |
|
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 { |
69 | 84 |
|
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)}`); |
83 | 86 |
|
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 | + } |
86 | 90 |
|
87 |
| - // custom header templates that are language specific |
88 |
| - let topperTemplates = workspace.getConfiguration(TOPPER).get(TOPPER_HEADER_TEMPLATES); |
| 91 | + callback(); |
| 92 | + }); |
| 93 | + } |
89 | 94 |
|
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 () { |
93 | 99 |
|
94 |
| - // choose the header template depending on the languageId of the file |
95 |
| - let selectedHeaderTemplate = (function () { |
| 100 | + const editor = vscode.window.activeTextEditor; |
96 | 101 |
|
97 |
| - let selectedTemplate = null; |
| 102 | + if (!workspace || !editor) { |
98 | 103 |
|
99 |
| - topperTemplates.forEach((headerTemplate) => { |
| 104 | + loginfo(`Situation doesn't look all that well, not going to load the extension! Workspace: ${workspace}; Editor: ${editor}`); |
100 | 105 |
|
101 |
| - let templateName = Object.getOwnPropertyNames(headerTemplate)[0]; |
| 106 | + return; |
| 107 | + } |
102 | 108 |
|
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; |
104 | 114 |
|
105 |
| - return; |
106 |
| - } |
| 115 | + // topper intrinsic params |
| 116 | + const intrinsicParams = { |
| 117 | + "createdDate": null, |
| 118 | + "lastModifiedDate": null, |
| 119 | + "fileName": fileName, |
| 120 | + "fileVersion": fileVersion |
| 121 | + }; |
107 | 122 |
|
108 |
| - if (templateName.toLowerCase() === languageId.toLowerCase()) { |
| 123 | + // custom topper template parameters |
| 124 | + let topperTemplateParams = workspace.getConfiguration(TOPPER).get(TOPPER_CUSTOM_TEMPLATE_PARAMETERS); |
109 | 125 |
|
110 |
| - selectedTemplate = headerTemplate[languageId]; |
111 |
| - } |
112 |
| - }); |
| 126 | + // custom header templates that are language specific |
| 127 | + let topperTemplates = workspace.getConfiguration(TOPPER).get(TOPPER_HEADER_TEMPLATES); |
113 | 128 |
|
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)}`); |
115 | 132 |
|
116 |
| - selectedTemplate = topperTemplates[0][DEFAULT_HEADER_TEMPLATE]; |
117 |
| - } |
| 133 | + // choose the header template depending on the languageId of the file |
| 134 | + let selectedHeaderTemplate = (function () { |
118 | 135 |
|
119 |
| - return selectedTemplate; |
120 |
| - }()); |
| 136 | + let selectedTemplate = null; |
121 | 137 |
|
122 |
| - loginfo(`\n\nSelected Header Template:: ${JSON.stringify(selectedHeaderTemplate)}`); |
| 138 | + topperTemplates.forEach((headerTemplate) => { |
123 | 139 |
|
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) { |
128 | 143 |
|
129 |
| - intrinsicParams["createdDate"] = new Date(); |
130 |
| - intrinsicParams["lastModifiedDate"] = new Date(); |
| 144 | + return; |
| 145 | + } |
131 | 146 |
|
132 |
| - let customProfiles = new Map(); |
133 |
| - let customProfileNames = []; |
| 147 | + if (templateName.toLowerCase() === languageId.toLowerCase()) { |
134 | 148 |
|
135 |
| - // create the list of all the profile names for the selection menu |
136 |
| - topperTemplateParams.forEach(function (templateParamObject) { |
| 149 | + selectedTemplate = headerTemplate[languageId]; |
| 150 | + } |
| 151 | + }); |
137 | 152 |
|
138 |
| - for (let templateParamElement in templateParamObject) { |
| 153 | + if (!selectedTemplate) { |
139 | 154 |
|
140 |
| - customProfiles.set(templateParamElement, templateParamObject[templateParamElement]); |
141 |
| - customProfileNames.push(templateParamElement); |
| 155 | + selectedTemplate = topperTemplates[0][DEFAULT_HEADER_TEMPLATE]; |
142 | 156 | }
|
143 |
| - }); |
144 | 157 |
|
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 = []; |
146 | 167 |
|
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) { |
149 | 170 |
|
150 |
| - // show the quickpick selection menu to select the profile |
151 |
| - window.showQuickPick(customProfileNames).then((selectedProfileName) => { |
| 171 | + for (let templateParamElement in templateParamObject) { |
152 | 172 |
|
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(() => { |
155 | 182 |
|
156 |
| - loginfo(`Selected Params: ${JSON.stringify(selectedTemplateParams)}`); |
| 183 | + // show the quickpick selection menu to select the profile |
| 184 | + window.showQuickPick(customProfileNames).then((selectedProfileName) => { |
157 | 185 |
|
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 | + }); |
159 | 193 | });
|
160 | 194 | });
|
161 | 195 | };
|
|
166 | 200 | * @param {*} selectedTemplateParams the template parameters of the selected template profile
|
167 | 201 | * @param {*} callback the function to do after initial setup is done
|
168 | 202 | */
|
169 |
| - function makeHeaderString(selectedTemplateParams, callback) { |
| 203 | + function makeHeaderString(editor, selectedHeaderTemplate, selectedTemplateParams, intrinsicParams) { |
170 | 204 |
|
171 | 205 | let headerTemplateLines = selectedHeaderTemplate[TEMPLATE];
|
172 | 206 |
|
|
201 | 235 | templateValue = intrinsicParams[templateName];
|
202 | 236 | }
|
203 | 237 |
|
| 238 | + templateValue = templateValue.toString().replace(/\n/g, `\n${selectedHeaderTemplate[HEADER_PREFIX]}`); |
| 239 | + |
204 | 240 | headerLine.push(templateValue);
|
205 | 241 | });
|
206 | 242 |
|
|
209 | 245 |
|
210 | 246 | loginfo(`Header String: ${headerLines.join("\n")}`);
|
211 | 247 |
|
212 |
| - callback(headerLines.join("\n")); |
| 248 | + publishHeaderString(editor, headerLines.join("\n")); |
213 | 249 | }
|
214 | 250 |
|
215 | 251 | /**
|
216 | 252 | * Publishes the headerString to the text editor. It writes the header to the active text editor at the topmost position.
|
217 | 253 | *
|
218 | 254 | * @param {*} headerString the header string made after replacing all the templates with their values.
|
219 | 255 | */
|
220 |
| - function publishHeaderString(headerString) { |
| 256 | + function publishHeaderString(editor, headerString) { |
221 | 257 |
|
222 | 258 | loginfo("Publishing to the document");
|
223 | 259 |
|
|
0 commit comments