tree-sitter-analyzer 0.8.3__py3-none-any.whl → 0.9.2__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 (62) hide show
  1. tree_sitter_analyzer/__init__.py +132 -132
  2. tree_sitter_analyzer/__main__.py +11 -11
  3. tree_sitter_analyzer/api.py +533 -533
  4. tree_sitter_analyzer/cli/__init__.py +39 -39
  5. tree_sitter_analyzer/cli/__main__.py +12 -12
  6. tree_sitter_analyzer/cli/commands/__init__.py +26 -26
  7. tree_sitter_analyzer/cli/commands/advanced_command.py +88 -88
  8. tree_sitter_analyzer/cli/commands/base_command.py +182 -180
  9. tree_sitter_analyzer/cli/commands/structure_command.py +138 -138
  10. tree_sitter_analyzer/cli/commands/summary_command.py +101 -101
  11. tree_sitter_analyzer/core/__init__.py +15 -15
  12. tree_sitter_analyzer/core/analysis_engine.py +74 -78
  13. tree_sitter_analyzer/core/cache_service.py +320 -320
  14. tree_sitter_analyzer/core/engine.py +566 -566
  15. tree_sitter_analyzer/core/parser.py +293 -293
  16. tree_sitter_analyzer/encoding_utils.py +459 -459
  17. tree_sitter_analyzer/file_handler.py +210 -210
  18. tree_sitter_analyzer/formatters/__init__.py +1 -1
  19. tree_sitter_analyzer/formatters/base_formatter.py +167 -167
  20. tree_sitter_analyzer/formatters/formatter_factory.py +78 -78
  21. tree_sitter_analyzer/formatters/java_formatter.py +18 -18
  22. tree_sitter_analyzer/formatters/python_formatter.py +19 -19
  23. tree_sitter_analyzer/interfaces/__init__.py +9 -9
  24. tree_sitter_analyzer/interfaces/cli.py +528 -528
  25. tree_sitter_analyzer/interfaces/cli_adapter.py +344 -343
  26. tree_sitter_analyzer/interfaces/mcp_adapter.py +206 -206
  27. tree_sitter_analyzer/language_detector.py +53 -53
  28. tree_sitter_analyzer/languages/__init__.py +10 -10
  29. tree_sitter_analyzer/languages/java_plugin.py +1 -1
  30. tree_sitter_analyzer/languages/javascript_plugin.py +446 -446
  31. tree_sitter_analyzer/languages/python_plugin.py +755 -755
  32. tree_sitter_analyzer/mcp/__init__.py +34 -31
  33. tree_sitter_analyzer/mcp/resources/__init__.py +44 -44
  34. tree_sitter_analyzer/mcp/resources/code_file_resource.py +209 -209
  35. tree_sitter_analyzer/mcp/server.py +623 -436
  36. tree_sitter_analyzer/mcp/tools/__init__.py +30 -30
  37. tree_sitter_analyzer/mcp/tools/analyze_scale_tool.py +10 -6
  38. tree_sitter_analyzer/mcp/tools/analyze_scale_tool_cli_compatible.py +247 -242
  39. tree_sitter_analyzer/mcp/tools/base_tool.py +54 -54
  40. tree_sitter_analyzer/mcp/tools/read_partial_tool.py +310 -308
  41. tree_sitter_analyzer/mcp/tools/table_format_tool.py +386 -379
  42. tree_sitter_analyzer/mcp/tools/universal_analyze_tool.py +563 -559
  43. tree_sitter_analyzer/mcp/utils/__init__.py +107 -107
  44. tree_sitter_analyzer/models.py +10 -10
  45. tree_sitter_analyzer/output_manager.py +253 -253
  46. tree_sitter_analyzer/plugins/__init__.py +280 -280
  47. tree_sitter_analyzer/plugins/base.py +529 -529
  48. tree_sitter_analyzer/plugins/manager.py +379 -379
  49. tree_sitter_analyzer/queries/__init__.py +26 -26
  50. tree_sitter_analyzer/queries/java.py +391 -391
  51. tree_sitter_analyzer/queries/javascript.py +148 -148
  52. tree_sitter_analyzer/queries/python.py +285 -285
  53. tree_sitter_analyzer/queries/typescript.py +229 -229
  54. tree_sitter_analyzer/query_loader.py +257 -257
  55. tree_sitter_analyzer/security/boundary_manager.py +237 -279
  56. tree_sitter_analyzer/security/validator.py +60 -58
  57. tree_sitter_analyzer/utils.py +294 -277
  58. {tree_sitter_analyzer-0.8.3.dist-info → tree_sitter_analyzer-0.9.2.dist-info}/METADATA +28 -19
  59. tree_sitter_analyzer-0.9.2.dist-info/RECORD +77 -0
  60. {tree_sitter_analyzer-0.8.3.dist-info → tree_sitter_analyzer-0.9.2.dist-info}/entry_points.txt +1 -0
  61. tree_sitter_analyzer-0.8.3.dist-info/RECORD +0 -77
  62. {tree_sitter_analyzer-0.8.3.dist-info → tree_sitter_analyzer-0.9.2.dist-info}/WHEEL +0 -0
@@ -1,528 +1,528 @@
1
- #!/usr/bin/env python3
2
- """
3
- Command Line Interface
4
-
5
- New CLI implementation that uses the API facade for all operations.
6
- Provides a clean separation between CLI concerns and core analysis logic.
7
- """
8
-
9
- import argparse
10
- import json
11
- import logging
12
- import sys
13
- from pathlib import Path
14
- from typing import Any
15
-
16
- from .. import api
17
-
18
- # Configure logging for CLI
19
- logging.basicConfig(
20
- level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
21
- )
22
- logger = logging.getLogger(__name__)
23
-
24
-
25
- def create_parser() -> argparse.ArgumentParser:
26
- """Create and configure the argument parser."""
27
- parser = argparse.ArgumentParser(
28
- prog="tree-sitter-analyzer",
29
- description="Extensible multi-language code analyzer using Tree-sitter",
30
- formatter_class=argparse.RawDescriptionHelpFormatter,
31
- epilog="""
32
- Examples:
33
- # Analyze a Java file
34
- tree-sitter-analyzer analyze example.java
35
-
36
- # Analyze with specific language
37
- tree-sitter-analyzer analyze --language python script.py
38
-
39
- # Execute specific queries
40
- tree-sitter-analyzer analyze --queries functions,classes example.java
41
-
42
- # Extract only code elements
43
- tree-sitter-analyzer extract example.py
44
-
45
- # List supported languages
46
- tree-sitter-analyzer languages
47
-
48
- # Get framework information
49
- tree-sitter-analyzer info
50
-
51
- For more information, visit: https://github.com/aimasteracc/tree-sitter-analyzer
52
- """,
53
- )
54
-
55
- # Global options
56
- parser.add_argument(
57
- "--version", action="version", version="tree-sitter-analyzer 0.0.1"
58
- )
59
- parser.add_argument(
60
- "--verbose", "-v", action="store_true", help="Enable verbose output"
61
- )
62
- parser.add_argument(
63
- "--quiet", "-q", action="store_true", help="Suppress non-essential output"
64
- )
65
- parser.add_argument(
66
- "--output",
67
- "-o",
68
- choices=["json", "text", "table"],
69
- default="text",
70
- help="Output format (default: text)",
71
- )
72
-
73
- # Subcommands
74
- subparsers = parser.add_subparsers(dest="command", help="Available commands")
75
-
76
- # Analyze command
77
- analyze_parser = subparsers.add_parser(
78
- "analyze",
79
- help="Analyze source code files",
80
- description="Perform comprehensive analysis of source code files",
81
- )
82
- analyze_parser.add_argument("file_path", help="Path to the source file to analyze")
83
- analyze_parser.add_argument(
84
- "--language", "-l", help="Programming language (auto-detected if not specified)"
85
- )
86
- analyze_parser.add_argument(
87
- "--queries", help="Comma-separated list of queries to execute"
88
- )
89
- analyze_parser.add_argument(
90
- "--no-elements", action="store_true", help="Skip code element extraction"
91
- )
92
- analyze_parser.add_argument(
93
- "--no-queries", action="store_true", help="Skip query execution"
94
- )
95
-
96
- # Extract command
97
- extract_parser = subparsers.add_parser(
98
- "extract",
99
- help="Extract code elements from files",
100
- description="Extract specific code elements like functions, classes, etc.",
101
- )
102
- extract_parser.add_argument("file_path", help="Path to the source file")
103
- extract_parser.add_argument(
104
- "--language", "-l", help="Programming language (auto-detected if not specified)"
105
- )
106
- extract_parser.add_argument(
107
- "--types",
108
- help="Comma-separated list of element types to extract (e.g., functions,classes)",
109
- )
110
-
111
- # Query command
112
- query_parser = subparsers.add_parser(
113
- "query",
114
- help="Execute specific queries on files",
115
- description="Execute specific tree-sitter queries on source files",
116
- )
117
- query_parser.add_argument("file_path", help="Path to the source file")
118
- query_parser.add_argument("query_name", help="Name of the query to execute")
119
- query_parser.add_argument(
120
- "--language", "-l", help="Programming language (auto-detected if not specified)"
121
- )
122
-
123
- # Validate command
124
- validate_parser = subparsers.add_parser(
125
- "validate",
126
- help="Validate source files",
127
- description="Check if files can be parsed and analyzed",
128
- )
129
- validate_parser.add_argument(
130
- "file_path", help="Path to the source file to validate"
131
- )
132
-
133
- # Languages command
134
- languages_parser = subparsers.add_parser(
135
- "languages",
136
- help="List supported languages",
137
- description="Show all supported programming languages and their extensions",
138
- )
139
- languages_parser.add_argument(
140
- "--extensions",
141
- action="store_true",
142
- help="Show file extensions for each language",
143
- )
144
-
145
- # Info command
146
- subparsers.add_parser(
147
- "info",
148
- help="Show framework information",
149
- description="Display information about the analyzer framework",
150
- )
151
-
152
- # Queries command
153
- queries_parser = subparsers.add_parser(
154
- "queries",
155
- help="List available queries",
156
- description="Show available queries for a specific language",
157
- )
158
- queries_parser.add_argument("language", help="Programming language name")
159
-
160
- return parser
161
-
162
-
163
- def handle_analyze_command(args: argparse.Namespace) -> int:
164
- """Handle the analyze command."""
165
- try:
166
- file_path = Path(args.file_path)
167
-
168
- if not file_path.exists():
169
- print(f"Error: File '{file_path}' does not exist", file=sys.stderr)
170
- return 1
171
-
172
- # Parse queries if provided
173
- queries = None
174
- if args.queries:
175
- queries = [q.strip() for q in args.queries.split(",")]
176
-
177
- # Perform analysis
178
- result = api.analyze_file(
179
- file_path=file_path,
180
- language=args.language,
181
- queries=queries,
182
- include_elements=not args.no_elements,
183
- include_queries=not args.no_queries,
184
- )
185
-
186
- # Output results
187
- if args.output == "json":
188
- print(json.dumps(result, indent=2))
189
- else:
190
- format_analysis_output(result, args.output)
191
-
192
- return 0 if result.get("success", False) else 1
193
-
194
- except Exception as e:
195
- print(f"Error during analysis: {e}", file=sys.stderr)
196
- return 1
197
-
198
-
199
- def handle_extract_command(args: argparse.Namespace) -> int:
200
- """Handle the extract command."""
201
- try:
202
- file_path = Path(args.file_path)
203
-
204
- if not file_path.exists():
205
- print(f"Error: File '{file_path}' does not exist", file=sys.stderr)
206
- return 1
207
-
208
- # Parse element types if provided
209
- element_types = None
210
- if args.types:
211
- element_types = [t.strip() for t in args.types.split(",")]
212
-
213
- # Extract elements
214
- result = api.extract_elements(
215
- file_path=file_path, language=args.language, element_types=element_types
216
- )
217
-
218
- # Output results
219
- if args.output == "json":
220
- print(json.dumps(result, indent=2))
221
- else:
222
- format_extraction_output(result, args.output)
223
-
224
- return 0 if result.get("success", False) else 1
225
-
226
- except Exception as e:
227
- print(f"Error during extraction: {e}", file=sys.stderr)
228
- return 1
229
-
230
-
231
- def handle_query_command(args: argparse.Namespace) -> int:
232
- """Handle the query command."""
233
- try:
234
- file_path = Path(args.file_path)
235
-
236
- if not file_path.exists():
237
- print(f"Error: File '{file_path}' does not exist", file=sys.stderr)
238
- return 1
239
-
240
- # Execute query
241
- result = api.execute_query(
242
- file_path=file_path, query_name=args.query_name, language=args.language
243
- )
244
-
245
- # Output results
246
- if args.output == "json":
247
- print(json.dumps(result, indent=2))
248
- else:
249
- format_query_output(result, args.output)
250
-
251
- return 0 if result.get("success", False) else 1
252
-
253
- except Exception as e:
254
- print(f"Error during query execution: {e}", file=sys.stderr)
255
- return 1
256
-
257
-
258
- def handle_validate_command(args: argparse.Namespace) -> int:
259
- """Handle the validate command."""
260
- try:
261
- file_path = Path(args.file_path)
262
-
263
- # Validate file
264
- result = api.validate_file(file_path)
265
-
266
- # Output results
267
- if args.output == "json":
268
- print(json.dumps(result, indent=2))
269
- else:
270
- format_validation_output(result, args.output)
271
-
272
- return 0 if result.get("valid", False) else 1
273
-
274
- except Exception as e:
275
- print(f"Error during validation: {e}", file=sys.stderr)
276
- return 1
277
-
278
-
279
- def handle_languages_command(args: argparse.Namespace) -> int:
280
- """Handle the languages command."""
281
- try:
282
- languages = api.get_supported_languages()
283
-
284
- if args.output == "json":
285
- if args.extensions:
286
- lang_info = {}
287
- for lang in languages:
288
- extensions = api.get_file_extensions(lang)
289
- lang_info[lang] = extensions
290
- print(json.dumps(lang_info, indent=2))
291
- else:
292
- print(json.dumps(languages, indent=2))
293
- else:
294
- print("Supported Languages:")
295
- print("=" * 20)
296
- for lang in sorted(languages):
297
- if args.extensions:
298
- extensions = api.get_file_extensions(lang)
299
- ext_str = ", ".join(extensions) if extensions else "No extensions"
300
- print(f" {lang:<12} - {ext_str}")
301
- else:
302
- print(f" {lang}")
303
-
304
- if not args.extensions:
305
- print(f"\nTotal: {len(languages)} languages")
306
- print("Use --extensions to see file extensions for each language")
307
-
308
- return 0
309
-
310
- except Exception as e:
311
- print(f"Error getting language information: {e}", file=sys.stderr)
312
- return 1
313
-
314
-
315
- def handle_info_command(args: argparse.Namespace) -> int:
316
- """Handle the info command."""
317
- try:
318
- info = api.get_framework_info()
319
-
320
- if args.output == "json":
321
- print(json.dumps(info, indent=2))
322
- else:
323
- print("Tree-sitter Analyzer Framework Information")
324
- print("=" * 45)
325
- print(f"Name: {info.get('name', 'Unknown')}")
326
- print(f"Version: {info.get('version', 'Unknown')}")
327
- print(f"Supported Languages: {info.get('total_languages', 0)}")
328
-
329
- languages = info.get("supported_languages", [])
330
- if languages:
331
- print(f"Languages: {', '.join(sorted(languages))}")
332
-
333
- components = info.get("core_components", [])
334
- if components:
335
- print(f"Core Components: {', '.join(components)}")
336
-
337
- return 0
338
-
339
- except Exception as e:
340
- print(f"Error getting framework information: {e}", file=sys.stderr)
341
- return 1
342
-
343
-
344
- def handle_queries_command(args: argparse.Namespace) -> int:
345
- """Handle the queries command."""
346
- try:
347
- if not api.is_language_supported(args.language):
348
- print(
349
- f"Error: Language '{args.language}' is not supported", file=sys.stderr
350
- )
351
- return 1
352
-
353
- queries = api.get_available_queries(args.language)
354
-
355
- if args.output == "json":
356
- print(json.dumps(queries, indent=2))
357
- else:
358
- print(f"Available Queries for {args.language}:")
359
- print("=" * (25 + len(args.language)))
360
- for query in sorted(queries):
361
- print(f" {query}")
362
-
363
- print(f"\nTotal: {len(queries)} queries")
364
-
365
- return 0
366
-
367
- except Exception as e:
368
- print(f"Error getting query information: {e}", file=sys.stderr)
369
- return 1
370
-
371
-
372
- def format_analysis_output(result: dict[str, Any], output_format: str) -> None:
373
- """Format and display analysis results."""
374
- if not result.get("success", False):
375
- print(
376
- f"Analysis failed: {result.get('error', 'Unknown error')}", file=sys.stderr
377
- )
378
- return
379
-
380
- print("Analysis Results")
381
- print("=" * 16)
382
-
383
- # File information
384
- file_info = result.get("file_info", {})
385
- print(f"File: {file_info.get('path', 'Unknown')}")
386
-
387
- # Language information
388
- lang_info = result.get("language_info", {})
389
- language = lang_info.get("language", "Unknown")
390
- auto_detected = lang_info.get("auto_detected", False)
391
- detection_str = " (auto-detected)" if auto_detected else ""
392
- print(f"Language: {language}{detection_str}")
393
-
394
- # AST information
395
- ast_info = result.get("ast_info", {})
396
- print(f"Source Lines: {ast_info.get('source_lines', 0)}")
397
- print(f"AST Nodes: {ast_info.get('node_count', 0)}")
398
-
399
- # Query results
400
- query_results = result.get("query_results", {})
401
- if query_results:
402
- print("\nQuery Results:")
403
- for query_name, matches in query_results.items():
404
- print(f" {query_name}: {len(matches)} matches")
405
-
406
- # Elements
407
- elements = result.get("elements", [])
408
- if elements:
409
- print(f"\nCode Elements: {len(elements)} found")
410
- element_types: dict[str, int] = {}
411
- for element in elements:
412
- elem_type = element.get("type", "unknown")
413
- element_types[elem_type] = element_types.get(elem_type, 0) + 1
414
-
415
- for elem_type, count in sorted(element_types.items()):
416
- print(f" {elem_type}: {count}")
417
-
418
-
419
- def format_extraction_output(result: dict[str, Any], output_format: str) -> None:
420
- """Format and display extraction results."""
421
- if not result.get("success", False):
422
- print(
423
- f"Extraction failed: {result.get('error', 'Unknown error')}",
424
- file=sys.stderr,
425
- )
426
- return
427
-
428
- elements = result.get("elements", [])
429
- language = result.get("language", "Unknown")
430
-
431
- print("Code Element Extraction Results")
432
- print("=" * 31)
433
- print(f"File: {result.get('file_path', 'Unknown')}")
434
- print(f"Language: {language}")
435
- print(f"Elements Found: {len(elements)}")
436
-
437
- if elements:
438
- print("\nElements:")
439
- for element in elements:
440
- name = element.get("name", "Unknown")
441
- elem_type = element.get("type", "unknown")
442
- start_line = element.get("start_line", 0)
443
- print(f" {elem_type}: {name} (line {start_line})")
444
-
445
-
446
- def format_query_output(result: dict[str, Any], output_format: str) -> None:
447
- """Format and display query results."""
448
- if not result.get("success", False):
449
- print(f"Query failed: {result.get('error', 'Unknown error')}", file=sys.stderr)
450
- return
451
-
452
- query_name = result.get("query_name", "Unknown")
453
- matches = result.get("results", [])
454
- language = result.get("language", "Unknown")
455
-
456
- print("Query Execution Results")
457
- print("=" * 23)
458
- print(f"File: {result.get('file_path', 'Unknown')}")
459
- print(f"Language: {language}")
460
- print(f"Query: {query_name}")
461
- print(f"Matches: {len(matches)}")
462
-
463
- if matches:
464
- print("\nMatches:")
465
- for i, match in enumerate(matches, 1):
466
- start_line = match.get("start_line", 0)
467
- content = match.get("content", "").strip()
468
- if len(content) > 50:
469
- content = content[:47] + "..."
470
- print(f" {i}. Line {start_line}: {content}")
471
-
472
-
473
- def format_validation_output(result: dict[str, Any], output_format: str) -> None:
474
- """Format and display validation results."""
475
- valid = result.get("valid", False)
476
- exists = result.get("exists", False)
477
- readable = result.get("readable", False)
478
- language = result.get("language")
479
- supported = result.get("supported", False)
480
- errors = result.get("errors", [])
481
-
482
- print("File Validation Results")
483
- print("=" * 23)
484
- print(f"Valid: {'✓' if valid else '✗'}")
485
- print(f"Exists: {'✓' if exists else '✗'}")
486
- print(f"Readable: {'✓' if readable else '✗'}")
487
- print(f"Language: {language or 'Unknown'}")
488
- print(f"Supported: {'✓' if supported else '✗'}")
489
-
490
- if errors:
491
- print("\nErrors:")
492
- for error in errors:
493
- print(f" - {error}")
494
-
495
-
496
- def main() -> int:
497
- """Main CLI entry point."""
498
- parser = create_parser()
499
- args = parser.parse_args()
500
-
501
- # Configure logging based on verbosity
502
- if args.quiet:
503
- logging.getLogger().setLevel(logging.ERROR)
504
- elif args.verbose:
505
- logging.getLogger().setLevel(logging.DEBUG)
506
-
507
- # Handle commands
508
- if args.command == "analyze":
509
- return handle_analyze_command(args)
510
- elif args.command == "extract":
511
- return handle_extract_command(args)
512
- elif args.command == "query":
513
- return handle_query_command(args)
514
- elif args.command == "validate":
515
- return handle_validate_command(args)
516
- elif args.command == "languages":
517
- return handle_languages_command(args)
518
- elif args.command == "info":
519
- return handle_info_command(args)
520
- elif args.command == "queries":
521
- return handle_queries_command(args)
522
- else:
523
- parser.print_help()
524
- return 1
525
-
526
-
527
- if __name__ == "__main__":
528
- sys.exit(main())
1
+ #!/usr/bin/env python3
2
+ """
3
+ Command Line Interface
4
+
5
+ New CLI implementation that uses the API facade for all operations.
6
+ Provides a clean separation between CLI concerns and core analysis logic.
7
+ """
8
+
9
+ import argparse
10
+ import json
11
+ import logging
12
+ import sys
13
+ from pathlib import Path
14
+ from typing import Any
15
+
16
+ from .. import api
17
+
18
+ # Configure logging for CLI
19
+ logging.basicConfig(
20
+ level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
21
+ )
22
+ logger = logging.getLogger(__name__)
23
+
24
+
25
+ def create_parser() -> argparse.ArgumentParser:
26
+ """Create and configure the argument parser."""
27
+ parser = argparse.ArgumentParser(
28
+ prog="tree-sitter-analyzer",
29
+ description="Extensible multi-language code analyzer using Tree-sitter",
30
+ formatter_class=argparse.RawDescriptionHelpFormatter,
31
+ epilog="""
32
+ Examples:
33
+ # Analyze a Java file
34
+ tree-sitter-analyzer analyze example.java
35
+
36
+ # Analyze with specific language
37
+ tree-sitter-analyzer analyze --language python script.py
38
+
39
+ # Execute specific queries
40
+ tree-sitter-analyzer analyze --queries functions,classes example.java
41
+
42
+ # Extract only code elements
43
+ tree-sitter-analyzer extract example.py
44
+
45
+ # List supported languages
46
+ tree-sitter-analyzer languages
47
+
48
+ # Get framework information
49
+ tree-sitter-analyzer info
50
+
51
+ For more information, visit: https://github.com/aimasteracc/tree-sitter-analyzer
52
+ """,
53
+ )
54
+
55
+ # Global options
56
+ parser.add_argument(
57
+ "--version", action="version", version="tree-sitter-analyzer 0.0.1"
58
+ )
59
+ parser.add_argument(
60
+ "--verbose", "-v", action="store_true", help="Enable verbose output"
61
+ )
62
+ parser.add_argument(
63
+ "--quiet", "-q", action="store_true", help="Suppress non-essential output"
64
+ )
65
+ parser.add_argument(
66
+ "--output",
67
+ "-o",
68
+ choices=["json", "text", "table"],
69
+ default="text",
70
+ help="Output format (default: text)",
71
+ )
72
+
73
+ # Subcommands
74
+ subparsers = parser.add_subparsers(dest="command", help="Available commands")
75
+
76
+ # Analyze command
77
+ analyze_parser = subparsers.add_parser(
78
+ "analyze",
79
+ help="Analyze source code files",
80
+ description="Perform comprehensive analysis of source code files",
81
+ )
82
+ analyze_parser.add_argument("file_path", help="Path to the source file to analyze")
83
+ analyze_parser.add_argument(
84
+ "--language", "-l", help="Programming language (auto-detected if not specified)"
85
+ )
86
+ analyze_parser.add_argument(
87
+ "--queries", help="Comma-separated list of queries to execute"
88
+ )
89
+ analyze_parser.add_argument(
90
+ "--no-elements", action="store_true", help="Skip code element extraction"
91
+ )
92
+ analyze_parser.add_argument(
93
+ "--no-queries", action="store_true", help="Skip query execution"
94
+ )
95
+
96
+ # Extract command
97
+ extract_parser = subparsers.add_parser(
98
+ "extract",
99
+ help="Extract code elements from files",
100
+ description="Extract specific code elements like functions, classes, etc.",
101
+ )
102
+ extract_parser.add_argument("file_path", help="Path to the source file")
103
+ extract_parser.add_argument(
104
+ "--language", "-l", help="Programming language (auto-detected if not specified)"
105
+ )
106
+ extract_parser.add_argument(
107
+ "--types",
108
+ help="Comma-separated list of element types to extract (e.g., functions,classes)",
109
+ )
110
+
111
+ # Query command
112
+ query_parser = subparsers.add_parser(
113
+ "query",
114
+ help="Execute specific queries on files",
115
+ description="Execute specific tree-sitter queries on source files",
116
+ )
117
+ query_parser.add_argument("file_path", help="Path to the source file")
118
+ query_parser.add_argument("query_name", help="Name of the query to execute")
119
+ query_parser.add_argument(
120
+ "--language", "-l", help="Programming language (auto-detected if not specified)"
121
+ )
122
+
123
+ # Validate command
124
+ validate_parser = subparsers.add_parser(
125
+ "validate",
126
+ help="Validate source files",
127
+ description="Check if files can be parsed and analyzed",
128
+ )
129
+ validate_parser.add_argument(
130
+ "file_path", help="Path to the source file to validate"
131
+ )
132
+
133
+ # Languages command
134
+ languages_parser = subparsers.add_parser(
135
+ "languages",
136
+ help="List supported languages",
137
+ description="Show all supported programming languages and their extensions",
138
+ )
139
+ languages_parser.add_argument(
140
+ "--extensions",
141
+ action="store_true",
142
+ help="Show file extensions for each language",
143
+ )
144
+
145
+ # Info command
146
+ subparsers.add_parser(
147
+ "info",
148
+ help="Show framework information",
149
+ description="Display information about the analyzer framework",
150
+ )
151
+
152
+ # Queries command
153
+ queries_parser = subparsers.add_parser(
154
+ "queries",
155
+ help="List available queries",
156
+ description="Show available queries for a specific language",
157
+ )
158
+ queries_parser.add_argument("language", help="Programming language name")
159
+
160
+ return parser
161
+
162
+
163
+ def handle_analyze_command(args: argparse.Namespace) -> int:
164
+ """Handle the analyze command."""
165
+ try:
166
+ file_path = Path(args.file_path)
167
+
168
+ if not file_path.exists():
169
+ print(f"Error: File '{file_path}' does not exist", file=sys.stderr)
170
+ return 1
171
+
172
+ # Parse queries if provided
173
+ queries = None
174
+ if args.queries:
175
+ queries = [q.strip() for q in args.queries.split(",")]
176
+
177
+ # Perform analysis
178
+ result = api.analyze_file(
179
+ file_path=file_path,
180
+ language=args.language,
181
+ queries=queries,
182
+ include_elements=not args.no_elements,
183
+ include_queries=not args.no_queries,
184
+ )
185
+
186
+ # Output results
187
+ if args.output == "json":
188
+ print(json.dumps(result, indent=2))
189
+ else:
190
+ format_analysis_output(result, args.output)
191
+
192
+ return 0 if result.get("success", False) else 1
193
+
194
+ except Exception as e:
195
+ print(f"Error during analysis: {e}", file=sys.stderr)
196
+ return 1
197
+
198
+
199
+ def handle_extract_command(args: argparse.Namespace) -> int:
200
+ """Handle the extract command."""
201
+ try:
202
+ file_path = Path(args.file_path)
203
+
204
+ if not file_path.exists():
205
+ print(f"Error: File '{file_path}' does not exist", file=sys.stderr)
206
+ return 1
207
+
208
+ # Parse element types if provided
209
+ element_types = None
210
+ if args.types:
211
+ element_types = [t.strip() for t in args.types.split(",")]
212
+
213
+ # Extract elements
214
+ result = api.extract_elements(
215
+ file_path=file_path, language=args.language, element_types=element_types
216
+ )
217
+
218
+ # Output results
219
+ if args.output == "json":
220
+ print(json.dumps(result, indent=2))
221
+ else:
222
+ format_extraction_output(result, args.output)
223
+
224
+ return 0 if result.get("success", False) else 1
225
+
226
+ except Exception as e:
227
+ print(f"Error during extraction: {e}", file=sys.stderr)
228
+ return 1
229
+
230
+
231
+ def handle_query_command(args: argparse.Namespace) -> int:
232
+ """Handle the query command."""
233
+ try:
234
+ file_path = Path(args.file_path)
235
+
236
+ if not file_path.exists():
237
+ print(f"Error: File '{file_path}' does not exist", file=sys.stderr)
238
+ return 1
239
+
240
+ # Execute query
241
+ result = api.execute_query(
242
+ file_path=file_path, query_name=args.query_name, language=args.language
243
+ )
244
+
245
+ # Output results
246
+ if args.output == "json":
247
+ print(json.dumps(result, indent=2))
248
+ else:
249
+ format_query_output(result, args.output)
250
+
251
+ return 0 if result.get("success", False) else 1
252
+
253
+ except Exception as e:
254
+ print(f"Error during query execution: {e}", file=sys.stderr)
255
+ return 1
256
+
257
+
258
+ def handle_validate_command(args: argparse.Namespace) -> int:
259
+ """Handle the validate command."""
260
+ try:
261
+ file_path = Path(args.file_path)
262
+
263
+ # Validate file
264
+ result = api.validate_file(file_path)
265
+
266
+ # Output results
267
+ if args.output == "json":
268
+ print(json.dumps(result, indent=2))
269
+ else:
270
+ format_validation_output(result, args.output)
271
+
272
+ return 0 if result.get("valid", False) else 1
273
+
274
+ except Exception as e:
275
+ print(f"Error during validation: {e}", file=sys.stderr)
276
+ return 1
277
+
278
+
279
+ def handle_languages_command(args: argparse.Namespace) -> int:
280
+ """Handle the languages command."""
281
+ try:
282
+ languages = api.get_supported_languages()
283
+
284
+ if args.output == "json":
285
+ if args.extensions:
286
+ lang_info = {}
287
+ for lang in languages:
288
+ extensions = api.get_file_extensions(lang)
289
+ lang_info[lang] = extensions
290
+ print(json.dumps(lang_info, indent=2))
291
+ else:
292
+ print(json.dumps(languages, indent=2))
293
+ else:
294
+ print("Supported Languages:")
295
+ print("=" * 20)
296
+ for lang in sorted(languages):
297
+ if args.extensions:
298
+ extensions = api.get_file_extensions(lang)
299
+ ext_str = ", ".join(extensions) if extensions else "No extensions"
300
+ print(f" {lang:<12} - {ext_str}")
301
+ else:
302
+ print(f" {lang}")
303
+
304
+ if not args.extensions:
305
+ print(f"\nTotal: {len(languages)} languages")
306
+ print("Use --extensions to see file extensions for each language")
307
+
308
+ return 0
309
+
310
+ except Exception as e:
311
+ print(f"Error getting language information: {e}", file=sys.stderr)
312
+ return 1
313
+
314
+
315
+ def handle_info_command(args: argparse.Namespace) -> int:
316
+ """Handle the info command."""
317
+ try:
318
+ info = api.get_framework_info()
319
+
320
+ if args.output == "json":
321
+ print(json.dumps(info, indent=2))
322
+ else:
323
+ print("Tree-sitter Analyzer Framework Information")
324
+ print("=" * 45)
325
+ print(f"Name: {info.get('name', 'Unknown')}")
326
+ print(f"Version: {info.get('version', 'Unknown')}")
327
+ print(f"Supported Languages: {info.get('total_languages', 0)}")
328
+
329
+ languages = info.get("supported_languages", [])
330
+ if languages:
331
+ print(f"Languages: {', '.join(sorted(languages))}")
332
+
333
+ components = info.get("core_components", [])
334
+ if components:
335
+ print(f"Core Components: {', '.join(components)}")
336
+
337
+ return 0
338
+
339
+ except Exception as e:
340
+ print(f"Error getting framework information: {e}", file=sys.stderr)
341
+ return 1
342
+
343
+
344
+ def handle_queries_command(args: argparse.Namespace) -> int:
345
+ """Handle the queries command."""
346
+ try:
347
+ if not api.is_language_supported(args.language):
348
+ print(
349
+ f"Error: Language '{args.language}' is not supported", file=sys.stderr
350
+ )
351
+ return 1
352
+
353
+ queries = api.get_available_queries(args.language)
354
+
355
+ if args.output == "json":
356
+ print(json.dumps(queries, indent=2))
357
+ else:
358
+ print(f"Available Queries for {args.language}:")
359
+ print("=" * (25 + len(args.language)))
360
+ for query in sorted(queries):
361
+ print(f" {query}")
362
+
363
+ print(f"\nTotal: {len(queries)} queries")
364
+
365
+ return 0
366
+
367
+ except Exception as e:
368
+ print(f"Error getting query information: {e}", file=sys.stderr)
369
+ return 1
370
+
371
+
372
+ def format_analysis_output(result: dict[str, Any], output_format: str) -> None:
373
+ """Format and display analysis results."""
374
+ if not result.get("success", False):
375
+ print(
376
+ f"Analysis failed: {result.get('error', 'Unknown error')}", file=sys.stderr
377
+ )
378
+ return
379
+
380
+ print("Analysis Results")
381
+ print("=" * 16)
382
+
383
+ # File information
384
+ file_info = result.get("file_info", {})
385
+ print(f"File: {file_info.get('path', 'Unknown')}")
386
+
387
+ # Language information
388
+ lang_info = result.get("language_info", {})
389
+ language = lang_info.get("language", "Unknown")
390
+ auto_detected = lang_info.get("auto_detected", False)
391
+ detection_str = " (auto-detected)" if auto_detected else ""
392
+ print(f"Language: {language}{detection_str}")
393
+
394
+ # AST information
395
+ ast_info = result.get("ast_info", {})
396
+ print(f"Source Lines: {ast_info.get('source_lines', 0)}")
397
+ print(f"AST Nodes: {ast_info.get('node_count', 0)}")
398
+
399
+ # Query results
400
+ query_results = result.get("query_results", {})
401
+ if query_results:
402
+ print("\nQuery Results:")
403
+ for query_name, matches in query_results.items():
404
+ print(f" {query_name}: {len(matches)} matches")
405
+
406
+ # Elements
407
+ elements = result.get("elements", [])
408
+ if elements:
409
+ print(f"\nCode Elements: {len(elements)} found")
410
+ element_types: dict[str, int] = {}
411
+ for element in elements:
412
+ elem_type = element.get("type", "unknown")
413
+ element_types[elem_type] = element_types.get(elem_type, 0) + 1
414
+
415
+ for elem_type, count in sorted(element_types.items()):
416
+ print(f" {elem_type}: {count}")
417
+
418
+
419
+ def format_extraction_output(result: dict[str, Any], output_format: str) -> None:
420
+ """Format and display extraction results."""
421
+ if not result.get("success", False):
422
+ print(
423
+ f"Extraction failed: {result.get('error', 'Unknown error')}",
424
+ file=sys.stderr,
425
+ )
426
+ return
427
+
428
+ elements = result.get("elements", [])
429
+ language = result.get("language", "Unknown")
430
+
431
+ print("Code Element Extraction Results")
432
+ print("=" * 31)
433
+ print(f"File: {result.get('file_path', 'Unknown')}")
434
+ print(f"Language: {language}")
435
+ print(f"Elements Found: {len(elements)}")
436
+
437
+ if elements:
438
+ print("\nElements:")
439
+ for element in elements:
440
+ name = element.get("name", "Unknown")
441
+ elem_type = element.get("type", "unknown")
442
+ start_line = element.get("start_line", 0)
443
+ print(f" {elem_type}: {name} (line {start_line})")
444
+
445
+
446
+ def format_query_output(result: dict[str, Any], output_format: str) -> None:
447
+ """Format and display query results."""
448
+ if not result.get("success", False):
449
+ print(f"Query failed: {result.get('error', 'Unknown error')}", file=sys.stderr)
450
+ return
451
+
452
+ query_name = result.get("query_name", "Unknown")
453
+ matches = result.get("results", [])
454
+ language = result.get("language", "Unknown")
455
+
456
+ print("Query Execution Results")
457
+ print("=" * 23)
458
+ print(f"File: {result.get('file_path', 'Unknown')}")
459
+ print(f"Language: {language}")
460
+ print(f"Query: {query_name}")
461
+ print(f"Matches: {len(matches)}")
462
+
463
+ if matches:
464
+ print("\nMatches:")
465
+ for i, match in enumerate(matches, 1):
466
+ start_line = match.get("start_line", 0)
467
+ content = match.get("content", "").strip()
468
+ if len(content) > 50:
469
+ content = content[:47] + "..."
470
+ print(f" {i}. Line {start_line}: {content}")
471
+
472
+
473
+ def format_validation_output(result: dict[str, Any], output_format: str) -> None:
474
+ """Format and display validation results."""
475
+ valid = result.get("valid", False)
476
+ exists = result.get("exists", False)
477
+ readable = result.get("readable", False)
478
+ language = result.get("language")
479
+ supported = result.get("supported", False)
480
+ errors = result.get("errors", [])
481
+
482
+ print("File Validation Results")
483
+ print("=" * 23)
484
+ print(f"Valid: {'✓' if valid else '✗'}")
485
+ print(f"Exists: {'✓' if exists else '✗'}")
486
+ print(f"Readable: {'✓' if readable else '✗'}")
487
+ print(f"Language: {language or 'Unknown'}")
488
+ print(f"Supported: {'✓' if supported else '✗'}")
489
+
490
+ if errors:
491
+ print("\nErrors:")
492
+ for error in errors:
493
+ print(f" - {error}")
494
+
495
+
496
+ def main() -> int:
497
+ """Main CLI entry point."""
498
+ parser = create_parser()
499
+ args = parser.parse_args()
500
+
501
+ # Configure logging based on verbosity
502
+ if args.quiet:
503
+ logging.getLogger().setLevel(logging.ERROR)
504
+ elif args.verbose:
505
+ logging.getLogger().setLevel(logging.DEBUG)
506
+
507
+ # Handle commands
508
+ if args.command == "analyze":
509
+ return handle_analyze_command(args)
510
+ elif args.command == "extract":
511
+ return handle_extract_command(args)
512
+ elif args.command == "query":
513
+ return handle_query_command(args)
514
+ elif args.command == "validate":
515
+ return handle_validate_command(args)
516
+ elif args.command == "languages":
517
+ return handle_languages_command(args)
518
+ elif args.command == "info":
519
+ return handle_info_command(args)
520
+ elif args.command == "queries":
521
+ return handle_queries_command(args)
522
+ else:
523
+ parser.print_help()
524
+ return 1
525
+
526
+
527
+ if __name__ == "__main__":
528
+ sys.exit(main())