tree-sitter-analyzer 0.2.0__py3-none-any.whl → 0.3.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.
Potentially problematic release.
This version of tree-sitter-analyzer might be problematic. Click here for more details.
- tree_sitter_analyzer/__init__.py +133 -121
- tree_sitter_analyzer/__main__.py +11 -12
- tree_sitter_analyzer/api.py +531 -539
- tree_sitter_analyzer/cli/__init__.py +39 -39
- tree_sitter_analyzer/cli/__main__.py +12 -13
- tree_sitter_analyzer/cli/commands/__init__.py +26 -27
- tree_sitter_analyzer/cli/commands/advanced_command.py +88 -88
- tree_sitter_analyzer/cli/commands/base_command.py +160 -155
- tree_sitter_analyzer/cli/commands/default_command.py +18 -19
- tree_sitter_analyzer/cli/commands/partial_read_command.py +141 -133
- tree_sitter_analyzer/cli/commands/query_command.py +81 -82
- tree_sitter_analyzer/cli/commands/structure_command.py +138 -121
- tree_sitter_analyzer/cli/commands/summary_command.py +101 -93
- tree_sitter_analyzer/cli/commands/table_command.py +232 -233
- tree_sitter_analyzer/cli/info_commands.py +120 -121
- tree_sitter_analyzer/cli_main.py +277 -276
- tree_sitter_analyzer/core/__init__.py +15 -20
- tree_sitter_analyzer/core/analysis_engine.py +591 -574
- tree_sitter_analyzer/core/cache_service.py +320 -330
- tree_sitter_analyzer/core/engine.py +557 -560
- tree_sitter_analyzer/core/parser.py +293 -288
- tree_sitter_analyzer/core/query.py +494 -502
- tree_sitter_analyzer/encoding_utils.py +458 -460
- tree_sitter_analyzer/exceptions.py +337 -340
- tree_sitter_analyzer/file_handler.py +217 -222
- tree_sitter_analyzer/formatters/__init__.py +1 -1
- tree_sitter_analyzer/formatters/base_formatter.py +167 -168
- tree_sitter_analyzer/formatters/formatter_factory.py +78 -74
- tree_sitter_analyzer/formatters/java_formatter.py +287 -270
- tree_sitter_analyzer/formatters/python_formatter.py +255 -235
- tree_sitter_analyzer/interfaces/__init__.py +9 -10
- tree_sitter_analyzer/interfaces/cli.py +528 -557
- tree_sitter_analyzer/interfaces/cli_adapter.py +322 -319
- tree_sitter_analyzer/interfaces/mcp_adapter.py +180 -170
- tree_sitter_analyzer/interfaces/mcp_server.py +405 -416
- tree_sitter_analyzer/java_analyzer.py +218 -219
- tree_sitter_analyzer/language_detector.py +398 -400
- tree_sitter_analyzer/language_loader.py +224 -228
- tree_sitter_analyzer/languages/__init__.py +10 -11
- tree_sitter_analyzer/languages/java_plugin.py +1129 -1113
- tree_sitter_analyzer/languages/python_plugin.py +737 -712
- tree_sitter_analyzer/mcp/__init__.py +31 -32
- tree_sitter_analyzer/mcp/resources/__init__.py +44 -47
- tree_sitter_analyzer/mcp/resources/code_file_resource.py +212 -213
- tree_sitter_analyzer/mcp/resources/project_stats_resource.py +560 -550
- tree_sitter_analyzer/mcp/server.py +333 -345
- tree_sitter_analyzer/mcp/tools/__init__.py +30 -31
- tree_sitter_analyzer/mcp/tools/analyze_scale_tool.py +621 -557
- tree_sitter_analyzer/mcp/tools/analyze_scale_tool_cli_compatible.py +242 -245
- tree_sitter_analyzer/mcp/tools/base_tool.py +54 -55
- tree_sitter_analyzer/mcp/tools/read_partial_tool.py +300 -302
- tree_sitter_analyzer/mcp/tools/table_format_tool.py +362 -359
- tree_sitter_analyzer/mcp/tools/universal_analyze_tool.py +543 -476
- tree_sitter_analyzer/mcp/utils/__init__.py +105 -106
- tree_sitter_analyzer/mcp/utils/error_handler.py +549 -549
- tree_sitter_analyzer/models.py +470 -481
- tree_sitter_analyzer/output_manager.py +261 -264
- tree_sitter_analyzer/plugins/__init__.py +333 -334
- tree_sitter_analyzer/plugins/base.py +477 -446
- tree_sitter_analyzer/plugins/java_plugin.py +608 -625
- tree_sitter_analyzer/plugins/javascript_plugin.py +446 -439
- tree_sitter_analyzer/plugins/manager.py +362 -355
- tree_sitter_analyzer/plugins/plugin_loader.py +85 -83
- tree_sitter_analyzer/plugins/python_plugin.py +606 -598
- tree_sitter_analyzer/plugins/registry.py +374 -366
- tree_sitter_analyzer/queries/__init__.py +26 -27
- tree_sitter_analyzer/queries/java.py +391 -394
- tree_sitter_analyzer/queries/javascript.py +148 -149
- tree_sitter_analyzer/queries/python.py +285 -286
- tree_sitter_analyzer/queries/typescript.py +229 -230
- tree_sitter_analyzer/query_loader.py +254 -260
- tree_sitter_analyzer/table_formatter.py +468 -448
- tree_sitter_analyzer/utils.py +277 -277
- {tree_sitter_analyzer-0.2.0.dist-info → tree_sitter_analyzer-0.3.0.dist-info}/METADATA +21 -6
- tree_sitter_analyzer-0.3.0.dist-info/RECORD +77 -0
- tree_sitter_analyzer-0.2.0.dist-info/RECORD +0 -77
- {tree_sitter_analyzer-0.2.0.dist-info → tree_sitter_analyzer-0.3.0.dist-info}/WHEEL +0 -0
- {tree_sitter_analyzer-0.2.0.dist-info → tree_sitter_analyzer-0.3.0.dist-info}/entry_points.txt +0 -0
|
@@ -1,219 +1,218 @@
|
|
|
1
|
-
#!/usr/bin/env python3
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
import
|
|
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
|
-
self.
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
output_error(
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
source_bytes
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
self.
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
"
|
|
160
|
-
"
|
|
161
|
-
"
|
|
162
|
-
"
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
node
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
"
|
|
187
|
-
"
|
|
188
|
-
"
|
|
189
|
-
"
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
""
|
|
211
|
-
output_warning("
|
|
212
|
-
|
|
213
|
-
output_info("
|
|
214
|
-
output_info("
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
main()
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""
|
|
3
|
+
Java Code Analyzer with tree-sitter
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
try:
|
|
9
|
+
import tree_sitter
|
|
10
|
+
import tree_sitter_java as tsjava
|
|
11
|
+
from tree_sitter import Language, Parser
|
|
12
|
+
|
|
13
|
+
TREE_SITTER_AVAILABLE = True
|
|
14
|
+
except ImportError:
|
|
15
|
+
TREE_SITTER_AVAILABLE = False
|
|
16
|
+
from .utils import log_error
|
|
17
|
+
|
|
18
|
+
log_error(
|
|
19
|
+
"tree-sitter libraries not found. Please install tree-sitter and tree-sitter-java."
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
from .file_handler import read_file_with_fallback
|
|
23
|
+
from .output_manager import output_error, output_info, output_warning
|
|
24
|
+
from .utils import log_error, log_info
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class CodeAnalyzer:
|
|
28
|
+
"""
|
|
29
|
+
Tree-sitterを使用してソースコードを解析するコアクラス。
|
|
30
|
+
|
|
31
|
+
Attributes:
|
|
32
|
+
language: Tree-sitter言語オブジェクト
|
|
33
|
+
parser: Tree-sitterパーサー
|
|
34
|
+
source_code_bytes: 解析対象のソースコードのバイト列
|
|
35
|
+
tree: 構築されたAST
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
def __init__(self, language: str = "java") -> None:
|
|
39
|
+
"""
|
|
40
|
+
指定された言語用のパーサーを初期化する。
|
|
41
|
+
|
|
42
|
+
Args:
|
|
43
|
+
language: 解析対象のプログラミング言語。現在はJavaのみサポート。
|
|
44
|
+
|
|
45
|
+
Raises:
|
|
46
|
+
SystemExit: Tree-sitterライブラリの初期化に失敗した場合
|
|
47
|
+
"""
|
|
48
|
+
if not TREE_SITTER_AVAILABLE:
|
|
49
|
+
output_error("ERROR: Tree-sitter libraries not available.")
|
|
50
|
+
raise RuntimeError("Tree-sitter libraries not available")
|
|
51
|
+
|
|
52
|
+
try:
|
|
53
|
+
if language != "java":
|
|
54
|
+
output_warning(
|
|
55
|
+
"WARNING: Currently only Java is supported. Using Java parser."
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
self.language = Language(tsjava.language())
|
|
59
|
+
self.parser = Parser(self.language)
|
|
60
|
+
self.source_code_bytes: bytes = b""
|
|
61
|
+
self.tree: tree_sitter.Tree | None = None
|
|
62
|
+
|
|
63
|
+
except Exception as e:
|
|
64
|
+
output_error(
|
|
65
|
+
f"ERROR: '{language}' 言語の初期化に失敗しました。ライブラリが正しくインストールされているか確認してください。"
|
|
66
|
+
)
|
|
67
|
+
output_error(f"詳細: {e}")
|
|
68
|
+
raise RuntimeError(
|
|
69
|
+
f"Failed to initialize language '{language}': {e}"
|
|
70
|
+
) from e
|
|
71
|
+
|
|
72
|
+
def parse_file(self, file_path: str) -> bool:
|
|
73
|
+
"""
|
|
74
|
+
指定されたファイルを解析し、AST(抽象構文木)を構築する。
|
|
75
|
+
|
|
76
|
+
Args:
|
|
77
|
+
file_path: 解析するソースファイルのパス
|
|
78
|
+
|
|
79
|
+
Returns:
|
|
80
|
+
解析に成功した場合はTrue、失敗した場合はFalse
|
|
81
|
+
|
|
82
|
+
Raises:
|
|
83
|
+
None: エラーは内部でハンドリングし、戻り値で示す
|
|
84
|
+
"""
|
|
85
|
+
try:
|
|
86
|
+
source_bytes = read_file_with_fallback(file_path)
|
|
87
|
+
if source_bytes is None:
|
|
88
|
+
output_error(
|
|
89
|
+
f"ERROR: ファイル '{file_path}' の読み込みに失敗しました。"
|
|
90
|
+
)
|
|
91
|
+
return False
|
|
92
|
+
|
|
93
|
+
self.source_code_bytes = source_bytes
|
|
94
|
+
self.tree = self.parser.parse(self.source_code_bytes)
|
|
95
|
+
|
|
96
|
+
if self.tree is None:
|
|
97
|
+
output_error(f"ERROR: '{file_path}' のAST構築に失敗しました。")
|
|
98
|
+
return False
|
|
99
|
+
|
|
100
|
+
log_info(f"INFO: '{file_path}' の解析が完了し、ASTを構築しました。")
|
|
101
|
+
return True
|
|
102
|
+
|
|
103
|
+
except Exception as e:
|
|
104
|
+
output_error(f"ERROR: ファイル解析中にエラーが発生しました: {e}")
|
|
105
|
+
return False
|
|
106
|
+
|
|
107
|
+
def execute_query(self, query_string: str) -> list[dict[str, Any]]:
|
|
108
|
+
"""
|
|
109
|
+
ASTに対して指定されたクエリを実行し、マッチしたノードの情報を抽出する。
|
|
110
|
+
|
|
111
|
+
Args:
|
|
112
|
+
query_string: 実行するTree-sitterクエリ
|
|
113
|
+
|
|
114
|
+
Returns:
|
|
115
|
+
マッチした各ノードの情報(内容、位置、キャプチャ名)のリスト
|
|
116
|
+
|
|
117
|
+
Raises:
|
|
118
|
+
None: エラーは内部でハンドリングし、空リストを返す
|
|
119
|
+
"""
|
|
120
|
+
if not self.tree:
|
|
121
|
+
output_error(
|
|
122
|
+
"ERROR: ASTが構築されていません。先にparse_fileを実行してください。"
|
|
123
|
+
)
|
|
124
|
+
return []
|
|
125
|
+
|
|
126
|
+
try:
|
|
127
|
+
query = self.language.query(query_string)
|
|
128
|
+
except Exception as e:
|
|
129
|
+
output_error(
|
|
130
|
+
f"ERROR: クエリのコンパイルに失敗しました。\nクエリ: {query_string}\nエラー: {e}"
|
|
131
|
+
)
|
|
132
|
+
return []
|
|
133
|
+
|
|
134
|
+
try:
|
|
135
|
+
captures = query.captures(self.tree.root_node)
|
|
136
|
+
except Exception as e:
|
|
137
|
+
output_error(f"ERROR: クエリの実行に失敗しました: {e}")
|
|
138
|
+
return []
|
|
139
|
+
|
|
140
|
+
results = []
|
|
141
|
+
|
|
142
|
+
# Tree-sitter 0.24以降の辞書形式に対応
|
|
143
|
+
try:
|
|
144
|
+
if isinstance(captures, dict):
|
|
145
|
+
# 新しい辞書形式: {capture_name: [nodes...]}
|
|
146
|
+
for capture_name, nodes in captures.items():
|
|
147
|
+
if isinstance(nodes, list):
|
|
148
|
+
for node in nodes:
|
|
149
|
+
try:
|
|
150
|
+
start_line = node.start_point[0] + 1
|
|
151
|
+
end_line = node.end_point[0] + 1
|
|
152
|
+
node_text = self.source_code_bytes[
|
|
153
|
+
node.start_byte : node.end_byte
|
|
154
|
+
].decode("utf-8", errors="ignore")
|
|
155
|
+
|
|
156
|
+
results.append(
|
|
157
|
+
{
|
|
158
|
+
"capture_name": capture_name,
|
|
159
|
+
"content": node_text,
|
|
160
|
+
"start_line": start_line,
|
|
161
|
+
"end_line": end_line,
|
|
162
|
+
"node_type": node.type,
|
|
163
|
+
}
|
|
164
|
+
)
|
|
165
|
+
except Exception as e:
|
|
166
|
+
output_warning(
|
|
167
|
+
f"WARNING: ノード処理中にエラーが発生しました: {e}"
|
|
168
|
+
)
|
|
169
|
+
continue
|
|
170
|
+
else:
|
|
171
|
+
# 古い形式への対応(フォールバック)
|
|
172
|
+
if hasattr(captures, "__iter__"):
|
|
173
|
+
for capture in captures:
|
|
174
|
+
try:
|
|
175
|
+
if isinstance(capture, tuple) and len(capture) == 2:
|
|
176
|
+
node, capture_name = capture
|
|
177
|
+
start_line = node.start_point[0] + 1
|
|
178
|
+
end_line = node.end_point[0] + 1
|
|
179
|
+
node_text = self.source_code_bytes[
|
|
180
|
+
node.start_byte : node.end_byte
|
|
181
|
+
].decode("utf-8", errors="ignore")
|
|
182
|
+
|
|
183
|
+
results.append(
|
|
184
|
+
{
|
|
185
|
+
"capture_name": capture_name,
|
|
186
|
+
"content": node_text,
|
|
187
|
+
"start_line": start_line,
|
|
188
|
+
"end_line": end_line,
|
|
189
|
+
"node_type": node.type,
|
|
190
|
+
}
|
|
191
|
+
)
|
|
192
|
+
except Exception as e:
|
|
193
|
+
output_warning(
|
|
194
|
+
f"WARNING: ノード処理中にエラーが発生しました: {e}"
|
|
195
|
+
)
|
|
196
|
+
continue
|
|
197
|
+
|
|
198
|
+
except Exception as e:
|
|
199
|
+
output_error(f"ERROR: capture処理中に予期しないエラーが発生しました: {e}")
|
|
200
|
+
return []
|
|
201
|
+
|
|
202
|
+
return results
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def main() -> None:
|
|
206
|
+
"""
|
|
207
|
+
モジュールが直接実行された場合のエントリーポイント。
|
|
208
|
+
通常はcli.pyを使用することを推奨。
|
|
209
|
+
"""
|
|
210
|
+
output_warning("注意: 直接的なモジュール実行は非推奨です。")
|
|
211
|
+
output_warning("代わりに以下を使用してください:")
|
|
212
|
+
output_info(" uv run java-analyzer <file> --query-key <key>")
|
|
213
|
+
output_info(" または")
|
|
214
|
+
output_info(" python -m tree_sitter_analyzer.cli <file> --query-key <key>")
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
if __name__ == "__main__":
|
|
218
|
+
main()
|