MediumRetrievalPython 3

Codebase Symbol Index

Build an incremental code symbol index with file replacement, deletion, case-insensitive prefix search, and stable ranking.

40m3 sample tests5 hidden tests

Implement SymbolIndex, a small in-memory index for code symbols.

Requirements

  • update_file(path, content) extracts symbols from one file and replaces any old symbols for that path.
  • remove_file(path) removes that file's symbols. If the path isn't indexed, it's a no-op.
  • search(prefix, limit=10) returns matching symbols sorted by:
    1. Exact match first (case-insensitive name equal to prefix),
    2. Symbol name case-insensitively,
    3. File path alphabetically,
    4. line number in ascending order.
  • A symbol is a dictionary with name (str), path (str), and line (1-indexed int).
  • Extract symbols from lines whose first non-whitespace token is def, class, function, const, let, or var (optional leading indentation is allowed). The symbol name is the identifier immediately following the keyword.
  • Prefix matching is case-insensitive.

Example

Updating a file indexes its declarations and allows case-insensitive prefix lookup.

python
1index = SymbolIndex() 2index.update_file("src/cache.py", "class Cache:\n pass\ndef clear_cache():\n pass") 3 4assert index.search("Ca")[0] == {"name": "Cache", "path": "src/cache.py", "line": 1}

Constraints

  • Keep state in memory.
  • Don't use a parser library.
  • File updates must remove stale symbols from previous content.

Editor