@@ -6,7 +6,7 @@ import { readFileSync, existsSync, statSync, Stats } from 'fs'
6
6
import { conf } from './config'
7
7
import { postError , formatURI , getDocumentContents , trimPath } from './utils'
8
8
import { platform } from 'os'
9
- import { Graph } from './graph'
9
+ import { Graph , Node } from './graph'
10
10
import { Comment } from './comment'
11
11
import { linterLog } from './logging'
12
12
@@ -91,7 +91,7 @@ export function preprocess(lines: string[], docURI: string) {
91
91
92
92
const includeMap = new Map < string , IncludeObj > ( Array . from ( allIncludes ) . map ( obj => [ obj . path , obj ] ) as [ string , IncludeObj ] [ ] )
93
93
94
- lint ( docURI , lines , includeMap , diagnostics )
94
+ lint ( docURI , lines , includeMap , diagnostics , hasDirective )
95
95
}
96
96
97
97
function includeDirective ( lines : string [ ] , docURI : string ) : boolean {
@@ -123,11 +123,24 @@ function includeDirective(lines: string[], docURI: string): boolean {
123
123
const buildIncludeGraph = ( inc : IncludeObj ) => includeGraph . setParent ( inc . path , inc . parent , inc . lineNum )
124
124
125
125
function processIncludes ( lines : string [ ] , incStack : string [ ] , allIncludes : Set < IncludeObj > , diagnostics : Map < string , Diagnostic [ ] > , hasDirective : boolean ) {
126
+ // todo figure out why incStack[0] here
126
127
const includes = getIncludes ( incStack [ 0 ] , lines )
127
128
includes . forEach ( i => allIncludes . add ( i ) )
128
- if ( includes . length > 0 ) {
129
- linterLog . info ( ( ) => `${ trimPath ( incStack [ 0 ] ) } has ${ includes . length } include(s). [${ includes . map ( i => '\n\t\t' + trimPath ( i . path ) ) } \n\t]` )
130
- includes . reverse ( ) . forEach ( inc => {
129
+
130
+ const parent = incStack [ incStack . length - 1 ]
131
+ includeGraph . nodes . get ( parent ) . children . forEach ( ( node , uri ) => {
132
+ if ( ! includes . has ( uri ) ) {
133
+ includeGraph . nodes . get ( parent ) . children . delete ( uri )
134
+ node . parents . delete ( parent )
135
+ }
136
+ } )
137
+
138
+ const includeList = Array . from ( includes . values ( ) )
139
+
140
+ if ( includeList . length > 0 ) {
141
+ linterLog . info ( ( ) => `${ trimPath ( parent ) } has ${ includeList . length } include(s). [${ includeList . map ( i => '\n\t\t' + trimPath ( i . path ) ) } \n\t]` )
142
+
143
+ includeList . reverse ( ) . forEach ( inc => {
131
144
buildIncludeGraph ( inc )
132
145
mergeInclude ( inc , lines , incStack , diagnostics , hasDirective )
133
146
} )
@@ -137,16 +150,14 @@ function processIncludes(lines: string[], incStack: string[], allIncludes: Set<I
137
150
}
138
151
139
152
function getIncludes ( uri : string , lines : string [ ] ) {
140
- // the numbers start at -1 because we increment them as soon as we enter the loop so that we
141
- // dont have to put an incrememnt at each return
142
153
const lineInfo : LinesProcessingInfo = {
143
154
total : - 1 ,
144
155
comment : Comment . State . No ,
145
156
parStack : [ uri ] ,
146
- count : [ - 1 ] ,
157
+ count : [ 0 ] ,
147
158
}
148
159
149
- return lines . reduce < IncludeObj [ ] > ( ( out , line , i ) : IncludeObj [ ] => processLine ( out , line , lines , i , lineInfo ) , [ ] )
160
+ return new Map ( lines . reduce < IncludeObj [ ] > ( ( out , line , i ) => processLine ( out , line , lines , i , lineInfo ) , [ ] ) . map ( inc => [ inc . path , inc ] as [ string , IncludeObj ] ) )
150
161
}
151
162
152
163
// TODO can surely be reworked
@@ -191,7 +202,7 @@ function processLine(includes: IncludeObj[], line: string, lines: string[], i: n
191
202
return includes
192
203
}
193
204
194
- function ifInvalidFile ( inc : IncludeObj , lines : string [ ] , incStack : string [ ] , diagnostics : Map < string , Diagnostic [ ] > ) {
205
+ function ifInvalidFile ( inc : IncludeObj , lines : string [ ] , incStack : string [ ] , diagnostics : Map < string , Diagnostic [ ] > , hasDirective : boolean ) {
195
206
const msg = `${ trimPath ( inc . path ) } is missing or an invalid file.`
196
207
197
208
linterLog . error ( msg , null )
@@ -212,7 +223,7 @@ function ifInvalidFile(inc: IncludeObj, lines: string[], incStack: string[], dia
212
223
line : inc . lineNum ,
213
224
msg, file,
214
225
}
215
- propogateDiagnostic ( error , diagnostics )
226
+ propogateDiagnostic ( error , diagnostics , hasDirective )
216
227
}
217
228
218
229
function mergeInclude ( inc : IncludeObj , lines : string [ ] , incStack : string [ ] , diagnostics : Map < string , Diagnostic [ ] > , hasDirective : boolean ) {
@@ -221,14 +232,14 @@ function mergeInclude(inc: IncludeObj, lines: string[], incStack: string[], diag
221
232
stats = statSync ( inc . path )
222
233
} catch ( e ) {
223
234
if ( e . code === 'ENOENT' ) {
224
- ifInvalidFile ( inc , lines , incStack , diagnostics )
235
+ ifInvalidFile ( inc , lines , incStack , diagnostics , hasDirective )
225
236
return
226
237
}
227
238
throw e
228
239
}
229
240
230
241
if ( ! stats . isFile ( ) ) {
231
- ifInvalidFile ( inc , lines , incStack , diagnostics )
242
+ ifInvalidFile ( inc , lines , incStack , diagnostics , hasDirective )
232
243
return
233
244
}
234
245
@@ -248,7 +259,7 @@ function mergeInclude(inc: IncludeObj, lines: string[], incStack: string[], diag
248
259
lines . splice ( inc . lineNumTopLevel + 1 + dataLines . length , 0 , `#line ${ inc . lineNum + 1 } "${ inc . parent } "` )
249
260
}
250
261
251
- function lint ( docURI : string , lines : string [ ] , includes : Map < string , IncludeObj > , diagnostics : Map < string , Diagnostic [ ] > ) {
262
+ function lint ( docURI : string , lines : string [ ] , includes : Map < string , IncludeObj > , diagnostics : Map < string , Diagnostic [ ] > , hasDirective : boolean ) {
252
263
let out : string = ''
253
264
try {
254
265
execSync ( `${ conf . glslangPath } --stdin -S ${ ext . get ( path . extname ( docURI ) ) } ` , { input : lines . join ( '\n' ) } )
@@ -261,15 +272,15 @@ function lint(docURI: string, lines: string[], includes: Map<string, IncludeObj>
261
272
if ( ! diagnostics . has ( key ) ) diagnostics . set ( key , [ ] )
262
273
} )
263
274
264
- processErrors ( out , docURI , diagnostics )
275
+ processErrors ( out , docURI , diagnostics , hasDirective )
265
276
266
277
daigsArray ( diagnostics ) . forEach ( d => {
267
278
if ( win ) d . uri = d . uri . replace ( 'file://C:' , 'file:///c%3A' )
268
279
connection . sendDiagnostics ( { uri : d . uri , diagnostics : d . diag } )
269
280
} )
270
281
}
271
282
272
- function processErrors ( out : string , docURI : string , diagnostics : Map < string , Diagnostic [ ] > ) {
283
+ function processErrors ( out : string , docURI : string , diagnostics : Map < string , Diagnostic [ ] > , hasDirective : boolean ) {
273
284
filterMatches ( out ) . forEach ( match => {
274
285
const error : ErrorMatch = {
275
286
type : errorType ( match [ 1 ] ) ,
@@ -289,16 +300,16 @@ function processErrors(out: string, docURI: string, diagnostics: Map<string, Dia
289
300
diagnostics . get ( error . file . length - 1 ? error . file : docURI ) . push ( diag )
290
301
291
302
// if is an include, highlight an error in the parents line of inclusion
292
- propogateDiagnostic ( error , diagnostics )
303
+ propogateDiagnostic ( error , diagnostics , hasDirective )
293
304
} )
294
305
}
295
306
296
307
//errorFile: string, type: DiagnosticSeverity, line: number, msg: string
297
- function propogateDiagnostic ( error : ErrorMatch , diagnostics : Map < string , Diagnostic [ ] > , parentURI ?: string ) {
308
+ function propogateDiagnostic ( error : ErrorMatch , diagnostics : Map < string , Diagnostic [ ] > , hasDirective : boolean , parentURI ?: string ) {
298
309
includeGraph . get ( parentURI || error . file ) . parents . forEach ( ( pair , parURI ) => {
299
310
const diag : Diagnostic = {
300
311
severity : error . type ,
301
- range : calcRange ( pair . first - 1 , parURI ) ,
312
+ range : calcRange ( pair . first - ( pair . second . parents . size > 0 ? 0 : ( 2 - ( hasDirective ? 1 : 0 ) ) ) , parURI ) ,
302
313
message : `Line ${ error . line } ${ trimPath ( error . file ) } ${ replaceWords ( error . msg ) } ` ,
303
314
source : 'mc-glsl'
304
315
}
@@ -307,7 +318,7 @@ function propogateDiagnostic(error: ErrorMatch, diagnostics: Map<string, Diagnos
307
318
diagnostics . get ( parURI ) . push ( diag )
308
319
309
320
if ( pair . second . parents . size > 0 ) {
310
- propogateDiagnostic ( error , diagnostics , parURI )
321
+ propogateDiagnostic ( error , diagnostics , hasDirective , parURI )
311
322
}
312
323
} )
313
324
}
0 commit comments