scip-cli 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
scip_cli/SKILL.md ADDED
@@ -0,0 +1,76 @@
1
+ ---
2
+ name: scip-cli
3
+ description: Read when needing to find symbols, definitions, references, or members in TypeScript/JavaScript code
4
+ ---
5
+
6
+ TypeScript/JavaScript only (.ts, .tsx, .js, .jsx) — not GraphQL, CSS, or other files.
7
+
8
+ All commands are sub-commands of `scip-cli`. Run from the project root.
9
+
10
+ ## Quick Decision Guide
11
+
12
+ | Question | Use | What you get |
13
+ |----------|-----|--------------|
14
+ | "Where is X defined and what does it do?" | `def X` | Functions: full body. Classes: full definition. Multiple exact matches returned together |
15
+ | "Where is X used/called?" | `refs X` | All file:line locations (~0.03s). Ambiguous bare names → first match only |
16
+ | "What's in this file?" | `symbols file` | All symbols — bare filename works (`HistoryTab`, `usePatientEntries`) |
17
+ | "Find symbols by name" | `search name` | Functions, types, interfaces, classes. Use `--kind variable` for consts |
18
+ | "What files depend on this file?" | `rdeps file` | Importers — bare name works |
19
+ | "What methods does this class have?" | `members ClassName` | All methods/fields with line ranges |
20
+
21
+ ## Gotchas
22
+
23
+ - **Bare names** resolve functions, types (aliases + interfaces), and classes. Consts/variables need `def --type variable X` or `search --kind variable X`. Class methods need `members ClassName`, not bare `def methodName`.
24
+ - **Ambiguous types** (e.g. `Opts` in multiple hooks) — `def` returns all; `refs` picks the first and warns. Use `search` or a qualified `src:…` name to disambiguate.
25
+ - **First run** in a project may auto-index (one-time wait, ~10-30s for large codebases).
26
+ - **Precision escape hatch**: qualified names like `src:hooks:usePatientEntries:usePatientEntries()` always work.
27
+
28
+ ## Details
29
+
30
+ ### def
31
+
32
+ ```bash
33
+ def [--type <kind>] <symbol>
34
+ ```
35
+
36
+ Kinds: `function`, `class`, `interface`, `type`, `method`, `variable` — use `--type` when the bare name isn't in the default set above.
37
+
38
+ ### refs
39
+
40
+ ```bash
41
+ refs <symbol>
42
+ ```
43
+
44
+ Returns `file:line` for each reference. Reads source files to find exact line numbers.
45
+
46
+ ### search
47
+
48
+ ```bash
49
+ search [--kind <kind>] <pattern>
50
+ ```
51
+
52
+ Returns `file:line Kind symbolName`. Filters noisy symbols (file-level, parameters, type literals).
53
+
54
+ ### symbols
55
+
56
+ ```bash
57
+ symbols <file>
58
+ ```
59
+
60
+ Returns `startLine-endLine kind name` for each symbol in the file.
61
+
62
+ ### rdeps
63
+
64
+ ```bash
65
+ rdeps <file>
66
+ ```
67
+
68
+ Returns list of files that import from this file.
69
+
70
+ ### members
71
+
72
+ ```bash
73
+ members <symbol>
74
+ ```
75
+
76
+ Returns `startLine:endLine kind name` for each member. Note: limited by database coverage — `enclosing_symbol` data is sparse for many indexers.
scip_cli/__init__.py ADDED
@@ -0,0 +1,2 @@
1
+ """scip-cli: Fast code intelligence via SCIP indexes."""
2
+ __version__ = "1.0.0"
scip_cli/__main__.py ADDED
@@ -0,0 +1,70 @@
1
+ #!/usr/bin/env python3
2
+ """CLI entry point for scip-cli."""
3
+ import argparse
4
+ import sys
5
+
6
+ from .commands import refs, def_cmd, search, symbols, rdeps, members, skill
7
+
8
+
9
+ def main():
10
+ parser = argparse.ArgumentParser(
11
+ prog="scip-cli",
12
+ description="Fast code intelligence via SCIP indexes"
13
+ )
14
+ subparsers = parser.add_subparsers(dest="command", help="Available commands")
15
+
16
+ # refs
17
+ refs_parser = subparsers.add_parser("refs", help="Find references to a symbol")
18
+ refs_parser.add_argument("symbol", help="Symbol name")
19
+
20
+ # def
21
+ def_parser = subparsers.add_parser("def", help="Find symbol definition")
22
+ def_parser.add_argument("--type", dest="kind", help="Filter by kind (function, class, etc)")
23
+ def_parser.add_argument("symbol", help="Symbol name")
24
+
25
+ # search
26
+ search_parser = subparsers.add_parser("search", help="Search symbols by pattern")
27
+ search_parser.add_argument("--kind", help="Filter by kind")
28
+ search_parser.add_argument("pattern", help="Search pattern")
29
+
30
+ # symbols
31
+ symbols_parser = subparsers.add_parser("symbols", help="List symbols in a file")
32
+ symbols_parser.add_argument("file", help="File path or pattern")
33
+
34
+ # rdeps
35
+ rdeps_parser = subparsers.add_parser("rdeps", help="Find reverse dependencies of a file")
36
+ rdeps_parser.add_argument("file", help="File path or pattern")
37
+
38
+ # members
39
+ members_parser = subparsers.add_parser("members", help="List members of a class/interface")
40
+ members_parser.add_argument("symbol", help="Symbol name")
41
+
42
+ # skill
43
+ skill_parser = subparsers.add_parser("skill", help="Install or dump the scip-cli SKILL.md")
44
+ skill_parser.add_argument("path", nargs="?", help="Optional file path to write to (creates dirs)")
45
+
46
+ args = parser.parse_args()
47
+
48
+ if not args.command:
49
+ parser.print_help()
50
+ sys.exit(1)
51
+
52
+ # Dispatch to command handlers
53
+ if args.command == "refs":
54
+ refs.main(args)
55
+ elif args.command == "def":
56
+ def_cmd.main(args)
57
+ elif args.command == "search":
58
+ search.main(args)
59
+ elif args.command == "symbols":
60
+ symbols.main(args)
61
+ elif args.command == "rdeps":
62
+ rdeps.main(args)
63
+ elif args.command == "members":
64
+ members.main(args)
65
+ elif args.command == "skill":
66
+ skill.main(args)
67
+
68
+
69
+ if __name__ == "__main__":
70
+ main()
@@ -0,0 +1 @@
1
+ """Command modules for scip-cli."""
@@ -0,0 +1,50 @@
1
+ """def command - find symbol definitions."""
2
+ import sys
3
+
4
+ from ..lib import (
5
+ get_db,
6
+ resolve_symbol,
7
+ find_project_root,
8
+ read_source_lines,
9
+ infer_kind,
10
+ )
11
+
12
+
13
+ def main(args):
14
+ """Find the definition of a symbol."""
15
+ db = get_db()
16
+
17
+ symbols = resolve_symbol(db, args.symbol, args.kind)
18
+ if not symbols:
19
+ print(f"Symbol '{args.symbol}' not found", file=sys.stderr)
20
+ sys.exit(1)
21
+
22
+ project_root = find_project_root()
23
+ if not project_root:
24
+ print("Error: Could not find project root", file=sys.stderr)
25
+ sys.exit(1)
26
+
27
+ for symbol_id, symbol_str, display_name in symbols:
28
+ # Get definition location from defn_enclosing_ranges
29
+ row = db.execute("""
30
+ SELECT d.relative_path, der.start_line, der.end_line
31
+ FROM defn_enclosing_ranges der
32
+ JOIN documents d ON der.document_id = d.id
33
+ WHERE der.symbol_id = ?
34
+ """, (symbol_id,)).fetchone()
35
+
36
+ if not row:
37
+ continue
38
+
39
+ rel_path, start_line, end_line = row
40
+ kind = infer_kind(symbol_str)
41
+
42
+ # Read source from filesystem
43
+ lines = read_source_lines(project_root, rel_path, start_line, end_line)
44
+ if lines is None:
45
+ source_snippet = "(could not read source)"
46
+ else:
47
+ source_snippet = ''.join(lines).rstrip('\n')
48
+
49
+ print(f"{rel_path}:{start_line + 1}:{end_line + 1}")
50
+ print(source_snippet)
@@ -0,0 +1,84 @@
1
+ """members command - list members of a class/interface."""
2
+ import sys
3
+ import re
4
+
5
+ from ..lib import (
6
+ get_db,
7
+ resolve_symbol,
8
+ warn_ambiguous,
9
+ get_members,
10
+ infer_kind,
11
+ extract_leaf_name,
12
+ read_source_lines,
13
+ find_project_root,
14
+ )
15
+
16
+
17
+ def main(args):
18
+ """List members of a class or interface."""
19
+ db = get_db()
20
+
21
+ symbols = resolve_symbol(db, args.symbol)
22
+ if not symbols:
23
+ print(f"Symbol '{args.symbol}' not found", file=sys.stderr)
24
+ sys.exit(1)
25
+
26
+ if len(symbols) > 1:
27
+ warn_ambiguous(args.symbol, symbols, "symbol")
28
+
29
+ symbol_id, symbol_str, display_name = symbols[0]
30
+ members = get_members(db, symbol_id)
31
+
32
+ if not members:
33
+ print(f"No members found for '{args.symbol}'", file=sys.stderr)
34
+ sys.exit(1)
35
+
36
+ # Get parent's file path and line range for fallback
37
+ project_root = find_project_root()
38
+ parent_def = db.execute("""
39
+ SELECT d.relative_path, der.start_line, der.end_line
40
+ FROM defn_enclosing_ranges der
41
+ JOIN documents d ON der.document_id = d.id
42
+ WHERE der.symbol_id = ?
43
+ """, (symbol_id,)).fetchone()
44
+
45
+ parent_file = parent_def[0] if parent_def else None
46
+ parent_start = parent_def[1] if parent_def else None
47
+ parent_end = parent_def[2] if parent_def else None
48
+
49
+ # Check if any members need line number lookup
50
+ needs_lookup = any(m[3] is None for m in members)
51
+
52
+ # Read source file once if needed
53
+ source_lines = None
54
+ if needs_lookup and project_root and parent_file and parent_start is not None:
55
+ source_lines = read_source_lines(project_root, parent_file, parent_start, parent_end)
56
+
57
+ for member_id, member_symbol, member_name, start_line, end_line in members:
58
+ kind = infer_kind(member_symbol)
59
+ short = extract_leaf_name(member_symbol)
60
+
61
+ # Skip function parameters (they have ().( in the symbol)
62
+ if ").(" in member_symbol:
63
+ continue
64
+
65
+ # If no line numbers from DB, try to find in source
66
+ if start_line is None and source_lines:
67
+ if "<constructor>" in member_symbol:
68
+ pattern = rf'^\s*constructor\s*\('
69
+ elif "<get>" in member_symbol:
70
+ pattern = rf'^\s*(?:public\s+|private\s+|protected\s+|static\s+|readonly\s+)*get\s+{re.escape(short)}\s*\('
71
+ elif "<set>" in member_symbol:
72
+ pattern = rf'^\s*(?:public\s+|private\s+|protected\s+|static\s+)*set\s+{re.escape(short)}\s*\('
73
+ else:
74
+ # Regular property/method - handle TypeScript modifiers
75
+ pattern = rf'^\s*(?:public\s+|private\s+|protected\s+|static\s+|readonly\s+)*{re.escape(short)}\s*\??\s*[:=(]'
76
+
77
+ for i, line in enumerate(source_lines):
78
+ if re.match(pattern, line):
79
+ start_line = parent_start + i
80
+ end_line = start_line
81
+ break
82
+
83
+ line_info = f"{start_line + 1}:{end_line + 1}" if start_line is not None else "??"
84
+ print(f"{line_info} {kind} {short}")
@@ -0,0 +1,52 @@
1
+ """rdeps command - find reverse dependencies of a file."""
2
+ import sys
3
+
4
+ from ..lib import (
5
+ get_db,
6
+ resolve_file,
7
+ warn_ambiguous,
8
+ get_file_symbols,
9
+ get_refs_for_symbols,
10
+ )
11
+
12
+
13
+ def main(args):
14
+ """Find all files that import from this file."""
15
+ db = get_db()
16
+
17
+ # Resolve file pattern to actual path
18
+ files = resolve_file(db, args.file)
19
+ if not files:
20
+ print(f"File '{args.file}' not found", file=sys.stderr)
21
+ sys.exit(1)
22
+
23
+ if len(files) > 1:
24
+ warn_ambiguous(args.file, files, "file")
25
+
26
+ file_path = files[0]
27
+
28
+ # Get all symbols defined in this file
29
+ symbols = get_file_symbols(db, file_path)
30
+ if not symbols:
31
+ print(f"No symbols found in '{file_path}'", file=sys.stderr)
32
+ sys.exit(1)
33
+
34
+ # Get all symbol IDs
35
+ symbol_ids = [s[0] for s in symbols]
36
+
37
+ # Find all references to these symbols from OTHER files in one query
38
+ refs = get_refs_for_symbols(db, symbol_ids)
39
+
40
+ # Collect unique file paths that reference this file
41
+ rdeps = set()
42
+ for symbol_id, ref_list in refs.items():
43
+ for ref_path, ref_line in ref_list:
44
+ if ref_path != file_path:
45
+ rdeps.add(ref_path)
46
+
47
+ if not rdeps:
48
+ print(f"No reverse dependencies found for '{file_path}'", file=sys.stderr)
49
+ sys.exit(1)
50
+
51
+ for dep_path in sorted(rdeps):
52
+ print(dep_path)
@@ -0,0 +1,107 @@
1
+ """refs command - find all references to a symbol."""
2
+ import sys
3
+
4
+ from ..lib import (
5
+ get_db,
6
+ resolve_symbol,
7
+ warn_ambiguous,
8
+ find_project_root,
9
+ read_source_lines,
10
+ extract_leaf_name,
11
+ )
12
+
13
+
14
+ def get_exact_refs(db, symbol_id, project_root):
15
+ """Get references with exact line numbers by reading source files."""
16
+ # Get symbol name once
17
+ sym_row = db.execute("SELECT symbol FROM global_symbols WHERE id = ?", (symbol_id,)).fetchone()
18
+ if not sym_row:
19
+ return []
20
+
21
+ leaf = extract_leaf_name(sym_row[0])
22
+
23
+ # Get all chunks that reference this symbol
24
+ chunks = db.execute("""
25
+ SELECT c.id, c.document_id, c.start_line, c.end_line, d.relative_path
26
+ FROM mentions m
27
+ JOIN chunks c ON m.chunk_id = c.id
28
+ JOIN documents d ON c.document_id = d.id
29
+ WHERE m.symbol_id = ? AND m.role != 1
30
+ """, (symbol_id,)).fetchall()
31
+
32
+ if not chunks:
33
+ return []
34
+
35
+ # Group by document
36
+ by_doc = {}
37
+ for chunk_id, doc_id, start_line, end_line, rel_path in chunks:
38
+ if doc_id not in by_doc:
39
+ by_doc[doc_id] = {'path': rel_path, 'chunks': []}
40
+ by_doc[doc_id]['chunks'].append((chunk_id, start_line, end_line))
41
+
42
+ results = []
43
+
44
+ # For each document, read source and find exact lines
45
+ for doc_id, info in by_doc.items():
46
+ rel_path = info['path']
47
+
48
+ # Get min/max line range for this document
49
+ min_line = min(c[1] for c in info['chunks'])
50
+ max_line = max(c[2] for c in info['chunks'])
51
+
52
+ # Read only the needed range
53
+ lines = read_source_lines(project_root, rel_path, min_line, max_line)
54
+ if lines is None:
55
+ # If can't read file, fall back to chunk start lines
56
+ for chunk_id, start_line, end_line in info['chunks']:
57
+ results.append((rel_path, start_line + 1))
58
+ continue
59
+
60
+ # Search for the symbol in each chunk's line range
61
+ for chunk_id, start_line, end_line in info['chunks']:
62
+ # Search within the chunk range (adjusted for offset)
63
+ offset = min_line
64
+ for line_idx in range(start_line - offset, min(end_line - offset + 1, len(lines))):
65
+ line = lines[line_idx]
66
+ # Simple check: does the line contain the symbol name?
67
+ if leaf in line:
68
+ results.append((rel_path, line_idx + offset + 1))
69
+ break # One match per chunk is enough
70
+ else:
71
+ # Fallback to chunk start line
72
+ results.append((rel_path, start_line + 1))
73
+
74
+ return results
75
+
76
+
77
+ def main(args):
78
+ """Find all references to a symbol."""
79
+ db = get_db()
80
+
81
+ symbols = resolve_symbol(db, args.symbol)
82
+ if not symbols:
83
+ print(f"Symbol '{args.symbol}' not found", file=sys.stderr)
84
+ sys.exit(1)
85
+
86
+ warn_ambiguous(args.symbol, symbols, "symbol")
87
+
88
+ symbol_id, symbol_str, display_name = symbols[0]
89
+
90
+ project_root = find_project_root()
91
+ if not project_root:
92
+ print("Error: Could not find project root", file=sys.stderr)
93
+ sys.exit(1)
94
+
95
+ refs = get_exact_refs(db, symbol_id, project_root)
96
+
97
+ if not refs:
98
+ print(f"No references found for '{args.symbol}'", file=sys.stderr)
99
+ sys.exit(1)
100
+
101
+ # Deduplicate and sort
102
+ seen = set()
103
+ for path, line in refs:
104
+ key = (path, line)
105
+ if key not in seen:
106
+ seen.add(key)
107
+ print(f"{path}:{line}")
@@ -0,0 +1,120 @@
1
+ """search command - search symbols by pattern."""
2
+ import sys
3
+ import re
4
+
5
+ from ..lib import (
6
+ get_db,
7
+ infer_kind,
8
+ )
9
+
10
+
11
+ def parse_symbol(symbol):
12
+ """Parse SCIP symbol into (file_path, symbol_name).
13
+
14
+ Example:
15
+ scip-typescript npm rovetia-app 1.2 src/hooks/`useDictation.ts`/useDictation().
16
+ -> ('src/hooks/useDictation.ts', 'useDictation()')
17
+ """
18
+ # Find the backtick-wrapped file path
19
+ match = re.search(r'`([^`]+)`', symbol)
20
+ if not match:
21
+ return ('?', '?')
22
+
23
+ filename = match.group(1)
24
+
25
+ # Get the directory path before the backtick
26
+ before = symbol[:match.start()]
27
+ # Extract path after version (4th space-separated token)
28
+ parts = before.split()
29
+ if len(parts) >= 5:
30
+ dir_path = ' '.join(parts[4:])
31
+ file_path = dir_path + filename
32
+ else:
33
+ file_path = filename
34
+
35
+ # Symbol name is everything after the closing backtick + /
36
+ after_file = symbol[match.end():]
37
+ if after_file.startswith('/'):
38
+ after_file = after_file[1:]
39
+
40
+ # Remove trailing .
41
+ symbol_name = after_file.rstrip('.')
42
+
43
+ return (file_path, symbol_name)
44
+
45
+
46
+ def is_noisy_symbol(symbol_str):
47
+ """Filter out noisy symbols (file-level, parameters, etc)."""
48
+ # File-level symbol (ends with /)
49
+ if symbol_str.endswith('/'):
50
+ return True
51
+
52
+ # Parameters (contain 0: like "phrases0:")
53
+ if re.search(r'\d+:', symbol_str):
54
+ return True
55
+
56
+ # Anonymous type literals (contain "typeLiteral")
57
+ if 'typeLiteral' in symbol_str:
58
+ return True
59
+
60
+ # Function parameters (like "isNotSupportedError().(err)")
61
+ if ').(' in symbol_str:
62
+ return True
63
+
64
+ return False
65
+
66
+
67
+ def kind_to_display(kind):
68
+ """Convert kind to display format (capitalized like bash version)."""
69
+ kind_map = {
70
+ 'function': 'Function',
71
+ 'method': 'Method',
72
+ 'class': 'Class',
73
+ 'interface': 'Interface',
74
+ 'type': 'TypeAlias',
75
+ 'variable': 'Variable',
76
+ }
77
+ return kind_map.get(kind, 'Unknown')
78
+
79
+
80
+ def main(args):
81
+ """Search symbols by pattern."""
82
+ db = get_db()
83
+
84
+ # Search symbols with line numbers in one query
85
+ rows = db.execute("""
86
+ SELECT gs.id, gs.symbol, gs.display_name, der.start_line
87
+ FROM global_symbols gs
88
+ LEFT JOIN defn_enclosing_ranges der ON gs.id = der.symbol_id
89
+ WHERE gs.symbol LIKE ?
90
+ LIMIT 100
91
+ """, (f"%{args.pattern}%",)).fetchall()
92
+
93
+ if not rows:
94
+ print(f"No symbols found matching '{args.pattern}'", file=sys.stderr)
95
+ sys.exit(1)
96
+
97
+ # Filter by kind if requested
98
+ if args.kind:
99
+ rows = [r for r in rows if infer_kind(r[1]) == args.kind]
100
+ if not rows:
101
+ print(f"No {args.kind} symbols found matching '{args.pattern}'", file=sys.stderr)
102
+ sys.exit(1)
103
+
104
+ for symbol_id, symbol_str, display_name, start_line in rows:
105
+ # Skip noisy symbols
106
+ if is_noisy_symbol(symbol_str):
107
+ continue
108
+
109
+ kind = infer_kind(symbol_str)
110
+ file_path, symbol_name = parse_symbol(symbol_str)
111
+
112
+ line = start_line + 1 if start_line is not None else 0
113
+
114
+ # Clean up symbol name: remove SCIP notation
115
+ symbol_name = symbol_name.rstrip('.#')
116
+ if symbol_name.endswith('()'):
117
+ symbol_name = symbol_name[:-2]
118
+
119
+ kind_display = kind_to_display(kind)
120
+ print(f"{file_path}:{line} {kind_display} {symbol_name}")
@@ -0,0 +1,24 @@
1
+ """skill command - install the scip-cli skill file."""
2
+ import sys
3
+ from pathlib import Path
4
+
5
+
6
+ def main(args):
7
+ """Dump or install the scip-cli SKILL.md."""
8
+ skill_path = Path(__file__).parent.parent / "SKILL.md"
9
+
10
+ if not skill_path.exists():
11
+ print("Error: SKILL.md not found in package", file=sys.stderr)
12
+ sys.exit(1)
13
+
14
+ content = skill_path.read_text()
15
+
16
+ if args.path:
17
+ # Write to file, creating parent directories
18
+ target = Path(args.path).expanduser()
19
+ target.parent.mkdir(parents=True, exist_ok=True)
20
+ target.write_text(content)
21
+ print(f"Installed skill to {target}")
22
+ else:
23
+ # Print to stdout
24
+ print(content)
@@ -0,0 +1,42 @@
1
+ """symbols command - list symbols in a file."""
2
+ import sys
3
+
4
+ from ..lib import (
5
+ get_db,
6
+ resolve_file,
7
+ warn_ambiguous,
8
+ get_file_symbols,
9
+ infer_kind,
10
+ extract_leaf_name,
11
+ )
12
+
13
+
14
+ def main(args):
15
+ """List all symbols in a file."""
16
+ db = get_db()
17
+
18
+ # Resolve file pattern to actual path
19
+ files = resolve_file(db, args.file)
20
+ if not files:
21
+ print(f"File '{args.file}' not found", file=sys.stderr)
22
+ sys.exit(1)
23
+
24
+ if len(files) > 1:
25
+ warn_ambiguous(args.file, files, "file")
26
+
27
+ file_path = files[0]
28
+
29
+ # Get all symbols defined in this file
30
+ symbols = get_file_symbols(db, file_path)
31
+ if not symbols:
32
+ print(f"No symbols found in '{file_path}'", file=sys.stderr)
33
+ sys.exit(1)
34
+
35
+ for symbol_id, symbol_str, display_name, start_line, end_line in symbols:
36
+ # Skip file-level symbols (end with /)
37
+ if symbol_str.endswith('/'):
38
+ continue
39
+ kind = infer_kind(symbol_str)
40
+ short = extract_leaf_name(symbol_str)
41
+ line_info = f"{start_line + 1}-{end_line + 1}" if start_line is not None else "??"
42
+ print(f"{line_info} {kind} {short}")
scip_cli/lib.py ADDED
@@ -0,0 +1,369 @@
1
+ #!/usr/bin/env python3
2
+ """Common utilities for read-symbol Python scripts."""
3
+ import hashlib
4
+ import os
5
+ import sqlite3
6
+ import subprocess
7
+ import sys
8
+ import tempfile
9
+ from pathlib import Path
10
+
11
+
12
+ def find_project_root(start_dir=None):
13
+ """Walk up from start_dir (or cwd) to find project root."""
14
+ markers = ["package.json", "tsconfig.json", "pyproject.toml", "Cargo.toml", "go.mod"]
15
+ d = Path(start_dir or os.getcwd()).resolve()
16
+ while d != d.parent:
17
+ if any((d / m).exists() for m in markers):
18
+ return d
19
+ d = d.parent
20
+ return None
21
+
22
+
23
+ def detect_language(project_root):
24
+ """Detect language from project markers.
25
+
26
+ Returns: 'typescript', 'python', 'rust', 'go', or 'typescript' (default)
27
+ """
28
+ root = Path(project_root)
29
+ if (root / "tsconfig.json").exists() or (root / "package.json").exists():
30
+ return "typescript"
31
+ elif (root / "pyproject.toml").exists() or (root / "setup.py").exists():
32
+ return "python"
33
+ elif (root / "Cargo.toml").exists():
34
+ return "rust"
35
+ elif (root / "go.mod").exists():
36
+ return "go"
37
+ else:
38
+ return "typescript" # default
39
+
40
+
41
+ def find_db(project_root=None):
42
+ """Find the index.db for the given project (or cwd)."""
43
+ root = project_root or find_project_root()
44
+ if not root:
45
+ return None
46
+ h = hashlib.sha256(str(root).encode()).hexdigest()[:12]
47
+ cache = Path.home() / ".cache" / "scip-query" / "projects" / h / "index.db"
48
+ if cache.exists():
49
+ return cache
50
+ return None
51
+
52
+
53
+ def get_db(project_root=None):
54
+ """Get a sqlite3 connection to the index.db.
55
+
56
+ If no index exists, auto-index the project with the detected language.
57
+ """
58
+ db_path = find_db(project_root)
59
+ if not db_path:
60
+ # Auto-index
61
+ root = project_root or find_project_root()
62
+ if not root:
63
+ print("Error: Could not find project root", file=sys.stderr)
64
+ sys.exit(1)
65
+
66
+ lang = detect_language(root)
67
+ print(f"Auto-indexing {root} ({lang})...", file=sys.stderr)
68
+
69
+ # Create cache directory
70
+ h = hashlib.sha256(str(root).encode()).hexdigest()[:12]
71
+ cache_dir = Path.home() / ".cache" / "scip-query" / "projects" / h
72
+ cache_dir.mkdir(parents=True, exist_ok=True)
73
+
74
+ # Run language-specific indexer
75
+ with tempfile.TemporaryDirectory() as tmpdir:
76
+ index_scip = os.path.join(tmpdir, "index.scip")
77
+
78
+ if lang == "typescript":
79
+ indexer_cmd = ["scip-typescript", "index", "--output", index_scip]
80
+ elif lang == "python":
81
+ indexer_cmd = ["scip-python", "index", ".", "--output", index_scip]
82
+ else:
83
+ print(f"Error: Unsupported language '{lang}'", file=sys.stderr)
84
+ sys.exit(1)
85
+
86
+ result = subprocess.run(
87
+ indexer_cmd,
88
+ cwd=str(root),
89
+ capture_output=True,
90
+ text=True
91
+ )
92
+
93
+ if result.returncode != 0:
94
+ print(f"Error: Failed to index project", file=sys.stderr)
95
+ print(result.stderr, file=sys.stderr)
96
+ sys.exit(1)
97
+
98
+ # Convert index.scip to index.db
99
+ index_db = cache_dir / "index.db"
100
+ result = subprocess.run(
101
+ ["scip", "expt-convert", index_scip, "--output", str(index_db)],
102
+ capture_output=True,
103
+ text=True
104
+ )
105
+
106
+ if result.returncode != 0:
107
+ print(f"Error: Failed to convert index", file=sys.stderr)
108
+ print(result.stderr, file=sys.stderr)
109
+ sys.exit(1)
110
+
111
+ # Try again
112
+ db_path = find_db(project_root)
113
+ if not db_path:
114
+ print("Error: No index.db found after indexing", file=sys.stderr)
115
+ sys.exit(1)
116
+
117
+ return sqlite3.connect(str(db_path))
118
+
119
+
120
+ def infer_kind(symbol):
121
+ """Infer symbol kind from symbol string pattern.
122
+
123
+ Returns: 'function', 'method', 'class', 'interface', 'type', 'property', 'variable', or 'unknown'
124
+ """
125
+ # Method: has # in middle and ends with ().
126
+ if "#" in symbol and symbol.endswith("()."):
127
+ return "method"
128
+ # Function: ends with (). but no # before it
129
+ if symbol.endswith("().") and "#" not in symbol.split("/")[-1]:
130
+ return "function"
131
+ # Class: ends with # and name starts with uppercase
132
+ if symbol.endswith("#"):
133
+ name = symbol.split("/")[-1].rstrip("#")
134
+ if name and name[0].isupper():
135
+ # Could be class or interface - check if it's an interface
136
+ # Interfaces often have "Interface" in display_name or are in .d.ts
137
+ return "class" # default to class
138
+ # Type alias: ends with # and name starts with uppercase (but not class)
139
+ if symbol.endswith("#"):
140
+ return "type"
141
+ # Property: type literal property (ParentClass#typeLiteral0:propertyName.)
142
+ if "#typeLiteral" in symbol and ":" in symbol and symbol.endswith("."):
143
+ return "property"
144
+ # Variable/const: ends with . but not ()
145
+ if symbol.endswith(".") and not symbol.endswith("()."):
146
+ return "variable"
147
+ return "unknown"
148
+
149
+
150
+ def resolve_symbol(db, name, kind_filter=None):
151
+ """Resolve bare name to symbol_id(s).
152
+
153
+ Args:
154
+ db: sqlite3 connection
155
+ name: bare symbol name (e.g., "useDictation")
156
+ kind_filter: optional kind filter ('function', 'class', etc)
157
+
158
+ Returns:
159
+ List of (symbol_id, symbol, display_name) tuples
160
+ """
161
+ # Try exact leaf match first with single query using OR
162
+ rows = db.execute("""
163
+ SELECT id, symbol, display_name FROM global_symbols
164
+ WHERE symbol LIKE ? OR symbol LIKE ? OR symbol LIKE ?
165
+ """, (
166
+ f"%/{name}().", # function/method
167
+ f"%/{name}#", # class/interface/type
168
+ f"%/{name}.", # variable/property
169
+ )).fetchall()
170
+
171
+ results = list(rows)
172
+
173
+ # If no exact match, try contains
174
+ if not results:
175
+ rows = db.execute(
176
+ "SELECT id, symbol, display_name FROM global_symbols WHERE symbol LIKE ?",
177
+ (f"%{name}%",)
178
+ ).fetchall()
179
+ # Filter to leaf matches (name appears after last /)
180
+ results = [r for r in rows if name in r[1].split("/")[-1]]
181
+
182
+ # Apply kind filter if provided
183
+ if kind_filter and results:
184
+ filtered = [r for r in results if infer_kind(r[1]) == kind_filter]
185
+ if filtered:
186
+ results = filtered
187
+
188
+ return results
189
+
190
+
191
+ def resolve_file(db, file_pattern):
192
+ """Resolve file pattern to relative_path.
193
+
194
+ Args:
195
+ db: sqlite3 connection
196
+ file_pattern: file path or pattern (e.g., "useDictation" or "src/hooks/useDictation.ts")
197
+
198
+ Returns:
199
+ List of matching relative_paths
200
+ """
201
+ # Try exact match first
202
+ rows = db.execute(
203
+ "SELECT relative_path FROM documents WHERE relative_path = ?",
204
+ (file_pattern,)
205
+ ).fetchall()
206
+ if rows:
207
+ return [r[0] for r in rows]
208
+
209
+ # Try LIKE match
210
+ if "/" not in file_pattern and "." not in file_pattern:
211
+ # Bare name - search in filename
212
+ pattern = f"%{file_pattern}%"
213
+ else:
214
+ pattern = file_pattern.replace("*", "%")
215
+
216
+ rows = db.execute(
217
+ "SELECT relative_path FROM documents WHERE relative_path LIKE ?",
218
+ (pattern,)
219
+ ).fetchall()
220
+ return [r[0] for r in rows]
221
+
222
+
223
+ def get_file_symbols(db, relative_path):
224
+ """Get all symbols defined in a file.
225
+
226
+ Returns:
227
+ List of (symbol_id, symbol, display_name, start_line, end_line) tuples
228
+ """
229
+ rows = db.execute("""
230
+ SELECT gs.id, gs.symbol, gs.display_name, der.start_line, der.end_line
231
+ FROM global_symbols gs
232
+ JOIN defn_enclosing_ranges der ON gs.id = der.symbol_id
233
+ JOIN documents d ON der.document_id = d.id
234
+ WHERE d.relative_path = ?
235
+ ORDER BY der.start_line
236
+ """, (relative_path,)).fetchall()
237
+ return rows
238
+
239
+
240
+ def get_refs_for_symbols(db, symbol_ids):
241
+ """Get all references for multiple symbol_ids in one query.
242
+
243
+ Args:
244
+ db: sqlite3 connection
245
+ symbol_ids: list of symbol IDs
246
+
247
+ Returns:
248
+ Dict mapping symbol_id -> list of (relative_path, start_line) tuples
249
+ """
250
+ if not symbol_ids:
251
+ return {}
252
+
253
+ placeholders = ','.join('?' * len(symbol_ids))
254
+ rows = db.execute(f"""
255
+ SELECT m.symbol_id, d.relative_path, c.start_line
256
+ FROM mentions m
257
+ JOIN chunks c ON m.chunk_id = c.id
258
+ JOIN documents d ON c.document_id = d.id
259
+ WHERE m.symbol_id IN ({placeholders}) AND m.role != 1
260
+ """, symbol_ids).fetchall()
261
+
262
+ result = {}
263
+ for symbol_id, path, line in rows:
264
+ if symbol_id not in result:
265
+ result[symbol_id] = []
266
+ result[symbol_id].append((path, line))
267
+
268
+ return result
269
+
270
+
271
+ def get_members(db, symbol_id):
272
+ """Get members (children) of a symbol.
273
+
274
+ Returns:
275
+ List of (symbol_id, symbol, display_name, start_line, end_line) tuples
276
+ """
277
+ # Get the symbol string for this symbol_id
278
+ row = db.execute("SELECT symbol FROM global_symbols WHERE id = ?", (symbol_id,)).fetchone()
279
+ if not row:
280
+ return []
281
+ parent_symbol = row[0]
282
+
283
+ # Try enclosing_symbol first
284
+ rows = db.execute("""
285
+ SELECT gs.id, gs.symbol, gs.display_name, der.start_line, der.end_line
286
+ FROM global_symbols gs
287
+ LEFT JOIN defn_enclosing_ranges der ON gs.id = der.symbol_id
288
+ WHERE gs.enclosing_symbol = ?
289
+ ORDER BY der.start_line
290
+ """, (parent_symbol,)).fetchall()
291
+
292
+ # Fall back to symbol prefix matching if no results
293
+ if not rows:
294
+ rows = db.execute("""
295
+ SELECT gs.id, gs.symbol, gs.display_name, der.start_line, der.end_line
296
+ FROM global_symbols gs
297
+ LEFT JOIN defn_enclosing_ranges der ON gs.id = der.symbol_id
298
+ WHERE gs.symbol LIKE ? AND gs.symbol != ?
299
+ ORDER BY der.start_line
300
+ """, (parent_symbol + '%', parent_symbol)).fetchall()
301
+
302
+ # Filter out function parameters (contain "().(" pattern)
303
+ rows = [r for r in rows if ").(" not in r[1]]
304
+
305
+ return rows
306
+
307
+
308
+ def extract_leaf_name(symbol_str):
309
+ """Extract the leaf name from a SCIP symbol string.
310
+
311
+ Example:
312
+ .../useDictationOrRecording(). -> useDictationOrRecording
313
+ .../UseDictationOrRecordingOptions# -> UseDictationOrRecordingOptions
314
+ .../UseDictationOrRecordingOptions#typeLiteral0:onFallbackToRecording. -> onFallbackToRecording
315
+ .../GameEngine#config. -> config
316
+ .../GameEngine#`<get>aliveHeroes`(). -> aliveHeroes
317
+ """
318
+ leaf = symbol_str.split("/")[-1].rstrip(".#")
319
+ if leaf.endswith("()"):
320
+ leaf = leaf[:-2]
321
+ # Handle type literal properties (ParentClass#typeLiteral0:propertyName)
322
+ if ":" in leaf:
323
+ leaf = leaf.split(":")[-1]
324
+ # Handle class members (ParentClass#memberName)
325
+ if "#" in leaf:
326
+ leaf = leaf.split("#")[-1]
327
+ # Remove backticks
328
+ leaf = leaf.replace("`", "")
329
+ # Handle getters/setters: <get>name -> name
330
+ if leaf.startswith("<get>"):
331
+ leaf = leaf[5:]
332
+ elif leaf.startswith("<set>"):
333
+ leaf = leaf[5:]
334
+ return leaf
335
+
336
+
337
+ def read_source_lines(project_root, relative_path, start_line=None, end_line=None):
338
+ """Read source lines from filesystem.
339
+
340
+ Args:
341
+ project_root: Project root path
342
+ relative_path: Relative path to file
343
+ start_line: Optional start line (0-indexed)
344
+ end_line: Optional end line (0-indexed, inclusive)
345
+
346
+ Returns:
347
+ List of lines, or None if file cannot be read
348
+ """
349
+ full_path = os.path.join(str(project_root), relative_path)
350
+ try:
351
+ with open(full_path, 'r', encoding='utf-8') as f:
352
+ lines = f.readlines()
353
+ if start_line is not None and end_line is not None:
354
+ return lines[start_line:end_line + 1]
355
+ return lines
356
+ except Exception:
357
+ return None
358
+
359
+
360
+ def warn_ambiguous(name, matches, context="symbol"):
361
+ """Print warning if multiple matches found.
362
+
363
+ Args:
364
+ name: The search pattern
365
+ matches: List of matches
366
+ context: Description of what was searched (e.g., "symbol", "file")
367
+ """
368
+ if len(matches) > 1:
369
+ print(f"Ambiguous {context} '{name}' ({len(matches)} matches). Using first match: {matches[0][1] if isinstance(matches[0], tuple) else matches[0]}", file=sys.stderr)
@@ -0,0 +1,154 @@
1
+ Metadata-Version: 2.4
2
+ Name: scip-cli
3
+ Version: 1.0.0
4
+ Summary: Fast code intelligence via SCIP indexes
5
+ Home-page: https://github.com/flesler/scip-cli
6
+ Author: Ariel Flesler
7
+ License: MIT
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Classifier: Topic :: Software Development :: Code Generators
12
+ Requires-Python: >=3.7
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE
15
+ Dynamic: author
16
+ Dynamic: classifier
17
+ Dynamic: description
18
+ Dynamic: description-content-type
19
+ Dynamic: home-page
20
+ Dynamic: license
21
+ Dynamic: license-file
22
+ Dynamic: requires-python
23
+ Dynamic: summary
24
+
25
+ # scip-cli
26
+
27
+ [![PyPI version](https://badge.fury.io/py/scip-cli.svg)](https://badge.fury.io/py/scip-cli)
28
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
29
+
30
+ Fast code intelligence CLI for TypeScript/JavaScript projects. Query SCIP indexes directly via SQLite for instant results.
31
+
32
+ ## Features
33
+
34
+ - **Fast**: Direct SQLite queries, 100-500x faster than bash wrappers
35
+ - **Simple**: Single binary with subcommands
36
+ - **Auto-indexing**: Automatically indexes projects on first query
37
+ - **Token-efficient**: Clean, minimal output optimized for AI consumption
38
+
39
+ ## Installation
40
+
41
+ ### 1. Install scip-cli
42
+
43
+ ```bash
44
+ pip install scip-cli
45
+ ```
46
+
47
+ ### 2. Install prerequisites
48
+
49
+ scip-cli requires two external tools for indexing:
50
+
51
+ **For TypeScript/JavaScript projects:**
52
+
53
+ ```bash
54
+ # Install scip-typescript (SCIP indexer)
55
+ npm install -g @sourcegraph/scip-typescript
56
+
57
+ # Install scip (SCIP CLI for index conversion)
58
+ npm install -g @sourcegraph/scip
59
+ ```
60
+
61
+ **Verify installation:**
62
+
63
+ ```bash
64
+ scip-cli --help
65
+ scip-typescript --version
66
+ scip --version
67
+ ```
68
+
69
+ ## Prerequisites
70
+
71
+ - Python 3.7+
72
+ - `scip-typescript` (for indexing TypeScript/JavaScript)
73
+ - `scip` CLI (for converting indexes)
74
+
75
+ ## Usage
76
+
77
+ All commands are subcommands of `scip-cli`:
78
+
79
+ ```bash
80
+ scip-cli <command> [arguments]
81
+ ```
82
+
83
+ ### Commands
84
+
85
+ - `refs <symbol>` - Find all references to a symbol
86
+ - `def <symbol>` - Find symbol definition with source code
87
+ - `search <pattern>` - Search symbols by name pattern
88
+ - `symbols <file>` - List all symbols in a file
89
+ - `rdeps <file>` - Find files that depend on a file
90
+ - `members <symbol>` - List members of a class/interface
91
+ - `skill [path]` - Install or dump the SKILL.md
92
+
93
+ ### Examples
94
+
95
+ ```bash
96
+ # Find where useDictation is used
97
+ scip-cli refs useDictation
98
+
99
+ # Get definition of useDictation
100
+ scip-cli def useDictation
101
+
102
+ # Search for symbols matching "Dictation"
103
+ scip-cli search Dictation
104
+
105
+ # List symbols in a file
106
+ scip-cli symbols src/hooks/useDictation.ts
107
+
108
+ # Find files that import from useDictation.ts
109
+ scip-cli rdeps src/hooks/useDictation.ts
110
+
111
+ # List members of a class
112
+ scip-cli members UseDictationOptions
113
+
114
+ # Install skill file
115
+ scip-cli skill ~/.claude/skills/scip/SKILL.md
116
+ ```
117
+
118
+ ## How It Works
119
+
120
+ 1. On first query, automatically indexes the project using `scip-typescript`
121
+ 2. Converts the SCIP index to SQLite using `scip expt-convert`
122
+ 3. Caches the database in `~/.cache/scip-query/projects/<hash>/index.db`
123
+ 4. Subsequent queries are instant SQLite lookups
124
+
125
+ ## Performance
126
+
127
+ Compared to bash wrappers:
128
+ - `refs`: 6.4s → 0.03s (213x faster)
129
+ - `def`: 2.8s → 0.05s (56x faster)
130
+ - `search`: 2.6s → 0.03s (87x faster)
131
+ - `symbols`: 0.3s → 0.02s (15x faster)
132
+ - `rdeps`: 0.2s → 0.02s (10x faster)
133
+ - `members`: 3.1s → 0.03s (103x faster)
134
+
135
+ ## Architecture
136
+
137
+ ```
138
+ scip_cli/
139
+ ├── __init__.py
140
+ ├── __main__.py # CLI entry point
141
+ ├── lib.py # Core utilities (indexing, symbol resolution)
142
+ └── commands/ # Subcommand implementations
143
+ ├── refs.py
144
+ ├── def_cmd.py
145
+ ├── search.py
146
+ ├── symbols.py
147
+ ├── rdeps.py
148
+ ├── members.py
149
+ └── skill.py
150
+ ```
151
+
152
+ ## License
153
+
154
+ MIT
@@ -0,0 +1,18 @@
1
+ scip_cli/SKILL.md,sha256=FegRCcs89-zfgRYRIK-jEER8Oo7W1mfH_nh6TcJ9Pnc,2565
2
+ scip_cli/__init__.py,sha256=n7LqaekbFRVz8NfgMtvorJznVidEAbvYHnCwq4UmhrE,79
3
+ scip_cli/__main__.py,sha256=fF4bAaprUmJwo7qc8T2h4DMMdcE39ky9LoypKhvJmPg,2346
4
+ scip_cli/lib.py,sha256=43oqd8wry7mZo0bd_-jUJAq8ApD-tKFOA9nSSeuiiJs,12552
5
+ scip_cli/commands/__init__.py,sha256=kPFvjkdKpwb__8ce7gmDLM0Shi7aCNCjYCoctY8NxQw,36
6
+ scip_cli/commands/def_cmd.py,sha256=jGywWvbYbnSM0XqwNz0xV7ZFpYOQP73SuZQHqqDLBrY,1489
7
+ scip_cli/commands/members.py,sha256=6ayDcEXicbAKAKE0-M82viesZVkGgRSOrV1t47_MVL4,3112
8
+ scip_cli/commands/rdeps.py,sha256=oQIYtkqUZNJ_NOAitz3tl-Wn6ZGbwjY2jec99wpVYpo,1409
9
+ scip_cli/commands/refs.py,sha256=z_jBTMXxtElSBycchrR2ZNqmtdVJ4TMEWYArkW0hAU0,3531
10
+ scip_cli/commands/search.py,sha256=gYt0F7DRnh3fnx76yiPAes4sPGe7YnGYSxGPCljti8g,3500
11
+ scip_cli/commands/skill.py,sha256=34MtCB8K5zeOwr2IoH34O9VcwDgZXMqo0bFZ2_DdDY0,706
12
+ scip_cli/commands/symbols.py,sha256=IEQo2iAlPAVlhSwGdJ17Egi0GLik9pQAmI9KsCG19Jc,1174
13
+ scip_cli-1.0.0.dist-info/licenses/LICENSE,sha256=lFrvF9tXp4iR2I6tUUH1uZn2Fi3-YO2zftDzjHG7jeY,1070
14
+ scip_cli-1.0.0.dist-info/METADATA,sha256=78WMIKLkzB7HOy3jywtwyQeYwOPd-8z1qE2GB7Th6zE,3838
15
+ scip_cli-1.0.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
16
+ scip_cli-1.0.0.dist-info/entry_points.txt,sha256=UhydAcSviNn3eyCU0DHIKqTsUYs9PEFCbT5Ks6yAPOQ,52
17
+ scip_cli-1.0.0.dist-info/top_level.txt,sha256=oyGHgK3piiQuYzJ0_H7unVzQOMdsIjIk6yX05o6-zXc,9
18
+ scip_cli-1.0.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (82.0.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ scip-cli = scip_cli.__main__:main
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Ariel Flesler
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ scip_cli