@@ -12,6 +12,7 @@ import type {
1212 TemplateRepository ,
1313 TemplatePass2 ,
1414 TemplatePass1 ,
15+ GenerateOptions ,
1516} from "./types.js" ;
1617
1718interface FunctionCallState {
@@ -113,7 +114,7 @@ function matchNextPattern(
113114 return earliestMatch ;
114115}
115116
116- function generateImpl ( generatorState : GeneratorState ) {
117+ function generateImpl ( generatorState : GeneratorState , options : GenerateOptions ) {
117118 const { pass1, codeGenerator, preprocessIfStack, patterns, currentFunctionCall } = generatorState ;
118119
119120 let currentLine = generatorState . currentLine ;
@@ -382,6 +383,13 @@ function generateImpl(generatorState: GeneratorState) {
382383 output ( "code" , "\n" ) ;
383384 } ;
384385
386+ if ( options . preserveCodeReference ) {
387+ const maxLineNumber = pass1 . length ;
388+ const lineNumberWidth = String ( maxLineNumber ) . length ;
389+ const paddedLineNumber = String ( currentLine + 1 ) . padStart ( lineNumberWidth , " " ) ;
390+ output ( "raw" , `// ${ paddedLineNumber } | ${ line } \n` ) ;
391+ }
392+
385393 if ( line . startsWith ( "#" ) ) {
386394 if ( line . startsWith ( "#use " ) ) {
387395 const uses = line
@@ -663,7 +671,8 @@ function generateImpl(generatorState: GeneratorState) {
663671const generate = (
664672 filePath : string ,
665673 repo : TemplateRepository < TemplatePass1 > ,
666- codeGenerator : CodeGenerator
674+ codeGenerator : CodeGenerator ,
675+ options ?: GenerateOptions
667676) : GenerateResult => {
668677 const pass1 = repo . templates . get ( filePath ) ?. pass1 ;
669678 if ( ! pass1 ) {
@@ -689,7 +698,7 @@ const generate = (
689698 usedVariables : new Map ( ) ,
690699 } ;
691700
692- generateImpl ( generatorState ) ;
701+ generateImpl ( generatorState , options || { } ) ;
693702
694703 if ( generatorState . preprocessIfStack . length > 0 ) {
695704 throw new WgslTemplateGenerateError (
@@ -738,11 +747,11 @@ const generate = (
738747export const generator : Generator = {
739748 generate,
740749
741- generateDirectory ( repo , codeGenerator ) : TemplateRepository < TemplatePass2 > {
750+ generateDirectory ( repo , codeGenerator , options ) : TemplateRepository < TemplatePass2 > {
742751 const result = new Map < string , TemplatePass2 > ( ) ;
743752
744753 for ( const [ filePath , template ] of repo . templates ) {
745- const generateResult = generate ( filePath , repo , codeGenerator ) ;
754+ const generateResult = generate ( filePath , repo , codeGenerator , options ) ;
746755 result . set ( filePath , {
747756 filePath : template . filePath ,
748757 generateResult,
0 commit comments