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
//! File buffer with UTF-8 positioning and encoding detection
//!
//! Provides efficient file content management with UTF-8 code point positioning,
//! automatic change detection, and comprehensive encoding support.
#![allow(dead_code)]

use crate::io::file_system::FileSystemTrait;
use sha2::{Digest, Sha256};
use std::path::{Path, PathBuf};
use std::time::SystemTime;

// ============================================================================
// File Position
// ============================================================================

/// Represents a position in a file using 0-based line and column coordinates
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FilePosition {
    pub line: u32,
    pub column: u32,
}

impl FilePosition {
    pub fn new(line: u32, column: u32) -> Self {
        Self { line, column }
    }
}

// ============================================================================
// File Buffer Errors
// ============================================================================

/// File buffer operation errors
#[derive(Debug, thiserror::Error)]
pub enum FileBufferError {
    #[error("Position out of bounds: {pos:?}")]
    PositionOutOfBounds { pos: FilePosition },

    #[error("Invalid range: start {start:?} after end {end:?}")]
    InvalidRange {
        start: FilePosition,
        end: FilePosition,
    },

    #[error("Unsupported encoding in file: {0}")]
    UnsupportedEncoding(String),

    #[error("Invalid UTF-8 sequence: {0}")]
    InvalidUtf8(#[from] std::string::FromUtf8Error),

    #[error("File operation failed: {0}")]
    Io(#[from] std::io::Error),
}

// ============================================================================
// File Buffer
// ============================================================================

/// File buffer with UTF-8 positioning and automatic refresh detection
#[derive(Debug)]
pub struct FileBuffer<F: FileSystemTrait> {
    /// UTF-8 normalized content
    content: String,
    /// Line start positions in UTF-8 code point offsets
    line_starts: Vec<usize>,
    /// File modification time for refresh detection
    last_modified: SystemTime,
    /// Content hash for change detection
    content_hash: String,
    /// File path for reload operations
    path: PathBuf,
    /// Filesystem instance for operations
    filesystem: F,
}

impl<F: FileSystemTrait> FileBuffer<F> {
    /// Create buffer with provided filesystem instance
    pub fn new_with_filesystem(
        path: impl AsRef<Path>,
        filesystem: F,
    ) -> Result<Self, FileBufferError> {
        let path = path.as_ref().to_path_buf();
        let metadata = filesystem.metadata(&path)?;
        let last_modified = metadata.modified;

        let bytes = filesystem.read(&path)?;
        let content = Self::normalize_encoding(&bytes)?;
        let line_starts = Self::build_line_index(&content);
        let content_hash = Self::compute_hash(&content);

        Ok(Self {
            content,
            line_starts,
            last_modified,
            content_hash,
            path,
            filesystem,
        })
    }

    /// Extract text between two positions (inclusive start, exclusive end)
    ///
    /// Automatically refreshes content if file has been modified.
    /// Uses 0-based UTF-8 code point positioning for proper international text handling.
    pub fn text_between(
        &mut self,
        start: FilePosition,
        end: FilePosition,
    ) -> Result<String, FileBufferError> {
        // Check for file changes and refresh if needed using stored filesystem
        self.refresh_if_changed()?;

        use tracing::info;

        info!(
            "Extracting text from {:?} to {:?} in file {}",
            start,
            end,
            self.path.display()
        );

        // Validate position order
        if start.line > end.line || (start.line == end.line && start.column > end.column) {
            return Err(FileBufferError::InvalidRange { start, end });
        }

        let start_offset = self.position_to_offset(start)?;
        let end_offset = self.position_to_offset(end)?;

        Ok(self.content[start_offset..end_offset].to_string())
    }

    /// Extract the full line at the given line number (0-based), trimmed on both ends
    ///
    /// Automatically refreshes content if file has been modified.
    pub fn get_line(&mut self, line_number: u32) -> Result<String, FileBufferError> {
        // Check for file changes and refresh if needed using stored filesystem
        self.refresh_if_changed()?;

        let line_index = line_number as usize;

        // Check if line number is valid
        if line_index >= self.line_starts.len() {
            return Err(FileBufferError::PositionOutOfBounds {
                pos: FilePosition::new(line_number, 0),
            });
        }

        let line_start = self.line_starts[line_index];
        let line_end = if line_index + 1 < self.line_starts.len() {
            // Not the last line - exclude the newline character
            self.line_starts[line_index + 1].saturating_sub(1)
        } else {
            // Last line - go to end of content
            self.content.len()
        };

        let line_content = &self.content[line_start..line_end];
        Ok(line_content.trim().to_string())
    }

    // ========================================================================
    // Internal Methods
    // ========================================================================

    /// Refresh file content if it has been modified
    fn refresh_if_changed(&mut self) -> Result<(), FileBufferError> {
        // Skip refresh for test paths that don't exist on filesystem
        if !self.filesystem.exists(&self.path) {
            return Ok(());
        }

        let metadata = self.filesystem.metadata(&self.path)?;
        let current_modified = metadata.modified;

        if current_modified != self.last_modified {
            let bytes = self.filesystem.read(&self.path)?;
            let new_content = Self::normalize_encoding(&bytes)?;
            let new_hash = Self::compute_hash(&new_content);

            // Only update if content actually changed
            if new_hash != self.content_hash {
                self.content = new_content;
                self.line_starts = Self::build_line_index(&self.content);
                self.content_hash = new_hash;
            }

            self.last_modified = current_modified;
        }

        Ok(())
    }

    /// Convert file position to byte offset in UTF-8 content
    fn position_to_offset(&self, pos: FilePosition) -> Result<usize, FileBufferError> {
        use tracing::error;

        // Positions are already 0-based
        let line_index = pos.line as usize;
        let column_index = pos.column as usize;

        // Check line bounds
        if line_index >= self.line_starts.len() {
            error!(
                "File position out of bounds: {:?}, line count: {}",
                pos,
                self.line_starts.len()
            );
            return Err(FileBufferError::PositionOutOfBounds { pos });
        }

        let line_start = self.line_starts[line_index];

        // For the last line, calculate end using content length
        let line_end = if line_index + 1 < self.line_starts.len() {
            self.line_starts[line_index + 1] - 1 // Exclude newline character
        } else {
            self.content.len()
        };

        // Calculate target position in UTF-8 code points
        let line_content = &self.content[line_start..line_end];
        let chars: Vec<char> = line_content.chars().collect();

        if column_index > chars.len() {
            error!(
                "File position out of bounds: {:?}, chars count: {}",
                pos,
                chars.len()
            );
            return Err(FileBufferError::PositionOutOfBounds { pos });
        }

        // Calculate byte offset for the column position
        let column_offset: usize = chars.iter().take(column_index).map(|c| c.len_utf8()).sum();

        Ok(line_start + column_offset)
    }

    /// Build line start index for efficient line-based operations
    fn build_line_index(content: &str) -> Vec<usize> {
        let mut line_starts = vec![0];

        for (i, ch) in content.char_indices() {
            if ch == '\n' {
                line_starts.push(i + ch.len_utf8());
            }
        }

        line_starts
    }

    /// Normalize encoding and line endings
    fn normalize_encoding(bytes: &[u8]) -> Result<String, FileBufferError> {
        // Handle UTF-8 BOM
        let content_bytes = if bytes.starts_with(&[0xEF, 0xBB, 0xBF]) {
            &bytes[3..]
        } else {
            bytes
        };

        // Convert to UTF-8 string
        let mut content = String::from_utf8(content_bytes.to_vec())?;

        // Normalize line endings to \n
        content = content.replace("\r\n", "\n").replace('\r', "\n");

        Ok(content)
    }

    /// Compute SHA256 hash of content for change detection
    fn compute_hash(content: &str) -> String {
        let mut hasher = Sha256::new();
        hasher.update(content.as_bytes());
        format!("{:x}", hasher.finalize())
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::io::file_system::{FileMetadata, MockFileSystemTrait, TestFileSystem};
    use std::time::{Duration, UNIX_EPOCH};
    use tempfile::tempdir;

    // Auto-initialize logging for all tests in this module
    #[cfg(feature = "test-logging")]
    #[ctor::ctor]
    fn init_test_logging() {
        crate::test_utils::logging::init();
    }

    #[test]
    fn test_file_position() {
        let pos = FilePosition::new(5, 10);
        assert_eq!(pos.line, 5);
        assert_eq!(pos.column, 10);
    }

    #[test]
    fn test_line_index_building() {
        let content = "Hello\nWorld\n\nTest";
        let line_starts = FileBuffer::<TestFileSystem>::build_line_index(content);
        assert_eq!(line_starts, vec![0, 6, 12, 13]);
    }

    #[test]
    fn test_file_buffer_utf8_positioning() {
        let filesystem = TestFileSystem::new();
        let test_path = PathBuf::from("/test/utf8.txt");
        let content = "Hello 🌍\nWorld 🚀\nTest 📝";
        let time = UNIX_EPOCH + Duration::from_secs(1000);

        filesystem.set_file_content(&test_path, content, time);

        let mut buffer = FileBuffer::new_with_filesystem(&test_path, filesystem).unwrap();

        // Extract text across UTF-8 emoji boundaries
        let start = FilePosition::new(0, 7); // After the emoji (end of line 0)
        let end = FilePosition::new(1, 5); // Up to "World"
        let result = buffer.text_between(start, end).unwrap();
        assert_eq!(result, "\nWorld");

        // Extract single emoji character
        let start = FilePosition::new(0, 6);
        let end = FilePosition::new(0, 7);
        let result = buffer.text_between(start, end).unwrap();
        assert_eq!(result, "🌍");
    }

    #[test]
    fn test_complex_utf8_text_extraction() {
        let filesystem = TestFileSystem::new();
        let test_path = PathBuf::from("/test/complex.txt");
        let content = "Hello 🌍\nWorld 🚀\nTest 📝";
        let time = UNIX_EPOCH + Duration::from_secs(1000);

        filesystem.set_file_content(&test_path, content, time);

        let mut buffer = FileBuffer::new_with_filesystem(&test_path, filesystem).unwrap();

        // Extract ASCII text
        let start = FilePosition::new(0, 0);
        let end = FilePosition::new(0, 5);
        let result = buffer.text_between(start, end).unwrap();
        assert_eq!(result, "Hello");

        // Extract text with Unicode characters
        let start = FilePosition::new(1, 0);
        let end = FilePosition::new(1, 5);
        let result = buffer.text_between(start, end).unwrap();
        assert_eq!(result, "World");

        // Extract emoji
        let start = FilePosition::new(2, 5);
        let end = FilePosition::new(2, 6);
        let result = buffer.text_between(start, end).unwrap();
        assert_eq!(result, "📝");
    }

    #[test]
    fn test_encoding_detection_utf8_bom() {
        let content_with_bom = [0xEF, 0xBB, 0xBF, b'H', b'e', b'l', b'l', b'o'];
        let normalized =
            FileBuffer::<TestFileSystem>::normalize_encoding(&content_with_bom).unwrap();
        assert_eq!(normalized, "Hello");
    }

    #[test]
    fn test_line_ending_normalization() {
        let content_crlf = b"Line1\r\nLine2\rLine3\nLine4";
        let normalized = FileBuffer::<TestFileSystem>::normalize_encoding(content_crlf).unwrap();
        assert_eq!(normalized, "Line1\nLine2\nLine3\nLine4");
    }

    #[test]
    fn test_content_hash_consistency() {
        let content1 = "Same content";
        let content2 = "Same content";
        let content3 = "Different content";

        let hash1 = FileBuffer::<TestFileSystem>::compute_hash(content1);
        let hash2 = FileBuffer::<TestFileSystem>::compute_hash(content2);
        let hash3 = FileBuffer::<TestFileSystem>::compute_hash(content3);

        assert_eq!(hash1, hash2);
        assert_ne!(hash1, hash3);
    }

    #[test]
    fn test_file_buffer_with_real_filesystem() {
        let temp_dir = tempdir().unwrap();
        let file_path = temp_dir.path().join("test.txt");
        let content = "Real file content\nSecond line\nThird line";
        std::fs::write(&file_path, content).unwrap();

        let mut buffer =
            FileBuffer::new_with_filesystem(&file_path, crate::io::file_system::RealFileSystem)
                .unwrap();

        // Test text extraction
        let start = FilePosition::new(0, 0);
        let end = FilePosition::new(0, 4);
        let result = buffer.text_between(start, end).unwrap();
        assert_eq!(result, "Real");

        // Test second line
        let start = FilePosition::new(1, 0);
        let end = FilePosition::new(1, 6);
        let result = buffer.text_between(start, end).unwrap();
        assert_eq!(result, "Second");
    }

    #[test]
    fn test_position_out_of_bounds_error() {
        let filesystem = TestFileSystem::new();
        let test_path = PathBuf::from("/test/bounds.txt");
        let content = "Short\nFile";
        let time = UNIX_EPOCH + Duration::from_secs(1000);

        filesystem.set_file_content(&test_path, content, time);

        let mut buffer = FileBuffer::new_with_filesystem(&test_path, filesystem).unwrap();

        // Test line out of bounds
        let start = FilePosition::new(4, 0);
        let end = FilePosition::new(4, 4);
        let result = buffer.text_between(start, end);
        assert!(matches!(
            result,
            Err(FileBufferError::PositionOutOfBounds { .. })
        ));

        // Test column out of bounds
        let start = FilePosition::new(0, 19);
        let end = FilePosition::new(0, 24);
        let result = buffer.text_between(start, end);
        assert!(matches!(
            result,
            Err(FileBufferError::PositionOutOfBounds { .. })
        ));
    }

    #[test]
    fn test_invalid_range_error() {
        let filesystem = TestFileSystem::new();
        let test_path = PathBuf::from("/test/range.txt");
        let content = "Test content for range validation";
        let time = UNIX_EPOCH + Duration::from_secs(1000);

        filesystem.set_file_content(&test_path, content, time);

        let mut buffer = FileBuffer::new_with_filesystem(&test_path, filesystem).unwrap();

        // Test start after end
        let start = FilePosition::new(0, 9);
        let end = FilePosition::new(0, 4);
        let result = buffer.text_between(start, end);
        assert!(matches!(result, Err(FileBufferError::InvalidRange { .. })));
    }

    #[test]
    fn test_text_extraction_edge_cases() {
        let filesystem = TestFileSystem::new();
        let test_path = PathBuf::from("/test/edge.txt");
        let content = "A\n\nC";
        let time = UNIX_EPOCH + Duration::from_secs(1000);

        filesystem.set_file_content(&test_path, content, time);

        let mut buffer = FileBuffer::new_with_filesystem(&test_path, filesystem).unwrap();

        // Test empty line
        let start = FilePosition::new(1, 0);
        let end = FilePosition::new(1, 0);
        let result = buffer.text_between(start, end).unwrap();
        assert_eq!(result, "");

        // Test single character lines
        let start = FilePosition::new(0, 0);
        let end = FilePosition::new(0, 1);
        let result = buffer.text_between(start, end).unwrap();
        assert_eq!(result, "A");

        let start = FilePosition::new(2, 0);
        let end = FilePosition::new(2, 1);
        let result = buffer.text_between(start, end).unwrap();
        assert_eq!(result, "C");
    }

    #[test]
    fn test_file_modification_detection_with_test_filesystem() {
        let filesystem = TestFileSystem::new();
        let test_path = PathBuf::from("/test/changeable.txt");
        let initial_time = UNIX_EPOCH + Duration::from_secs(1000);
        let later_time = UNIX_EPOCH + Duration::from_secs(2000);
        let initial_content = "Initial content\nLine 2";
        let updated_content = "Updated content\nNew line 2";

        filesystem.set_file_content(&test_path, initial_content, initial_time);

        let mut buffer = FileBuffer::new_with_filesystem(&test_path, filesystem.clone()).unwrap();

        // Initial state - should work without refresh
        let start = FilePosition::new(0, 0);
        let end = FilePosition::new(0, 7);
        let result = buffer.text_between(start, end).unwrap();
        assert_eq!(result, "Initial");

        // Simulate file modification
        filesystem.update_file_content(&test_path, updated_content, later_time);

        // Next text_between call should detect change and refresh
        let result = buffer.text_between(start, end).unwrap();
        assert_eq!(result, "Updated"); // Content should be refreshed

        // Verify the buffer's internal state was updated
        assert_eq!(buffer.last_modified, later_time);
        assert_eq!(
            buffer.content_hash,
            FileBuffer::<TestFileSystem>::compute_hash(updated_content)
        );
    }

    #[test]
    fn test_metadata_mock_scenarios() {
        let mut mock_fs = MockFileSystemTrait::new();
        let test_path = PathBuf::from("/mock/controlled.txt");

        // Scenario: Precise time control
        let time1 = UNIX_EPOCH + Duration::from_millis(1234567890);
        let time2 = UNIX_EPOCH + Duration::from_millis(1234567891); // 1ms later

        mock_fs
            .expect_exists()
            .with(mockall::predicate::eq(test_path.clone()))
            .returning(|_| true)
            .times(1);

        // When refresh_if_changed() calls metadata, return the newer time to trigger refresh
        mock_fs
            .expect_metadata()
            .with(mockall::predicate::eq(test_path.clone()))
            .returning(move |_| Ok(FileMetadata::new(time2, 105))) // Return newer time
            .times(1);

        mock_fs
            .expect_read()
            .with(mockall::predicate::eq(test_path.clone()))
            .returning(|_| Ok(b"Updated content".to_vec()))
            .times(1);

        // Create buffer with mock, starting with older time
        let mut buffer = FileBuffer::<MockFileSystemTrait> {
            content: "Original content".to_string(),
            line_starts: vec![0],
            last_modified: time1, // Buffer starts with older time
            content_hash: FileBuffer::<MockFileSystemTrait>::compute_hash("Original content"),
            path: test_path,
            filesystem: mock_fs,
        };

        // This should trigger refresh due to time difference (time1 vs time2)
        let start = FilePosition::new(0, 0);
        let end = FilePosition::new(0, 7);
        let result = buffer.text_between(start, end).unwrap();
        assert_eq!(result, "Updated");
    }

    #[test]
    fn test_edge_case_empty_file() {
        let filesystem = TestFileSystem::new();
        let test_path = PathBuf::from("/test/empty.txt");
        let time = UNIX_EPOCH + Duration::from_secs(1000);

        filesystem.set_file_content(&test_path, "", time);

        let mut buffer = FileBuffer::new_with_filesystem(&test_path, filesystem).unwrap();

        // Test extracting from empty file
        let start = FilePosition::new(0, 0);
        let end = FilePosition::new(0, 0);
        let result = buffer.text_between(start, end).unwrap();
        assert_eq!(result, "");
    }

    #[test]
    fn test_single_character_file() {
        let filesystem = TestFileSystem::new();
        let test_path = PathBuf::from("/test/single.txt");
        let time = UNIX_EPOCH + Duration::from_secs(1000);

        filesystem.set_file_content(&test_path, "X", time);

        let mut buffer = FileBuffer::new_with_filesystem(&test_path, filesystem).unwrap();

        let start = FilePosition::new(0, 0);
        let end = FilePosition::new(0, 1);
        let result = buffer.text_between(start, end).unwrap();
        assert_eq!(result, "X");
    }
}