diff --git a/git.odin b/git.odin
index cbabf3c..0f7c9af 100644
--- a/git.odin
+++ b/git.odin
@@ -638,7 +638,10 @@ symbol_name :: proc(file, name: string) -> string {
if i := strings.last_index_byte(base, '/'); i >= 0 {
base = base[i + 1:]
}
- extensions := [?]string{".odin", ".rs"}
+ // A C-family name keeps its extension (lmm.cpp.mdb_analyze) because a header
+ // declaration and the definition in the source are different rows; the others
+ // are single-file languages, where the base name is enough.
+ extensions := [?]string{".odin", ".rs", ".py", ".rb"}
for ext in extensions {
if strings.has_suffix(base, ext) {
base = base[:len(base) - len(ext)]
diff --git a/guix.scm b/guix.scm
index a281fee..0a0885b 100644
--- a/guix.scm
+++ b/guix.scm
@@ -351,7 +351,34 @@ to link the system SQLite3 instead of a bundled copy.")
(invoke "sh" "-c"
(string-append "./bin/ai-memory -C tmp/cpp --db"
" tmp/cpp/store.sqlite why inc"
- " | grep -q 'fn kern.cpp.inc, context'")))))
+ " | grep -q 'fn kern.cpp.inc, context'"))
+ ;; Python and Ruby are line-scanned, which is also the only
+ ;; thing that can attribute a file that was just *added*: git
+ ;; has no before-side for it, so its hunks carry no context.
+ (call-with-output-file "tmp/cpp/script.py"
+ (lambda (out)
+ (display "import os\n\ndef helper(x):\n return x\n" out)))
+ (call-with-output-file "tmp/cpp/script.rb"
+ (lambda (out)
+ (display "module Util\n def self.twice(x)\n x * 2\n end\nend\n" out)))
+ (invoke "git" "-C" "tmp/cpp" "add" "script.py" "script.rb")
+ (invoke "git" "-C" "tmp/cpp" "-c" "user.name=check"
+ "-c" "user.email=check@example" "commit" "-qm" "add scripts")
+ (invoke "./bin/ai-memory" "-C" "tmp/cpp" "--db"
+ "tmp/cpp/store.sqlite" "sync")
+ (invoke "sh" "-c"
+ (string-append "./bin/ai-memory -C tmp/cpp --db"
+ " tmp/cpp/store.sqlite why helper"
+ " | grep -q 'fn script.helper, add'"))
+ (invoke "sh" "-c"
+ (string-append "./bin/ai-memory -C tmp/cpp --db"
+ " tmp/cpp/store.sqlite why twice"
+ " | grep -q 'fn script.twice, add'"))
+ ;; The substring lookup: `why script' reaches both files.
+ (invoke "sh" "-c"
+ (string-append "./bin/ai-memory -C tmp/cpp --db"
+ " tmp/cpp/store.sqlite why script"
+ " | grep -q 'fn script.helper, add'")))))
(replace 'install
(lambda _
(install-file "bin/ai-memory"
diff --git a/lang.odin b/lang.odin
index 58d2f34..8a56013 100644
--- a/lang.odin
+++ b/lang.odin
@@ -8,6 +8,8 @@ Lang :: enum {
Odin,
Rust,
C_Family, // .c .h .cc .cpp .cxx .hpp .hh .hxx .ipp .tpp .inl
+ Python, // .py
+ Ruby, // .rb
Other,
}
@@ -36,6 +38,10 @@ lang_of :: proc(file: string) -> Lang {
return .Odin
case strings.has_suffix(file, ".rs"):
return .Rust
+ case strings.has_suffix(file, ".py"):
+ return .Python
+ case strings.has_suffix(file, ".rb"):
+ return .Ruby
}
for ext in C_EXTENSIONS {
if strings.has_suffix(file, ext) {
@@ -53,6 +59,10 @@ parse_decl :: proc(file, line: string) -> (name: string, kind: string, ok: bool)
return parse_odin_decl(line)
case .Rust:
return parse_rust_decl(line)
+ case .Python:
+ return parse_python_decl(line)
+ case .Ruby:
+ return parse_ruby_decl(line)
case .C_Family:
// Deliberately no lexical scan. In C and C++ a diff line cannot say
// whether it declares anything: inside a body `foo(bar);' is a call and
@@ -212,6 +222,125 @@ parse_rust_decl :: proc(line: string) -> (name: string, kind: string, ok: bool)
return "", "", false
}
+// -- Python -----------------------------------------------------------------
+//
+// def NAME(...): -> fn (and `async def')
+// class NAME: -> type
+// NAME = value -> const (module level only; indented is inside a def)
+// NAME: Type -> var (module level only)
+//
+// A line scanner is the right tool here, unlike C and C++: a Python
+// declaration is the first thing on its line, so there is no statement that
+// can look like one -- `foo(bar)` is a call and `def foo(bar):` is not, and the
+// keyword settles it. What it cannot see is a definition built at run time
+// (`f = lambda x: x`, a `def` inside an `exec`), which is why the gist of a
+// symbol is a name and the file it lives in, not a promise about the AST.
+parse_python_decl :: proc(line: string) -> (name: string, kind: string, ok: bool) {
+ indented := len(line) > 0 && (line[0] == ' ' || line[0] == '\t')
+ s := strings.trim_left_space(line)
+ if len(s) == 0 || s[0] == '#' || s[0] == '@' || s[0] == '"' || s[0] == '\'' {
+ return "", "", false
+ }
+ if strings.has_prefix(s, "async def ") {
+ if n, nok := ident_after(s, "async def "); nok {
+ return n, "fn", true
+ }
+ return "", "", false
+ }
+ if strings.has_prefix(s, "def ") {
+ if n, nok := ident_after(s, "def "); nok {
+ return n, "fn", true
+ }
+ return "", "", false
+ }
+ if strings.has_prefix(s, "class ") {
+ if n, nok := ident_after(s, "class "); nok {
+ return n, "type", true
+ }
+ return "", "", false
+ }
+ if indented {
+ // Inside a def or a class body: an assignment there is a local, not a
+ // declaration a reader asks "why" about.
+ return "", "", false
+ }
+ if n, aok := assignment_name(s); aok {
+ return n, "const", true
+ }
+ if sep := strings.index_byte(s, ':'); sep > 0 {
+ if lhs := strings.trim_space(s[:sep]); is_ident(lhs) && !strings.contains(s, "=") {
+ return lhs, "var", true
+ }
+ }
+ return "", "", false
+}
+
+// -- Ruby -------------------------------------------------------------------
+//
+// def NAME / def self.NAME / def NAME? -> fn
+// class NAME / module NAME -> type
+// NAME = value -> const (module level, capitalised)
+//
+// Ruby's declarations are line-anchored in the same way, with one trap the
+// callers of this store care about: `def` and `class` also appear inside
+// strings and heredocs, and no line scanner can tell. Anchoring on the first
+// token keeps that rare.
+parse_ruby_decl :: proc(line: string) -> (name: string, kind: string, ok: bool) {
+ indented := len(line) > 0 && (line[0] == ' ' || line[0] == '\t')
+ s := strings.trim_left_space(line)
+ if len(s) == 0 || s[0] == '#' || s[0] == '=' {
+ return "", "", false
+ }
+ if strings.has_prefix(s, "def ") {
+ rest := s[len("def "):]
+ if strings.has_prefix(rest, "self.") {
+ rest = rest[len("self."):]
+ }
+ if n, nok := ident_after(rest, ""); nok {
+ // `ready?', `save!', `name=' are one identifier to Ruby.
+ return strings.trim_right(n, "?!=:"), "fn", true
+ }
+ return "", "", false
+ }
+ if strings.has_prefix(s, "class ") || strings.has_prefix(s, "module ") {
+ prefix := strings.has_prefix(s, "class ") ? "class " : "module "
+ if n, nok := ident_after(s, prefix); nok {
+ return n, "type", true
+ }
+ return "", "", false
+ }
+ if indented {
+ return "", "", false
+ }
+ // A Ruby constant is capitalised; `x = 1` at column 0 is a local in a
+ // script and not worth a symbol.
+ if n, aok := assignment_name(s); aok && n[0] >= 'A' && n[0] <= 'Z' {
+ return n, "const", true
+ }
+ return "", "", false
+}
+
+// assignment_name reports NAME from `NAME = value' (not `NAME == value').
+assignment_name :: proc(s: string) -> (name: string, ok: bool) {
+ for i in 0 ..< len(s) {
+ if s[i] != '=' {
+ continue
+ }
+ if i + 1 < len(s) && s[i + 1] == '=' {
+ return "", false
+ }
+ if i > 0 && (s[i - 1] == '!' || s[i - 1] == '<' || s[i - 1] == '>' || s[i - 1] == '=') {
+ return "", false
+ }
+ lhs := strings.trim_space(s[:i])
+ if is_ident(lhs) {
+ return lhs, true
+ }
+ return "", false
+ }
+ return "", false
+}
+
// ident_after reads the identifier that follows PREFIX in S.
ident_after :: proc(s, prefix: string) -> (name: string, ok: bool) {
rest := strings.trim_left_space(s[len(prefix):])
diff --git a/main.odin b/main.odin
index 0d4c58a..44c2ff1 100644
--- a/main.odin
+++ b/main.odin
@@ -655,8 +655,11 @@ lookup_touches :: proc(db: ^Db, name: string) -> []Touch_Row {
if len(by_basename) > 0 {
return by_basename
}
+ // Substring, not suffix: `why geno2mdb' should reach geno2mdb.main, and the
+ // documented behaviour is a LIKE '%...%' scan. It was a suffix match
+ // (LIKE '%name'), which could not find a file's own symbols at all.
return run_touches(db, fmt.tprintf(TOUCH_QUERY, "s.name like ?1"),
- fmt.tprintf("%%%s", name))
+ fmt.tprintf("%%%s%%", name))
}
run_touches :: proc(db: ^Db, query, name: string) -> []Touch_Row {
|