plum

#treesitter#compiler#wasm

git clone https://git.pyrossh.dev/plum

A statically typed, imperative programming language inspired by rust, python


d17c1fa — Peter John 2026-11-10T23:12:54+05:30
plum-lsp protocol test: UTF-16 positions, generics, parse errors, an untitled document, a long file
Files changed (1) hide show
  1. compiler/lsp/tests/protocol.rs +147 -0
compiler/lsp/tests/protocol.rs CHANGED
@@ -140,3 +140,150 @@ fn diagnosticsAreVersionedDebouncedAndDefinitionsFound() {
140
140
  drop(s);
141
141
  let _ = std::fs::remove_dir_all(&dir);
142
142
  }
143
+
144
+ /// UTF-16 columns of `prefix` (what LSP counts).
145
+ fn utf16(prefix: &str) -> usize {
146
+ prefix.encode_utf16().count()
147
+ }
148
+
149
+ impl Server {
150
+ fn initialize(&mut self) {
151
+ self.send(r#"{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"capabilities":{}}}"#);
152
+ self.waitFor(r#""id":1"#);
153
+ self.send(r#"{"jsonrpc":"2.0","method":"initialized","params":{}}"#);
154
+ }
155
+
156
+ fn open(&mut self, uri: &str, version: i32, text: &str) {
157
+ self.send(&format!(
158
+ r#"{{"jsonrpc":"2.0","method":"textDocument/didOpen","params":{{"textDocument":{{"uri":"{uri}","languageId":"plum","version":{version},"text":"{}"}}}}}}"#,
159
+ escape(text)
160
+ ));
161
+ }
162
+
163
+ /// The diagnostics published for `uri`.
164
+ fn diagnosticsOf(&self, uri: &str) -> String {
165
+ let end = Instant::now() + Duration::from_secs(60);
166
+ while let Some(left) = end.checked_duration_since(Instant::now()) {
167
+ match self.messages.recv_timeout(left) {
168
+ Ok(m) if m.contains("publishDiagnostics") && m.contains(&format!(r#""uri":"{uri}""#)) => return m,
169
+ Ok(_) => {}
170
+ Err(_) => break,
171
+ }
172
+ }
173
+ panic!("no diagnostics for {uri}");
174
+ }
175
+
176
+ /// Sends request `id` (`method` with `params`) and returns its response.
177
+ fn request(&mut self, id: u32, method: &str, params: &str) -> String {
178
+ self.send(&format!(r#"{{"jsonrpc":"2.0","id":{id},"method":"{method}","params":{params}}}"#));
179
+ self.waitFor(&format!(r#""id":{id}"#))
180
+ }
181
+ }
182
+
183
+ fn at(uri: &str, line: usize, character: usize) -> String {
184
+ format!(r#"{{"textDocument":{{"uri":"{uri}"}},"position":{{"line":{line},"character":{character}}}}}"#)
185
+ }
186
+
187
+ /// The new syntax and non-ASCII text, in a file outside the repository: diagnostics,
188
+ /// hover, definition and completion positions count UTF-16 units; a parse error is a
189
+ /// syntax diagnostic on its own line; a document with no file still gets its syntax checked.
190
+ #[test]
191
+ fn positionsGenericsAndParseErrorsOverStdio() {
192
+ let dir = std::env::temp_dir().join(format!("plum_lsp_positions_{}", std::process::id()));
193
+ std::fs::create_dir_all(&dir).unwrap();
194
+ let path = dir.join("généric.plum");
195
+ let call = " pick(\"😀é\", nope)";
196
+ let text = format!(
197
+ "import std/Int\nimport std/Str\n\nstruct Box<T>(value: T)\n\nfun first<T>(b: Box<T>) -> T =\n b.value\n\nfun pick(s: Str, n: Int) -> Int =\n n\n\nfun main() -> Int =\n{call}\n\nfun other() -> Int =\n first(Box(value: 2))\n"
198
+ );
199
+ std::fs::write(&path, &text).unwrap();
200
+ // As a client sends it: percent-encoded.
201
+ let uri = format!("file://{}", path.display()).replace('é', "%C3%A9");
202
+
203
+ let mut s = Server::start();
204
+ s.initialize();
205
+ s.open(&uri, 1, &text);
206
+ let diags = s.diagnosticsOf(&uri);
207
+ let col = utf16(&call[..call.find("nope").unwrap()]);
208
+ assert!(diags.contains(&format!(r#""start":{{"character":{col},"line":12}}"#)), "nope at UTF-16 column {col}: {diags}");
209
+ assert!(diags.contains("nope"), "{diags}");
210
+
211
+ // Hover on the generic struct's name; definition of the generic function from its call.
212
+ let hover = s.request(2, "textDocument/hover", &at(&uri, 3, 8));
213
+ assert!(hover.contains("Box(value: T)"), "{hover}");
214
+ let def = s.request(3, "textDocument/definition", &at(&uri, 15, 3));
215
+ assert!(def.contains(r#""start":{"character":0,"line":5}"#), "{def}");
216
+ // A definition request after non-ASCII text on the line: `pick`'s call is before it,
217
+ // `nope` (unknown) has none, and neither request may crash the server.
218
+ let def = s.request(4, "textDocument/definition", &at(&uri, 12, 3));
219
+ assert!(def.contains(r#""start":{"character":0,"line":8}"#), "{def}");
220
+ let none = s.request(5, "textDocument/definition", &at(&uri, 12, col + 1));
221
+ assert!(none.contains(r#""result":null"#), "{none}");
222
+
223
+ // `@` completion after non-ASCII text: the edit range is in UTF-16 units.
224
+ let typed = format!("{call} + @ty");
225
+ let edited = text.replace(call, &typed);
226
+ s.send(&format!(
227
+ r#"{{"jsonrpc":"2.0","method":"textDocument/didChange","params":{{"textDocument":{{"uri":"{uri}","version":2}},"contentChanges":[{{"text":"{}"}}]}}}}"#,
228
+ escape(&edited)
229
+ ));
230
+ let end = utf16(&typed);
231
+ let done = s.request(6, "textDocument/completion", &at(&uri, 12, end));
232
+ assert!(done.contains("@typeName"), "{done}");
233
+ assert!(done.contains(&format!(r#""end":{{"character":{end},"line":12}}"#)), "{done}");
234
+ assert!(done.contains(&format!(r#""start":{{"character":{},"line":12}}"#, end - 3)), "{done}");
235
+
236
+ // A parse error: one syntax diagnostic, on the broken line.
237
+ let broken = "import std/Int\n\nfun main() -> Int =\n (1 +\n";
238
+ s.send(&format!(
239
+ r#"{{"jsonrpc":"2.0","method":"textDocument/didChange","params":{{"textDocument":{{"uri":"{uri}","version":3}},"contentChanges":[{{"text":"{}"}}]}}}}"#,
240
+ escape(broken)
241
+ ));
242
+ let diags = loop {
243
+ let d = s.diagnosticsOf(&uri);
244
+ if d.contains(r#""version":3"#) {
245
+ break d;
246
+ }
247
+ };
248
+ assert!(diags.contains("plum-syntax"), "{diags}");
249
+ assert!(diags.contains(r#""line":3"#) || diags.contains(r#""line":4"#), "{diags}");
250
+
251
+ // A document with no file on disk: syntax only, no crash.
252
+ s.open("untitled:Untitled-1", 1, broken);
253
+ let diags = s.diagnosticsOf("untitled:Untitled-1");
254
+ assert!(diags.contains("plum-syntax"), "{diags}");
255
+ drop(s);
256
+ let _ = std::fs::remove_dir_all(&dir);
257
+ }
258
+
259
+ /// A 2,000-line document is checked, and answers completion, within seconds.
260
+ #[test]
261
+ fn aLongDocumentIsCheckedQuickly() {
262
+ let dir = std::env::temp_dir().join(format!("plum_lsp_long_{}", std::process::id()));
263
+ std::fs::create_dir_all(&dir).unwrap();
264
+ let path = dir.join("long.plum");
265
+ let mut text = String::from("import std/Int\n\n");
266
+ for i in 0..666 {
267
+ text.push_str(&format!("fun f{i}(n: Int) -> Int =\n n + {i}\n\n"));
268
+ }
269
+ text.push_str("fun main() -> Int =\n f665(1)\n");
270
+ assert!(text.lines().count() >= 2000);
271
+ std::fs::write(&path, &text).unwrap();
272
+ let uri = format!("file://{}", path.display());
273
+
274
+ let mut s = Server::start();
275
+ s.initialize();
276
+ let started = Instant::now();
277
+ s.open(&uri, 1, &text);
278
+ let diags = s.diagnosticsOf(&uri);
279
+ let checked = started.elapsed();
280
+ assert!(diags.contains(r#""diagnostics":[]"#), "{diags}");
281
+ let started = Instant::now();
282
+ let items = s.request(2, "textDocument/completion", &at(&uri, 2001, 3));
283
+ let completed = started.elapsed();
284
+ assert!(items.contains(r#""label":"f42""#), "{}", &items[..items.len().min(400)]);
285
+ assert!(checked < Duration::from_secs(20) && completed < Duration::from_secs(20), "check {checked:?}, completion {completed:?}");
286
+ eprintln!("2,000 lines: checked in {checked:?}, completion in {completed:?}");
287
+ drop(s);
288
+ let _ = std::fs::remove_dir_all(&dir);
289
+ }