internal/shaderlister: bug fix: directives in multiline comments didn't work

This commit is contained in:
Hajime Hoshi 2025-02-09 19:14:26 +09:00
parent 1a206ae53c
commit 696e6484fc
2 changed files with 62 additions and 53 deletions

View File

@ -187,16 +187,18 @@ const (
)
var (
reShaderSourceDirective = regexp.MustCompile(`(?m)^\s*//` + regexp.QuoteMeta(shaderSourceDirective) + `$`)
reShaderFileDirective = regexp.MustCompile(`(?m)^\s*//` + regexp.QuoteMeta(shaderFileDirective) + ` `)
reShaderSourceDirective = regexp.MustCompile(`^\s*//` + regexp.QuoteMeta(shaderSourceDirective) + `$`)
reShaderFileDirective = regexp.MustCompile(`^\s*//` + regexp.QuoteMeta(shaderFileDirective) + ` `)
)
func hasShaderSourceDirectiveInComment(commentGroup *ast.CommentGroup) bool {
for _, line := range commentGroup.List {
if reShaderSourceDirective.MatchString(line.Text) {
for _, c := range commentGroup.List {
for _, line := range strings.Split(c.Text, "\n") {
if reShaderSourceDirective.MatchString(line) {
return true
}
}
}
return false
}
@ -221,20 +223,21 @@ func appendShaderSources(shaders []Shader, pkg *packages.Package) ([]Shader, err
funcs = append(funcs, f)
}
}
for _, c := range f.Comments {
for _, l := range c.List {
for _, cg := range f.Comments {
for _, c := range cg.List {
// Ignore the line if it is in a function declaration.
if slices.ContainsFunc(funcs, func(f *ast.FuncDecl) bool {
return f.Pos() <= l.Pos() && l.Pos() < f.End()
return f.Pos() <= c.Pos() && c.Pos() < f.End()
}) {
continue
}
m := reShaderFileDirective.FindString(l.Text)
if m == "" {
for _, line := range strings.Split(c.Text, "\n") {
m := reShaderFileDirective.FindString(line)
if len(m) == 0 {
continue
}
patterns := strings.TrimPrefix(l.Text, m)
patterns := strings.TrimPrefix(line, m)
for _, pattern := range strings.FieldsFunc(patterns, isAsciiSpace) {
pattern := filepath.Join(pkg.Dir, filepath.FromSlash(pattern))
if _, ok := visitedPatterns[pattern]; ok {
@ -256,7 +259,7 @@ func appendShaderSources(shaders []Shader, pkg *packages.Package) ([]Shader, err
return nil
}
visitedPaths[path] = struct{}{}
goFile := pkg.Fset.Position(c.Pos()).Filename
goFile := pkg.Fset.Position(cg.Pos()).Filename
shaders, err = appendShaderFromFile(shaders, pkg.PkgPath, goFile, path)
if err != nil {
return err
@ -280,7 +283,7 @@ func appendShaderSources(shaders []Shader, pkg *packages.Package) ([]Shader, err
continue
}
visitedPaths[path] = struct{}{}
goFile := pkg.Fset.Position(c.Pos()).Filename
goFile := pkg.Fset.Position(cg.Pos()).Filename
shaders, err = appendShaderFromFile(shaders, pkg.PkgPath, goFile, path)
if err != nil {
return nil, err
@ -290,6 +293,7 @@ func appendShaderSources(shaders []Shader, pkg *packages.Package) ([]Shader, err
}
}
}
}
topLevelDecls := map[ast.Decl]struct{}{}
for _, file := range pkg.Syntax {

View File

@ -81,3 +81,8 @@ func foo() {
//ebitengine:shaderfile *_notkage.go
}
// A directive in a comment block is not ignored.
/*
//ebitengine:shaderfile *_kage.go
*/