@@ -95,6 +95,13 @@ export function activate(context: vscode.ExtensionContext) {
95
95
}
96
96
diagnosticCollection . set ( file . document . uri , diagnostics )
97
97
}
98
+
99
+ // ======= Highlight keys without description when .env.jsonc file is changed =======
100
+ highlightKeysWithoutDescription ( file . document )
101
+ } )
102
+
103
+ vscode . workspace . onDidOpenTextDocument ( async _ => {
104
+ highlightKeysWithoutDescription ( vscode . window . activeTextEditor ?. document )
98
105
} )
99
106
100
107
// ======================== Add suggestions to the editor ========================
@@ -156,6 +163,58 @@ const matchMultipleLines = (
156
163
return isMatching
157
164
}
158
165
166
+ const highlightKeysWithoutDescription = ( document ?: vscode . TextDocument ) => {
167
+ if ( ! vscode . workspace . workspaceFolders ) {
168
+ return
169
+ }
170
+ if ( document ?. fileName . endsWith ( '.env.jsonc' ) ) {
171
+ const text = document . getText ( )
172
+ const pattern = / \" .* " : / g
173
+ const matchedConfigKeys = text . match ( pattern ) ?? [ ]
174
+ const diagnostics : vscode . Diagnostic [ ] = [ ]
175
+
176
+ for ( const key of matchedConfigKeys ) {
177
+ const keyStartPos = text . indexOf ( key )
178
+ const keyLine = document . positionAt ( keyStartPos ) . line
179
+
180
+ const folderUri = vscode . workspace . workspaceFolders [ 0 ] . uri
181
+ const fileUri = folderUri . with ( {
182
+ path : path . posix . join ( folderUri . path , '.env.jsonc' ) ,
183
+ } )
184
+
185
+ const lineAboveKey = document . lineAt ( keyLine - 1 ) . text . trim ( )
186
+ if ( ! lineAboveKey . startsWith ( '//' ) && ! lineAboveKey . endsWith ( '*/' ) ) {
187
+ const keyName = key . split ( '"' ) [ 1 ]
188
+ const startPos = text . indexOf ( `"${ keyName } "` )
189
+ const endPos = startPos + keyName . length + 2
190
+ const keyRange = new vscode . Range (
191
+ document . positionAt ( startPos ) ,
192
+ document . positionAt ( endPos )
193
+ )
194
+
195
+ const warningMessage = new vscode . Diagnostic (
196
+ keyRange ,
197
+ `Key '${ keyName } ' does not have a description. Add a comment describing its purpose.` ,
198
+ vscode . DiagnosticSeverity . Warning
199
+ )
200
+ warningMessage . relatedInformation = [
201
+ {
202
+ location : new vscode . Location ( document . uri , keyRange ) ,
203
+ message : 'Missing description for this key.' ,
204
+ } ,
205
+ ]
206
+ warningMessage . code = {
207
+ value : 'key-without-description' ,
208
+ target : fileUri ,
209
+ }
210
+ warningMessage . source = 'configuru'
211
+ diagnostics . push ( warningMessage )
212
+ }
213
+ }
214
+ diagnosticCollection . set ( document . uri , diagnostics )
215
+ }
216
+ }
217
+
159
218
// This method is called when extension is deactivated
160
219
export function deactivate ( ) {
161
220
if ( diagnosticCollection ) {
0 commit comments