@@ -6,7 +6,7 @@ import { readFileSync, existsSync, statSync, Stats } from 'fs'
66import { conf } from './config'
77import { postError , formatURI , getDocumentContents , trimPath } from './utils'
88import { platform } from 'os'
9- import { Graph } from './graph'
9+ import { Graph , Node } from './graph'
1010import { Comment } from './comment'
1111import { linterLog } from './logging'
1212
@@ -91,7 +91,7 @@ export function preprocess(lines: string[], docURI: string) {
9191
9292 const includeMap = new Map < string , IncludeObj > ( Array . from ( allIncludes ) . map ( obj => [ obj . path , obj ] ) as [ string , IncludeObj ] [ ] )
9393
94- lint ( docURI , lines , includeMap , diagnostics )
94+ lint ( docURI , lines , includeMap , diagnostics , hasDirective )
9595}
9696
9797function includeDirective ( lines : string [ ] , docURI : string ) : boolean {
@@ -123,11 +123,24 @@ function includeDirective(lines: string[], docURI: string): boolean {
123123const buildIncludeGraph = ( inc : IncludeObj ) => includeGraph . setParent ( inc . path , inc . parent , inc . lineNum )
124124
125125function processIncludes ( lines : string [ ] , incStack : string [ ] , allIncludes : Set < IncludeObj > , diagnostics : Map < string , Diagnostic [ ] > , hasDirective : boolean ) {
126+ // todo figure out why incStack[0] here
126127 const includes = getIncludes ( incStack [ 0 ] , lines )
127128 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 => {
131144 buildIncludeGraph ( inc )
132145 mergeInclude ( inc , lines , incStack , diagnostics , hasDirective )
133146 } )
@@ -137,16 +150,14 @@ function processIncludes(lines: string[], incStack: string[], allIncludes: Set<I
137150}
138151
139152function 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
142153 const lineInfo : LinesProcessingInfo = {
143154 total : - 1 ,
144155 comment : Comment . State . No ,
145156 parStack : [ uri ] ,
146- count : [ - 1 ] ,
157+ count : [ 0 ] ,
147158 }
148159
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 ] ) )
150161}
151162
152163// TODO can surely be reworked
@@ -191,7 +202,7 @@ function processLine(includes: IncludeObj[], line: string, lines: string[], i: n
191202 return includes
192203}
193204
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 ) {
195206 const msg = `${ trimPath ( inc . path ) } is missing or an invalid file.`
196207
197208 linterLog . error ( msg , null )
@@ -212,7 +223,7 @@ function ifInvalidFile(inc: IncludeObj, lines: string[], incStack: string[], dia
212223 line : inc . lineNum ,
213224 msg, file,
214225 }
215- propogateDiagnostic ( error , diagnostics )
226+ propogateDiagnostic ( error , diagnostics , hasDirective )
216227}
217228
218229function 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
221232 stats = statSync ( inc . path )
222233 } catch ( e ) {
223234 if ( e . code === 'ENOENT' ) {
224- ifInvalidFile ( inc , lines , incStack , diagnostics )
235+ ifInvalidFile ( inc , lines , incStack , diagnostics , hasDirective )
225236 return
226237 }
227238 throw e
228239 }
229240
230241 if ( ! stats . isFile ( ) ) {
231- ifInvalidFile ( inc , lines , incStack , diagnostics )
242+ ifInvalidFile ( inc , lines , incStack , diagnostics , hasDirective )
232243 return
233244 }
234245
@@ -248,7 +259,7 @@ function mergeInclude(inc: IncludeObj, lines: string[], incStack: string[], diag
248259 lines . splice ( inc . lineNumTopLevel + 1 + dataLines . length , 0 , `#line ${ inc . lineNum + 1 } "${ inc . parent } "` )
249260}
250261
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 ) {
252263 let out : string = ''
253264 try {
254265 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>
261272 if ( ! diagnostics . has ( key ) ) diagnostics . set ( key , [ ] )
262273 } )
263274
264- processErrors ( out , docURI , diagnostics )
275+ processErrors ( out , docURI , diagnostics , hasDirective )
265276
266277 daigsArray ( diagnostics ) . forEach ( d => {
267278 if ( win ) d . uri = d . uri . replace ( 'file://C:' , 'file:///c%3A' )
268279 connection . sendDiagnostics ( { uri : d . uri , diagnostics : d . diag } )
269280 } )
270281}
271282
272- function processErrors ( out : string , docURI : string , diagnostics : Map < string , Diagnostic [ ] > ) {
283+ function processErrors ( out : string , docURI : string , diagnostics : Map < string , Diagnostic [ ] > , hasDirective : boolean ) {
273284 filterMatches ( out ) . forEach ( match => {
274285 const error : ErrorMatch = {
275286 type : errorType ( match [ 1 ] ) ,
@@ -289,16 +300,16 @@ function processErrors(out: string, docURI: string, diagnostics: Map<string, Dia
289300 diagnostics . get ( error . file . length - 1 ? error . file : docURI ) . push ( diag )
290301
291302 // if is an include, highlight an error in the parents line of inclusion
292- propogateDiagnostic ( error , diagnostics )
303+ propogateDiagnostic ( error , diagnostics , hasDirective )
293304 } )
294305}
295306
296307//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 ) {
298309 includeGraph . get ( parentURI || error . file ) . parents . forEach ( ( pair , parURI ) => {
299310 const diag : Diagnostic = {
300311 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 ) ,
302313 message : `Line ${ error . line } ${ trimPath ( error . file ) } ${ replaceWords ( error . msg ) } ` ,
303314 source : 'mc-glsl'
304315 }
@@ -307,7 +318,7 @@ function propogateDiagnostic(error: ErrorMatch, diagnostics: Map<string, Diagnos
307318 diagnostics . get ( parURI ) . push ( diag )
308319
309320 if ( pair . second . parents . size > 0 ) {
310- propogateDiagnostic ( error , diagnostics , parURI )
321+ propogateDiagnostic ( error , diagnostics , hasDirective , parURI )
311322 }
312323 } )
313324}
0 commit comments