@@ -131,10 +131,13 @@ function lint(
131
131
132
132
// Look for matches for the regex
133
133
for ( const match of document . data . matchAll ( new RegExp (
134
- `(?<ignore>\\/\\/\\ s*@geode-ignore\\( ${ code } \ \).*?$\\ r?\\ n^.*?|\\/\\ /.*?)? ${ regex . source } ` ,
134
+ `${ / ( ( \/ \/ \ s* @ g e o d e - i g n o r e \( (?< ignoreTags > [ ^ \) ] * ) \) .* ?$ \r ? \n ^ .* ?) | ( \/ \ / .* ?) ) ? / . source } ${ regex . source } ` ,
135
135
regex . flags . includes ( "m" ) ? regex . flags : regex . flags + "m"
136
136
) ) ) {
137
- if ( match . index === undefined || match . groups ?. ignore || ignoreRanges . some ( range => range . from <= match . index ! && range . to >= match . index ! ) ) {
137
+ if (
138
+ match . index === undefined || match . groups ?. ignoreTags ?. includes ( code ) ||
139
+ ignoreRanges . some ( range => range . from <= match . index ! && range . to >= match . index ! )
140
+ ) {
138
141
continue ;
139
142
}
140
143
@@ -227,8 +230,8 @@ function lintSettings(document: MaybeDocument, diagnostics: Diagnostic[]) {
227
230
// }
228
231
229
232
function lintUnknownResource ( document : MaybeDocument , diagnostics : Diagnostic [ ] , initialRun : boolean ) {
230
- const modJson = getProjectFromDocument ( document . uri ) ?. modJson ;
231
- if ( ! modJson ) {
233
+ const mod = getProjectFromDocument ( document . uri ) ;
234
+ if ( ! mod ) {
232
235
return ;
233
236
}
234
237
@@ -238,94 +241,60 @@ function lintUnknownResource(document: MaybeDocument, diagnostics: Diagnostic[],
238
241
239
242
// Reload DB on filesave (in case new resources have been added to fix the issues)
240
243
if ( ! initialRun ) {
241
- db . refresh ( ) ;
244
+ db . refreshModSprites ( mod ) ;
242
245
}
243
246
244
- if ( modJson ? .dependencies ) {
247
+ if ( mod . modJson . dependencies ) {
245
248
// TODO: Deprecate
246
- if ( modJson . dependencies instanceof Array ) {
247
- dependencies . push ( ...modJson . dependencies . map ( ( d ) => d . id ) ) ;
249
+ if ( mod . modJson . dependencies instanceof Array ) {
250
+ dependencies . push ( ...mod . modJson . dependencies . map ( ( d ) => d . id ) ) ;
248
251
} else {
249
- dependencies . push ( ...Object . keys ( modJson . dependencies ) ) ;
252
+ dependencies . push ( ...Object . keys ( mod . modJson . dependencies ) ) ;
250
253
}
251
254
}
252
255
253
256
lint (
254
257
document , diagnostics ,
255
258
"unknown-resource" ,
256
- / (?< method > e x p a n d S p r i t e N a m e | [ S s ] p r i t e | [ L l ] a b e l ) (?< args > .* ( " [ ^ " ] + \. [ ^ " ] + " ( _ s p r ) ? ) ) + / gs,
257
- ( { groups : { method, args } , offset } ) => {
258
- // Don't lint `expandSpriteName` because if someone is using it they
259
- // should know what they are doing
260
- if ( method === "expandSpriteName" ) {
259
+ // Match resource-name-looking strings ("x.png", "thing.fnt" etc.)
260
+ // todo: this method doesn't actually match mistakes like "thing" where you forget the file extension
261
+ / " (?< modID > [ a - z 0 - 9 \- _ \. ] + \/ ) ? (?< name > \. ? ( [ \w \- \s ] + \. ) + ( p n g | f n t | o g g | m p 3 ) ) " (?< suffix > _ s p r ) ? / g,
262
+ ( { groups : { modID, name, suffix } , range } ) => {
263
+ // Resource might have the same name as a GD resource, so first
264
+ // try to find it in the mod's resources
265
+ const item = db . getCollectionById ( `mod:${ mod . modJson . id } ` ) ?. findByName ( name )
266
+ ?? db . findItemByName ( name ) ;
267
+
268
+ // Avoid matching resources from dependencies
269
+ if ( modID && dependencies . includes ( modID ) ) {
261
270
return undefined ;
262
271
}
263
272
264
- const results = [ ] ;
265
-
266
- // Extract any arguments that look like "sprite.png"
267
- for ( const arg of args . matchAll ( / " (?< name > [ ^ " ] + \. [ ^ " ] + ) " (?< suffix > _ s p r ) ? / g) ) {
268
- const { name, suffix } = arg . groups ! ;
269
- // `offset` is the offset of the lint regex, `method.length` is
270
- // because we are matching only the `args` match
271
- const nameRange = rangeFromRegex ( document , arg , offset + method . length ) ;
272
-
273
- // Resource might have the same name as a GD resource, so first
274
- // try to find it in the mod's resources
275
- const item = db . getCollectionById ( `mod:${ modJson . id } ` ) ?. findByName ( name )
276
- ?? db . findItemByName ( name ) ;
277
-
278
- {
279
- // Avoid matching stuff that doesnt look like a resource
280
- const parts = name . split ( "." ) ;
281
- if ( parts . length < 2 ) {
282
- continue ;
283
- }
284
- const ext = parts [ parts . length - 1 ] . toLowerCase ( ) ;
285
- if ( ! knownResourceExts . includes ( ext ) ) {
286
- continue ;
287
- }
288
-
289
- let shouldBreak = false ;
290
-
291
- // Avoid matching resources from dependencies
292
- for ( const dep of dependencies ) {
293
- if ( name . startsWith ( `${ dep } /` ) ) {
294
- shouldBreak = true ;
295
- break ;
296
- }
297
- }
298
-
299
- if ( shouldBreak ) {
300
- continue ;
301
- }
302
- }
303
-
304
- if ( ! item ) {
305
- results . push ( {
273
+ if ( ! item ) {
274
+ return [ {
275
+ level : DiagnosticSeverity . Warning ,
276
+ msg : `Resource "${ name } " doesn't exist` ,
277
+ range,
278
+ } ] ;
279
+ }
280
+ else {
281
+ if ( ! suffix && typeIsProject ( item . src ) ) {
282
+ return [ {
306
283
level : DiagnosticSeverity . Warning ,
307
- msg : `Resource "${ name } " doesn't exist ` ,
308
- range : nameRange ,
309
- } ) ;
284
+ msg : `Resource is missing _spr, perhaps you meant "${ name } "_spr? ` ,
285
+ range,
286
+ } ] ;
310
287
}
311
- else {
312
- if ( ! suffix && typeIsProject ( item . src ) ) {
313
- results . push ( {
314
- level : DiagnosticSeverity . Warning ,
315
- msg : `Resource is missing _spr, perhaps you meant "${ name } "_spr?` ,
316
- range : nameRange ,
317
- } ) ;
318
- }
319
- else if ( suffix && ! typeIsProject ( item . src ) ) {
320
- results . push ( {
321
- level : DiagnosticSeverity . Warning ,
322
- msg : `Resource "${ name } " was not found in mod.json` ,
323
- range : nameRange ,
324
- } ) ;
325
- }
288
+ else if ( suffix && ! typeIsProject ( item . src ) ) {
289
+ return [ {
290
+ level : DiagnosticSeverity . Warning ,
291
+ msg : `Resource "${ name } " was not found in mod.json` ,
292
+ range,
293
+ } ] ;
326
294
}
327
295
}
328
- return results ;
296
+
297
+ return undefined ;
329
298
}
330
299
) ;
331
300
}
@@ -381,6 +350,10 @@ export function registerLinters(context: ExtensionContext) {
381
350
if ( getExtConfig ( ) . get < boolean > ( "lints.enable" ) ) {
382
351
applyGeodeLints ( { uri : ev . document . uri , data : ev . document . getText ( ) } , geodeDiagnostics , false ) ;
383
352
}
353
+ // If lints aren't enabled, reset diagnostics to remove existing ones
354
+ else {
355
+ geodeDiagnostics . set ( ev . document . uri , [ ] ) ;
356
+ }
384
357
} ) ) ;
385
358
386
359
// This is for adding Quick Fixes for those diagnostics
0 commit comments