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