summer-lsp 0.5.0

Language Server Protocol implementation for summer-rs framework
Documentation
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
//! 配置扫描器模块
//!
//! 扫描项目中所有带有 `#[derive(Configurable)]` 的配置结构体

use crate::Result;
use lsp_types::{Location, Position, Range, Url};
use serde::{Deserialize, Serialize};
use std::path::Path;
use syn::spanned::Spanned;
use walkdir::WalkDir;

/// 配置扫描请求
#[derive(Debug, Deserialize)]
pub struct ConfigurationsRequest {
    /// 应用路径
    #[serde(rename = "appPath")]
    pub app_path: String,
}

/// 配置扫描响应
#[derive(Debug, Serialize)]
pub struct ConfigurationsResponse {
    /// 配置结构列表
    pub configurations: Vec<ConfigurationStruct>,
}

/// 配置结构信息
#[derive(Debug, Clone, Serialize)]
pub struct ConfigurationStruct {
    /// 结构体名称
    pub name: String,
    /// 配置前缀(从 #[config_prefix = "..."] 提取)
    pub prefix: String,
    /// 字段列表
    pub fields: Vec<ConfigField>,
    /// 定义位置
    #[serde(skip_serializing_if = "Option::is_none")]
    pub location: Option<Location>,
}

/// 配置字段信息
#[derive(Debug, Clone, Serialize)]
pub struct ConfigField {
    /// 字段名称
    pub name: String,
    /// 字段类型
    #[serde(rename = "type")]
    pub type_name: String,
    /// 是否可选
    pub optional: bool,
    /// 描述(从文档注释提取)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
}

/// 配置扫描器
pub struct ConfigScanner;

impl ConfigScanner {
    /// 创建新的配置扫描器
    pub fn new() -> Self {
        Self
    }

    /// 扫描项目中的所有配置结构
    ///
    /// # Arguments
    ///
    /// * `project_path` - 项目根目录路径
    ///
    /// # Returns
    ///
    /// 返回找到的所有配置结构列表
    pub fn scan_configurations(&self, project_path: &Path) -> Result<Vec<ConfigurationStruct>> {
        tracing::info!("Scanning configurations in: {:?}", project_path);

        let mut configurations = Vec::new();

        // 遍历项目中的所有 Rust 文件
        for entry in WalkDir::new(project_path)
            .follow_links(true)
            .into_iter()
            .filter_map(|e| e.ok())
        {
            let path = entry.path();

            // 跳过 target 目录
            if path.components().any(|c| c.as_os_str() == "target") {
                continue;
            }

            // 只处理 .rs 文件
            if path.extension().and_then(|s| s.to_str()) != Some("rs") {
                continue;
            }

            // 读取文件内容
            let content = match std::fs::read_to_string(path) {
                Ok(content) => content,
                Err(e) => {
                    tracing::warn!("Failed to read file {:?}: {}", path, e);
                    continue;
                }
            };

            // 解析文件
            let syntax_tree = match syn::parse_file(&content) {
                Ok(tree) => tree,
                Err(e) => {
                    tracing::debug!("Failed to parse file {:?}: {}", path, e);
                    continue;
                }
            };

            // 提取配置结构
            if let Ok(file_configs) = self.extract_configurations_from_file(&syntax_tree, path) {
                configurations.extend(file_configs);
            }
        }

        tracing::info!("Found {} configuration structs", configurations.len());
        Ok(configurations)
    }

    /// 从单个文件中提取配置结构
    fn extract_configurations_from_file(
        &self,
        syntax_tree: &syn::File,
        file_path: &Path,
    ) -> Result<Vec<ConfigurationStruct>> {
        let mut configurations = Vec::new();

        // 遍历所有项
        for item in &syntax_tree.items {
            if let syn::Item::Struct(item_struct) = item {
                // 检查是否有 Configurable derive
                if self.has_configurable_derive(item_struct) {
                    if let Some(config) = self.extract_configuration_struct(item_struct, file_path)
                    {
                        configurations.push(config);
                    }
                }
            }
        }

        Ok(configurations)
    }

    /// 检查结构体是否派生了 Configurable
    fn has_configurable_derive(&self, item_struct: &syn::ItemStruct) -> bool {
        for attr in &item_struct.attrs {
            if attr.path().is_ident("derive") {
                if let Ok(meta_list) = attr.meta.require_list() {
                    let tokens_str = meta_list.tokens.to_string();
                    if tokens_str.contains("Configurable") {
                        return true;
                    }
                }
            }
        }
        false
    }

    /// 提取配置结构信息
    fn extract_configuration_struct(
        &self,
        item_struct: &syn::ItemStruct,
        file_path: &Path,
    ) -> Option<ConfigurationStruct> {
        // 提取配置前缀
        let prefix = self.extract_config_prefix(item_struct)?;

        // 提取字段
        let fields = self.extract_fields(&item_struct.fields);

        // 提取文档注释
        let _doc_comment = self.extract_doc_comment(&item_struct.attrs);

        // 构建位置信息
        let location = self.build_location(item_struct, file_path);

        Some(ConfigurationStruct {
            name: item_struct.ident.to_string(),
            prefix,
            fields,
            location,
        })
    }

    /// 提取配置前缀
    fn extract_config_prefix(&self, item_struct: &syn::ItemStruct) -> Option<String> {
        for attr in &item_struct.attrs {
            if attr.path().is_ident("config_prefix") {
                // 解析 #[config_prefix = "prefix"]
                if let Ok(meta_name_value) = attr.meta.require_name_value() {
                    if let syn::Expr::Lit(expr_lit) = &meta_name_value.value {
                        if let syn::Lit::Str(lit_str) = &expr_lit.lit {
                            return Some(lit_str.value());
                        }
                    }
                }
            }
        }
        None
    }

    /// 提取结构体字段
    fn extract_fields(&self, fields: &syn::Fields) -> Vec<ConfigField> {
        let mut result = Vec::new();

        if let syn::Fields::Named(fields_named) = fields {
            for field in &fields_named.named {
                if let Some(ident) = &field.ident {
                    // 提取字段类型
                    let type_name = self.type_to_string(&field.ty);

                    // 检查是否是 Option<T>
                    let optional = self.is_option_type(&field.ty);

                    // 提取文档注释
                    let description = self.extract_doc_comment(&field.attrs);

                    result.push(ConfigField {
                        name: ident.to_string(),
                        type_name,
                        optional,
                        description,
                    });
                }
            }
        }

        result
    }

    /// 将类型转换为字符串
    fn type_to_string(&self, ty: &syn::Type) -> String {
        match ty {
            syn::Type::Path(type_path) => {
                // 提取类型路径的最后一段
                if let Some(segment) = type_path.path.segments.last() {
                    let ident = segment.ident.to_string();

                    // 处理泛型参数
                    if let syn::PathArguments::AngleBracketed(args) = &segment.arguments {
                        let generic_args: Vec<String> = args
                            .args
                            .iter()
                            .filter_map(|arg| {
                                if let syn::GenericArgument::Type(ty) = arg {
                                    Some(self.type_to_string(ty))
                                } else {
                                    None
                                }
                            })
                            .collect();

                        if !generic_args.is_empty() {
                            return format!("{}<{}>", ident, generic_args.join(", "));
                        }
                    }

                    ident
                } else {
                    "Unknown".to_string()
                }
            }
            syn::Type::Reference(type_ref) => {
                format!("&{}", self.type_to_string(&type_ref.elem))
            }
            syn::Type::Tuple(type_tuple) => {
                let elem_types: Vec<String> = type_tuple
                    .elems
                    .iter()
                    .map(|ty| self.type_to_string(ty))
                    .collect();
                format!("({})", elem_types.join(", "))
            }
            _ => "Unknown".to_string(),
        }
    }

    /// 检查类型是否是 Option<T>
    fn is_option_type(&self, ty: &syn::Type) -> bool {
        if let syn::Type::Path(type_path) = ty {
            if let Some(segment) = type_path.path.segments.last() {
                return segment.ident == "Option";
            }
        }
        false
    }

    /// 提取文档注释
    fn extract_doc_comment(&self, attrs: &[syn::Attribute]) -> Option<String> {
        let mut doc_lines = Vec::new();

        for attr in attrs {
            if attr.path().is_ident("doc") {
                if let Ok(meta_name_value) = attr.meta.require_name_value() {
                    if let syn::Expr::Lit(expr_lit) = &meta_name_value.value {
                        if let syn::Lit::Str(lit_str) = &expr_lit.lit {
                            let line = lit_str.value().trim().to_string();
                            if !line.is_empty() {
                                doc_lines.push(line);
                            }
                        }
                    }
                }
            }
        }

        if doc_lines.is_empty() {
            None
        } else {
            Some(doc_lines.join(" "))
        }
    }

    /// 构建位置信息
    fn build_location(&self, item_struct: &syn::ItemStruct, file_path: &Path) -> Option<Location> {
        // 将文件路径转换为 URI
        let uri = match Url::from_file_path(file_path) {
            Ok(uri) => uri,
            Err(_) => {
                tracing::warn!("Failed to convert path to URI: {:?}", file_path);
                return None;
            }
        };

        // 获取结构体的 span
        let span = item_struct.span();
        let start = span.start();
        let end = span.end();

        // 构建 Range
        let range = Range {
            start: Position {
                line: start.line.saturating_sub(1) as u32, // syn 的行号从 1 开始
                character: start.column as u32,
            },
            end: Position {
                line: end.line.saturating_sub(1) as u32,
                character: end.column as u32,
            },
        };

        Some(Location { uri, range })
    }
}

impl Default for ConfigScanner {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_extract_config_prefix() {
        let code = r#"
            #[derive(Debug, Configurable, Deserialize)]
            #[config_prefix = "database"]
            struct DatabaseConfig {
                host: String,
                port: u16,
            }
        "#;

        let syntax_tree = syn::parse_file(code).unwrap();
        let scanner = ConfigScanner::new();

        if let syn::Item::Struct(item_struct) = &syntax_tree.items[0] {
            let prefix = scanner.extract_config_prefix(item_struct);
            assert_eq!(prefix, Some("database".to_string()));
        } else {
            panic!("Expected struct item");
        }
    }

    #[test]
    fn test_extract_fields() {
        let code = r#"
            struct Config {
                /// Database host
                host: String,
                /// Database port
                port: u16,
                /// Optional timeout
                timeout: Option<u64>,
            }
        "#;

        let syntax_tree = syn::parse_file(code).unwrap();
        let scanner = ConfigScanner::new();

        if let syn::Item::Struct(item_struct) = &syntax_tree.items[0] {
            let fields = scanner.extract_fields(&item_struct.fields);

            assert_eq!(fields.len(), 3);

            assert_eq!(fields[0].name, "host");
            assert_eq!(fields[0].type_name, "String");
            assert!(!fields[0].optional);
            assert_eq!(fields[0].description, Some("Database host".to_string()));

            assert_eq!(fields[1].name, "port");
            assert_eq!(fields[1].type_name, "u16");
            assert!(!fields[1].optional);

            assert_eq!(fields[2].name, "timeout");
            assert_eq!(fields[2].type_name, "Option<u64>");
            assert!(fields[2].optional);
        } else {
            panic!("Expected struct item");
        }
    }

    #[test]
    fn test_is_option_type() {
        let scanner = ConfigScanner::new();

        // Option<String>
        let ty: syn::Type = syn::parse_str("Option<String>").unwrap();
        assert!(scanner.is_option_type(&ty));

        // String
        let ty: syn::Type = syn::parse_str("String").unwrap();
        assert!(!scanner.is_option_type(&ty));

        // Vec<String>
        let ty: syn::Type = syn::parse_str("Vec<String>").unwrap();
        assert!(!scanner.is_option_type(&ty));
    }
}