|
| 1 | +use code_analysis::{EmmyrcFilenameConvention, ModuleInfo}; |
| 2 | +use emmylua_parser::{LuaAstNode, LuaNameExpr}; |
| 3 | +use lsp_types::CompletionItem; |
| 4 | + |
| 5 | +use crate::handlers::completion::completion_builder::CompletionBuilder; |
| 6 | + |
| 7 | +pub fn add_completion(builder: &mut CompletionBuilder) -> Option<()> { |
| 8 | + if builder.is_cancelled() { |
| 9 | + return None; |
| 10 | + } |
| 11 | + |
| 12 | + let name_expr = LuaNameExpr::cast(builder.trigger_token.parent()?)?; |
| 13 | + // optimize for large project |
| 14 | + let prefix = name_expr.get_name_text()?.to_lowercase(); |
| 15 | + let file_convension = builder |
| 16 | + .semantic_model |
| 17 | + .get_emmyrc() |
| 18 | + .completion |
| 19 | + .auto_require_naming_convention; |
| 20 | + let file_id = builder.semantic_model.get_file_id(); |
| 21 | + let module_infos = builder |
| 22 | + .semantic_model |
| 23 | + .get_db() |
| 24 | + .get_module_index() |
| 25 | + .get_module_infos(); |
| 26 | + |
| 27 | + let mut completions = Vec::new(); |
| 28 | + for module_info in module_infos { |
| 29 | + if module_info.visible |
| 30 | + && module_info.file_id != file_id |
| 31 | + && module_info.export_type.is_some() |
| 32 | + { |
| 33 | + add_module_completion_item( |
| 34 | + builder, |
| 35 | + &prefix, |
| 36 | + &module_info, |
| 37 | + file_convension, |
| 38 | + &mut completions, |
| 39 | + ); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + for completion in completions { |
| 44 | + builder.add_completion_item(completion); |
| 45 | + } |
| 46 | + |
| 47 | + Some(()) |
| 48 | +} |
| 49 | + |
| 50 | +fn add_module_completion_item( |
| 51 | + builder: &CompletionBuilder, |
| 52 | + prefix: &str, |
| 53 | + module_info: &ModuleInfo, |
| 54 | + file_convension: EmmyrcFilenameConvention, |
| 55 | + completions: &mut Vec<CompletionItem>, |
| 56 | +) -> Option<()> { |
| 57 | + let completion_name = module_name_convert(&module_info.name, file_convension); |
| 58 | + if !completion_name.to_lowercase().starts_with(prefix) { |
| 59 | + return None; |
| 60 | + } |
| 61 | + |
| 62 | + if builder.env_duplicate_name.contains(&completion_name) { |
| 63 | + return None; |
| 64 | + } |
| 65 | + |
| 66 | + let completion_item = CompletionItem { |
| 67 | + label: completion_name, |
| 68 | + kind: Some(lsp_types::CompletionItemKind::MODULE), |
| 69 | + label_details: Some(lsp_types::CompletionItemLabelDetails { |
| 70 | + detail: Some(format!(" (in {})", module_info.full_module_name)), |
| 71 | + ..Default::default() |
| 72 | + }), |
| 73 | + ..Default::default() |
| 74 | + }; |
| 75 | + |
| 76 | + completions.push(completion_item); |
| 77 | + |
| 78 | + Some(()) |
| 79 | +} |
| 80 | + |
| 81 | +fn module_name_convert(name: &str, file_convension: EmmyrcFilenameConvention) -> String { |
| 82 | + let mut module_name = name.to_string(); |
| 83 | + |
| 84 | + match file_convension { |
| 85 | + EmmyrcFilenameConvention::SnakeCase => { |
| 86 | + module_name = to_snake_case(&module_name); |
| 87 | + } |
| 88 | + EmmyrcFilenameConvention::CamelCase => { |
| 89 | + module_name = to_camel_case(&module_name); |
| 90 | + } |
| 91 | + EmmyrcFilenameConvention::PascalCase => { |
| 92 | + module_name = to_pascal_case(&module_name); |
| 93 | + } |
| 94 | + EmmyrcFilenameConvention::Keep => {} |
| 95 | + } |
| 96 | + |
| 97 | + module_name |
| 98 | +} |
| 99 | + |
| 100 | +fn to_snake_case(s: &str) -> String { |
| 101 | + let mut result = String::new(); |
| 102 | + for (i, ch) in s.chars().enumerate() { |
| 103 | + if ch.is_uppercase() && i != 0 { |
| 104 | + result.push('_'); |
| 105 | + result.push(ch.to_ascii_lowercase()); |
| 106 | + } else { |
| 107 | + result.push(ch.to_ascii_lowercase()); |
| 108 | + } |
| 109 | + } |
| 110 | + result |
| 111 | +} |
| 112 | + |
| 113 | +fn to_camel_case(s: &str) -> String { |
| 114 | + let mut result = String::new(); |
| 115 | + let mut next_uppercase = false; |
| 116 | + for (i, ch) in s.chars().enumerate() { |
| 117 | + if ch == '_' || ch == '-' || ch == '.' { |
| 118 | + next_uppercase = true; |
| 119 | + } else if next_uppercase { |
| 120 | + result.push(ch.to_ascii_uppercase()); |
| 121 | + next_uppercase = false; |
| 122 | + } else if i == 0 { |
| 123 | + result.push(ch.to_ascii_lowercase()); |
| 124 | + } else { |
| 125 | + result.push(ch); |
| 126 | + } |
| 127 | + } |
| 128 | + result |
| 129 | +} |
| 130 | + |
| 131 | +fn to_pascal_case(s: &str) -> String { |
| 132 | + let mut result = String::new(); |
| 133 | + let mut next_uppercase = true; |
| 134 | + for ch in s.chars() { |
| 135 | + if ch == '_' || ch == '-' || ch == '.' { |
| 136 | + next_uppercase = true; |
| 137 | + } else if next_uppercase { |
| 138 | + result.push(ch.to_ascii_uppercase()); |
| 139 | + next_uppercase = false; |
| 140 | + } else { |
| 141 | + result.push(ch.to_ascii_lowercase()); |
| 142 | + } |
| 143 | + } |
| 144 | + result |
| 145 | +} |
0 commit comments