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,14 +187,16 @@ const (
) )
var ( var (
reShaderSourceDirective = regexp.MustCompile(`(?m)^\s*//` + regexp.QuoteMeta(shaderSourceDirective) + `$`) reShaderSourceDirective = regexp.MustCompile(`^\s*//` + regexp.QuoteMeta(shaderSourceDirective) + `$`)
reShaderFileDirective = regexp.MustCompile(`(?m)^\s*//` + regexp.QuoteMeta(shaderFileDirective) + ` `) reShaderFileDirective = regexp.MustCompile(`^\s*//` + regexp.QuoteMeta(shaderFileDirective) + ` `)
) )
func hasShaderSourceDirectiveInComment(commentGroup *ast.CommentGroup) bool { func hasShaderSourceDirectiveInComment(commentGroup *ast.CommentGroup) bool {
for _, line := range commentGroup.List { for _, c := range commentGroup.List {
if reShaderSourceDirective.MatchString(line.Text) { for _, line := range strings.Split(c.Text, "\n") {
return true if reShaderSourceDirective.MatchString(line) {
return true
}
} }
} }
return false return false
@ -221,70 +223,72 @@ func appendShaderSources(shaders []Shader, pkg *packages.Package) ([]Shader, err
funcs = append(funcs, f) funcs = append(funcs, f)
} }
} }
for _, c := range f.Comments { for _, cg := range f.Comments {
for _, l := range c.List { for _, c := range cg.List {
// Ignore the line if it is in a function declaration. // Ignore the line if it is in a function declaration.
if slices.ContainsFunc(funcs, func(f *ast.FuncDecl) bool { 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 continue
} }
m := reShaderFileDirective.FindString(l.Text) for _, line := range strings.Split(c.Text, "\n") {
if m == "" { m := reShaderFileDirective.FindString(line)
continue if len(m) == 0 {
}
patterns := strings.TrimPrefix(l.Text, m)
for _, pattern := range strings.FieldsFunc(patterns, isAsciiSpace) {
pattern := filepath.Join(pkg.Dir, filepath.FromSlash(pattern))
if _, ok := visitedPatterns[pattern]; ok {
continue continue
} }
visitedPatterns[pattern] = struct{}{} patterns := strings.TrimPrefix(line, m)
if !includesGlobMetaChar(pattern) { for _, pattern := range strings.FieldsFunc(patterns, isAsciiSpace) {
stat, err := os.Stat(pattern) pattern := filepath.Join(pkg.Dir, filepath.FromSlash(pattern))
if err == nil && stat.IsDir() { if _, ok := visitedPatterns[pattern]; ok {
// If the pattern is a directory, read all files in the directory recursively. continue
if err := filepath.WalkDir(pattern, func(path string, d os.DirEntry, err error) error { }
if err != nil { visitedPatterns[pattern] = struct{}{}
return err if !includesGlobMetaChar(pattern) {
} stat, err := os.Stat(pattern)
if d.IsDir() { if err == nil && stat.IsDir() {
// If the pattern is a directory, read all files in the directory recursively.
if err := filepath.WalkDir(pattern, func(path string, d os.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
if _, ok := visitedPaths[path]; ok {
return nil
}
visitedPaths[path] = struct{}{}
goFile := pkg.Fset.Position(cg.Pos()).Filename
shaders, err = appendShaderFromFile(shaders, pkg.PkgPath, goFile, path)
if err != nil {
return err
}
return nil return nil
}); err != nil {
return nil, err
} }
if _, ok := visitedPaths[path]; ok { continue
return nil }
} if err != nil && !errors.Is(err, os.ErrNotExist) {
visitedPaths[path] = struct{}{}
goFile := pkg.Fset.Position(c.Pos()).Filename
shaders, err = appendShaderFromFile(shaders, pkg.PkgPath, goFile, path)
if err != nil {
return err
}
return nil
}); err != nil {
return nil, err return nil, err
} }
continue
} }
if err != nil && !errors.Is(err, os.ErrNotExist) { paths, err := filepath.Glob(pattern)
return nil, err
}
}
paths, err := filepath.Glob(pattern)
if err != nil {
return nil, err
}
for _, path := range paths {
if _, ok := visitedPaths[path]; ok {
continue
}
visitedPaths[path] = struct{}{}
goFile := pkg.Fset.Position(c.Pos()).Filename
shaders, err = appendShaderFromFile(shaders, pkg.PkgPath, goFile, path)
if err != nil { if err != nil {
return nil, err return nil, err
} }
for _, path := range paths {
if _, ok := visitedPaths[path]; ok {
continue
}
visitedPaths[path] = struct{}{}
goFile := pkg.Fset.Position(cg.Pos()).Filename
shaders, err = appendShaderFromFile(shaders, pkg.PkgPath, goFile, path)
if err != nil {
return nil, err
}
}
} }
} }
} }

View File

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