codemap-ruby 0.1.0a1__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.
codemap_ruby/__init__.py
ADDED
codemap_ruby/indexer.py
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
"""Ruby indexer built on tree-sitter-ruby.
|
|
2
|
+
|
|
3
|
+
Tracks a class stack so nested modules / classes produce qualified
|
|
4
|
+
symbol IDs (``Outer#Inner#m()``). Top-level ``def`` is a function; ``def``
|
|
5
|
+
inside a class or module body is a method. ``singleton_method`` (``def
|
|
6
|
+
self.x``) is recorded as a method tagged ``extra.ruby_kind="singleton"``.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from pathlib import Path, PurePosixPath
|
|
12
|
+
from typing import ClassVar
|
|
13
|
+
|
|
14
|
+
import tree_sitter
|
|
15
|
+
import tree_sitter_ruby
|
|
16
|
+
|
|
17
|
+
from codemap.core.models import Diagnostic, Edge, IndexResult, Range, Symbol
|
|
18
|
+
from codemap.core.symbol import Descriptor, DescriptorKind, SymbolID
|
|
19
|
+
from codemap.indexers.base import IndexContext
|
|
20
|
+
|
|
21
|
+
SCHEME = "scip-ruby"
|
|
22
|
+
LANG = "ruby"
|
|
23
|
+
|
|
24
|
+
_RB_LANG = tree_sitter.Language(tree_sitter_ruby.language())
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class RubyIndexer:
|
|
28
|
+
name: ClassVar[str] = "ruby"
|
|
29
|
+
version: ClassVar[str] = "0.1.0"
|
|
30
|
+
file_patterns: ClassVar[list[str]] = ["*.rb"]
|
|
31
|
+
languages: ClassVar[list[str]] = [LANG]
|
|
32
|
+
|
|
33
|
+
def supports(self, path: Path) -> bool:
|
|
34
|
+
return path.suffix == ".rb"
|
|
35
|
+
|
|
36
|
+
def index_file(
|
|
37
|
+
self,
|
|
38
|
+
path: Path,
|
|
39
|
+
source: bytes,
|
|
40
|
+
ctx: IndexContext,
|
|
41
|
+
) -> IndexResult:
|
|
42
|
+
try:
|
|
43
|
+
source.decode("utf-8")
|
|
44
|
+
except UnicodeDecodeError as exc:
|
|
45
|
+
return IndexResult(
|
|
46
|
+
diagnostics=[
|
|
47
|
+
Diagnostic(
|
|
48
|
+
severity="error",
|
|
49
|
+
file=ctx.relative_path,
|
|
50
|
+
code="RB002",
|
|
51
|
+
message=f"not valid UTF-8: {exc}",
|
|
52
|
+
producer=self.name,
|
|
53
|
+
)
|
|
54
|
+
]
|
|
55
|
+
)
|
|
56
|
+
parser = tree_sitter.Parser(_RB_LANG)
|
|
57
|
+
tree = parser.parse(source)
|
|
58
|
+
visitor = _Visitor(ctx.relative_path)
|
|
59
|
+
visitor.visit(tree.root_node)
|
|
60
|
+
diagnostics = list(visitor.diagnostics)
|
|
61
|
+
if tree.root_node.has_error:
|
|
62
|
+
diagnostics.append(
|
|
63
|
+
Diagnostic(
|
|
64
|
+
severity="warning",
|
|
65
|
+
file=ctx.relative_path,
|
|
66
|
+
range=Range(start_line=1, end_line=1),
|
|
67
|
+
code="RB001",
|
|
68
|
+
message="tree-sitter reported parse errors; symbols may be incomplete",
|
|
69
|
+
producer=self.name,
|
|
70
|
+
)
|
|
71
|
+
)
|
|
72
|
+
return IndexResult(
|
|
73
|
+
symbols=visitor.symbols,
|
|
74
|
+
edges=visitor.edges,
|
|
75
|
+
diagnostics=diagnostics,
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
class _Visitor:
|
|
80
|
+
def __init__(self, relative_path: PurePosixPath) -> None:
|
|
81
|
+
self.relative_path = relative_path
|
|
82
|
+
self.symbols: list[Symbol] = []
|
|
83
|
+
self.edges: list[Edge] = []
|
|
84
|
+
self.diagnostics: list[Diagnostic] = []
|
|
85
|
+
self._type_stack: list[str] = []
|
|
86
|
+
|
|
87
|
+
def visit(self, node: tree_sitter.Node) -> None:
|
|
88
|
+
kind = node.type
|
|
89
|
+
if kind == "class":
|
|
90
|
+
self._visit_type(node, ruby_kind="class")
|
|
91
|
+
return
|
|
92
|
+
if kind == "module":
|
|
93
|
+
self._visit_type(node, ruby_kind="module")
|
|
94
|
+
return
|
|
95
|
+
if kind == "method":
|
|
96
|
+
self._visit_method(node, singleton=False)
|
|
97
|
+
return
|
|
98
|
+
if kind == "singleton_method":
|
|
99
|
+
self._visit_method(node, singleton=True)
|
|
100
|
+
return
|
|
101
|
+
if kind == "assignment":
|
|
102
|
+
self._visit_assignment(node)
|
|
103
|
+
for child in node.children:
|
|
104
|
+
self.visit(child)
|
|
105
|
+
|
|
106
|
+
# ----------------------------------------------------- type-level
|
|
107
|
+
|
|
108
|
+
def _visit_type(self, node: tree_sitter.Node, *, ruby_kind: str) -> None:
|
|
109
|
+
name = _first_constant(node)
|
|
110
|
+
if name is None:
|
|
111
|
+
return
|
|
112
|
+
sid = self._make_id(name, kind=DescriptorKind.TYPE)
|
|
113
|
+
self.symbols.append(
|
|
114
|
+
Symbol(
|
|
115
|
+
id=sid,
|
|
116
|
+
kind="class",
|
|
117
|
+
language=LANG,
|
|
118
|
+
file=self.relative_path,
|
|
119
|
+
range=_node_range(node),
|
|
120
|
+
extra={"ruby_kind": ruby_kind},
|
|
121
|
+
)
|
|
122
|
+
)
|
|
123
|
+
self._type_stack.append(name)
|
|
124
|
+
try:
|
|
125
|
+
for child in node.children:
|
|
126
|
+
if child.type == "body_statement":
|
|
127
|
+
for grand in child.children:
|
|
128
|
+
self.visit(grand)
|
|
129
|
+
finally:
|
|
130
|
+
self._type_stack.pop()
|
|
131
|
+
|
|
132
|
+
# ----------------------------------------------------- methods
|
|
133
|
+
|
|
134
|
+
def _visit_method(self, node: tree_sitter.Node, *, singleton: bool) -> None:
|
|
135
|
+
name = _method_name(node)
|
|
136
|
+
if name is None:
|
|
137
|
+
return
|
|
138
|
+
sym_kind: str = "method" if self._type_stack else "function"
|
|
139
|
+
sid = self._make_id(name, kind=DescriptorKind.METHOD)
|
|
140
|
+
extra: dict[str, str] = {}
|
|
141
|
+
if singleton:
|
|
142
|
+
extra["ruby_kind"] = "singleton"
|
|
143
|
+
self.symbols.append(
|
|
144
|
+
Symbol(
|
|
145
|
+
id=sid,
|
|
146
|
+
kind=sym_kind, # type: ignore[arg-type]
|
|
147
|
+
language=LANG,
|
|
148
|
+
file=self.relative_path,
|
|
149
|
+
range=_node_range(node),
|
|
150
|
+
signature=f"def {name}",
|
|
151
|
+
extra=extra,
|
|
152
|
+
)
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
# ------------------------------------------------- constants / vars
|
|
156
|
+
|
|
157
|
+
def _visit_assignment(self, node: tree_sitter.Node) -> None:
|
|
158
|
+
# Capture top-level / inside-class assignments where the lhs is a
|
|
159
|
+
# constant (uppercase Ruby identifier). Ignore everything else
|
|
160
|
+
# (local variable assignments inside methods are not symbols).
|
|
161
|
+
lhs = node.children[0] if node.children else None
|
|
162
|
+
if lhs is None or lhs.type != "constant":
|
|
163
|
+
return
|
|
164
|
+
name = _node_text(lhs)
|
|
165
|
+
if not name:
|
|
166
|
+
return
|
|
167
|
+
sym_kind: str = "field" if self._type_stack else "variable"
|
|
168
|
+
sid = self._make_id(name, kind=DescriptorKind.TERM)
|
|
169
|
+
self.symbols.append(
|
|
170
|
+
Symbol(
|
|
171
|
+
id=sid,
|
|
172
|
+
kind=sym_kind, # type: ignore[arg-type]
|
|
173
|
+
language=LANG,
|
|
174
|
+
file=self.relative_path,
|
|
175
|
+
range=_node_range(node),
|
|
176
|
+
)
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
# -------------------------------------------------------- helpers
|
|
180
|
+
|
|
181
|
+
def _make_id(self, name: str, *, kind: DescriptorKind) -> SymbolID:
|
|
182
|
+
descriptors = list(_path_namespaces(self.relative_path))
|
|
183
|
+
descriptors.extend(Descriptor(name=t, kind=DescriptorKind.TYPE) for t in self._type_stack)
|
|
184
|
+
descriptors.append(Descriptor(name=name, kind=kind))
|
|
185
|
+
return SymbolID(scheme=SCHEME, descriptors=tuple(descriptors))
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
# ---------------------------------------------------------------------------
|
|
189
|
+
# Pure helpers
|
|
190
|
+
# ---------------------------------------------------------------------------
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def _path_namespaces(path: PurePosixPath) -> list[Descriptor]:
|
|
194
|
+
return [Descriptor(name=part, kind=DescriptorKind.NAMESPACE) for part in path.parts]
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
def _node_range(node: tree_sitter.Node) -> Range:
|
|
198
|
+
sr, sc = node.start_point
|
|
199
|
+
er, ec = node.end_point
|
|
200
|
+
return Range(
|
|
201
|
+
start_line=sr + 1,
|
|
202
|
+
start_col=sc,
|
|
203
|
+
end_line=max(er + 1, sr + 1),
|
|
204
|
+
end_col=ec,
|
|
205
|
+
)
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def _node_text(node: tree_sitter.Node) -> str:
|
|
209
|
+
return node.text.decode("utf-8") if node.text is not None else ""
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
def _first_constant(node: tree_sitter.Node) -> str | None:
|
|
213
|
+
for child in node.children:
|
|
214
|
+
if child.type == "constant":
|
|
215
|
+
return _node_text(child)
|
|
216
|
+
if child.type == "scope_resolution":
|
|
217
|
+
# nested: `class A::B` — take the trailing constant.
|
|
218
|
+
last = None
|
|
219
|
+
for grand in child.children:
|
|
220
|
+
if grand.type == "constant":
|
|
221
|
+
last = grand
|
|
222
|
+
if last is not None:
|
|
223
|
+
return _node_text(last)
|
|
224
|
+
return None
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
def _method_name(node: tree_sitter.Node) -> str | None:
|
|
228
|
+
"""For ``def x`` returns ``x``; for ``def self.x`` returns ``x``."""
|
|
229
|
+
for child in node.children:
|
|
230
|
+
if child.type == "identifier":
|
|
231
|
+
return _node_text(child)
|
|
232
|
+
return None
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: codemap-ruby
|
|
3
|
+
Version: 0.1.0a1
|
|
4
|
+
Summary: Ruby indexer plugin for CodeMap
|
|
5
|
+
Project-URL: Homepage, https://github.com/qxbyte/codemap
|
|
6
|
+
Author: CodeMap Contributors
|
|
7
|
+
License: MIT
|
|
8
|
+
Keywords: codemap,indexer,ruby,tree-sitter
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Ruby
|
|
12
|
+
Classifier: Topic :: Software Development
|
|
13
|
+
Requires-Python: >=3.11
|
|
14
|
+
Requires-Dist: codemap-core<0.2,>=0.1.0a1
|
|
15
|
+
Requires-Dist: tree-sitter-ruby>=0.23
|
|
16
|
+
Requires-Dist: tree-sitter>=0.25
|
|
17
|
+
Provides-Extra: dev
|
|
18
|
+
Requires-Dist: pytest>=8.0; extra == 'dev'
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
|
|
21
|
+
# codemap-ruby
|
|
22
|
+
|
|
23
|
+
> A Ruby indexer for [CodeMap](https://github.com/qxbyte/codemap),
|
|
24
|
+
> shipped as an independent PyPI package.
|
|
25
|
+
|
|
26
|
+
## What it captures
|
|
27
|
+
|
|
28
|
+
Backed by `tree-sitter-ruby`:
|
|
29
|
+
|
|
30
|
+
| AST node | Symbol kind |
|
|
31
|
+
|---|---|
|
|
32
|
+
| `class` | `class` (with `extra.ruby_kind=class`) |
|
|
33
|
+
| `module` | `class` (with `extra.ruby_kind=module`) |
|
|
34
|
+
| `method` (inside class/module) | `method` |
|
|
35
|
+
| `method` (top level) | `function` |
|
|
36
|
+
| `singleton_method` | `method` (with `extra.ruby_kind=singleton`) |
|
|
37
|
+
| top-level constant `assignment` | `variable` |
|
|
38
|
+
| constant assignment inside class/module | `field` |
|
|
39
|
+
|
|
40
|
+
Nested class / module declarations are tracked with a class stack:
|
|
41
|
+
`module A; class B; def m; end; end; end` produces `A#B#m()`.
|
|
42
|
+
|
|
43
|
+
## Install
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
pip install "git+https://github.com/qxbyte/codemap.git#subdirectory=plugins/codemap-ruby"
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## SymbolID encoding
|
|
50
|
+
|
|
51
|
+
```
|
|
52
|
+
scip-ruby . . . app/models/user.rb/User#hello().
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Limits
|
|
56
|
+
|
|
57
|
+
* `attr_accessor` / `attr_reader` / `attr_writer` macros are not
|
|
58
|
+
expanded into individual field symbols.
|
|
59
|
+
* `define_method`, `class_eval`, and other metaprogramming are not
|
|
60
|
+
resolved.
|
|
61
|
+
* Mixin (`include`/`extend`) edges are not yet emitted.
|
|
62
|
+
|
|
63
|
+
## License
|
|
64
|
+
|
|
65
|
+
MIT.
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
codemap_ruby/__init__.py,sha256=xyLwk6u2vZxIMB71iBxn-ishGv2N13LlkEMw1anJXCw,170
|
|
2
|
+
codemap_ruby/indexer.py,sha256=pggpwnDuzUIcQTbvwIw9FRRo2mA7ztywJRjsv-Ze37A,7866
|
|
3
|
+
codemap_ruby-0.1.0a1.dist-info/METADATA,sha256=IUAzqM7iEEDYA6_F9s7_vB64lIF6HsHDyMSEv5Hhss8,1833
|
|
4
|
+
codemap_ruby-0.1.0a1.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
|
|
5
|
+
codemap_ruby-0.1.0a1.dist-info/entry_points.txt,sha256=V7qgfdvY1UyVO4OxfMvCGxQruNXHG0QVo1Rm2x5jPl8,51
|
|
6
|
+
codemap_ruby-0.1.0a1.dist-info/RECORD,,
|