mcp-cpp-server 0.2.2

A high-performance Model Context Protocol (MCP) server for C++ code analysis using clangd LSP integration
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
#!/usr/bin/env python3
"""
Script to generate clangd index by mimicking VS Code's initialization sequence.
Automatically exits when clang        # Use same arguments as VS Code (from the log analysis)
        args = [
            self.clangd_path,
            "--background-index",  # Fixed: not --background-index=true
            "--clang-tidy",
            "--completion-style=detailed",
            "--log=verbose",  # To see indexing messages
            "--query-driver=**",  # Allow querying all drivers
            # Pass build directory to clangd
            f"--compile-commands-dir={self.build_directory}"
        ]indexing.
"""

import json
import subprocess
import threading
import time
import os
import sys
import shutil
import argparse
from pathlib import Path
from typing import Dict, Any


class ClangdIndexGenerator:
    def __init__(self, build_directory: str, clangd_path: str = "clangd",
                 refresh_index: bool = False, log_file: str = None, verbose: bool = False):
        self.build_directory = Path(build_directory).resolve()
        self.clangd_path = clangd_path
        self.refresh_index = refresh_index
        self.log_file = log_file
        self.verbose = verbose
        self.log_file_handle = None
        self.process = None
        self.reader = None
        self.writer = None
        self.request_id = 0
        self.indexing_progress = {}
        self.indexed_files = set()  # All files indexed (including headers)
        self.processed_compile_files = set()  # Files from compile_commands.json that have been processed
        # Files from compile_commands.json for reporting (now stores full paths)
        self.compile_commands_files = set()  # Set of Path objects
        self.compile_commands_files_by_name = set()  # Set of filenames for backward compatibility
        self.failed_files = set()  # Files that failed to index
        self.files_with_errors = {}  # filename -> error count
        self.indexing_complete = False
        self.last_indexing_activity = time.time()
        self.diagnostic_errors = 0  # Total count of diagnostic errors
        self.diagnostic_warnings = 0  # Total count of diagnostic warnings
        self.lsp_errors = 0  # Count of LSP protocol errors
        self.current_processing_file = ""  # Current file being processed for progress display

        # Open log file if specified
        if self.log_file:
            try:
                self.log_file_handle = open(self.log_file, 'w',
                                            encoding='utf-8')
                print(f"📝 Logging clangd output to: {self.log_file}")
            except Exception as e:
                print(f"⚠️  Warning: Could not open log file "
                      f"{self.log_file}: {e}")
                self.log_file = None

    def _log_message(self, message: str, source: str = "STDOUT"):
        """Log a message to both console and file if log file is specified"""
        timestamped_msg = f"[{time.strftime('%H:%M:%S')}] [{source}] {message}"

        # Always print to console
        print(timestamped_msg)

        # Also write to log file if specified
        if self.log_file_handle:
            try:
                self.log_file_handle.write(timestamped_msg + "\n")
                self.log_file_handle.flush()
            except Exception as e:
                print(f"⚠️  Warning: Could not write to log file: {e}")

    def _log_clangd_stderr(self, line: str):
        """Log clangd stderr output with special handling"""
        if self.log_file_handle:
            try:
                timestamp = time.strftime('%H:%M:%S')
                self.log_file_handle.write(
                    f"[{timestamp}] [CLANGD_STDERR] {line}\n")
                self.log_file_handle.flush()
            except Exception as e:
                print(f"⚠️  Warning: Could not write stderr to log file: {e}")

    def _log_lsp_message(self, message: Dict[str, Any], direction: str):
        """Log LSP messages (JSON-RPC) to file if logging is enabled"""
        if self.log_file_handle:
            try:
                timestamp = time.strftime('%H:%M:%S')
                json_str = json.dumps(message, indent=2)
                self.log_file_handle.write(
                    f"[{timestamp}] [LSP_{direction}] {json_str}\n"
                )
                self.log_file_handle.flush()
            except Exception as e:
                print(f"⚠️  Warning: Could not write LSP message to log: {e}")

    def _print_verbose(self, message: str):
        """Print message only if in verbose mode"""
        if self.verbose:
            print(message)

    def _mark_file_as_processed(self, file_path_str: str, activity: str = ""):
        """Mark a file as processed if it's in compile_commands.json"""
        try:
            # Normalize the path from clangd to absolute path
            resolved_path = Path(file_path_str).resolve()
            filename = resolved_path.name
            
            # Check if this resolved path is in our compile_commands.json
            if resolved_path in self.compile_commands_files:
                if resolved_path not in self.processed_compile_files:
                    self.processed_compile_files.add(resolved_path)
                    self.current_processing_file = filename
                    self.last_indexing_activity = time.time()
                    
                    if activity and self.verbose:
                        self._print_verbose(f"📝 Processing {filename}: {activity}")
                    
                    return True
        except Exception:
            pass
        return False

    def _print_progress(self, update_in_place: bool = False):
        """Display clean progress indicator"""
        if not self.compile_commands_files:
            return
        
        processed_count = len(self.processed_compile_files)
        total_count = len(self.compile_commands_files)
        percentage = (processed_count / total_count) * 100 if total_count > 0 else 0
        
        progress_msg = f"Processing: {processed_count}/{total_count} files ({percentage:.1f}%)"
        if self.current_processing_file:
            progress_msg += f" [{self.current_processing_file}]"
        
        if update_in_place and not self.verbose:
            print(f"\r{progress_msg}", end='', flush=True)
        else:
            print(progress_msg)

    def clean_cache(self):
        """Clean clangd cache directories to ensure fresh indexing (only if refresh_index is True)"""
        if not self.refresh_index:
            print("🔄 Skipping cache cleaning (use --refresh-index to clean cache)")
            return

        cache_locations = [
            # Common clangd cache locations
            Path.home() / ".cache" / "clangd",
            Path.home() / ".clangd",
            self.build_directory / ".clangd",
            self.build_directory.parent / ".clangd",
            # Project-local cache directories
            self.build_directory.parent / ".cache" / "clangd",
            self.build_directory / ".cache" / "clangd",
            # XDG cache directory
            Path(os.environ.get("XDG_CACHE_HOME",
                                Path.home() / ".cache")) / "clangd",
        ]

        print("🧹 Cleaning clangd cache directories...")
        cleaned_any = False

        for cache_dir in cache_locations:
            if cache_dir.exists() and cache_dir.is_dir():
                try:
                    print(f"   Removing: {cache_dir}")
                    shutil.rmtree(cache_dir)
                    cleaned_any = True
                except Exception as e:
                    print(f"   ⚠️  Could not remove {cache_dir}: {e}")

        if not cleaned_any:
            print("   No cache directories found to clean")
        else:
            print("✅ Cache cleaning completed")
        print()

    def start_clangd(self):
        """Start clangd with the same arguments VS Code uses"""
        compile_commands = self.build_directory / "compile_commands.json"
        if not compile_commands.exists():
            raise FileNotFoundError(
                f"No compile_commands.json found in {self.build_directory}")

        # Use same arguments as VS Code (from the log analysis)
        args = [
            self.clangd_path,
            "--background-index",  # Fixed: not --background-index=true
            "--clang-tidy",
            "--completion-style=detailed",
            "--log=verbose",  # To see indexing messages
            "--query-driver=**",  # Allow querying all drivers for cross-compilation
            # Pass build directory to clangd
            f"--compile-commands-dir={self.build_directory}"
        ]

        print(f"Starting clangd with args: {' '.join(args)}")
        print(f"Working directory: {os.getcwd()}")
        print(f"Build directory: {self.build_directory}")

        # Run clangd from current working directory, pass build dir as argument
        self.process = subprocess.Popen(
            args,
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            text=True,
            bufsize=0
        )

        # Set up JSON-RPC communication using simple JSON over stdio
        self.stdin_lock = threading.Lock()

        # Start message reader thread
        self.reader_thread = threading.Thread(
            target=self._read_messages, daemon=True)
        self.reader_thread.start()

        # Start stderr reader for clangd logs
        self.stderr_thread = threading.Thread(
            target=self._read_stderr, daemon=True)
        self.stderr_thread.start()

    def _read_stderr(self):
        """Read stderr for clangd log messages including indexing progress"""
        while self.process and self.process.poll() is None:
            try:
                line = self.process.stderr.readline()
                if not line:
                    break
                line = line.strip()
                if line:
                    # Log all stderr to file if logging is enabled
                    self._log_clangd_stderr(line)

                    # Track indexing-related messages with reduced verbosity
                    show_clangd_log = self.verbose and any(keyword in line for keyword in [
                        "Enqueueing", "commands for indexing", "Indexed",
                        "symbols", "backgroundIndexProgress",
                        "Building first preamble", "compilation database",
                        "Broadcasting", "ASTWorker", "Error", "Failed",
                        "error:", "warning:", "fatal error"
                    ])
                    
                    if show_clangd_log:
                        self._print_verbose(f"[CLANGD LOG] {line}")

                    # Track file processing from multiple log patterns
                    file_processed = False
                    
                    if "Indexed " in line and "symbols" in line:
                        # Extract filename from log line like:
                        # "Indexed /path/to/file.cpp (1234 symbols, ...)"
                        try:
                            file_part = line.split("Indexed ")[1].split(" (")[0]
                            filename = Path(file_part).name
                            self.indexed_files.add(filename)
                            
                            # Mark as processed if it's from compile_commands.json
                            if self._mark_file_as_processed(file_part, "indexed with symbols"):
                                file_processed = True
                                if self.verbose:
                                    symbols_part = line.split("(")[1].split(" symbols")[0] if "(" in line else "unknown"
                                    print(f"✅ Indexed {filename} ({symbols_part} symbols)")

                        except Exception:
                            pass  # Ignore parsing errors
                    
                    elif "Building first preamble for " in line:
                        # Extract filename from preamble build message
                        # Format: "Building first preamble for /path/to/file.cpp version N"
                        try:
                            after_for = line.split("Building first preamble for ")[1]
                            file_part = after_for.split(" version ")[0].strip()
                            if self._mark_file_as_processed(file_part, "building preamble"):
                                file_processed = True
                        except Exception:
                            pass
                    
                    elif "ASTWorker building file " in line:
                        # Extract filename from ASTWorker build messages
                        # Format: "ASTWorker building file /path/to/file.cpp version N with command ..."
                        try:
                            after_file = line.split("ASTWorker building file ")[1]
                            file_part = after_file.split(" version ")[0].strip()
                            if self._mark_file_as_processed(file_part, "ASTWorker building"):
                                file_processed = True
                        except Exception:
                            pass
                    
                    # Update progress display if a compile_commands.json file was processed
                    if file_processed and not self.verbose:
                        self._print_progress(update_in_place=True)

                    # Track indexing failures
                    elif any(error_indicator in line.lower() for error_indicator in [
                        "error:", "fatal error", "failed to", "could not", "cannot"
                    ]):
                        # Extract potential filename from error messages
                        try:
                            # Look for patterns like "file.cpp:line:col: error"
                            if ".cpp:" in line or ".cc:" in line or ".cxx:" in line:
                                for ext in [".cpp:", ".cc:", ".cxx:"]:
                                    if ext in line:
                                        file_part = line.split(ext)[0]
                                        filename_with_ext = file_part.split("/")[-1] + ext[:-1]
                                        if filename_with_ext in self.compile_commands_files_by_name:
                                            if filename_with_ext not in self.files_with_errors:
                                                self.files_with_errors[filename_with_ext] = 0
                                            self.files_with_errors[filename_with_ext] += 1
                                            error_msg = line.split('error:')[-1].strip()
                                            self._print_verbose(f"❌ Error in {filename_with_ext}: {error_msg}")
                                        break
                            else:
                                self._print_verbose(f"❌ General indexing error: {line}")
                        except Exception:
                            self._print_verbose(f"❌ Parse error in log: {line}")

                    # Also track from symbol slab messages
                    elif "symbol slab:" in line and "symbols" in line:
                        # These indicate files being processed
                        symbols_count = line.split("symbol slab:")[1].split("symbols")[0].strip()
                        if symbols_count.isdigit() and int(symbols_count) > 0:
                            self._print_verbose(f"📊 Processing symbols: {symbols_count} symbols indexed")
                            self.last_indexing_activity = time.time()

                    # Check for indexing completion signals
                    elif "backgroundIndexProgress" in line and "end" in line:
                        self._print_verbose("🎯 Background indexing progress ended")
                        self.indexing_complete = True

                    elif "ASTWorker" in line and ("idle" in line.lower() or "finished" in line.lower()):
                        self._print_verbose("🔄 ASTWorker activity completed")

            except Exception as e:
                print(f"Error reading stderr: {e}")
                break

    def _read_messages(self):
        """Read JSON-RPC messages from clangd using LSP protocol"""
        while self.process and self.process.poll() is None:
            try:
                # Read the Content-Length header
                while True:
                    line = self.process.stdout.readline()
                    if not line:
                        return
                    line = line.strip()
                    if line.startswith('Content-Length:'):
                        content_length = int(line.split(':')[1].strip())
                        break
                    elif line == '':
                        # Empty line after headers
                        break

                # Read any remaining header lines until empty line
                while True:
                    line = self.process.stdout.readline()
                    if not line:
                        return
                    line = line.strip()
                    if line == '':
                        break

                # Read the JSON content
                if 'content_length' in locals():
                    json_data = self.process.stdout.read(content_length)
                    if json_data:
                        try:
                            message = json.loads(json_data)
                            self._handle_message(message)
                        except json.JSONDecodeError as e:
                            print(f"JSON decode error: {e}")
                            print(f"Raw data: {json_data}")

            except Exception as e:
                print(f"Error reading message: {e}")
                break

    def _handle_message(self, message: Dict[str, Any]):
        """Handle incoming messages from clangd"""
        # Log incoming message if logging is enabled
        self._log_lsp_message(message, "INCOMING")

        method = message.get("method", "")

        # Debug: print all methods we receive in verbose mode
        if method:
            self._print_verbose(f"🔍 Received method: {method}")

        if method == "window/workDoneProgress/create":
            # clangd is requesting to create a progress token
            token = message.get("params", {}).get("token", "")
            self._print_verbose(f"🔄 Progress token created: {token}")
            # Send success response
            self._send_response(message.get("id"), None)

        elif method == "$/progress":
            # Progress update from clangd
            params = message.get("params", {})
            token = params.get("token", "")
            value = params.get("value", {})

            if token == "backgroundIndexProgress":
                kind = value.get("kind", "")
                if kind == "begin":
                    title = value.get("title", "")
                    percentage = value.get("percentage", 0)
                    print(f"🚀 Indexing started: {title}")
                    if self.verbose:
                        print(f"   Initial progress: {percentage}%")
                elif kind == "report":
                    message_text = value.get("message", "")
                    percentage = value.get("percentage", 0)
                    self._print_verbose(f"📊 Indexing progress: {message_text} ({percentage}%)")
                    self.last_indexing_activity = time.time()
                elif kind == "end":
                    if not self.verbose:
                        print()  # New line to finish the progress indicator
                    print("✅ Background indexing completed!")
                    self.indexing_complete = True

        elif method == "textDocument/clangd.fileStatus":
            # File status updates
            params = message.get("params", {})
            uri = params.get("uri", "")
            state = params.get("state", "")
            filename = Path(uri.replace("file://", "")).name
            self._print_verbose(f"📄 File status: {filename} - {state}")

        elif method == "textDocument/publishDiagnostics":
            # Diagnostic messages (errors, warnings, etc.)
            params = message.get("params", {})
            uri = params.get("uri", "")
            diagnostics = params.get("diagnostics", [])
            filename = Path(uri.replace("file://", "")).name

            if diagnostics:
                # Track errors and warnings separately
                errors = [d for d in diagnostics if d.get('severity') == 1]
                warnings = [d for d in diagnostics if d.get('severity') == 2]

                # Track diagnostics for failure reporting
                if filename in self.compile_commands_files_by_name:
                    if errors:
                        self.diagnostic_errors += len(errors)
                        if filename not in self.files_with_errors:
                            self.files_with_errors[filename] = 0
                        self.files_with_errors[filename] += len(errors)
                        self._print_verbose(f"{filename}: {len(errors)} error(s)")
                    if warnings:
                        self.diagnostic_warnings += len(warnings)
                        self._print_verbose(f"⚠️  {filename}: {len(warnings)} warning(s)")
                else:
                    self._print_verbose(f"🔍 Diagnostics for {filename}: {len(diagnostics)} issues")
            # Don't print "Unknown method" for this standard LSP method

        elif "result" in message:
            # Response to our request
            request_id = message.get("id")
            if request_id:
                self._print_verbose(f"✅ Response to request {request_id}")

        elif "error" in message:
            # Error response to our request
            request_id = message.get("id")
            error = message.get("error", {})
            error_code = error.get("code", "")
            error_message = error.get("message", "")
            if error_code == -32601:  # Method not found
                self._print_verbose(f"⚠️  Method not supported by clangd (request {request_id})")
            else:
                print(f"❌ Error response to request {request_id}: {error_message}")
                # Track LSP errors as potential indexing failures
                self.lsp_errors += 1
        else:
            # Debug: print unknown messages
            if method:
                self._print_verbose(f"❓ Unknown method: {method}")
            elif "result" not in message and "error" not in message:
                self._print_verbose(f"❓ Unknown message: {message}")

    def _send_json_rpc(self, message: Dict[str, Any]):
        """Send a JSON-RPC message to clangd using LSP format"""
        # Log outgoing message if logging is enabled
        self._log_lsp_message(message, "OUTGOING")

        json_text = json.dumps(message)
        content = f"Content-Length: {len(json_text)}\r\n\r\n{json_text}"

        with self.stdin_lock:
            try:
                self.process.stdin.write(content)
                self.process.stdin.flush()
            except Exception as e:
                print(f"Error writing to clangd: {e}")

    def _send_response(self, request_id: Any, result: Any):
        """Send a response to clangd"""
        response = {
            "jsonrpc": "2.0",
            "id": request_id,
            "result": result
        }
        self._send_json_rpc(response)

    def _send_request(self, method: str, params: Any = None) -> int:
        """Send a request to clangd"""
        self.request_id += 1
        request = {
            "jsonrpc": "2.0",
            "id": self.request_id,
            "method": method,
            "params": params or {}
        }
        self._print_verbose(f"📤 Sending {method} request (id: {self.request_id})")
        self._send_json_rpc(request)
        return self.request_id

    def _send_notification(self, method: str, params: Any = None):
        """Send a notification to clangd"""
        notification = {
            "jsonrpc": "2.0",
            "method": method,
            "params": params or {}
        }
        self._print_verbose(f"📤 Sending {method} notification")
        self._send_json_rpc(notification)

    def initialize_lsp(self):
        """Initialize clangd with comprehensive capabilities for AI agent use"""
        # Comprehensive capabilities for AI agents that work like humans
        init_params = {
            "processId": os.getpid(),
            "capabilities": {
                # INDEXING-CRITICAL CAPABILITIES
                "workspace": {
                    "workspaceFolders": True,  # Essential for multi-folder projects
                    "workDoneProgress": True,  # Essential for indexing progress
                    "configuration": True,     # May affect indexing behavior
                    "didChangeConfiguration": {
                        "dynamicRegistration": True
                    },
                    "didChangeWatchedFiles": {  # AI needs to know when files change
                        "dynamicRegistration": True
                    },
                    "symbol": {  # Workspace-wide symbol search - very useful for AI
                        "dynamicRegistration": True
                    },
                    "executeCommand": {  # AI may want to trigger clangd commands
                        "dynamicRegistration": True
                    }
                },
                "window": {
                    "workDoneProgress": True,   # Essential for indexing progress
                    "showMessage": {  # AI should see clangd messages/warnings
                        "messageActionItem": {
                            "additionalPropertiesSupport": True
                        }
                    }
                },

                # COMPREHENSIVE AI CAPABILITIES (Human-like interaction)
                "textDocument": {
                    # Code navigation - essential for AI
                    "definition": {
                        "linkSupport": True
                    },
                    "declaration": {
                        "linkSupport": True
                    },
                    "references": {
                        "context": True
                    },
                    "implementation": {
                        "linkSupport": True
                    },

                    # Code understanding - essential for AI
                    "hover": {
                        "contentFormat": ["markdown", "plaintext"]
                    },
                    "documentSymbol": {
                        "dynamicRegistration": True,
                        "hierarchicalDocumentSymbolSupport": True
                    },
                    "completion": {
                        "completionItem": {
                            "documentationFormat": ["markdown", "plaintext"],
                            "snippetSupport": True,  # AI can understand snippets
                            "commitCharactersSupport": True,
                            "resolveSupport": {
                                "properties": ["documentation", "detail"]
                            }
                        }
                    },

                    # Code quality - AI needs to see what humans see
                    "diagnostic": {
                        "dynamicRegistration": True,
                        "relatedDocumentSupport": True
                    },
                    "inlayHint": {  # Type hints, parameter names - useful for AI
                        "dynamicRegistration": True,
                        "resolveSupport": {
                            "properties": ["tooltip", "textEdits"]
                        }
                    },

                    # Code editing - AI edits code like humans
                    "codeAction": {
                        "codeActionLiteralSupport": {
                            "codeActionKind": {
                                "valueSet": ["quickfix", "refactor", "source"]
                            }
                        },
                        "resolveSupport": {
                            "properties": ["edit"]
                        }
                    },
                    "rename": {
                        "prepareSupport": True
                    },
                    "formatting": {
                        "dynamicRegistration": True
                    },
                    "rangeFormatting": {
                        "dynamicRegistration": True
                    },

                    # Code structure understanding
                    "foldingRange": {  # AI can understand code structure
                        "dynamicRegistration": True,
                        "rangeLimit": 5000
                    },
                    "selectionRange": {  # Smart selection for AI
                        "dynamicRegistration": True
                    },

                    # Enhanced code understanding
                    "semanticTokens": {  # AI can understand syntax roles
                        "dynamicRegistration": True,
                        "requests": {
                            "range": True,
                            "full": {
                                "delta": True
                            }
                        }
                    },
                    "documentHighlight": {  # AI can see related symbols
                        "dynamicRegistration": True
                    },
                    "documentLink": {  # AI can follow links in comments/docs
                        "dynamicRegistration": True,
                        "tooltipSupport": True
                    },

                    # Interactive features AI can use
                    "signatureHelp": {  # Function parameter hints
                        "signatureInformation": {
                            "documentationFormat": ["markdown", "plaintext"],
                            "parameterInformation": {
                                "labelOffsetSupport": True
                            }
                        }
                    },
                    "codeLens": {  # Inline references/implementations count
                        "dynamicRegistration": True
                    },
                    "callHierarchy": {  # Call relationships
                        "dynamicRegistration": True
                    }

                    # ONLY REMOVED: Capabilities that are truly not useful for AI
                    # - colorProvider (color swatches in UI)
                    # - onTypeFormatting (format-as-you-type)
                }
            },
            "initializationOptions": {
                "clangdFileStatus": True,  # Important for tracking file states
                "fallbackFlags": ["-std=c++20"]
            },
            "trace": "off"
        }

        print("🔧 Initializing LSP with comprehensive AI capabilities...")
        self._send_request("initialize", init_params)
        time.sleep(1)  # Wait for initialize response

        print("✅ Sending initialized notification...")
        self._send_notification("initialized")
        time.sleep(0.5)

    def trigger_indexing_by_opening_file(self):
        """
        The key insight: VS Code triggers indexing by opening a file!
        This is what actually starts the background indexing process.
        """
        # Get the first file from compile_commands.json - no need for folder introspection
        compile_commands = self.build_directory / "compile_commands.json"

        try:
            with open(compile_commands, 'r') as f:
                commands = json.load(f)

            if not commands:
                print("⚠️  No files in compile_commands.json. Cannot trigger indexing.")
                return

            # Use the first file from compile_commands.json and resolve it properly
            cmd = commands[0]
            directory = Path(cmd.get('directory', '.')).resolve()
            cpp_file = Path(cmd['file'])
            if not cpp_file.is_absolute():
                cpp_file = (directory / cpp_file).resolve()
            else:
                cpp_file = cpp_file.resolve()
            
            # Check if the file exists before trying to open it
            if not cpp_file.exists():
                print(f"❌ Error: First file from compile_commands.json does not exist: {cpp_file}")
                print("   Cannot trigger indexing without a valid source file.")
                return
            
            print(f"📂 Opening file to trigger indexing: {cpp_file}")

            with open(cpp_file, 'r', encoding='utf-8') as f:
                content = f.read()
        except Exception as e:
            print(f"❌ Could not read file from compile_commands.json: {e}")
            return

        # Send textDocument/didOpen - this is the trigger!
        did_open_params = {
            "textDocument": {
                "uri": f"file://{cpp_file}",
                "languageId": "cpp",
                "version": 1,
                "text": content
            }
        }

        print("🚀 Sending textDocument/didOpen - this should trigger indexing!")
        self._send_notification("textDocument/didOpen", did_open_params)

        # Also send some requests that VS Code typically sends
        time.sleep(0.1)

        doc_uri = {"uri": f"file://{cpp_file}"}

        # Request document symbols (this works with clangd)
        self._send_request("textDocument/documentSymbol",
                           {"textDocument": doc_uri})

        # Note: Don't request foldingRange as clangd doesn't support it

    def open_file_for_indexing(self, file_path: Path):
        """Open a specific file to trigger its indexing"""
        try:
            with open(file_path, 'r', encoding='utf-8') as f:
                content = f.read()
        except Exception as e:
            self._print_verbose(f"❌ Could not read file {file_path}: {e}")
            return False

        # Send textDocument/didOpen
        did_open_params = {
            "textDocument": {
                "uri": f"file://{file_path}",
                "languageId": "cpp",
                "version": 1,
                "text": content
            }
        }

        self._print_verbose(f"📂 Opening file to trigger indexing: {file_path.name}")
        self._send_notification("textDocument/didOpen", did_open_params)
        return True

    def ensure_all_files_indexed(self):
        """Open files from compile_commands.json one at a time to ensure complete indexing"""
        if not self.compile_commands_files:
            return True

        unprocessed_files = []
        for file_path in self.compile_commands_files:
            if file_path not in self.processed_compile_files:
                unprocessed_files.append(file_path)
        
        if not unprocessed_files:
            print(f"✅ All {len(self.compile_commands_files)} files have already been processed!")
            return True
        
        # Clear any lingering progress display before starting sequential processing
        if not self.verbose:
            print()  # Clear the progress line completely
        
        print(f"📂 Opening {len(unprocessed_files)} remaining files one at a time to ensure complete indexing...")
        
        files_opened = 0
        max_wait_per_file = 15  # 15 seconds timeout per file
        
        for file_path in sorted(unprocessed_files):
            files_opened += 1
            progress = f"({files_opened}/{len(unprocessed_files)})"
            
            if self.verbose:
                print(f"   {progress} Opening {file_path.name} and waiting for processing...")
            else:
                print(f"   {progress} Opening {file_path.name}...", end='', flush=True)
            
            # Open the file
            if not self.open_file_for_indexing(file_path):
                if not self.verbose:
                    print(" ❌ Failed to open")
                else:
                    print(f"   ❌ Failed to open {file_path.name}")
                continue
            
            # Wait for this specific file to be processed
            wait_start = time.time()
            file_processed = False
            
            while time.time() - wait_start < max_wait_per_file:
                time.sleep(0.3)
                
                # Check if this file has been processed
                if file_path in self.processed_compile_files:
                    file_processed = True
                    if not self.verbose:
                        print("")
                    else:
                        print(f"{file_path.name} processed successfully")
                    break
                
                # Check for ongoing activity - extend timeout if there's activity
                if time.time() - self.last_indexing_activity < 2:
                    # Reset timeout if there's recent activity
                    wait_start = time.time()
                    if not self.verbose:
                        print(".", end='', flush=True)  # Show activity dots
            
            if not file_processed:
                if not self.verbose:
                    print(" ⏰ timeout")
                else:
                    print(f"   ⏰ Timeout waiting for {file_path.name} to be processed")
        
        # Final status check
        final_processed = len(self.processed_compile_files)
        total_count = len(self.compile_commands_files)
        
        if final_processed == total_count:
            print(f"✅ All {total_count} files have been processed by clangd!")
            return True
        else:
            remaining = total_count - final_processed
            print(f"⚠️  {remaining} files could not be processed:")
            
            unprocessed = []
            for file_path in self.compile_commands_files:
                if file_path not in self.processed_compile_files:
                    unprocessed.append(file_path.name)
            
            if self.verbose or len(unprocessed) <= 10:
                for filename in sorted(unprocessed):
                    print(f"   - {filename}")
            else:
                for filename in sorted(unprocessed[:5]):
                    print(f"   - {filename}")
                print(f"   ... and {len(unprocessed) - 5} more (use --verbose for full list)")
            
            return False

    def wait_for_indexing_completion(self):
        """Wait for clangd to finish indexing (no timeout)"""
        print("👀 Waiting for indexing to complete...")
        print("   Use Ctrl+C to interrupt if needed")
        print()

        # Wait for completion signals or process end
        start_time = time.time()
        last_progress_time = time.time()

        while self.process and self.process.poll() is None:
            time.sleep(1)
            current_time = time.time()

            # Primary completion signal: LSP progress "end"
            if self.indexing_complete:
                print("🎯 Primary signal: LSP progress indicates indexing complete")
                # Give a bit more time for final file indexing messages
                time.sleep(2)
                break

            # Fallback 1: No indexing activity for extended period
            if (self.indexed_files and
                    current_time - self.last_indexing_activity > 45):
                print("🔄 No indexing activity for 45 seconds, assuming completion")
                break

            # Fallback 2: Reasonable time limit (10 minutes max)
            if current_time - start_time > 600:
                print("⏰ Maximum indexing time reached (10 minutes), stopping")
                break

        # Clear any in-place progress display before final messages
        if not self.verbose:
            print()  # Ensure clean line after progress updates
        
        # Determine completion status
        if self.indexing_complete:
            print("✅ Initial indexing completed successfully!")
            # Now check coverage and ensure all files are indexed
            print("\n🔍 Checking compile commands coverage...")
            if not self.ensure_all_files_indexed():
                print("⚠️  Not all files could be indexed completely")
        elif self.process and self.process.poll() is not None:
            print("⚠️  clangd process ended")
        else:
            print("🔄 Indexing monitoring stopped")

        # Final summary with detailed analysis
        if self.compile_commands_files:
            processed_from_compile = len(self.processed_compile_files)
            total_compile = len(self.compile_commands_files)
            final_percentage = (processed_from_compile /
                                total_compile) * 100 if total_compile > 0 else 0
            
            print(f"\n📊 FINAL SUMMARY")
            print("=" * 40)
            print(f"📋 Files in compile_commands.json: {total_compile}")
            print(f"✅ Files processed by clangd: {processed_from_compile}")
            print(f"📊 Coverage: {final_percentage:.1f}%")

            # Show which files were/weren't processed only in verbose mode or if there are missing files
            if processed_from_compile < total_compile:
                missing_files = set()
                for file_path in self.compile_commands_files:
                    if file_path not in self.processed_compile_files:
                        missing_files.add(file_path.name)
                
                print(f"❓ Files not processed: {len(missing_files)}")
                if self.verbose or len(missing_files) <= 5:
                    for filename in sorted(missing_files):
                        print(f"   - {filename}")
                elif len(missing_files) > 5:
                    for filename in sorted(list(missing_files)[:3]):
                        print(f"   - {filename}")
                    print(f"   ... and {len(missing_files) - 3} more (use --verbose to see all)")

            # Also show total indexed files (including headers) in verbose mode
            if self.verbose:
                print(f"\n📁 Total files indexed (including headers): {len(self.indexed_files)}")
                if len(self.indexed_files) <= 20:
                    print(f"   Files: {', '.join(sorted(self.indexed_files))}")
                else:
                    sample_files = sorted(list(self.indexed_files))[:10]
                    print(f"   Sample: {', '.join(sample_files)} ... and {len(self.indexed_files) - 10} more")

        # Additional diagnostics only if no files were indexed at all
        if not self.indexed_files and self.verbose:
            print("⚠️  Warning: No individual file indexing detected in logs")
            print("   This might indicate:")
            print("   - Files were indexed but not logged with expected format")
            print("   - Indexing completed too quickly to capture")
            print("   - clangd used cached index data")

        # Report indexing failures and issues only if there are errors or in verbose mode
        has_issues = (self.files_with_errors or self.diagnostic_errors > 0 or 
                     self.diagnostic_warnings > 0 or self.lsp_errors > 0)
        
        if has_issues or self.verbose:
            print("\n📊 INDEXING ISSUES SUMMARY")
            print("=" * 40)

            if self.files_with_errors:
                print(f"❌ Files with errors: {len(self.files_with_errors)}")
                if self.verbose:
                    for filename, error_count in sorted(self.files_with_errors.items()):
                        print(f"{filename}: {error_count} error(s)")
                elif len(self.files_with_errors) <= 3:
                    for filename, error_count in sorted(self.files_with_errors.items()):
                        print(f"{filename}: {error_count} error(s)")
                else:
                    # Show first 3 files with errors
                    for filename, error_count in sorted(list(self.files_with_errors.items())[:3]):
                        print(f"{filename}: {error_count} error(s)")
                    print(f"   ... and {len(self.files_with_errors) - 3} more (use --verbose for details)")
            else:
                print("✅ No files with compile errors detected")

            if self.diagnostic_errors > 0:
                print(f"❌ Total diagnostic errors: {self.diagnostic_errors}")
            elif self.verbose:
                print("✅ No diagnostic errors reported")

            if self.diagnostic_warnings > 0:
                print(f"⚠️  Total diagnostic warnings: {self.diagnostic_warnings}")
            elif self.verbose:
                print("✅ No diagnostic warnings reported")

            if self.lsp_errors > 0:
                print(f"❌ LSP protocol errors: {self.lsp_errors}")
            elif self.verbose:
                print("✅ No LSP protocol errors")

            # Calculate success rate
            failed_files = set(self.files_with_errors.keys())
            successful_files = self.indexed_files - failed_files

            if self.compile_commands_files and self.verbose:
                success_rate = (len(successful_files & self.compile_commands_files_by_name) /
                                len(self.compile_commands_files)) * 100
                print(f"\n🎯 Overall success rate: {success_rate:.1f}% "
                      f"({len(successful_files & self.compile_commands_files_by_name)}/"
                      f"{len(self.compile_commands_files)} files)")

            print("=" * 40)

    def shutdown(self):
        """Shutdown clangd gracefully"""
        if self.writer:
            print("🛑 Shutting down clangd...")
            self._send_request("shutdown")
            time.sleep(1)
            self._send_notification("exit")

        if self.process:
            self.process.terminate()
            try:
                self.process.wait(timeout=5)
            except subprocess.TimeoutExpired:
                self.process.kill()

        # Close log file if it was opened
        if self.log_file_handle:
            try:
                self.log_file_handle.close()
                print(f"📝 Clangd log saved to: {self.log_file}")
            except Exception as e:
                print(f"⚠️  Warning: Error closing log file: {e}")

    def load_compile_commands_info(self):
        """Load files from compile_commands.json for information and reporting"""
        compile_commands = self.build_directory / "compile_commands.json"
        if not compile_commands.exists():
            raise FileNotFoundError(
                f"No compile_commands.json found in {self.build_directory}")

        try:
            with open(compile_commands, 'r') as f:
                commands = json.load(f)

            # Track seen files to avoid duplicates - use the first occurrence
            seen_files = set()
            missing_files = []
            
            for cmd in commands:
                if 'file' in cmd:
                    # Resolve relative paths using the directory field
                    directory = Path(cmd.get('directory', '.')).resolve()
                    file_path = Path(cmd['file'])
                    if not file_path.is_absolute():
                        file_path = (directory / file_path).resolve()
                    else:
                        file_path = file_path.resolve()
                    
                    # Only add if we haven't seen this absolute path before (first occurrence wins)
                    if file_path not in seen_files:
                        seen_files.add(file_path)
                        
                        # Check if file exists
                        if file_path.exists():
                            self.compile_commands_files.add(file_path)  # Store resolved absolute Path object
                            self.compile_commands_files_by_name.add(file_path.name)  # Store filename for backward compatibility
                        else:
                            missing_files.append(file_path)

            total_files = len(self.compile_commands_files)
            print(f"📋 Found {total_files} files in compile_commands.json")
            if self.verbose:
                print(f"   Files: {', '.join(sorted(self.compile_commands_files_by_name))}")
            else:
                print(f"   Use --verbose to see file list")
            
            # Warn about missing files
            if missing_files:
                print(f"⚠️  Warning: {len(missing_files)} files from compile_commands.json do not exist:")
                if self.verbose or len(missing_files) <= 5:
                    for missing_file in missing_files:
                        print(f"   - {missing_file}")
                else:
                    for missing_file in missing_files[:3]:
                        print(f"   - {missing_file}")
                    print(f"   ... and {len(missing_files) - 3} more (use --verbose for full list)")

        except Exception as e:
            print(f"❌ Error reading compile_commands.json: {e}")
            raise


def main():
    parser = argparse.ArgumentParser(description="Generate clangd index")
    parser.add_argument("build_directory",
                        help="Build directory with compile_commands.json")
    parser.add_argument("--refresh-index", action="store_true",
                        help="Clean cache before indexing")
    parser.add_argument("--clangd-path", default="clangd",
                        help="Path to clangd executable (default: "
                        "CLANGD_PATH env var, then /usr/bin/clangd)")
    parser.add_argument("--log-file",
                        help="Save all clangd logs to specified file for "
                        "investigation (optional)")
    parser.add_argument("--verbose", action="store_true",
                        help="Show detailed clangd logs and progress messages")

    args = parser.parse_args()

    if not Path(args.build_directory).exists():
        print(f"Error: Build directory {args.build_directory} does not exist")
        sys.exit(1)

    # Determine clangd path with priority order
    clangd_path = None

    # 1. Command-line argument (highest priority)
    if args.clangd_path != "clangd":  # Only if user explicitly provided a path
        clangd_path = args.clangd_path
        print(f"Using clangd from command line: {clangd_path}")

    # 2. Environment variable
    elif "CLANGD_PATH" in os.environ:
        clangd_path = os.environ["CLANGD_PATH"]
        print(f"Using clangd from CLANGD_PATH env var: {clangd_path}")

    # 3. Fallback to /usr/bin/clangd
    elif Path("/usr/bin/clangd").exists():
        clangd_path = "/usr/bin/clangd"
        print(f"Using fallback clangd: {clangd_path}")

    # 4. Error if none found
    else:
        print("Error: clangd not found.")
        print("Please either:")
        print("  1. Use --clangd-path to specify the path")
        print("  2. Set CLANGD_PATH environment variable")
        print("  3. Install clangd at /usr/bin/clangd")
        sys.exit(1)

    # Verify the clangd path works
    try:
        subprocess.run([clangd_path, "--version"],
                       capture_output=True, check=True)
    except (subprocess.CalledProcessError, FileNotFoundError):
        print(f"Error: clangd not found or not executable at '{clangd_path}'")
        sys.exit(1)

    generator = ClangdIndexGenerator(
        args.build_directory, clangd_path, args.refresh_index, args.log_file, args.verbose)

    try:
        print("=" * 60)
        print("🎯 clangd Index Generator")
        print("=" * 60)

        generator.load_compile_commands_info()  # Load files for information
        generator.clean_cache()  # Clean cache only if --refresh-index
        generator.start_clangd()
        time.sleep(1)

        generator.initialize_lsp()
        time.sleep(2)

        generator.trigger_indexing_by_opening_file()

        # Wait for indexing to complete (no timeout)
        generator.wait_for_indexing_completion()

    except KeyboardInterrupt:
        print("\n🛑 Interrupted by user")
    except Exception as e:
        print(f"❌ Error: {e}")
    finally:
        generator.shutdown()
        print("👋 Done!")


if __name__ == "__main__":
    main()