|
| 1 | +#[cfg(test)] |
| 2 | +mod tests { |
| 3 | + use crate::common::create_test_backend; |
| 4 | + use tower_lsp::LanguageServer; |
| 5 | + use tower_lsp::lsp_types::*; |
| 6 | + |
| 7 | + #[tokio::test] |
| 8 | + async fn test_blade_hover_range_translated_back() { |
| 9 | + let backend = create_test_backend(); |
| 10 | + |
| 11 | + let php_uri = Url::parse("file:///BlogAuthor.php").unwrap(); |
| 12 | + let php_text = |
| 13 | + "<?php namespace App\\Models; class BlogAuthor { public string $name = ''; }"; |
| 14 | + backend |
| 15 | + .did_open(DidOpenTextDocumentParams { |
| 16 | + text_document: TextDocumentItem { |
| 17 | + uri: php_uri.clone(), |
| 18 | + language_id: "php".to_string(), |
| 19 | + version: 1, |
| 20 | + text: php_text.to_string(), |
| 21 | + }, |
| 22 | + }) |
| 23 | + .await; |
| 24 | + |
| 25 | + let blade_uri = Url::parse("file:///email.blade.php").unwrap(); |
| 26 | + // Line 0: @php |
| 27 | + // Line 1: /** |
| 28 | + // Line 2: * @var \App\Models\BlogAuthor $author |
| 29 | + // Line 3: */ |
| 30 | + // Line 4: @endphp |
| 31 | + // Line 5: (empty) |
| 32 | + // Line 6: <p>{{ $author->name }}</p> |
| 33 | + let blade_text = "@php\n/**\n * @var \\App\\Models\\BlogAuthor $author\n */\n@endphp\n\n<p>{{ $author->name }}</p>"; |
| 34 | + |
| 35 | + backend |
| 36 | + .did_open(DidOpenTextDocumentParams { |
| 37 | + text_document: TextDocumentItem { |
| 38 | + uri: blade_uri.clone(), |
| 39 | + language_id: "blade".to_string(), |
| 40 | + version: 1, |
| 41 | + text: blade_text.to_string(), |
| 42 | + }, |
| 43 | + }) |
| 44 | + .await; |
| 45 | + |
| 46 | + // Hover on $author (line 6, character 6 = the '$' of $author) |
| 47 | + let params = HoverParams { |
| 48 | + text_document_position_params: TextDocumentPositionParams { |
| 49 | + text_document: TextDocumentIdentifier { |
| 50 | + uri: blade_uri.clone(), |
| 51 | + }, |
| 52 | + position: Position { |
| 53 | + line: 6, |
| 54 | + character: 6, |
| 55 | + }, |
| 56 | + }, |
| 57 | + work_done_progress_params: WorkDoneProgressParams::default(), |
| 58 | + }; |
| 59 | + |
| 60 | + let result = backend.hover(params).await.unwrap(); |
| 61 | + assert!(result.is_some(), "Should return hover for $author"); |
| 62 | + |
| 63 | + let hover = result.unwrap(); |
| 64 | + // The range should be in Blade space (line 6), not PHP virtual space (line 11) |
| 65 | + if let Some(range) = hover.range { |
| 66 | + assert_eq!( |
| 67 | + range.start.line, 6, |
| 68 | + "Hover range start line should be in Blade space (6), got {}", |
| 69 | + range.start.line |
| 70 | + ); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + #[tokio::test] |
| 75 | + async fn test_blade_completion_with_var_annotation() { |
| 76 | + let backend = create_test_backend(); |
| 77 | + |
| 78 | + let php_uri = Url::parse("file:///BlogAuthor.php").unwrap(); |
| 79 | + let php_text = "<?php namespace App\\Models; class BlogAuthor { public string $name = ''; public int $age = 0; }"; |
| 80 | + backend |
| 81 | + .did_open(DidOpenTextDocumentParams { |
| 82 | + text_document: TextDocumentItem { |
| 83 | + uri: php_uri.clone(), |
| 84 | + language_id: "php".to_string(), |
| 85 | + version: 1, |
| 86 | + text: php_text.to_string(), |
| 87 | + }, |
| 88 | + }) |
| 89 | + .await; |
| 90 | + |
| 91 | + let blade_uri = Url::parse("file:///email.blade.php").unwrap(); |
| 92 | + let blade_text = "@php\n/**\n * @var \\App\\Models\\BlogAuthor $author\n */\n@endphp\n\n<p>{{ $author-> }}</p>"; |
| 93 | + |
| 94 | + backend |
| 95 | + .did_open(DidOpenTextDocumentParams { |
| 96 | + text_document: TextDocumentItem { |
| 97 | + uri: blade_uri.clone(), |
| 98 | + language_id: "blade".to_string(), |
| 99 | + version: 1, |
| 100 | + text: blade_text.to_string(), |
| 101 | + }, |
| 102 | + }) |
| 103 | + .await; |
| 104 | + |
| 105 | + // Complete after "$author->" on line 6 |
| 106 | + // "<p>{{ $author-> }}</p>" |
| 107 | + // 0123456789012345 |
| 108 | + let params = CompletionParams { |
| 109 | + text_document_position: TextDocumentPositionParams { |
| 110 | + text_document: TextDocumentIdentifier { |
| 111 | + uri: blade_uri.clone(), |
| 112 | + }, |
| 113 | + position: Position { |
| 114 | + line: 6, |
| 115 | + character: 15, // after "$author->" |
| 116 | + }, |
| 117 | + }, |
| 118 | + work_done_progress_params: WorkDoneProgressParams::default(), |
| 119 | + partial_result_params: PartialResultParams::default(), |
| 120 | + context: Some(CompletionContext { |
| 121 | + trigger_kind: CompletionTriggerKind::TRIGGER_CHARACTER, |
| 122 | + trigger_character: Some(">".to_string()), |
| 123 | + }), |
| 124 | + }; |
| 125 | + |
| 126 | + let result = backend.completion(params).await.unwrap(); |
| 127 | + assert!(result.is_some(), "Should return completions for $author->"); |
| 128 | + |
| 129 | + let items = match result.unwrap() { |
| 130 | + CompletionResponse::Array(items) => items, |
| 131 | + CompletionResponse::List(list) => list.items, |
| 132 | + }; |
| 133 | + |
| 134 | + let labels: Vec<&str> = items.iter().map(|i| i.label.as_str()).collect(); |
| 135 | + assert!( |
| 136 | + labels.contains(&"name"), |
| 137 | + "Should complete 'name' property, got: {:?}", |
| 138 | + labels |
| 139 | + ); |
| 140 | + assert!( |
| 141 | + labels.contains(&"age"), |
| 142 | + "Should complete 'age' property, got: {:?}", |
| 143 | + labels |
| 144 | + ); |
| 145 | + } |
| 146 | + |
| 147 | + #[tokio::test] |
| 148 | + async fn test_blade_goto_definition_member() { |
| 149 | + let backend = create_test_backend(); |
| 150 | + |
| 151 | + let php_uri = Url::parse("file:///BlogAuthor.php").unwrap(); |
| 152 | + let php_text = |
| 153 | + "<?php namespace App\\Models; class BlogAuthor { public string $name = ''; }"; |
| 154 | + backend |
| 155 | + .did_open(DidOpenTextDocumentParams { |
| 156 | + text_document: TextDocumentItem { |
| 157 | + uri: php_uri.clone(), |
| 158 | + language_id: "php".to_string(), |
| 159 | + version: 1, |
| 160 | + text: php_text.to_string(), |
| 161 | + }, |
| 162 | + }) |
| 163 | + .await; |
| 164 | + |
| 165 | + let blade_uri = Url::parse("file:///email.blade.php").unwrap(); |
| 166 | + let blade_text = "@php\n/**\n * @var \\App\\Models\\BlogAuthor $author\n */\n@endphp\n\n<p>{{ $author->name }}</p>"; |
| 167 | + |
| 168 | + backend |
| 169 | + .did_open(DidOpenTextDocumentParams { |
| 170 | + text_document: TextDocumentItem { |
| 171 | + uri: blade_uri.clone(), |
| 172 | + language_id: "blade".to_string(), |
| 173 | + version: 1, |
| 174 | + text: blade_text.to_string(), |
| 175 | + }, |
| 176 | + }) |
| 177 | + .await; |
| 178 | + |
| 179 | + // GTD on "name" in "$author->name" on line 6 |
| 180 | + // "<p>{{ $author->name }}</p>" |
| 181 | + // 0123456789012345678 |
| 182 | + let params = GotoDefinitionParams { |
| 183 | + text_document_position_params: TextDocumentPositionParams { |
| 184 | + text_document: TextDocumentIdentifier { |
| 185 | + uri: blade_uri.clone(), |
| 186 | + }, |
| 187 | + position: Position { |
| 188 | + line: 6, |
| 189 | + character: 16, // "name" |
| 190 | + }, |
| 191 | + }, |
| 192 | + work_done_progress_params: WorkDoneProgressParams::default(), |
| 193 | + partial_result_params: PartialResultParams::default(), |
| 194 | + }; |
| 195 | + |
| 196 | + let result = backend.goto_definition(params).await.unwrap(); |
| 197 | + assert!( |
| 198 | + result.is_some(), |
| 199 | + "Should resolve definition for ->name in Blade file" |
| 200 | + ); |
| 201 | + } |
| 202 | +} |
0 commit comments