From 4189a1299a7d0395a48e0deaa91b02eb0a042ee4 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Thu, 5 Jun 2025 23:05:14 -0400 Subject: [PATCH] lang: ast: Add scope feedback for classes We did this elsewhere for functions, let's add classes too. --- lang/ast/structs.go | 3 +++ lang/ast/util.go | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/lang/ast/structs.go b/lang/ast/structs.go index 69ab85e5..c6cbe999 100644 --- a/lang/ast/structs.go +++ b/lang/ast/structs.go @@ -6828,6 +6828,9 @@ func (obj *StmtInclude) SetScope(scope *interfaces.Scope) error { stmt, exists := scope.Classes[obj.Name] if !exists { + if obj.data.Debug || true { // TODO: leave this on permanently? + classScopeFeedback(scope, obj.data.Logf) + } err := fmt.Errorf("class `%s` does not exist in this scope", obj.Name) return highlightHelper(obj, obj.data.Logf, err) } diff --git a/lang/ast/util.go b/lang/ast/util.go index 1e6ae6eb..e676ef10 100644 --- a/lang/ast/util.go +++ b/lang/ast/util.go @@ -562,6 +562,21 @@ func lambdaScopeFeedback(scope *interfaces.Scope, logf func(format string, v ... } } +// classScopeFeedback logs some messages about what is actually in scope so that +// the user gets a hint about what's going on. This is useful for catching bugs +// in our programming or in user code! +func classScopeFeedback(scope *interfaces.Scope, logf func(format string, v ...interface{})) { + logf("classes in scope:") + names := []string{} + for name := range scope.Classes { + names = append(names, name) + } + sort.Strings(names) + for _, name := range names { + logf("class %s", name) + } +} + // highlightHelper give the user better file/line number feedback. func highlightHelper(node interfaces.Node, logf func(format string, v ...interface{}), err error) error { displayer, ok := node.(interfaces.TextDisplayer)