]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc_errors/diagnostic.rs
Rollup merge of #58296 - estebank:hidden-suggestion, r=oli-obk
[rust.git] / src / librustc_errors / diagnostic.rs
index 06a1761a1e76fb270163f8482437aa319dbdb0b3..851b19e8177b53917f37cf4a9bcab637fb5f2abe 100644 (file)
@@ -1,11 +1,12 @@
-use CodeSuggestion;
-use SubstitutionPart;
-use Substitution;
-use Applicability;
-use Level;
+use crate::CodeSuggestion;
+use crate::SuggestionStyle;
+use crate::SubstitutionPart;
+use crate::Substitution;
+use crate::Applicability;
+use crate::Level;
+use crate::snippet::Style;
 use std::fmt;
 use syntax_pos::{MultiSpan, Span};
-use snippet::Style;
 
 #[must_use]
 #[derive(Clone, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]
@@ -118,7 +119,7 @@ pub fn cancelled(&self) -> bool {
         self.level == Level::Cancelled
     }
 
-    /// Add a span/label to be included in the resulting snippet.
+    /// Adds a span/label to be included in the resulting snippet.
     /// This is pushed onto the `MultiSpan` that was created when the
     /// diagnostic was first built. If you don't call this function at
     /// all, and you just supplied a `Span` to create the diagnostic,
@@ -243,7 +244,33 @@ pub fn multipart_suggestion(
                     .collect(),
             }],
             msg: msg.to_owned(),
-            show_code_when_inline: true,
+            style: SuggestionStyle::ShowCode,
+            applicability,
+        });
+        self
+    }
+
+    /// Prints out a message with for a multipart suggestion without showing the suggested code.
+    ///
+    /// This is intended to be used for suggestions that are obvious in what the changes need to
+    /// be from the message, showing the span label inline would be visually unpleasant
+    /// (marginally overlapping spans or multiline spans) and showing the snippet window wouldn't
+    /// improve understandability.
+    pub fn tool_only_multipart_suggestion(
+        &mut self,
+        msg: &str,
+        suggestion: Vec<(Span, String)>,
+        applicability: Applicability,
+    ) -> &mut Self {
+        self.suggestions.push(CodeSuggestion {
+            substitutions: vec![Substitution {
+                parts: suggestion
+                    .into_iter()
+                    .map(|(span, snippet)| SubstitutionPart { snippet, span })
+                    .collect(),
+            }],
+            msg: msg.to_owned(),
+            style: SuggestionStyle::CompletelyHidden,
             applicability,
         });
         self
@@ -277,7 +304,7 @@ pub fn span_suggestion(&mut self, sp: Span, msg: &str,
                 }],
             }],
             msg: msg.to_owned(),
-            show_code_when_inline: true,
+            style: SuggestionStyle::ShowCode,
             applicability,
         });
         self
@@ -295,7 +322,7 @@ pub fn span_suggestions(&mut self, sp: Span, msg: &str,
                 }],
             }).collect(),
             msg: msg.to_owned(),
-            show_code_when_inline: true,
+            style: SuggestionStyle::ShowCode,
             applicability,
         });
         self
@@ -316,7 +343,51 @@ pub fn span_suggestion_short(
                 }],
             }],
             msg: msg.to_owned(),
-            show_code_when_inline: false,
+            style: SuggestionStyle::HideCodeInline,
+            applicability,
+        });
+        self
+    }
+
+    /// Prints out a message with for a suggestion without showing the suggested code.
+    ///
+    /// This is intended to be used for suggestions that are obvious in what the changes need to
+    /// be from the message, showing the span label inline would be visually unpleasant
+    /// (marginally overlapping spans or multiline spans) and showing the snippet window wouldn't
+    /// improve understandability.
+    pub fn span_suggestion_hidden(
+        &mut self, sp: Span, msg: &str, suggestion: String, applicability: Applicability
+    ) -> &mut Self {
+        self.suggestions.push(CodeSuggestion {
+            substitutions: vec![Substitution {
+                parts: vec![SubstitutionPart {
+                    snippet: suggestion,
+                    span: sp,
+                }],
+            }],
+            msg: msg.to_owned(),
+            style: SuggestionStyle::HideCodeInline,
+            applicability,
+        });
+        self
+    }
+
+    /// Adds a suggestion to the json output, but otherwise remains silent/undisplayed in the cli.
+    ///
+    /// This is intended to be used for suggestions that are *very* obvious in what the changes
+    /// need to be from the message, but we still want other tools to be able to apply them.
+    pub fn tool_only_span_suggestion(
+        &mut self, sp: Span, msg: &str, suggestion: String, applicability: Applicability
+    ) -> &mut Self {
+        self.suggestions.push(CodeSuggestion {
+            substitutions: vec![Substitution {
+                parts: vec![SubstitutionPart {
+                    snippet: suggestion,
+                    span: sp,
+                }],
+            }],
+            msg: msg.to_owned(),
+            style: SuggestionStyle::CompletelyHidden,
             applicability: applicability,
         });
         self