tree-sitter-analyzer 0.9.2__py3-none-any.whl → 0.9.4__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 (37) hide show
  1. tree_sitter_analyzer/__init__.py +1 -1
  2. tree_sitter_analyzer/cli/commands/base_command.py +2 -3
  3. tree_sitter_analyzer/cli/commands/default_command.py +18 -18
  4. tree_sitter_analyzer/cli/commands/partial_read_command.py +139 -141
  5. tree_sitter_analyzer/cli/commands/query_command.py +92 -88
  6. tree_sitter_analyzer/cli/commands/table_command.py +235 -235
  7. tree_sitter_analyzer/cli/info_commands.py +121 -121
  8. tree_sitter_analyzer/cli_main.py +307 -303
  9. tree_sitter_analyzer/core/analysis_engine.py +584 -576
  10. tree_sitter_analyzer/core/cache_service.py +6 -5
  11. tree_sitter_analyzer/core/query.py +502 -502
  12. tree_sitter_analyzer/encoding_utils.py +6 -2
  13. tree_sitter_analyzer/exceptions.py +400 -406
  14. tree_sitter_analyzer/formatters/java_formatter.py +291 -291
  15. tree_sitter_analyzer/formatters/python_formatter.py +259 -259
  16. tree_sitter_analyzer/interfaces/cli.py +1 -1
  17. tree_sitter_analyzer/interfaces/cli_adapter.py +3 -3
  18. tree_sitter_analyzer/interfaces/mcp_server.py +426 -425
  19. tree_sitter_analyzer/language_detector.py +398 -398
  20. tree_sitter_analyzer/language_loader.py +224 -224
  21. tree_sitter_analyzer/languages/java_plugin.py +1202 -1202
  22. tree_sitter_analyzer/mcp/resources/project_stats_resource.py +559 -555
  23. tree_sitter_analyzer/mcp/server.py +30 -9
  24. tree_sitter_analyzer/mcp/tools/read_partial_tool.py +21 -4
  25. tree_sitter_analyzer/mcp/tools/table_format_tool.py +22 -4
  26. tree_sitter_analyzer/mcp/utils/error_handler.py +569 -567
  27. tree_sitter_analyzer/models.py +470 -470
  28. tree_sitter_analyzer/project_detector.py +330 -317
  29. tree_sitter_analyzer/security/__init__.py +22 -22
  30. tree_sitter_analyzer/security/boundary_manager.py +243 -237
  31. tree_sitter_analyzer/security/regex_checker.py +297 -292
  32. tree_sitter_analyzer/table_formatter.py +703 -652
  33. tree_sitter_analyzer/utils.py +53 -22
  34. {tree_sitter_analyzer-0.9.2.dist-info → tree_sitter_analyzer-0.9.4.dist-info}/METADATA +13 -13
  35. {tree_sitter_analyzer-0.9.2.dist-info → tree_sitter_analyzer-0.9.4.dist-info}/RECORD +37 -37
  36. {tree_sitter_analyzer-0.9.2.dist-info → tree_sitter_analyzer-0.9.4.dist-info}/WHEEL +0 -0
  37. {tree_sitter_analyzer-0.9.2.dist-info → tree_sitter_analyzer-0.9.4.dist-info}/entry_points.txt +0 -0
@@ -1,303 +1,307 @@
1
- #!/usr/bin/env python3
2
- """CLI Main Module - Entry point for command-line interface."""
3
-
4
- import argparse
5
- import logging
6
- import sys
7
- from typing import Any
8
-
9
- # Import command classes
10
- from .cli.commands import (
11
- AdvancedCommand,
12
- DefaultCommand,
13
- PartialReadCommand,
14
- QueryCommand,
15
- StructureCommand,
16
- SummaryCommand,
17
- TableCommand,
18
- )
19
- from .cli.info_commands import (
20
- DescribeQueryCommand,
21
- ListQueriesCommand,
22
- ShowExtensionsCommand,
23
- ShowLanguagesCommand,
24
- )
25
- from .output_manager import output_error, output_info, output_list
26
- from .query_loader import query_loader
27
-
28
-
29
- class CLICommandFactory:
30
- """Factory for creating CLI commands based on arguments."""
31
-
32
- @staticmethod
33
- def create_command(args: argparse.Namespace) -> Any:
34
- """Create appropriate command based on arguments."""
35
-
36
- # Information commands (no file analysis required)
37
- if args.list_queries:
38
- return ListQueriesCommand(args)
39
-
40
- if args.describe_query:
41
- return DescribeQueryCommand(args)
42
-
43
- if args.show_supported_languages:
44
- return ShowLanguagesCommand(args)
45
-
46
- if args.show_supported_extensions:
47
- return ShowExtensionsCommand(args)
48
-
49
- # File analysis commands (require file path)
50
- if not args.file_path:
51
- return None
52
-
53
- # Partial read command - highest priority for file operations
54
- if hasattr(args, "partial_read") and args.partial_read:
55
- return PartialReadCommand(args)
56
-
57
- if hasattr(args, "table") and args.table:
58
- return TableCommand(args)
59
-
60
- if hasattr(args, "structure") and args.structure:
61
- return StructureCommand(args)
62
-
63
- if hasattr(args, "summary") and args.summary is not None:
64
- return SummaryCommand(args)
65
-
66
- if hasattr(args, "advanced") and args.advanced:
67
- return AdvancedCommand(args)
68
-
69
- if hasattr(args, "query_key") and args.query_key:
70
- return QueryCommand(args)
71
-
72
- if hasattr(args, "query_string") and args.query_string:
73
- return QueryCommand(args)
74
-
75
- # Default command - if file_path is provided but no specific command, use default analysis
76
- return DefaultCommand(args)
77
-
78
-
79
- def create_argument_parser() -> argparse.ArgumentParser:
80
- """Create and configure the argument parser."""
81
- parser = argparse.ArgumentParser(
82
- description="Analyze code using Tree-sitter and extract structured information.",
83
- epilog="Example: tree-sitter-analyzer example.java --table=full",
84
- )
85
-
86
- # File path
87
- parser.add_argument("file_path", nargs="?", help="Path to the file to analyze")
88
-
89
- # Query options
90
- query_group = parser.add_mutually_exclusive_group(required=False)
91
- query_group.add_argument(
92
- "--query-key", help="Available query key (e.g., class, method)"
93
- )
94
- query_group.add_argument(
95
- "--query-string", help="Directly specify Tree-sitter query to execute"
96
- )
97
-
98
- # Information options
99
- parser.add_argument(
100
- "--list-queries",
101
- action="store_true",
102
- help="Display list of available query keys",
103
- )
104
- parser.add_argument(
105
- "--describe-query", help="Display description of specified query key"
106
- )
107
- parser.add_argument(
108
- "--show-supported-languages",
109
- action="store_true",
110
- help="Display list of supported languages",
111
- )
112
- parser.add_argument(
113
- "--show-supported-extensions",
114
- action="store_true",
115
- help="Display list of supported file extensions",
116
- )
117
- parser.add_argument(
118
- "--show-common-queries",
119
- action="store_true",
120
- help="Display list of common queries across multiple languages",
121
- )
122
- parser.add_argument(
123
- "--show-query-languages",
124
- action="store_true",
125
- help="Display list of languages with query support",
126
- )
127
-
128
- # Output format options
129
- parser.add_argument(
130
- "--output-format",
131
- choices=["json", "text"],
132
- default="json",
133
- help="Specify output format",
134
- )
135
- parser.add_argument(
136
- "--table", choices=["full", "compact", "csv"], help="Output in table format"
137
- )
138
- parser.add_argument(
139
- "--include-javadoc",
140
- action="store_true",
141
- help="Include JavaDoc/documentation comments in output",
142
- )
143
-
144
- # Analysis options
145
- parser.add_argument(
146
- "--advanced", action="store_true", help="Use advanced analysis features"
147
- )
148
- parser.add_argument(
149
- "--summary",
150
- nargs="?",
151
- const="classes,methods",
152
- help="Display summary of specified element types",
153
- )
154
- parser.add_argument(
155
- "--structure",
156
- action="store_true",
157
- help="Output detailed structure information in JSON format",
158
- )
159
- parser.add_argument(
160
- "--statistics", action="store_true", help="Display only statistical information"
161
- )
162
-
163
- # Language options
164
- parser.add_argument(
165
- "--language",
166
- help="Explicitly specify language (auto-detected from extension if omitted)",
167
- )
168
-
169
- # Project options
170
- parser.add_argument(
171
- "--project-root",
172
- help="Project root directory for security validation (auto-detected if not specified)",
173
- )
174
-
175
- # Logging options
176
- parser.add_argument(
177
- "--quiet",
178
- action="store_true",
179
- help="Suppress INFO level logs (show errors only)",
180
- )
181
-
182
- # Partial reading options
183
- parser.add_argument(
184
- "--partial-read",
185
- action="store_true",
186
- help="Enable partial file reading mode",
187
- )
188
- parser.add_argument(
189
- "--start-line", type=int, help="Starting line number for reading (1-based)"
190
- )
191
- parser.add_argument(
192
- "--end-line", type=int, help="Ending line number for reading (1-based)"
193
- )
194
- parser.add_argument(
195
- "--start-column", type=int, help="Starting column number for reading (0-based)"
196
- )
197
- parser.add_argument(
198
- "--end-column", type=int, help="Ending column number for reading (0-based)"
199
- )
200
-
201
- return parser
202
-
203
-
204
- def handle_special_commands(args: argparse.Namespace) -> int | None:
205
- """Handle special commands that don't fit the normal pattern."""
206
-
207
- # Validate partial read options
208
- if hasattr(args, "partial_read") and args.partial_read:
209
- if args.start_line is None:
210
- output_error("--start-line is required")
211
- return 1
212
-
213
- if args.start_line < 1:
214
- output_error("--start-line must be 1 or greater")
215
- return 1
216
-
217
- if args.end_line and args.end_line < args.start_line:
218
- output_error(
219
- "--end-line must be greater than or equal to --start-line"
220
- )
221
- return 1
222
-
223
- if args.start_column is not None and args.start_column < 0:
224
- output_error("--start-column must be 0 or greater")
225
- return 1
226
-
227
- if args.end_column is not None and args.end_column < 0:
228
- output_error("--end-column must be 0 or greater")
229
- return 1
230
-
231
- # Query language commands
232
- if args.show_query_languages:
233
- output_list(["Languages with query support:"])
234
- for lang in query_loader.list_supported_languages():
235
- query_count = len(query_loader.list_queries_for_language(lang))
236
- output_list([f" {lang:<15} ({query_count} queries)"])
237
- return 0
238
-
239
- if args.show_common_queries:
240
- common_queries = query_loader.get_common_queries()
241
- if common_queries:
242
- output_list("Common queries across multiple languages:")
243
- for query in common_queries:
244
- output_list(f" {query}")
245
- else:
246
- output_info("No common queries found.")
247
- return 0
248
-
249
- return None
250
-
251
-
252
- def main() -> None:
253
- """Main entry point for the CLI."""
254
- # Early check for quiet mode to set environment variable before any imports
255
- import os
256
-
257
- if "--quiet" in sys.argv:
258
- os.environ["LOG_LEVEL"] = "ERROR"
259
-
260
- parser = create_argument_parser()
261
- args = parser.parse_args()
262
-
263
- # Configure logging for table output
264
- if hasattr(args, "table") and args.table:
265
- logging.getLogger().setLevel(logging.ERROR)
266
- logging.getLogger("tree_sitter_analyzer").setLevel(logging.ERROR)
267
- logging.getLogger("tree_sitter_analyzer.performance").setLevel(logging.ERROR)
268
-
269
- # Configure logging for quiet mode
270
- if hasattr(args, "quiet") and args.quiet:
271
- logging.getLogger().setLevel(logging.ERROR)
272
- logging.getLogger("tree_sitter_analyzer").setLevel(logging.ERROR)
273
- logging.getLogger("tree_sitter_analyzer.performance").setLevel(logging.ERROR)
274
-
275
- # Handle special commands first
276
- special_result = handle_special_commands(args)
277
- if special_result is not None:
278
- sys.exit(special_result)
279
-
280
- # Create and execute command
281
- command = CLICommandFactory.create_command(args)
282
-
283
- if command:
284
- exit_code = command.execute()
285
- sys.exit(exit_code)
286
- else:
287
- if not args.file_path:
288
- output_error("File path not specified.")
289
- else:
290
- output_error("No executable command specified.")
291
- parser.print_help()
292
- sys.exit(1)
293
-
294
-
295
- if __name__ == "__main__":
296
- try:
297
- main()
298
- except KeyboardInterrupt:
299
- output_info("\nOperation cancelled by user.")
300
- sys.exit(1)
301
- except Exception as e:
302
- output_error(f"Unexpected error: {e}")
303
- sys.exit(1)
1
+ #!/usr/bin/env python3
2
+ """CLI Main Module - Entry point for command-line interface."""
3
+
4
+ import argparse
5
+ import logging
6
+ import sys
7
+ from typing import Any
8
+
9
+ # Import command classes
10
+ from .cli.commands import (
11
+ AdvancedCommand,
12
+ DefaultCommand,
13
+ PartialReadCommand,
14
+ QueryCommand,
15
+ StructureCommand,
16
+ SummaryCommand,
17
+ TableCommand,
18
+ )
19
+ from .cli.info_commands import (
20
+ DescribeQueryCommand,
21
+ ListQueriesCommand,
22
+ ShowExtensionsCommand,
23
+ ShowLanguagesCommand,
24
+ )
25
+ from .output_manager import output_error, output_info, output_list
26
+ from .query_loader import query_loader
27
+
28
+
29
+ class CLICommandFactory:
30
+ """Factory for creating CLI commands based on arguments."""
31
+
32
+ @staticmethod
33
+ def create_command(args: argparse.Namespace) -> Any:
34
+ """Create appropriate command based on arguments."""
35
+
36
+ # Information commands (no file analysis required)
37
+ if args.list_queries:
38
+ return ListQueriesCommand(args)
39
+
40
+ if args.describe_query:
41
+ return DescribeQueryCommand(args)
42
+
43
+ if args.show_supported_languages:
44
+ return ShowLanguagesCommand(args)
45
+
46
+ if args.show_supported_extensions:
47
+ return ShowExtensionsCommand(args)
48
+
49
+ # File analysis commands (require file path)
50
+ if not args.file_path:
51
+ return None
52
+
53
+ # Partial read command - highest priority for file operations
54
+ if hasattr(args, "partial_read") and args.partial_read:
55
+ return PartialReadCommand(args)
56
+
57
+ if hasattr(args, "table") and args.table:
58
+ return TableCommand(args)
59
+
60
+ if hasattr(args, "structure") and args.structure:
61
+ return StructureCommand(args)
62
+
63
+ if hasattr(args, "summary") and args.summary is not None:
64
+ return SummaryCommand(args)
65
+
66
+ if hasattr(args, "advanced") and args.advanced:
67
+ return AdvancedCommand(args)
68
+
69
+ if hasattr(args, "query_key") and args.query_key:
70
+ return QueryCommand(args)
71
+
72
+ if hasattr(args, "query_string") and args.query_string:
73
+ return QueryCommand(args)
74
+
75
+ # Default command - if file_path is provided but no specific command, use default analysis
76
+ return DefaultCommand(args)
77
+
78
+
79
+ def create_argument_parser() -> argparse.ArgumentParser:
80
+ """Create and configure the argument parser."""
81
+ parser = argparse.ArgumentParser(
82
+ description="Analyze code using Tree-sitter and extract structured information.",
83
+ epilog="Example: tree-sitter-analyzer example.java --table=full",
84
+ )
85
+
86
+ # File path
87
+ parser.add_argument("file_path", nargs="?", help="Path to the file to analyze")
88
+
89
+ # Query options
90
+ query_group = parser.add_mutually_exclusive_group(required=False)
91
+ query_group.add_argument(
92
+ "--query-key", help="Available query key (e.g., class, method)"
93
+ )
94
+ query_group.add_argument(
95
+ "--query-string", help="Directly specify Tree-sitter query to execute"
96
+ )
97
+
98
+ # Information options
99
+ parser.add_argument(
100
+ "--list-queries",
101
+ action="store_true",
102
+ help="Display list of available query keys",
103
+ )
104
+ parser.add_argument(
105
+ "--describe-query", help="Display description of specified query key"
106
+ )
107
+ parser.add_argument(
108
+ "--show-supported-languages",
109
+ action="store_true",
110
+ help="Display list of supported languages",
111
+ )
112
+ parser.add_argument(
113
+ "--show-supported-extensions",
114
+ action="store_true",
115
+ help="Display list of supported file extensions",
116
+ )
117
+ parser.add_argument(
118
+ "--show-common-queries",
119
+ action="store_true",
120
+ help="Display list of common queries across multiple languages",
121
+ )
122
+ parser.add_argument(
123
+ "--show-query-languages",
124
+ action="store_true",
125
+ help="Display list of languages with query support",
126
+ )
127
+
128
+ # Output format options
129
+ parser.add_argument(
130
+ "--output-format",
131
+ choices=["json", "text"],
132
+ default="json",
133
+ help="Specify output format",
134
+ )
135
+ parser.add_argument(
136
+ "--table", choices=["full", "compact", "csv"], help="Output in table format"
137
+ )
138
+ parser.add_argument(
139
+ "--include-javadoc",
140
+ action="store_true",
141
+ help="Include JavaDoc/documentation comments in output",
142
+ )
143
+
144
+ # Analysis options
145
+ parser.add_argument(
146
+ "--advanced", action="store_true", help="Use advanced analysis features"
147
+ )
148
+ parser.add_argument(
149
+ "--summary",
150
+ nargs="?",
151
+ const="classes,methods",
152
+ help="Display summary of specified element types",
153
+ )
154
+ parser.add_argument(
155
+ "--structure",
156
+ action="store_true",
157
+ help="Output detailed structure information in JSON format",
158
+ )
159
+ parser.add_argument(
160
+ "--statistics", action="store_true", help="Display only statistical information"
161
+ )
162
+
163
+ # Language options
164
+ parser.add_argument(
165
+ "--language",
166
+ help="Explicitly specify language (auto-detected from extension if omitted)",
167
+ )
168
+
169
+ # Project options
170
+ parser.add_argument(
171
+ "--project-root",
172
+ help="Project root directory for security validation (auto-detected if not specified)",
173
+ )
174
+
175
+ # Logging options
176
+ parser.add_argument(
177
+ "--quiet",
178
+ action="store_true",
179
+ help="Suppress INFO level logs (show errors only)",
180
+ )
181
+
182
+ # Partial reading options
183
+ parser.add_argument(
184
+ "--partial-read",
185
+ action="store_true",
186
+ help="Enable partial file reading mode",
187
+ )
188
+ parser.add_argument(
189
+ "--start-line", type=int, help="Starting line number for reading (1-based)"
190
+ )
191
+ parser.add_argument(
192
+ "--end-line", type=int, help="Ending line number for reading (1-based)"
193
+ )
194
+ parser.add_argument(
195
+ "--start-column", type=int, help="Starting column number for reading (0-based)"
196
+ )
197
+ parser.add_argument(
198
+ "--end-column", type=int, help="Ending column number for reading (0-based)"
199
+ )
200
+
201
+ return parser
202
+
203
+
204
+ def handle_special_commands(args: argparse.Namespace) -> int | None:
205
+ """Handle special commands that don't fit the normal pattern."""
206
+
207
+ # Validate partial read options
208
+ if hasattr(args, "partial_read") and args.partial_read:
209
+ if args.start_line is None:
210
+ output_error("--start-line is required")
211
+ return 1
212
+
213
+ if args.start_line < 1:
214
+ output_error("--start-line must be 1 or greater")
215
+ return 1
216
+
217
+ if args.end_line and args.end_line < args.start_line:
218
+ output_error("--end-line must be greater than or equal to --start-line")
219
+ return 1
220
+
221
+ if args.start_column is not None and args.start_column < 0:
222
+ output_error("--start-column must be 0 or greater")
223
+ return 1
224
+
225
+ if args.end_column is not None and args.end_column < 0:
226
+ output_error("--end-column must be 0 or greater")
227
+ return 1
228
+
229
+ # Query language commands
230
+ if args.show_query_languages:
231
+ output_list(["Languages with query support:"])
232
+ for lang in query_loader.list_supported_languages():
233
+ query_count = len(query_loader.list_queries_for_language(lang))
234
+ output_list([f" {lang:<15} ({query_count} queries)"])
235
+ return 0
236
+
237
+ if args.show_common_queries:
238
+ common_queries = query_loader.get_common_queries()
239
+ if common_queries:
240
+ output_list("Common queries across multiple languages:")
241
+ for query in common_queries:
242
+ output_list(f" {query}")
243
+ else:
244
+ output_info("No common queries found.")
245
+ return 0
246
+
247
+ return None
248
+
249
+
250
+ def main() -> None:
251
+ """Main entry point for the CLI."""
252
+ # Early check for quiet mode to set environment variable before any imports
253
+ import os
254
+
255
+ if "--quiet" in sys.argv:
256
+ os.environ["LOG_LEVEL"] = "ERROR"
257
+ else:
258
+ # Set default log level to WARNING to reduce output
259
+ os.environ.setdefault("LOG_LEVEL", "WARNING")
260
+
261
+ parser = create_argument_parser()
262
+ args = parser.parse_args()
263
+
264
+ # Configure default logging levels to reduce output
265
+ logging.getLogger("tree_sitter_analyzer.performance").setLevel(logging.ERROR)
266
+
267
+ # Configure logging for table output
268
+ if hasattr(args, "table") and args.table:
269
+ logging.getLogger().setLevel(logging.ERROR)
270
+ logging.getLogger("tree_sitter_analyzer").setLevel(logging.ERROR)
271
+ logging.getLogger("tree_sitter_analyzer.performance").setLevel(logging.ERROR)
272
+
273
+ # Configure logging for quiet mode
274
+ if hasattr(args, "quiet") and args.quiet:
275
+ logging.getLogger().setLevel(logging.ERROR)
276
+ logging.getLogger("tree_sitter_analyzer").setLevel(logging.ERROR)
277
+ logging.getLogger("tree_sitter_analyzer.performance").setLevel(logging.ERROR)
278
+
279
+ # Handle special commands first
280
+ special_result = handle_special_commands(args)
281
+ if special_result is not None:
282
+ sys.exit(special_result)
283
+
284
+ # Create and execute command
285
+ command = CLICommandFactory.create_command(args)
286
+
287
+ if command:
288
+ exit_code = command.execute()
289
+ sys.exit(exit_code)
290
+ else:
291
+ if not args.file_path:
292
+ output_error("File path not specified.")
293
+ else:
294
+ output_error("No executable command specified.")
295
+ parser.print_help()
296
+ sys.exit(1)
297
+
298
+
299
+ if __name__ == "__main__":
300
+ try:
301
+ main()
302
+ except KeyboardInterrupt:
303
+ output_info("\nOperation cancelled by user.")
304
+ sys.exit(1)
305
+ except Exception as e:
306
+ output_error(f"Unexpected error: {e}")
307
+ sys.exit(1)