-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathannotator.py
More file actions
87 lines (80 loc) · 3.13 KB
/
annotator.py
File metadata and controls
87 lines (80 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# mcp-codebase-index - Structural codebase indexer with MCP server
# Copyright (C) 2026 Michael Doyle
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# Commercial licensing available. See COMMERCIAL-LICENSE.md for details.
"""Dispatch layer that selects the appropriate annotator by file type."""
from mcp_codebase_index.csharp_annotator import annotate_csharp
from mcp_codebase_index.generic_annotator import annotate_generic
from mcp_codebase_index.go_annotator import annotate_go
from mcp_codebase_index.json_annotator import annotate_json
from mcp_codebase_index.models import StructuralMetadata
from mcp_codebase_index.python_annotator import annotate_python
from mcp_codebase_index.rust_annotator import annotate_rust
from mcp_codebase_index.text_annotator import annotate_text
from mcp_codebase_index.typescript_annotator import annotate_typescript
_EXTENSION_MAP: dict[str, str] = {
".py": "python",
".pyw": "python",
".md": "text",
".txt": "text",
".rst": "text",
".ts": "typescript",
".tsx": "typescript",
".js": "javascript",
".jsx": "javascript",
".go": "go",
".rs": "rust",
".cs": "csharp",
".json": "json",
}
def annotate(
text: str,
source_name: str = "<source>",
file_type: str | None = None,
) -> StructuralMetadata:
"""Annotate text with structural metadata.
Dispatch rules:
- file_type overrides extension-based detection
- .py -> python annotator
- .md, .txt, .rst -> text annotator
- .ts, .tsx -> typescript annotator
- .js, .jsx -> typescript annotator (close enough for regex-based parsing)
- .go -> go annotator
- .rs -> rust annotator
- Otherwise -> generic annotator (line-only)
"""
if file_type is None:
# Detect from source_name extension
dot_idx = source_name.rfind(".")
if dot_idx >= 0:
ext = source_name[dot_idx:].lower()
file_type = _EXTENSION_MAP.get(ext)
if file_type == "python":
return annotate_python(text, source_name)
elif file_type == "text":
return annotate_text(text, source_name)
elif file_type in ("typescript", "javascript"):
return annotate_typescript(text, source_name)
elif file_type == "go":
return annotate_go(text, source_name)
elif file_type == "rust":
return annotate_rust(text, source_name)
elif file_type == "csharp":
return annotate_csharp(text, source_name)
elif file_type == "json":
return annotate_json(text, source_name)
else:
return annotate_generic(text, source_name)