qlue-ls 3.2.0

A language server for SPARQL
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
//! End-to-end tests for completion filtering
//!
//! Tests that keyword completions are filtered based on search term prefix.

mod harness;

use harness::TestClient;
use harness::runtime::run_lsp_test;
use serde_json::Value;

/// Helper to extract completion labels from a completion response
fn get_completion_labels(response: &Value) -> Vec<String> {
    response["result"]["items"]
        .as_array()
        .map(|items| {
            items
                .iter()
                .filter_map(|item| item["label"].as_str().map(|s| s.to_string()))
                .collect()
        })
        .unwrap_or_default()
}

/// Helper to check if a completion label exists in the response
fn has_completion_label(response: &Value, label: &str) -> bool {
    get_completion_labels(response).contains(&label.to_string())
}

#[test]
fn test_completion_filter_prefix_returns_filter() {
    run_lsp_test(|| async {
        let client = TestClient::new();
        client.initialize().await;

        // Open a document with "FI" typed in subject position
        // The cursor is at the end of "FI"
        client
            //                                     012345678901234567890
            .open_document("file:///test.sparql", "SELECT * WHERE { FI }")
            .await;

        // Request completion at position after "FI" (line 0, character 19)
        let id = client.complete("file:///test.sparql", 0, 19).await;
        let response = client
            .get_response(id)
            .expect("Should receive completion response");

        assert!(
            response.get("result").is_some(),
            "Completion should return a result: {:?}",
            response
        );

        // "FI" should match FILTER
        assert!(
            has_completion_label(&response, "FILTER"),
            "Should suggest FILTER for prefix 'FI', got: {:?}",
            get_completion_labels(&response)
        );

        // "FI" should NOT match other keywords like BIND, OPTIONAL, etc.
        assert!(
            !has_completion_label(&response, "BIND"),
            "Should NOT suggest BIND for prefix 'FI'"
        );
        assert!(
            !has_completion_label(&response, "OPTIONAL"),
            "Should NOT suggest OPTIONAL for prefix 'FI'"
        );
        assert!(
            !has_completion_label(&response, "VALUES"),
            "Should NOT suggest VALUES for prefix 'FI'"
        );
    });
}

#[test]
fn test_completion_non_keyword_prefix_excludes_keywords() {
    run_lsp_test(|| async {
        let client = TestClient::new();
        client.initialize().await;

        // Open a document with "Germany" typed in subject position
        client
            //                                     0000000000111111111122222222223
            //                                     0123456789012345678901234567890
            .open_document("file:///test.sparql", "SELECT * WHERE { Germany }")
            .await;

        // Request completion at position after "Germany" (line 0, character 24)
        let id = client.complete("file:///test.sparql", 0, 24).await;
        let response = client
            .get_response(id)
            .expect("Should receive completion response");

        assert!(
            response.get("result").is_some(),
            "Completion should return a result: {:?}",
            response
        );

        // "Germany" should NOT match any keywords
        assert!(
            !has_completion_label(&response, "FILTER"),
            "Should NOT suggest FILTER for 'Germany'"
        );
        assert!(
            !has_completion_label(&response, "BIND"),
            "Should NOT suggest BIND for 'Germany'"
        );
        assert!(
            !has_completion_label(&response, "OPTIONAL"),
            "Should NOT suggest OPTIONAL for 'Germany'"
        );
        assert!(
            !has_completion_label(&response, "VALUES"),
            "Should NOT suggest VALUES for 'Germany'"
        );
        assert!(
            !has_completion_label(&response, "SERVICE"),
            "Should NOT suggest SERVICE for 'Germany'"
        );
        assert!(
            !has_completion_label(&response, "MINUS"),
            "Should NOT suggest MINUS for 'Germany'"
        );
        assert!(
            !has_completion_label(&response, "UNION"),
            "Should NOT suggest UNION for 'Germany'"
        );
    });
}

#[test]
fn test_completion_optional_prefix_returns_optional() {
    run_lsp_test(|| async {
        let client = TestClient::new();
        client.initialize().await;

        // Open a document with "OP" typed in subject position
        client
            //                                     0000000000111111111122222222223
            //                                     0123456789012345678901234567890
            .open_document("file:///test.sparql", "SELECT * WHERE { OP }")
            .await;

        // Request completion at position after "OP" (line 0, character 19)
        let id = client.complete("file:///test.sparql", 0, 19).await;
        let response = client
            .get_response(id)
            .expect("Should receive completion response");

        assert!(
            response.get("result").is_some(),
            "Completion should return a result: {:?}",
            response
        );

        // "OP" should match OPTIONAL
        assert!(
            has_completion_label(&response, "OPTIONAL"),
            "Should suggest OPTIONAL for prefix 'OP', got: {:?}",
            get_completion_labels(&response)
        );

        // "OP" should NOT match other keywords
        assert!(
            !has_completion_label(&response, "FILTER"),
            "Should NOT suggest FILTER for prefix 'OP'"
        );
        assert!(
            !has_completion_label(&response, "BIND"),
            "Should NOT suggest BIND for prefix 'OP'"
        );
    });
}

#[test]
fn test_completion_case_insensitive_prefix() {
    run_lsp_test(|| async {
        let client = TestClient::new();
        client.initialize().await;

        // Open a document with lowercase "fi" typed in subject position
        client
            //                                     0000000000111111111122222222223
            //                                     0123456789012345678901234567890
            .open_document("file:///test.sparql", "SELECT * WHERE { fi }")
            .await;

        // Request completion at position after "fi" (line 0, character 19)
        let id = client.complete("file:///test.sparql", 0, 19).await;
        let response = client
            .get_response(id)
            .expect("Should receive completion response");

        assert!(
            response.get("result").is_some(),
            "Completion should return a result: {:?}",
            response
        );

        // lowercase "fi" should match FILTER (case insensitive)
        assert!(
            has_completion_label(&response, "FILTER"),
            "Should suggest FILTER for lowercase prefix 'fi', got: {:?}",
            get_completion_labels(&response)
        );
    });
}

#[test]
fn test_completion_bind_prefix_returns_bind() {
    run_lsp_test(|| async {
        let client = TestClient::new();
        client.initialize().await;

        // Open a document with "BI" typed in subject position
        client
            //                                     0000000000111111111122222222223
            //                                     0123456789012345678901234567890
            .open_document("file:///test.sparql", "SELECT * WHERE { BI }")
            .await;

        // Request completion at position after "BI" (line 0, character 19)
        let id = client.complete("file:///test.sparql", 0, 19).await;
        let response = client
            .get_response(id)
            .expect("Should receive completion response");

        assert!(
            response.get("result").is_some(),
            "Completion should return a result: {:?}",
            response
        );

        // "BI" should match BIND
        assert!(
            has_completion_label(&response, "BIND"),
            "Should suggest BIND for prefix 'BI', got: {:?}",
            get_completion_labels(&response)
        );

        // "BI" should NOT match other keywords
        assert!(
            !has_completion_label(&response, "FILTER"),
            "Should NOT suggest FILTER for prefix 'BI'"
        );
        assert!(
            !has_completion_label(&response, "OPTIONAL"),
            "Should NOT suggest OPTIONAL for prefix 'BI'"
        );
    });
}

#[test]
fn test_completion_s_prefix_returns_service_and_sub_select() {
    run_lsp_test(|| async {
        let client = TestClient::new();
        client.initialize().await;

        // Open a document with "S" typed in subject position
        client
            //                                     0000000000111111111122222222223
            //                                     0123456789012345678901234567890
            .open_document("file:///test.sparql", "SELECT * WHERE { S }")
            .await;

        // Request completion at position after "S" (line 0, character 18)
        let id = client.complete("file:///test.sparql", 0, 18).await;
        let response = client
            .get_response(id)
            .expect("Should receive completion response");

        assert!(
            response.get("result").is_some(),
            "Completion should return a result: {:?}",
            response
        );

        // "S" should match SERVICE and Sub select
        assert!(
            has_completion_label(&response, "SERVICE"),
            "Should suggest SERVICE for prefix 'S', got: {:?}",
            get_completion_labels(&response)
        );
        assert!(
            has_completion_label(&response, "Sub select"),
            "Should suggest 'Sub select' for prefix 'S', got: {:?}",
            get_completion_labels(&response)
        );

        // "S" should NOT match other keywords like FILTER, BIND
        assert!(
            !has_completion_label(&response, "FILTER"),
            "Should NOT suggest FILTER for prefix 'S'"
        );
        assert!(
            !has_completion_label(&response, "BIND"),
            "Should NOT suggest BIND for prefix 'S'"
        );
    });
}

/// Helper: labels that contain the given substring
fn labels_containing(response: &Value, substr: &str) -> Vec<String> {
    get_completion_labels(response)
        .into_iter()
        .filter(|label| label.contains(substr))
        .collect()
}

#[test]
fn test_aggregate_completion_with_group_by_no_prefix() {
    run_lsp_test(|| async {
        let client = TestClient::new();
        client.initialize().await;

        // Cursor in the (empty) select binding position, with GROUP BY present
        client
            //                                     0000000000111111111122222222223333333333
            //                                     0123456789012345678901234567890123456789
            .open_document(
                "file:///test.sparql",
                "SELECT  WHERE { ?s ?p ?o } GROUP BY ?s",
            )
            .await;

        let id = client.complete("file:///test.sparql", 0, 7).await;
        let response = client
            .get_response(id)
            .expect("Should receive completion response");

        // All aggregate functions should be offered for the non-grouped variables
        for aggregate in ["COUNT", "SUM", "MIN", "MAX", "AVG", "SAMPLE"] {
            assert!(
                !labels_containing(&response, &format!("{aggregate}(")).is_empty(),
                "Should suggest {aggregate} aggregate, got: {:?}",
                get_completion_labels(&response)
            );
        }
    });
}

#[test]
fn test_aggregate_completion_partial_s_suggests_sum_and_sample() {
    run_lsp_test(|| async {
        let client = TestClient::new();
        client.initialize().await;

        // Partially written aggregate "(S" in the select clause
        client
            //                                     0000000000111111111122222222223333333333444
            //                                     0123456789012345678901234567890123456789012
            .open_document(
                "file:///test.sparql",
                "SELECT (S WHERE { ?s ?p ?o } GROUP BY ?s",
            )
            .await;

        // Cursor right after "(S" (line 0, character 9)
        let id = client.complete("file:///test.sparql", 0, 9).await;
        let response = client
            .get_response(id)
            .expect("Should receive completion response");

        // "S" should match SUM and SAMPLE
        assert!(
            !labels_containing(&response, "SUM(").is_empty(),
            "Should suggest SUM for partial '(S', got: {:?}",
            get_completion_labels(&response)
        );
        assert!(
            !labels_containing(&response, "SAMPLE(").is_empty(),
            "Should suggest SAMPLE for partial '(S', got: {:?}",
            get_completion_labels(&response)
        );

        // "S" should NOT match the other aggregates
        for aggregate in ["COUNT", "MIN", "MAX", "AVG"] {
            assert!(
                labels_containing(&response, &format!("{aggregate}(")).is_empty(),
                "Should NOT suggest {aggregate} for partial '(S', got: {:?}",
                get_completion_labels(&response)
            );
        }
    });
}

#[test]
fn test_aggregate_completion_partial_su_suggests_only_sum() {
    run_lsp_test(|| async {
        let client = TestClient::new();
        client.initialize().await;

        client
            //                                     0000000000111111111122222222223333333333444
            //                                     0123456789012345678901234567890123456789012
            .open_document(
                "file:///test.sparql",
                "SELECT (SU WHERE { ?s ?p ?o } GROUP BY ?s",
            )
            .await;

        // Cursor right after "(SU" (line 0, character 10)
        let id = client.complete("file:///test.sparql", 0, 10).await;
        let response = client
            .get_response(id)
            .expect("Should receive completion response");

        assert!(
            !labels_containing(&response, "SUM(").is_empty(),
            "Should suggest SUM for partial '(SU', got: {:?}",
            get_completion_labels(&response)
        );
        for aggregate in ["COUNT", "MIN", "MAX", "AVG", "SAMPLE"] {
            assert!(
                labels_containing(&response, &format!("{aggregate}(")).is_empty(),
                "Should NOT suggest {aggregate} for partial '(SU', got: {:?}",
                get_completion_labels(&response)
            );
        }
    });
}

#[test]
fn test_aggregate_completion_partial_lowercase() {
    run_lsp_test(|| async {
        let client = TestClient::new();
        client.initialize().await;

        client
            //                                     0000000000111111111122222222223333333333444
            //                                     0123456789012345678901234567890123456789012
            .open_document(
                "file:///test.sparql",
                "SELECT (sa WHERE { ?s ?p ?o } GROUP BY ?s",
            )
            .await;

        // Cursor right after "(sa" (line 0, character 10)
        let id = client.complete("file:///test.sparql", 0, 10).await;
        let response = client
            .get_response(id)
            .expect("Should receive completion response");

        assert!(
            !labels_containing(&response, "SAMPLE(").is_empty(),
            "Should suggest SAMPLE for partial '(sa' (case-insensitive), got: {:?}",
            get_completion_labels(&response)
        );
        assert!(
            labels_containing(&response, "SUM(").is_empty(),
            "Should NOT suggest SUM for partial '(sa', got: {:?}",
            get_completion_labels(&response)
        );
    });
}

#[test]
fn test_aggregate_completion_partial_c_includes_count_star() {
    run_lsp_test(|| async {
        let client = TestClient::new();
        client.initialize().await;

        client
            //                                     0000000000111111111122222222223333333333444
            //                                     0123456789012345678901234567890123456789012
            .open_document(
                "file:///test.sparql",
                "SELECT (C WHERE { ?s ?p ?o } GROUP BY ?s",
            )
            .await;

        // Cursor right after "(C" (line 0, character 9)
        let id = client.complete("file:///test.sparql", 0, 9).await;
        let response = client
            .get_response(id)
            .expect("Should receive completion response");

        assert!(
            !labels_containing(&response, "COUNT(").is_empty(),
            "Should suggest COUNT for partial '(C', got: {:?}",
            get_completion_labels(&response)
        );
        assert!(
            has_completion_label(&response, "(COUNT(*) AS ?count)"),
            "Should suggest '(COUNT(*) AS ?count)' for partial '(C', got: {:?}",
            get_completion_labels(&response)
        );
        for aggregate in ["SUM", "MIN", "MAX", "AVG", "SAMPLE"] {
            assert!(
                labels_containing(&response, &format!("{aggregate}(")).is_empty(),
                "Should NOT suggest {aggregate} for partial '(C', got: {:?}",
                get_completion_labels(&response)
            );
        }
    });
}

#[test]
fn test_aggregate_completion_partial_without_paren() {
    run_lsp_test(|| async {
        let client = TestClient::new();
        client.initialize().await;

        // Partial aggregate without the opening paren
        client
            //                                     0000000000111111111122222222223333333333
            //                                     0123456789012345678901234567890123456789
            .open_document(
                "file:///test.sparql",
                "SELECT S WHERE { ?s ?p ?o } GROUP BY ?s",
            )
            .await;

        // Cursor right after "S" (line 0, character 8)
        let id = client.complete("file:///test.sparql", 0, 8).await;
        let response = client
            .get_response(id)
            .expect("Should receive completion response");

        assert!(
            !labels_containing(&response, "SUM(").is_empty(),
            "Should suggest SUM for partial 'S', got: {:?}",
            get_completion_labels(&response)
        );
        assert!(
            !labels_containing(&response, "SAMPLE(").is_empty(),
            "Should suggest SAMPLE for partial 'S', got: {:?}",
            get_completion_labels(&response)
        );
        for aggregate in ["COUNT", "MIN", "MAX", "AVG"] {
            assert!(
                labels_containing(&response, &format!("{aggregate}(")).is_empty(),
                "Should NOT suggest {aggregate} for partial 'S', got: {:?}",
                get_completion_labels(&response)
            );
        }
    });
}

#[test]
fn test_no_binding_snippets_inside_partial_aggregate() {
    run_lsp_test(|| async {
        let client = TestClient::new();
        client.initialize().await;

        // Cursor inside a partially written aggregate call: this is an
        // expression position, not a fresh select binding. Offering the
        // "(AGG(?var) AS ?alias)" snippets here would replace the inner
        // paren and produce garbage like "(SUM(SUM(?o) AS ?sum_o)".
        client
            //                                     0000000000111111111122222222223333333333444
            //                                     0123456789012345678901234567890123456789012
            .open_document(
                "file:///test.sparql",
                "SELECT (SUM( WHERE { ?s ?p ?o } GROUP BY ?s",
            )
            .await;

        // Cursor right after the inner "(" (line 0, character 12)
        let id = client.complete("file:///test.sparql", 0, 12).await;
        let response = client
            .get_response(id)
            .expect("Should receive completion response");

        assert!(
            labels_containing(&response, " AS ?").is_empty(),
            "Should NOT suggest binding snippets inside an aggregate call, got: {:?}",
            get_completion_labels(&response)
        );
        assert!(
            !has_completion_label(&response, "REDUCED"),
            "Should NOT suggest REDUCED inside an aggregate call"
        );
    });
}

#[test]
fn test_no_aggregate_completion_without_group_by_and_selected_var() {
    run_lsp_test(|| async {
        let client = TestClient::new();
        client.initialize().await;

        // A variable is already selected and there is no GROUP BY:
        // aggregates would be invalid here
        client
            //                                     000000000011111111112222222222
            //                                     012345678901234567890123456789
            .open_document("file:///test.sparql", "SELECT ?s  WHERE { ?s ?p ?o }")
            .await;

        let id = client.complete("file:///test.sparql", 0, 10).await;
        let response = client
            .get_response(id)
            .expect("Should receive completion response");

        for aggregate in ["COUNT", "SUM", "MIN", "MAX", "AVG", "SAMPLE"] {
            assert!(
                labels_containing(&response, &format!("{aggregate}(")).is_empty(),
                "Should NOT suggest {aggregate} without GROUP BY when a variable is selected, got: {:?}",
                get_completion_labels(&response)
            );
        }
    });
}

#[test]
fn test_completion_solution_modifier_group_prefix() {
    run_lsp_test(|| async {
        let client = TestClient::new();
        client.initialize().await;

        // Open a document with "GR" after the WHERE clause (solution modifier position)
        client
            //                                     0000000000111111111122222222223
            //                                     0123456789012345678901234567890
            .open_document("file:///test.sparql", "SELECT * WHERE { ?s ?p ?o } GR")
            .await;

        // Request completion at position after "GR" (line 0, character 30)
        let id = client.complete("file:///test.sparql", 0, 30).await;
        let response = client
            .get_response(id)
            .expect("Should receive completion response");

        assert!(
            response.get("result").is_some(),
            "Completion should return a result: {:?}",
            response
        );

        // "GR" should match GROUP BY
        assert!(
            has_completion_label(&response, "GROUP BY"),
            "Should suggest 'GROUP BY' for prefix 'GR', got: {:?}",
            get_completion_labels(&response)
        );

        // "GR" should NOT match other solution modifiers
        assert!(
            !has_completion_label(&response, "ORDER BY"),
            "Should NOT suggest 'ORDER BY' for prefix 'GR'"
        );
        assert!(
            !has_completion_label(&response, "LIMIT"),
            "Should NOT suggest LIMIT for prefix 'GR'"
        );
    });
}

#[test]
fn test_completion_solution_modifier_non_keyword_excludes_all() {
    run_lsp_test(|| async {
        let client = TestClient::new();
        client.initialize().await;

        // Open a document with "xyz" after the WHERE clause (solution modifier position)
        client
            //                                     00000000001111111111222222222233
            //                                     01234567890123456789012345678901
            .open_document("file:///test.sparql", "SELECT * WHERE { ?s ?p ?o } xyz")
            .await;

        // Request completion at position after "xyz" (line 0, character 31)
        let id = client.complete("file:///test.sparql", 0, 31).await;
        let response = client
            .get_response(id)
            .expect("Should receive completion response");

        assert!(
            response.get("result").is_some(),
            "Completion should return a result: {:?}",
            response
        );

        // "xyz" should NOT match any solution modifier keywords
        assert!(
            !has_completion_label(&response, "GROUP BY"),
            "Should NOT suggest 'GROUP BY' for 'xyz'"
        );
        assert!(
            !has_completion_label(&response, "ORDER BY"),
            "Should NOT suggest 'ORDER BY' for 'xyz'"
        );
        assert!(
            !has_completion_label(&response, "HAVING"),
            "Should NOT suggest HAVING for 'xyz'"
        );
        assert!(
            !has_completion_label(&response, "LIMIT"),
            "Should NOT suggest LIMIT for 'xyz'"
        );
        assert!(
            !has_completion_label(&response, "OFFSET"),
            "Should NOT suggest OFFSET for 'xyz'"
        );
    });
}