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
use crate::suggestion::SuggestionChange;
use crate::{
    file::{FileId, FileSpan, Span},
    Applicability, CodeSuggestion, DiagnosticTag, Severity, SuggestionStyle,
};
use rslint_text_edit::*;

/// A diagnostic message that can give information
/// like errors or warnings.
#[derive(Debug, Clone, PartialEq, Hash)]
pub struct Diagnostic {
    pub file_id: FileId,

    pub severity: Severity,
    pub code: Option<String>,
    pub title: String,
    pub tag: Option<DiagnosticTag>,

    pub primary: Option<SubDiagnostic>,
    pub children: Vec<SubDiagnostic>,
    pub suggestions: Vec<CodeSuggestion>,
    pub footers: Vec<Footer>,
}

impl Diagnostic {
    /// Creates a new [`Diagnostic`] with the `Error` severity.
    pub fn error(file_id: FileId, code: impl Into<String>, title: impl Into<String>) -> Self {
        Self::new_with_code(file_id, Severity::Error, title, Some(code.into()))
    }

    /// Creates a new [`Diagnostic`] with the `Warning` severity.
    pub fn warning(file_id: FileId, code: impl Into<String>, title: impl Into<String>) -> Self {
        Self::new_with_code(file_id, Severity::Warning, title, Some(code.into()))
    }

    /// Creates a new [`Diagnostic`] with the `Help` severity.
    pub fn help(file_id: FileId, code: impl Into<String>, title: impl Into<String>) -> Self {
        Self::new_with_code(file_id, Severity::Help, title, Some(code.into()))
    }

    /// Creates a new [`Diagnostic`] with the `Note` severity.
    pub fn note(file_id: FileId, code: impl Into<String>, title: impl Into<String>) -> Self {
        Self::new_with_code(file_id, Severity::Note, title, Some(code.into()))
    }

    /// Creates a new [`Diagnostic`] that will be used in a builder-like way
    /// to modify labels, and suggestions.
    pub fn new(file_id: FileId, severity: Severity, title: impl Into<String>) -> Self {
        Self::new_with_code(file_id, severity, title, None)
    }

    /// Creates a new [`Diagnostic`] with an error code that will be used in a builder-like way
    /// to modify labels, and suggestions.
    pub fn new_with_code(
        file_id: FileId,
        severity: Severity,
        title: impl Into<String>,
        code: Option<String>,
    ) -> Self {
        Self {
            file_id,
            code,
            severity,
            title: title.into(),
            primary: None,
            tag: None,
            children: vec![],
            suggestions: vec![],
            footers: vec![],
        }
    }

    /// Overwrites the severity of this diagnostic.
    pub fn severity(mut self, severity: Severity) -> Self {
        self.severity = severity;
        self
    }

    /// Marks this diagnostic as deprecated code, which will
    /// be displayed in the language server.
    ///
    /// This does not have any influence on the diagnostic rendering.
    pub fn deprecated(mut self) -> Self {
        self.tag = if matches!(self.tag, Some(DiagnosticTag::Unnecessary)) {
            Some(DiagnosticTag::Both)
        } else {
            Some(DiagnosticTag::Deprecated)
        };
        self
    }

    /// Marks this diagnostic as unnecessary code, which will
    /// be displayed in the language server.
    ///
    /// This does not have any influence on the diagnostic rendering.
    pub fn unnecessary(mut self) -> Self {
        self.tag = if matches!(self.tag, Some(DiagnosticTag::Deprecated)) {
            Some(DiagnosticTag::Both)
        } else {
            Some(DiagnosticTag::Unnecessary)
        };
        self
    }

    /// Attaches a label to this [`Diagnostic`], that will point to another file
    /// that is provided.
    pub fn label_in_file(mut self, severity: Severity, span: FileSpan, msg: String) -> Self {
        self.children.push(SubDiagnostic {
            severity,
            msg,
            span,
        });
        self
    }

    /// Attaches a label to this [`Diagnostic`].
    ///
    /// The given span has to be in the file that was provided while creating this [`Diagnostic`].
    pub fn label(mut self, severity: Severity, span: impl Span, msg: impl Into<String>) -> Self {
        self.children.push(SubDiagnostic {
            severity,
            msg: msg.into(),
            span: FileSpan::new(self.file_id, span),
        });
        self
    }

    /// Attaches a primary label to this [`Diagnostic`].
    pub fn primary(mut self, span: impl Span, msg: impl Into<String>) -> Self {
        self.primary = Some(SubDiagnostic {
            severity: self.severity,
            msg: msg.into(),
            span: FileSpan::new(self.file_id, span),
        });
        self
    }

    /// Attaches a secondary label to this [`Diagnostic`].
    pub fn secondary(self, span: impl Span, msg: impl Into<String>) -> Self {
        self.label(Severity::Note, span, msg)
    }

    /// Prints out a message that suggests a possible solution, that is in another
    /// file as this `Diagnostic`, to the error.
    ///
    /// If the message plus the suggestion is longer than 25 chars,
    /// the suggestion is displayed as a new children of this `Diagnostic`,
    /// otherwise it will be inlined with the other labels.
    ///
    /// A suggestion is displayed like:
    /// ```no_rust
    /// try adding a `;`: console.log();
    /// ```
    /// or in a separate multiline suggestion
    ///
    /// The message should not contain the `:` because it's added automatically.
    /// The suggestion will automatically be wrapped inside two backticks.
    pub fn suggestion_in_file(
        self,
        span: impl Span,
        msg: &str,
        suggestion: impl Into<String>,
        applicability: Applicability,
        file: FileId,
    ) -> Self {
        self.suggestion_inner(span, msg, suggestion, applicability, None, file)
    }

    fn auto_suggestion_style(span: &impl Span, msg: &str) -> SuggestionStyle {
        if span.as_range().len() + msg.len() > 25 {
            SuggestionStyle::Full
        } else {
            SuggestionStyle::Inline
        }
    }

    /// Prints out a message that suggests a possible solution to the error.
    ///
    /// If the message plus the suggestion is longer than 25 chars,
    /// the suggestion is displayed as a new children of this `Diagnostic`,
    /// otherwise it will be inlined with the other labels.
    ///
    /// A suggestion is displayed like:
    /// ```no_rust
    /// try adding a `;`: console.log();
    /// ```
    /// or in a separate multiline suggestion
    ///
    /// The message should not contain the `:` because it's added automatically.
    /// The suggestion will automatically be wrapped inside two backticks.
    pub fn suggestion(
        self,
        span: impl Span,
        msg: &str,
        suggestion: impl Into<String>,
        applicability: Applicability,
    ) -> Self {
        let file = self.file_id;
        self.suggestion_inner(span, msg, suggestion, applicability, None, file)
    }

    /// Add a suggestion which is always shown in the [Full](SuggestionStyle::Full) style.
    pub fn suggestion_full(
        self,
        span: impl Span,
        msg: &str,
        suggestion: impl Into<String>,
        applicability: Applicability,
    ) -> Self {
        let file = self.file_id;
        self.suggestion_inner(
            span,
            msg,
            suggestion,
            applicability,
            SuggestionStyle::Full,
            file,
        )
    }

    /// Add a suggestion which is always shown in the [Inline](SuggestionStyle::Inline) style.
    pub fn suggestion_inline(
        self,
        span: impl Span,
        msg: &str,
        suggestion: impl Into<String>,
        applicability: Applicability,
    ) -> Self {
        let file = self.file_id;
        self.suggestion_inner(
            span,
            msg,
            suggestion,
            applicability,
            SuggestionStyle::Inline,
            file,
        )
    }

    /// Add a suggestion which does not have a suggestion code.
    pub fn suggestion_no_code(
        self,
        span: impl Span,
        msg: &str,
        applicability: Applicability,
    ) -> Self {
        let file = self.file_id;
        self.suggestion_inner(
            span,
            msg,
            "",
            applicability,
            SuggestionStyle::HideCode,
            file,
        )
    }

    pub fn indel_suggestion(
        mut self,
        indels: impl IntoIterator<Item = Indel>,
        span: impl Span,
        msg: &str,
        applicability: Applicability,
    ) -> Self {
        let span = FileSpan {
            file: self.file_id,
            range: span.as_range(),
        };
        let indels = indels.into_iter().collect::<Vec<_>>();
        let labels = indels
            .iter()
            .filter(|x| !x.insert.is_empty())
            .map(|x| x.delete.as_range().start..x.delete.as_range().start + x.insert.len())
            .collect();

        let suggestion = CodeSuggestion {
            substitution: SuggestionChange::Indels(indels),
            applicability,
            msg: msg.to_string(),
            labels,
            span,
            style: SuggestionStyle::Full,
        };
        self.suggestions.push(suggestion);
        self
    }

    /// Add a suggestion with info labels which point to places in the suggestion.
    ///
    /// **The label ranges are relative to the start of the span, not relative to the original code**
    pub fn suggestion_with_labels(
        mut self,
        span: impl Span,
        msg: &str,
        suggestion: impl Into<String>,
        applicability: Applicability,
        labels: impl IntoIterator<Item = impl Span>,
    ) -> Self {
        let span = FileSpan {
            file: self.file_id,
            range: span.as_range(),
        };

        let labels = labels
            .into_iter()
            .map(|x| {
                let range = x.as_range();
                span.range.start + range.start..span.range.start + range.end
            })
            .collect::<Vec<_>>();
        let suggestion = CodeSuggestion {
            substitution: SuggestionChange::String(suggestion.into()),
            applicability,
            msg: msg.to_string(),
            labels,
            span,
            style: SuggestionStyle::Full,
        };
        self.suggestions.push(suggestion);
        self
    }

    /// Add a suggestion with info labels which point to places in the suggestion.
    ///
    /// **The label ranges are relative to the source code, not relative to the original code**
    pub fn suggestion_with_src_labels(
        mut self,
        span: impl Span,
        msg: &str,
        suggestion: impl Into<String>,
        applicability: Applicability,
        labels: impl IntoIterator<Item = impl Span>,
    ) -> Self {
        let span = FileSpan {
            file: self.file_id,
            range: span.as_range(),
        };

        let labels = labels.into_iter().map(|x| x.as_range()).collect::<Vec<_>>();
        let suggestion = CodeSuggestion {
            substitution: SuggestionChange::String(suggestion.into()),
            applicability,
            msg: msg.to_string(),
            labels,
            span,
            style: SuggestionStyle::Full,
        };
        self.suggestions.push(suggestion);
        self
    }

    fn suggestion_inner(
        mut self,
        span: impl Span,
        msg: &str,
        suggestion: impl Into<String>,
        applicability: Applicability,
        style: impl Into<Option<SuggestionStyle>>,
        file: FileId,
    ) -> Self {
        let style = style
            .into()
            .unwrap_or_else(|| Self::auto_suggestion_style(&span, msg));
        let span = FileSpan {
            file,
            range: span.as_range(),
        };
        let suggestion = CodeSuggestion {
            substitution: SuggestionChange::String(suggestion.into()),
            applicability,
            msg: msg.to_string(),
            labels: vec![],
            span,
            style,
        };
        self.suggestions.push(suggestion);
        self
    }

    /// Adds a footer to this `Diagnostic`, which will be displayed under the actual error.
    pub fn footer(mut self, severity: Severity, msg: impl Into<String>) -> Self {
        self.footers.push(Footer {
            msg: msg.into(),
            severity,
        });
        self
    }

    /// Adds a footer to this `Diagnostic`, with the `Help` severity.
    pub fn footer_help(self, msg: impl Into<String>) -> Self {
        self.footer(Severity::Help, msg)
    }

    /// Adds a footer to this `Diagnostic`, with the `Note` severity.
    pub fn footer_note(self, msg: impl Into<String>) -> Self {
        self.footer(Severity::Note, msg)
    }
}

/// Everything that can be added to a diagnostic, like
/// a suggestion that will be displayed under the actual error.
#[derive(Debug, Clone, PartialEq, Hash)]
pub struct SubDiagnostic {
    pub severity: Severity,
    pub msg: String,
    pub span: FileSpan,
}

/// A note or help that is displayed under the diagnostic.
#[derive(Debug, Clone, PartialEq, Hash)]
pub struct Footer {
    pub msg: String,
    pub severity: Severity,
}