tree-sitter-analyzer 0.1.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.

Files changed (78) hide show
  1. tree_sitter_analyzer/__init__.py +121 -0
  2. tree_sitter_analyzer/__main__.py +12 -0
  3. tree_sitter_analyzer/api.py +539 -0
  4. tree_sitter_analyzer/cli/__init__.py +39 -0
  5. tree_sitter_analyzer/cli/__main__.py +13 -0
  6. tree_sitter_analyzer/cli/commands/__init__.py +27 -0
  7. tree_sitter_analyzer/cli/commands/advanced_command.py +88 -0
  8. tree_sitter_analyzer/cli/commands/base_command.py +155 -0
  9. tree_sitter_analyzer/cli/commands/default_command.py +19 -0
  10. tree_sitter_analyzer/cli/commands/partial_read_command.py +133 -0
  11. tree_sitter_analyzer/cli/commands/query_command.py +82 -0
  12. tree_sitter_analyzer/cli/commands/structure_command.py +121 -0
  13. tree_sitter_analyzer/cli/commands/summary_command.py +93 -0
  14. tree_sitter_analyzer/cli/commands/table_command.py +233 -0
  15. tree_sitter_analyzer/cli/info_commands.py +121 -0
  16. tree_sitter_analyzer/cli_main.py +276 -0
  17. tree_sitter_analyzer/core/__init__.py +20 -0
  18. tree_sitter_analyzer/core/analysis_engine.py +574 -0
  19. tree_sitter_analyzer/core/cache_service.py +330 -0
  20. tree_sitter_analyzer/core/engine.py +560 -0
  21. tree_sitter_analyzer/core/parser.py +288 -0
  22. tree_sitter_analyzer/core/query.py +502 -0
  23. tree_sitter_analyzer/encoding_utils.py +460 -0
  24. tree_sitter_analyzer/exceptions.py +340 -0
  25. tree_sitter_analyzer/file_handler.py +222 -0
  26. tree_sitter_analyzer/formatters/__init__.py +1 -0
  27. tree_sitter_analyzer/formatters/base_formatter.py +168 -0
  28. tree_sitter_analyzer/formatters/formatter_factory.py +74 -0
  29. tree_sitter_analyzer/formatters/java_formatter.py +270 -0
  30. tree_sitter_analyzer/formatters/python_formatter.py +235 -0
  31. tree_sitter_analyzer/interfaces/__init__.py +10 -0
  32. tree_sitter_analyzer/interfaces/cli.py +557 -0
  33. tree_sitter_analyzer/interfaces/cli_adapter.py +319 -0
  34. tree_sitter_analyzer/interfaces/mcp_adapter.py +170 -0
  35. tree_sitter_analyzer/interfaces/mcp_server.py +416 -0
  36. tree_sitter_analyzer/java_analyzer.py +219 -0
  37. tree_sitter_analyzer/language_detector.py +400 -0
  38. tree_sitter_analyzer/language_loader.py +228 -0
  39. tree_sitter_analyzer/languages/__init__.py +11 -0
  40. tree_sitter_analyzer/languages/java_plugin.py +1113 -0
  41. tree_sitter_analyzer/languages/python_plugin.py +712 -0
  42. tree_sitter_analyzer/mcp/__init__.py +32 -0
  43. tree_sitter_analyzer/mcp/resources/__init__.py +47 -0
  44. tree_sitter_analyzer/mcp/resources/code_file_resource.py +213 -0
  45. tree_sitter_analyzer/mcp/resources/project_stats_resource.py +550 -0
  46. tree_sitter_analyzer/mcp/server.py +319 -0
  47. tree_sitter_analyzer/mcp/tools/__init__.py +36 -0
  48. tree_sitter_analyzer/mcp/tools/analyze_scale_tool.py +558 -0
  49. tree_sitter_analyzer/mcp/tools/analyze_scale_tool_cli_compatible.py +245 -0
  50. tree_sitter_analyzer/mcp/tools/base_tool.py +55 -0
  51. tree_sitter_analyzer/mcp/tools/get_positions_tool.py +448 -0
  52. tree_sitter_analyzer/mcp/tools/read_partial_tool.py +302 -0
  53. tree_sitter_analyzer/mcp/tools/table_format_tool.py +359 -0
  54. tree_sitter_analyzer/mcp/tools/universal_analyze_tool.py +476 -0
  55. tree_sitter_analyzer/mcp/utils/__init__.py +106 -0
  56. tree_sitter_analyzer/mcp/utils/error_handler.py +549 -0
  57. tree_sitter_analyzer/models.py +481 -0
  58. tree_sitter_analyzer/output_manager.py +264 -0
  59. tree_sitter_analyzer/plugins/__init__.py +334 -0
  60. tree_sitter_analyzer/plugins/base.py +446 -0
  61. tree_sitter_analyzer/plugins/java_plugin.py +625 -0
  62. tree_sitter_analyzer/plugins/javascript_plugin.py +439 -0
  63. tree_sitter_analyzer/plugins/manager.py +355 -0
  64. tree_sitter_analyzer/plugins/plugin_loader.py +83 -0
  65. tree_sitter_analyzer/plugins/python_plugin.py +598 -0
  66. tree_sitter_analyzer/plugins/registry.py +366 -0
  67. tree_sitter_analyzer/queries/__init__.py +27 -0
  68. tree_sitter_analyzer/queries/java.py +394 -0
  69. tree_sitter_analyzer/queries/javascript.py +149 -0
  70. tree_sitter_analyzer/queries/python.py +286 -0
  71. tree_sitter_analyzer/queries/typescript.py +230 -0
  72. tree_sitter_analyzer/query_loader.py +260 -0
  73. tree_sitter_analyzer/table_formatter.py +448 -0
  74. tree_sitter_analyzer/utils.py +201 -0
  75. tree_sitter_analyzer-0.1.0.dist-info/METADATA +581 -0
  76. tree_sitter_analyzer-0.1.0.dist-info/RECORD +78 -0
  77. tree_sitter_analyzer-0.1.0.dist-info/WHEEL +4 -0
  78. tree_sitter_analyzer-0.1.0.dist-info/entry_points.txt +8 -0
@@ -0,0 +1,446 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ Plugin Base Classes
5
+
6
+ Defines the base interfaces for language plugins and element extractors.
7
+ All language plugins must inherit from these base classes.
8
+ """
9
+
10
+ from abc import ABC, abstractmethod
11
+ from typing import TYPE_CHECKING, Any, Dict, List, Optional
12
+ import logging
13
+
14
+ if TYPE_CHECKING:
15
+ import tree_sitter
16
+
17
+ from ..models import Class as ModelClass
18
+ from ..models import (
19
+ CodeElement,
20
+ Function as ModelFunction,
21
+ Import as ModelImport,
22
+ Variable as ModelVariable,
23
+ )
24
+ from ..utils import log_debug, log_error, log_warning
25
+
26
+ logger = logging.getLogger(__name__)
27
+
28
+
29
+ class ElementExtractor(ABC):
30
+ """
31
+ Abstract base class for language-specific element extractors.
32
+
33
+ Element extractors are responsible for parsing ASTs and extracting
34
+ meaningful code elements like functions, classes, variables, etc.
35
+ """
36
+
37
+ @abstractmethod
38
+ def extract_functions(
39
+ self, tree: "tree_sitter.Tree", source_code: str
40
+ ) -> List[ModelFunction]:
41
+ """
42
+ Extract function definitions from the syntax tree.
43
+
44
+ Args:
45
+ tree: Tree-sitter AST
46
+ source_code: Original source code
47
+
48
+ Returns:
49
+ List of extracted function objects
50
+ """
51
+ pass
52
+
53
+ @abstractmethod
54
+ def extract_classes(
55
+ self, tree: "tree_sitter.Tree", source_code: str
56
+ ) -> List[ModelClass]:
57
+ """
58
+ Extract class definitions from the syntax tree.
59
+
60
+ Args:
61
+ tree: Tree-sitter AST
62
+ source_code: Original source code
63
+
64
+ Returns:
65
+ List of extracted class objects
66
+ """
67
+ pass
68
+
69
+ @abstractmethod
70
+ def extract_variables(
71
+ self, tree: "tree_sitter.Tree", source_code: str
72
+ ) -> List[ModelVariable]:
73
+ """
74
+ Extract variable declarations from the syntax tree.
75
+
76
+ Args:
77
+ tree: Tree-sitter AST
78
+ source_code: Original source code
79
+
80
+ Returns:
81
+ List of extracted variable objects
82
+ """
83
+ pass
84
+
85
+ @abstractmethod
86
+ def extract_imports(
87
+ self, tree: "tree_sitter.Tree", source_code: str
88
+ ) -> List[ModelImport]:
89
+ """
90
+ Extract import statements from the syntax tree.
91
+
92
+ Args:
93
+ tree: Tree-sitter AST
94
+ source_code: Original source code
95
+
96
+ Returns:
97
+ List of extracted import objects
98
+ """
99
+ pass
100
+
101
+ def extract_all_elements(
102
+ self, tree: "tree_sitter.Tree", source_code: str
103
+ ) -> List[CodeElement]:
104
+ """
105
+ Extract all code elements from the syntax tree.
106
+
107
+ Args:
108
+ tree: Tree-sitter AST
109
+ source_code: Original source code
110
+
111
+ Returns:
112
+ List of all extracted code elements
113
+ """
114
+ elements = []
115
+
116
+ try:
117
+ elements.extend(self.extract_functions(tree, source_code))
118
+ elements.extend(self.extract_classes(tree, source_code))
119
+ elements.extend(self.extract_variables(tree, source_code))
120
+ elements.extend(self.extract_imports(tree, source_code))
121
+ except Exception as e:
122
+ log_error(f"Failed to extract all elements: {e}")
123
+
124
+ return elements
125
+
126
+
127
+ class LanguagePlugin(ABC):
128
+ """
129
+ Abstract base class for language-specific plugins.
130
+
131
+ Language plugins provide language-specific functionality including
132
+ element extraction, file extension mapping, and language identification.
133
+ """
134
+
135
+ @abstractmethod
136
+ def get_language_name(self) -> str:
137
+ """
138
+ Return the name of the programming language this plugin supports.
139
+
140
+ Returns:
141
+ Language name (e.g., "java", "python", "javascript")
142
+ """
143
+ pass
144
+
145
+ @abstractmethod
146
+ def get_file_extensions(self) -> List[str]:
147
+ """
148
+ Return list of file extensions this plugin supports.
149
+
150
+ Returns:
151
+ List of file extensions (e.g., [".java", ".class"])
152
+ """
153
+ pass
154
+
155
+ @abstractmethod
156
+ def create_extractor(self) -> ElementExtractor:
157
+ """
158
+ Create and return an element extractor for this language.
159
+
160
+ Returns:
161
+ ElementExtractor instance for this language
162
+ """
163
+ pass
164
+
165
+ def is_applicable(self, file_path: str) -> bool:
166
+ """
167
+ Check if this plugin is applicable for the given file.
168
+
169
+ Args:
170
+ file_path: Path to the file to check
171
+
172
+ Returns:
173
+ True if this plugin can handle the file
174
+ """
175
+ extensions = self.get_file_extensions()
176
+ return any(file_path.lower().endswith(ext.lower()) for ext in extensions)
177
+
178
+ def get_plugin_info(self) -> Dict[str, Any]:
179
+ """
180
+ Get information about this plugin.
181
+
182
+ Returns:
183
+ Dictionary containing plugin information
184
+ """
185
+ return {
186
+ "language": self.get_language_name(),
187
+ "extensions": self.get_file_extensions(),
188
+ "class_name": self.__class__.__name__,
189
+ "module": self.__class__.__module__,
190
+ }
191
+
192
+
193
+ class DefaultExtractor(ElementExtractor):
194
+ """
195
+ Default implementation of ElementExtractor with basic functionality.
196
+
197
+ This extractor provides generic extraction logic that works across
198
+ multiple languages by looking for common node types.
199
+ """
200
+
201
+ def extract_functions(
202
+ self, tree: "tree_sitter.Tree", source_code: str
203
+ ) -> List[ModelFunction]:
204
+ """Basic function extraction implementation."""
205
+ functions: List[ModelFunction] = []
206
+
207
+ try:
208
+ if hasattr(tree, "root_node"):
209
+ lines = source_code.splitlines()
210
+ self._traverse_for_functions(tree.root_node, functions, lines, source_code)
211
+ except Exception as e:
212
+ log_error(f"Error in function extraction: {e}")
213
+
214
+ return functions
215
+
216
+ def extract_classes(
217
+ self, tree: "tree_sitter.Tree", source_code: str
218
+ ) -> List[ModelClass]:
219
+ """Basic class extraction implementation."""
220
+ classes: List[ModelClass] = []
221
+
222
+ try:
223
+ if hasattr(tree, "root_node"):
224
+ lines = source_code.splitlines()
225
+ self._traverse_for_classes(tree.root_node, classes, lines, source_code)
226
+ except Exception as e:
227
+ log_error(f"Error in class extraction: {e}")
228
+
229
+ return classes
230
+
231
+ def extract_variables(
232
+ self, tree: "tree_sitter.Tree", source_code: str
233
+ ) -> List[ModelVariable]:
234
+ """Basic variable extraction implementation."""
235
+ variables: List[ModelVariable] = []
236
+
237
+ try:
238
+ if hasattr(tree, "root_node"):
239
+ lines = source_code.splitlines()
240
+ self._traverse_for_variables(tree.root_node, variables, lines, source_code)
241
+ except Exception as e:
242
+ log_error(f"Error in variable extraction: {e}")
243
+
244
+ return variables
245
+
246
+ def extract_imports(
247
+ self, tree: "tree_sitter.Tree", source_code: str
248
+ ) -> List[ModelImport]:
249
+ """Basic import extraction implementation."""
250
+ imports: List[ModelImport] = []
251
+
252
+ try:
253
+ if hasattr(tree, "root_node"):
254
+ lines = source_code.splitlines()
255
+ self._traverse_for_imports(tree.root_node, imports, lines, source_code)
256
+ except Exception as e:
257
+ log_error(f"Error in import extraction: {e}")
258
+
259
+ return imports
260
+
261
+ def _traverse_for_functions(
262
+ self,
263
+ node: "tree_sitter.Node",
264
+ functions: List[ModelFunction],
265
+ lines: List[str],
266
+ source_code: str
267
+ ) -> None:
268
+ """Traverse tree to find function-like nodes."""
269
+ if hasattr(node, "type") and self._is_function_node(node.type):
270
+ try:
271
+ name = self._extract_node_name(node, source_code) or "unknown"
272
+ raw_text = self._extract_node_text(node, source_code)
273
+
274
+ func = ModelFunction(
275
+ name=name,
276
+ start_line=node.start_point[0] + 1 if hasattr(node, "start_point") else 0,
277
+ end_line=node.end_point[0] + 1 if hasattr(node, "end_point") else 0,
278
+ raw_text=raw_text,
279
+ language=self._get_language_hint(),
280
+ )
281
+ functions.append(func)
282
+ except Exception as e:
283
+ log_debug(f"Failed to extract function: {e}")
284
+
285
+ if hasattr(node, "children"):
286
+ for child in node.children:
287
+ self._traverse_for_functions(child, functions, lines, source_code)
288
+
289
+ def _traverse_for_classes(
290
+ self,
291
+ node: "tree_sitter.Node",
292
+ classes: List[ModelClass],
293
+ lines: List[str],
294
+ source_code: str
295
+ ) -> None:
296
+ """Traverse tree to find class-like nodes."""
297
+ if hasattr(node, "type") and self._is_class_node(node.type):
298
+ try:
299
+ name = self._extract_node_name(node, source_code) or "unknown"
300
+ raw_text = self._extract_node_text(node, source_code)
301
+
302
+ cls = ModelClass(
303
+ name=name,
304
+ start_line=node.start_point[0] + 1 if hasattr(node, "start_point") else 0,
305
+ end_line=node.end_point[0] + 1 if hasattr(node, "end_point") else 0,
306
+ raw_text=raw_text,
307
+ language=self._get_language_hint(),
308
+ )
309
+ classes.append(cls)
310
+ except Exception as e:
311
+ log_debug(f"Failed to extract class: {e}")
312
+
313
+ if hasattr(node, "children"):
314
+ for child in node.children:
315
+ self._traverse_for_classes(child, classes, lines, source_code)
316
+
317
+ def _traverse_for_variables(
318
+ self,
319
+ node: "tree_sitter.Node",
320
+ variables: List[ModelVariable],
321
+ lines: List[str],
322
+ source_code: str
323
+ ) -> None:
324
+ """Traverse tree to find variable declarations."""
325
+ if hasattr(node, "type") and self._is_variable_node(node.type):
326
+ try:
327
+ name = self._extract_node_name(node, source_code) or "unknown"
328
+ raw_text = self._extract_node_text(node, source_code)
329
+
330
+ var = ModelVariable(
331
+ name=name,
332
+ start_line=node.start_point[0] + 1 if hasattr(node, "start_point") else 0,
333
+ end_line=node.end_point[0] + 1 if hasattr(node, "end_point") else 0,
334
+ raw_text=raw_text,
335
+ language=self._get_language_hint(),
336
+ )
337
+ variables.append(var)
338
+ except Exception as e:
339
+ log_debug(f"Failed to extract variable: {e}")
340
+
341
+ if hasattr(node, "children"):
342
+ for child in node.children:
343
+ self._traverse_for_variables(child, variables, lines, source_code)
344
+
345
+ def _traverse_for_imports(
346
+ self,
347
+ node: "tree_sitter.Node",
348
+ imports: List[ModelImport],
349
+ lines: List[str],
350
+ source_code: str
351
+ ) -> None:
352
+ """Traverse tree to find import statements."""
353
+ if hasattr(node, "type") and self._is_import_node(node.type):
354
+ try:
355
+ name = self._extract_node_name(node, source_code) or "unknown"
356
+ raw_text = self._extract_node_text(node, source_code)
357
+
358
+ imp = ModelImport(
359
+ name=name,
360
+ start_line=node.start_point[0] + 1 if hasattr(node, "start_point") else 0,
361
+ end_line=node.end_point[0] + 1 if hasattr(node, "end_point") else 0,
362
+ raw_text=raw_text,
363
+ language=self._get_language_hint(),
364
+ )
365
+ imports.append(imp)
366
+ except Exception as e:
367
+ log_debug(f"Failed to extract import: {e}")
368
+
369
+ if hasattr(node, "children"):
370
+ for child in node.children:
371
+ self._traverse_for_imports(child, imports, lines, source_code)
372
+
373
+ def _is_function_node(self, node_type: str) -> bool:
374
+ """Check if a node type represents a function."""
375
+ function_types = [
376
+ "function_definition", "function_declaration", "method_definition",
377
+ "function", "method", "procedure", "subroutine"
378
+ ]
379
+ return any(ftype in node_type.lower() for ftype in function_types)
380
+
381
+ def _is_class_node(self, node_type: str) -> bool:
382
+ """Check if a node type represents a class."""
383
+ class_types = [
384
+ "class_definition", "class_declaration", "interface_definition",
385
+ "class", "interface", "struct", "enum"
386
+ ]
387
+ return any(ctype in node_type.lower() for ctype in class_types)
388
+
389
+ def _is_variable_node(self, node_type: str) -> bool:
390
+ """Check if a node type represents a variable."""
391
+ variable_types = [
392
+ "variable_declaration", "variable_definition", "field_declaration",
393
+ "assignment", "declaration", "variable", "field"
394
+ ]
395
+ return any(vtype in node_type.lower() for vtype in variable_types)
396
+
397
+ def _is_import_node(self, node_type: str) -> bool:
398
+ """Check if a node type represents an import."""
399
+ import_types = [
400
+ "import_statement", "import_declaration", "include_statement",
401
+ "import", "include", "require", "use"
402
+ ]
403
+ return any(itype in node_type.lower() for itype in import_types)
404
+
405
+ def _extract_node_name(self, node: "tree_sitter.Node", source_code: str) -> Optional[str]:
406
+ """Extract name from a tree-sitter node."""
407
+ try:
408
+ # Look for identifier children
409
+ if hasattr(node, "children"):
410
+ for child in node.children:
411
+ if hasattr(child, "type") and child.type == "identifier":
412
+ return self._extract_node_text(child, source_code)
413
+
414
+ # Fallback: use position-based name
415
+ return f"element_{node.start_point[0]}_{node.start_point[1]}"
416
+ except Exception:
417
+ return None
418
+
419
+ def _extract_node_text(self, node: "tree_sitter.Node", source_code: str) -> str:
420
+ """Extract text content from a tree-sitter node."""
421
+ try:
422
+ if hasattr(node, "start_byte") and hasattr(node, "end_byte"):
423
+ source_bytes = source_code.encode('utf-8')
424
+ node_bytes = source_bytes[node.start_byte:node.end_byte]
425
+ return node_bytes.decode('utf-8', errors='replace')
426
+ return ""
427
+ except Exception as e:
428
+ log_debug(f"Failed to extract node text: {e}")
429
+ return ""
430
+
431
+ def _get_language_hint(self) -> str:
432
+ """Get a hint about the language being processed."""
433
+ return "unknown"
434
+
435
+
436
+ class DefaultLanguagePlugin(LanguagePlugin):
437
+ """Default plugin that provides basic functionality for any language."""
438
+
439
+ def get_language_name(self) -> str:
440
+ return "generic"
441
+
442
+ def get_file_extensions(self) -> List[str]:
443
+ return [".txt", ".md"] # Fallback extensions
444
+
445
+ def create_extractor(self) -> ElementExtractor:
446
+ return DefaultExtractor()