| 1 | // LSP Test File for Rust (rust-analyzer) |
| 2 | // Tests code completion, hover, and diagnostics |
| 3 | // |
| 4 | // Required: rustup component add rust-analyzer |
| 5 | |
| 6 | use std::collections::HashMap; |
| 7 | use std::io::{self, Write}; |
| 8 | |
| 9 | /// Employee struct to test hover and completion |
| 10 | #[derive(Debug, Clone)] |
| 11 | struct Employee { |
| 12 | name: String, |
| 13 | age: u32, |
| 14 | salary: f64, |
| 15 | } |
| 16 | |
| 17 | impl Employee { |
| 18 | /// Creates a new Employee |
| 19 | fn new(name: String, age: u32, salary: f64) -> Self { |
| 20 | Employee { name, age, salary } |
| 21 | } |
| 22 | |
| 23 | /// Returns employee information as a string |
| 24 | fn get_info(&self) -> String { |
| 25 | format!("{}, {} years, ${:.2}", self.name, self.age, self.salary) |
| 26 | } |
| 27 | |
| 28 | /// Increases salary by given percentage |
| 29 | fn give_raise(&mut self, percentage: f64) { |
| 30 | self.salary *= 1.0 + percentage / 100.0; |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | /// Process employee data and return statistics |
| 35 | fn process_employees(employees: &[Employee]) -> HashMap<&str, f64> { |
| 36 | let mut stats = HashMap::new(); |
| 37 | |
| 38 | if employees.is_empty() { |
| 39 | stats.insert("average", 0.0); |
| 40 | stats.insert("total", 0.0); |
| 41 | return stats; |
| 42 | } |
| 43 | |
| 44 | let total: f64 = employees.iter().map(|emp| emp.salary).sum(); |
| 45 | |
| 46 | // Test completion: Type 'emp.' and press Ctrl+Space |
| 47 | // Should show: name, age, salary, get_info, give_raise, clone |
| 48 | let emp = &employees[0]; |
| 49 | emp. |
| 50 | |
| 51 | stats.insert("average", total / employees.len() as f64); |
| 52 | stats.insert("total", total); |
| 53 | stats.insert("count", employees.len() as f64); |
| 54 | |
| 55 | stats |
| 56 | } |
| 57 | |
| 58 | fn main() -> io::Result<()> { |
| 59 | // Test hover: Position cursor on 'Employee' and press Ctrl+H |
| 60 | // Should show struct documentation and fields |
| 61 | let mut employees = vec![ |
| 62 | Employee::new("Alice".to_string(), 30, 75000.0), |
| 63 | Employee::new("Bob".to_string(), 35, 85000.0), |
| 64 | Employee::new("Charlie".to_string(), 28, 65000.0), |
| 65 | ]; |
| 66 | |
| 67 | // Test completion: Type 'Vec::' and press Ctrl+Space |
| 68 | // Should show Vec associated functions |
| 69 | let numbers = Vec:: |
| 70 | |
| 71 | // Test completion: Type 'String::' and press Ctrl+Space |
| 72 | // Should show String associated functions |
| 73 | let text = String:: |
| 74 | |
| 75 | let stats = process_employees(&employees); |
| 76 | println!("Statistics: {:?}", stats); |
| 77 | |
| 78 | // Test hover on standard library types |
| 79 | let mut output = io::stdout(); |
| 80 | writeln!(output, "Employee data processed")?; |
| 81 | |
| 82 | // Intentional error for diagnostics test |
| 83 | // Should show: cannot find value `undefined_var` in this scope |
| 84 | println!("{}", undefined_var); |
| 85 | |
| 86 | Ok(()) |
| 87 | } |