|
5 | 5 | use tower_lsp::jsonrpc::Result; |
6 | 6 | use tower_lsp::lsp_types::*; |
7 | 7 |
|
| 8 | +use crate::lsp::stdlib_metadata::get_stdlib_completions; |
8 | 9 | use crate::lsp::Backend; |
9 | 10 |
|
| 11 | +/// WokeLang keywords for completion |
| 12 | +const KEYWORDS: &[&str] = &[ |
| 13 | + "to", |
| 14 | + "give", |
| 15 | + "back", |
| 16 | + "remember", |
| 17 | + "when", |
| 18 | + "is", |
| 19 | + "otherwise", |
| 20 | + "repeat", |
| 21 | + "times", |
| 22 | + "only", |
| 23 | + "if", |
| 24 | + "okay", |
| 25 | + "attempt", |
| 26 | + "safely", |
| 27 | + "handle", |
| 28 | + "reassure", |
| 29 | + "complain", |
| 30 | + "type", |
| 31 | + "use", |
| 32 | + "renamed", |
| 33 | + "as", |
| 34 | + "hello", |
| 35 | + "goodbye", |
| 36 | + "worker", |
| 37 | + "side", |
| 38 | + "quest", |
| 39 | + "spawn", |
| 40 | + "superpower", |
| 41 | + "thanks", |
| 42 | + "for", |
| 43 | + "each", |
| 44 | + "in", |
| 45 | + "measured", |
| 46 | + "then", |
| 47 | + "and", |
| 48 | + "or", |
| 49 | + "not", |
| 50 | + "true", |
| 51 | + "false", |
| 52 | +]; |
| 53 | + |
| 54 | +/// Standard library modules |
| 55 | +const STDLIB_MODULES: &[&str] = &[ |
| 56 | + "math", "string", "array", "io", "json", "time", "net", "chan", |
| 57 | +]; |
| 58 | + |
10 | 59 | /// Handle completion request |
11 | 60 | pub async fn completion( |
12 | | - _backend: &Backend, |
13 | | - _params: CompletionParams, |
| 61 | + backend: &Backend, |
| 62 | + params: CompletionParams, |
14 | 63 | ) -> Result<Option<CompletionResponse>> { |
15 | | - // TODO: Implement completion |
16 | | - Ok(None) |
| 64 | + let uri = params.text_document_position.text_document.uri; |
| 65 | + let position = params.text_document_position.position; |
| 66 | + |
| 67 | + // Get document |
| 68 | + let doc = match backend.document_map.get(&uri) { |
| 69 | + Some(d) => d, |
| 70 | + None => return Ok(None), |
| 71 | + }; |
| 72 | + |
| 73 | + // Get word at cursor |
| 74 | + let word = doc |
| 75 | + .word_at_position(position.line, position.character) |
| 76 | + .unwrap_or_default(); |
| 77 | + |
| 78 | + let mut items = Vec::new(); |
| 79 | + |
| 80 | + // Check if we're after "std.module." for function completion |
| 81 | + if word.starts_with("std.") && word.matches('.').count() >= 2 { |
| 82 | + // Extract module name (e.g., "std.math." -> "math") |
| 83 | + let parts: Vec<&str> = word.split('.').collect(); |
| 84 | + if parts.len() >= 2 { |
| 85 | + let module = parts[1]; |
| 86 | + items.extend(get_stdlib_completions(module)); |
| 87 | + } |
| 88 | + } |
| 89 | + // Check if we're after "std." for module completion |
| 90 | + else if word.starts_with("std.") || word == "std" { |
| 91 | + // Complete stdlib modules |
| 92 | + for module in STDLIB_MODULES { |
| 93 | + items.push(CompletionItem { |
| 94 | + label: module.to_string(), |
| 95 | + kind: Some(CompletionItemKind::MODULE), |
| 96 | + detail: Some(format!("Standard library module: {}", module)), |
| 97 | + documentation: Some(Documentation::String(format!( |
| 98 | + "WokeLang standard library module for {} operations", |
| 99 | + module |
| 100 | + ))), |
| 101 | + sort_text: Some(format!("0_{}", module)), |
| 102 | + ..Default::default() |
| 103 | + }); |
| 104 | + } |
| 105 | + } else { |
| 106 | + // Add keywords |
| 107 | + for keyword in KEYWORDS { |
| 108 | + items.push(CompletionItem { |
| 109 | + label: keyword.to_string(), |
| 110 | + kind: Some(CompletionItemKind::KEYWORD), |
| 111 | + detail: Some("Keyword".to_string()), |
| 112 | + sort_text: Some(format!("1_{}", keyword)), |
| 113 | + ..Default::default() |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + // Add local symbols from type environment |
| 118 | + if let Some(type_env) = doc.type_env() { |
| 119 | + for (name, ty) in &type_env.bindings { |
| 120 | + let kind = match ty { |
| 121 | + crate::typechecker::TypeInfo::Function(_, _) => CompletionItemKind::FUNCTION, |
| 122 | + _ => CompletionItemKind::VARIABLE, |
| 123 | + }; |
| 124 | + |
| 125 | + items.push(CompletionItem { |
| 126 | + label: name.clone(), |
| 127 | + kind: Some(kind), |
| 128 | + detail: Some(format!("{}", ty)), |
| 129 | + documentation: Some(Documentation::String(format!("Type: {}", ty))), |
| 130 | + sort_text: Some(format!("2_{}", name)), |
| 131 | + ..Default::default() |
| 132 | + }); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + Ok(Some(CompletionResponse::Array(items))) |
17 | 138 | } |
0 commit comments