ruff_server 0.0.10

This is an internal component crate of Ruff
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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
//! Access to the Ruff linting API for the LSP

use std::fmt::Write;
use std::path::Path;

use ruff_python_ast::{SourceType, TomlSourceType};
use ruff_workspace::Settings;
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};

use crate::{
    DIAGNOSTIC_NAME, PositionEncoding, TextDocument,
    edit::{NotebookDocument, NotebookRange, ToRangeExt},
    resolve::is_document_excluded_for_linting,
    session::DocumentQuery,
};
use ruff_db::diagnostic::{Annotation, Diagnostic, Span, SubDiagnostic};
use ruff_diagnostics::{Applicability, Edit, Fix};
use ruff_linter::{
    Locator, SuppressionKind,
    directives::{Flags, extract_directives},
    generate_suppression_edits,
    linter::{check_path, parse_unchecked_source},
    package::PackageRoot,
    packaging::detect_package_root,
    preview::is_human_readable_names_enabled,
    settings::flags,
    source_kind::SourceKind,
    suppression::Suppressions,
    toml::lint_toml,
};
use ruff_notebook::{Notebook, NotebookIndex};
use ruff_python_codegen::Stylist;
use ruff_python_index::Indexer;
use ruff_source_file::LineIndex;
use ruff_text_size::{Ranged, TextRange};

/// This is serialized on the diagnostic `data` field.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub(crate) struct AssociatedDiagnosticData {
    /// The message describing what the fix does, if it exists, or the diagnostic name otherwise.
    title: String,
    /// Edits to fix the diagnostic. If this is empty, a fix
    /// does not exist.
    edits: Vec<lsp_types::TextEdit>,
    /// The identifier displayed for the diagnostic.
    code: String,
    /// Possible edit to add a suppression comment which will disable this diagnostic.
    noqa_edit: Option<lsp_types::TextEdit>,
    /// Whether this fix corresponds to a preferred action that can be used by auto fix commands.
    is_preferred: Option<bool>,
}

/// Describes a fix for `fixed_diagnostic` that may have quick fix
/// edits available, `noqa` comment edits, or both.
#[derive(Clone, Debug)]
pub(crate) struct DiagnosticFix {
    /// The original diagnostic to be fixed
    pub(crate) fixed_diagnostic: lsp_types::Diagnostic,
    /// The message describing what the fix does.
    pub(crate) title: String,
    /// The identifier displayed for the diagnostic.
    pub(crate) code: String,
    /// Edits to fix the diagnostic. If this is empty, a fix
    /// does not exist.
    pub(crate) edits: Vec<lsp_types::TextEdit>,
    /// Possible edit to add a suppression comment which will disable this diagnostic.
    pub(crate) noqa_edit: Option<lsp_types::TextEdit>,
    /// Whether this fix corresponds to a preferred action that can be used by auto fix commands.
    pub(crate) is_preferred: Option<bool>,
}

/// A series of diagnostics across a single text document or an arbitrary number of notebook cells.
pub(crate) type DiagnosticsMap = FxHashMap<lsp_types::Uri, Vec<lsp_types::Diagnostic>>;

pub(crate) fn check(
    query: &DocumentQuery,
    encoding: PositionEncoding,
    show_syntax_errors: bool,
    supports_related_information: bool,
) -> DiagnosticsMap {
    let settings = query.settings();
    let document_path = query.virtual_file_path();

    // If the document is excluded, return an empty list of diagnostics.
    if is_document_excluded_for_linting(
        &document_path,
        &settings.file_resolver,
        &settings.linter,
        query.text_document_language_id(),
    ) {
        return DiagnosticsMap::default();
    }

    let result = match query.source_type_for_lint() {
        SourceType::Python(source_type) => check_python(query, source_type),
        SourceType::Toml(source_type @ (TomlSourceType::Pyproject | TomlSourceType::Ruff)) => {
            let Ok(document) = query.as_single_document() else {
                return DiagnosticsMap::default();
            };
            check_toml(query, document, source_type)
        }
        SourceType::Toml(_) | SourceType::Markdown => return DiagnosticsMap::default(),
    };

    let CheckResult {
        diagnostics,
        suppression_edits,
        document,
    } = result;
    let document_uri = query.make_key().into_uri();
    let context = LspDiagnosticContext {
        source: document.source(),
        index: document.index(),
        notebook_index: document.notebook_index(),
        encoding,
        document_path: &document_path,
        document_uri: &document_uri,
        notebook: query.as_notebook(),
        supports_related_information,
        settings,
    };

    let mut diagnostics_map = DiagnosticsMap::default();

    // Populates all relevant URLs with an empty diagnostic list.
    // This ensures that documents without diagnostics still get updated.
    if let Some(notebook) = query.as_notebook() {
        for uri in notebook.uris() {
            diagnostics_map.entry(uri.clone()).or_default();
        }
    } else {
        diagnostics_map
            .entry(query.make_key().into_uri())
            .or_default();
    }

    let mut suppression_edits = suppression_edits.into_iter();
    let lsp_diagnostics = diagnostics.into_iter().filter_map(|message| {
        let suppression_edit = suppression_edits.next().flatten();
        if message.is_invalid_syntax() && !show_syntax_errors {
            None
        } else {
            Some(to_lsp_diagnostic(&message, suppression_edit, &context))
        }
    });

    if let Some(notebook) = query.as_notebook() {
        for (index, diagnostic) in lsp_diagnostics {
            let Some(uri) = notebook.cell_uri_by_index(index) else {
                tracing::warn!("Unable to find notebook cell at index {index}.");
                continue;
            };
            diagnostics_map
                .entry(uri.clone())
                .or_default()
                .push(diagnostic);
        }
    } else {
        diagnostics_map
            .entry(query.make_key().into_uri())
            .or_default()
            .extend(lsp_diagnostics.map(|(_, diagnostic)| diagnostic));
    }

    diagnostics_map
}

fn check_python(
    query: &DocumentQuery,
    source_type: ruff_python_ast::PySourceType,
) -> CheckResult<'_> {
    let settings = query.settings();
    let document_path = query.virtual_file_path();
    let source_kind = query.make_python_source_kind(source_type);

    let file_path = query.file_path();
    let package = if let Some(file_path) = &file_path {
        detect_package_root(
            file_path
                .parent()
                .expect("a path to a document should have a parent path"),
            &settings.linter.namespace_packages,
        )
        .map(PackageRoot::root)
    } else {
        None
    };

    let target_version = settings.linter.resolve_target_version(&document_path);

    // Parse once.
    let parsed = parse_unchecked_source(&source_kind, source_type, target_version.parser_version());

    // Map row and column locations to byte slices (lazily).
    let locator = Locator::new(source_kind.source_code());

    // Detect the current code style (lazily).
    let stylist = Stylist::from_tokens(parsed.tokens(), locator.contents());

    // Extra indices from the code.
    let indexer = Indexer::from_tokens(parsed.tokens(), locator.contents());

    // Extract the `# noqa` and `# isort: skip` directives from the source.
    let directives = extract_directives(parsed.tokens(), Flags::all(), &locator, &indexer);

    // Parse range suppression comments
    let suppressions = Suppressions::from_tokens(
        locator.contents(),
        parsed.tokens(),
        &indexer,
        &settings.linter,
    );

    // Generate checks.
    let diagnostics = check_path(
        &document_path,
        package,
        &locator,
        &stylist,
        &indexer,
        &directives,
        &settings.linter,
        flags::Noqa::Enabled,
        &source_kind,
        source_type,
        &parsed,
        target_version,
        &suppressions,
    );

    let suppression_edits = generate_suppression_edits(
        &document_path,
        &diagnostics,
        &locator,
        indexer.comment_ranges(),
        &settings.linter.external,
        &directives.noqa_line_for,
        stylist.line_ending(),
        &suppressions,
        if is_human_readable_names_enabled(settings.linter.preview)
            && !settings.output_prefer_rule_codes
        {
            SuppressionKind::Ignore
        } else {
            SuppressionKind::Noqa
        },
        settings.linter.preview,
    );
    let index = locator.to_index().clone();

    CheckResult {
        diagnostics,
        suppression_edits,
        document: CheckedDocument::Python {
            source: source_kind,
            index,
        },
    }
}

fn check_toml<'a>(
    query: &DocumentQuery,
    document: &'a TextDocument,
    source_type: TomlSourceType,
) -> CheckResult<'a> {
    let settings = query.settings();
    let diagnostics = if settings
        .linter
        .rules
        .iter_enabled()
        .any(|rule| rule.lint_source().is_toml())
    {
        lint_toml(
            &query.virtual_file_path(),
            document.contents(),
            &settings.linter,
            source_type,
        )
    } else {
        Vec::new()
    };

    CheckResult {
        diagnostics,
        suppression_edits: Vec::new(),
        document: CheckedDocument::Toml(document),
    }
}

/// Converts LSP diagnostics to a list of `DiagnosticFix`es by deserializing associated data on each diagnostic.
pub(crate) fn fixes_for_diagnostics(
    diagnostics: Vec<lsp_types::Diagnostic>,
) -> crate::Result<Vec<DiagnosticFix>> {
    diagnostics
        .into_iter()
        .filter(|diagnostic| diagnostic.source.as_deref() == Some(DIAGNOSTIC_NAME))
        .map(move |mut diagnostic| {
            let Some(data) = diagnostic.data.take() else {
                return Ok(None);
            };
            let fixed_diagnostic = diagnostic;
            let associated_data: crate::lint::AssociatedDiagnosticData =
                serde_json::from_value(data).map_err(|err| {
                    anyhow::anyhow!("failed to deserialize diagnostic data: {err}")
                })?;
            Ok(Some(DiagnosticFix {
                fixed_diagnostic,
                code: associated_data.code,
                title: associated_data.title,
                noqa_edit: associated_data.noqa_edit,
                edits: associated_data.edits,
                is_preferred: associated_data.is_preferred,
            }))
        })
        .filter_map(crate::Result::transpose)
        .collect()
}

enum CheckedDocument<'a> {
    Python {
        source: SourceKind,
        index: LineIndex,
    },
    Toml(&'a TextDocument),
}

impl CheckedDocument<'_> {
    fn source(&self) -> &str {
        match self {
            Self::Python { source, .. } => source.source_code(),
            Self::Toml(document) => document.contents(),
        }
    }

    fn index(&self) -> &LineIndex {
        match self {
            Self::Python { index, .. } => index,
            Self::Toml(document) => document.index(),
        }
    }

    fn notebook_index(&self) -> Option<&NotebookIndex> {
        match self {
            Self::Python { source, .. } => source.as_ipy_notebook().map(Notebook::index),
            Self::Toml(_) => None,
        }
    }
}

struct CheckResult<'a> {
    diagnostics: Vec<Diagnostic>,
    suppression_edits: Vec<Option<Edit>>,
    document: CheckedDocument<'a>,
}

struct LspDiagnosticContext<'a> {
    source: &'a str,
    index: &'a LineIndex,
    notebook_index: Option<&'a NotebookIndex>,
    encoding: PositionEncoding,
    document_path: &'a Path,
    document_uri: &'a lsp_types::Uri,
    notebook: Option<&'a NotebookDocument>,
    supports_related_information: bool,
    settings: &'a Settings,
}

/// Generates an LSP diagnostic with an associated cell index for the diagnostic to go in.
/// If the source kind is a text document, the cell index will always be `0`.
fn to_lsp_diagnostic(
    diagnostic: &Diagnostic,
    noqa_edit: Option<Edit>,
    context: &LspDiagnosticContext,
) -> (usize, lsp_types::Diagnostic) {
    let diagnostic_range = diagnostic.range().unwrap_or_default();
    let name = diagnostic.name();
    let fix = diagnostic.fix();
    let suggestion = diagnostic.first_help_text();

    let (severity, code) = if let Some(code) = diagnostic.secondary_code() {
        let severity = severity(code);
        let code = if is_human_readable_names_enabled(context.settings.linter.preview)
            && !context.settings.output_prefer_rule_codes
        {
            name.to_string()
        } else {
            code.to_string()
        };
        (severity, code)
    } else {
        (
            match diagnostic.severity() {
                ruff_db::diagnostic::Severity::Info => lsp_types::DiagnosticSeverity::Information,
                ruff_db::diagnostic::Severity::Warning => lsp_types::DiagnosticSeverity::Warning,
                ruff_db::diagnostic::Severity::Error => lsp_types::DiagnosticSeverity::Error,
                ruff_db::diagnostic::Severity::Fatal => lsp_types::DiagnosticSeverity::Error,
            },
            diagnostic.id().to_string(),
        )
    };

    let data = (fix.is_some() || noqa_edit.is_some())
        .then(|| {
            let mut title = suggestion.unwrap_or(name).to_string();
            if fix.is_some_and(|fix| fix.applicability() == Applicability::DisplayOnly) {
                title.push_str(" (suggestion)");
            }
            let edits = fix
                .into_iter()
                .flat_map(Fix::edits)
                .map(|edit| lsp_types::TextEdit {
                    range: diagnostic_edit_range(edit.range(), context),
                    new_text: edit.content().unwrap_or_default().to_string(),
                })
                .collect();
            let noqa_edit = noqa_edit.map(|noqa_edit| lsp_types::TextEdit {
                range: diagnostic_edit_range(noqa_edit.range(), context),
                new_text: noqa_edit.into_content().unwrap_or_default().into_string(),
            });
            serde_json::to_value(AssociatedDiagnosticData {
                title,
                noqa_edit,
                edits,
                code: code.clone(),
                is_preferred: fix.map(|fix| fix.applicability().is_safe()),
            })
            .ok()
        })
        .flatten();

    let range: lsp_types::Range;
    let cell: usize;

    if let Some(notebook_index) = context.notebook_index {
        NotebookRange { cell, range } = diagnostic_range.to_notebook_range(
            context.source,
            context.index,
            notebook_index,
            context.encoding,
        );
    } else {
        cell = usize::default();
        range = diagnostic_range.to_range(context.source, context.index, context.encoding);
    }

    let related_information =
        if context.supports_related_information {
            let mut related_information = Vec::new();
            related_information.extend(
                diagnostic.secondary_annotations().filter_map(|annotation| {
                    annotation_to_related_information(annotation, context)
                }),
            );

            for sub_diagnostic in diagnostic.sub_diagnostics() {
                related_information.extend(sub_diagnostic_to_related_information(
                    sub_diagnostic,
                    context,
                ));
                related_information.extend(sub_diagnostic.secondary_annotations().filter_map(
                    |annotation| annotation_to_related_information(annotation, context),
                ));
            }

            Some(related_information)
        } else {
            None
        };

    let mut body = if context.supports_related_information {
        if let Some(annotation_message) = diagnostic
            .primary_annotation()
            .and_then(Annotation::get_message)
        {
            format!("{}: {annotation_message}", diagnostic.headline_message())
        } else {
            diagnostic.headline_message().to_string()
        }
    } else {
        diagnostic.concise_message().to_string()
    };

    // Append sub-diagnostics that have no location (and thus can't be shown as related
    // information) to the message.
    let mut first = true;
    for sub_diagnostic in diagnostic.sub_diagnostics() {
        if sub_diagnostic.primary_annotation().is_none() {
            if first {
                body.push('\n');
                first = false;
            }
            write!(
                body,
                "\n{severity}: {message}",
                severity = sub_diagnostic.severity(),
                message = sub_diagnostic.concise_message(),
            )
            .ok();
        }
    }

    (
        cell,
        lsp_types::Diagnostic {
            range,
            severity: Some(severity),
            tags: tags(diagnostic),
            code: Some(lsp_types::Code::String(code)),
            code_description: diagnostic.documentation_url().and_then(|url| {
                Some(lsp_types::CodeDescription {
                    href: lsp_types::Uri::parse(url).ok()?,
                })
            }),
            source: Some(DIAGNOSTIC_NAME.into()),
            message: body.into(),
            related_information,
            data,
        },
    )
}

fn annotation_to_related_information(
    annotation: &Annotation,
    context: &LspDiagnosticContext,
) -> Option<lsp_types::DiagnosticRelatedInformation> {
    Some(lsp_types::DiagnosticRelatedInformation {
        location: span_to_location(annotation.get_span(), context)?,
        message: annotation.get_message()?.to_string(),
    })
}

fn sub_diagnostic_to_related_information(
    diagnostic: &SubDiagnostic,
    context: &LspDiagnosticContext,
) -> Option<lsp_types::DiagnosticRelatedInformation> {
    Some(lsp_types::DiagnosticRelatedInformation {
        location: span_to_location(diagnostic.primary_annotation()?.get_span(), context)?,
        message: diagnostic.concise_message().to_string(),
    })
}

fn span_to_location(span: &Span, context: &LspDiagnosticContext) -> Option<lsp_types::Location> {
    let source_file = span.as_ruff_file()?;
    if Path::new(source_file.name()) != context.document_path {
        return None;
    }
    let range = span.range()?;

    if let Some(notebook) = context.notebook {
        let notebook_index = context.notebook_index?;
        let NotebookRange { cell, range } = range.to_notebook_range(
            source_file.source_text(),
            source_file.index(),
            notebook_index,
            context.encoding,
        );
        Some(lsp_types::Location {
            uri: notebook.cell_uri_by_index(cell)?.clone(),
            range,
        })
    } else {
        Some(lsp_types::Location {
            uri: context.document_uri.clone(),
            range: range.to_range(
                source_file.source_text(),
                source_file.index(),
                context.encoding,
            ),
        })
    }
}

fn diagnostic_edit_range(range: TextRange, context: &LspDiagnosticContext) -> lsp_types::Range {
    if let Some(notebook_index) = context.notebook_index {
        range
            .to_notebook_range(
                context.source,
                context.index,
                notebook_index,
                context.encoding,
            )
            .range
    } else {
        range.to_range(context.source, context.index, context.encoding)
    }
}

fn severity(code: &str) -> lsp_types::DiagnosticSeverity {
    match code {
        // F821: undefined name <name>
        // E902: IOError
        "F821" | "E902" => lsp_types::DiagnosticSeverity::Error,
        _ => lsp_types::DiagnosticSeverity::Warning,
    }
}

fn tags(diagnostic: &Diagnostic) -> Option<Vec<lsp_types::DiagnosticTag>> {
    diagnostic.primary_tags().map(|tags| {
        tags.iter()
            .map(|tag| match tag {
                ruff_db::diagnostic::DiagnosticTag::Unnecessary => {
                    lsp_types::DiagnosticTag::Unnecessary
                }
                ruff_db::diagnostic::DiagnosticTag::Deprecated => {
                    lsp_types::DiagnosticTag::Deprecated
                }
            })
            .collect()
    })
}

#[cfg(test)]
mod tests {
    use ruff_db::diagnostic::{DiagnosticId, Severity, SubDiagnosticSeverity};
    use ruff_linter::source_kind::SourceKind;
    use ruff_source_file::SourceFileBuilder;
    use ruff_text_size::{TextRange, TextSize};

    use super::*;

    #[test]
    fn all_annotations_are_related_information() {
        let source = "abcdef";
        let source_file = SourceFileBuilder::new("test.py", source).finish();
        let span = |offset| {
            Span::from(source_file.clone())
                .with_range(TextRange::at(TextSize::new(offset), TextSize::new(1)))
        };

        let mut diagnostic = Diagnostic::new(
            DiagnosticId::lint("synthetic"),
            Severity::Error,
            "Synthetic diagnostic",
        );
        diagnostic.annotate(Annotation::primary(span(0)).message("Primary annotation"));
        diagnostic.annotate(Annotation::secondary(span(1)).message("Secondary annotation"));
        diagnostic.annotate(Annotation::primary(span(2)).message("Additional primary annotation"));
        diagnostic.annotate(Annotation::secondary(span(3)));
        diagnostic.annotate(
            Annotation::secondary(
                Span::from(SourceFileBuilder::new("other.py", "x").finish())
                    .with_range(TextRange::new(TextSize::new(0), TextSize::new(1))),
            )
            .message("Foreign annotation"),
        );

        let mut sub_diagnostic =
            SubDiagnostic::new(SubDiagnosticSeverity::Info, "Synthetic subdiagnostic");
        sub_diagnostic.annotate(Annotation::secondary(span(3)).message("Secondary subannotation"));
        sub_diagnostic.annotate(Annotation::primary(span(4)).message("Primary subannotation"));
        sub_diagnostic
            .annotate(Annotation::primary(span(5)).message("Additional primary subannotation"));
        diagnostic.sub(sub_diagnostic);
        diagnostic.sub(SubDiagnostic::new(
            SubDiagnosticSeverity::Help,
            "Unlocated subdiagnostic",
        ));

        let source_kind = SourceKind::Python {
            code: source.to_string(),
            is_stub: false,
        };
        let index = LineIndex::from_source_text(source);
        let uri = lsp_types::Uri::parse("file:///test.py").expect("URI to be valid");
        let settings = Settings::default();
        let context = LspDiagnosticContext {
            source: source_kind.source_code(),
            index: &index,
            notebook_index: None,
            encoding: PositionEncoding::UTF8,
            document_path: Path::new("test.py"),
            document_uri: &uri,
            notebook: None,
            supports_related_information: true,
            settings: &settings,
        };
        let (_, lsp_diagnostic) = to_lsp_diagnostic(&diagnostic, None, &context);

        assert_eq!(
            lsp_diagnostic.message,
            lsp_types::Message::String(
                "Synthetic diagnostic: Primary annotation\n\nhelp: Unlocated subdiagnostic"
                    .to_string()
            )
        );
        let related_information = lsp_diagnostic
            .related_information
            .expect("client supports diagnostic related information");
        assert!(
            related_information
                .iter()
                .all(|information| information.location.uri == uri)
        );
        assert_eq!(
            related_information
                .iter()
                .map(|information| information.location.range.start.character)
                .collect::<Vec<_>>(),
            [1, 2, 4, 3, 5]
        );
        assert_eq!(
            related_information
                .iter()
                .map(|information| information.message.as_str())
                .collect::<Vec<_>>(),
            [
                "Secondary annotation",
                "Additional primary annotation",
                "Synthetic subdiagnostic: Primary subannotation",
                "Secondary subannotation",
                "Additional primary subannotation",
            ]
        );
    }
}