diff --git a/compiler/erg_common/error.rs b/compiler/erg_common/error.rs index 5af2c41f..55198aec 100644 --- a/compiler/erg_common/error.rs +++ b/compiler/erg_common/error.rs @@ -397,10 +397,56 @@ pub struct SubMessage { } impl SubMessage { + /// + /// Used when the msg or hint si empty. + /// `msg` is Vec\ instead of Option\ because it can be used when there are multiple `msg`s as well as multiple lines. + /// # Example + /// ``` + /// let msg = SubMessage::ambiguous_new(loc, vec![], None); // this code same as only_loc() + /// + /// let hint = Some("hint message here".to_string()) + /// let msg = SubMessage::ambiguous_new(loc, vec![], hint); + /// /* example + /// ------- + /// `- hint message here + /// */ + /// + /// let hint = Some("hint here".to_string()) + /// let first = StyledString::new("1th message", Color::Red, None); + /// let second = StyledString::new("2th message", Color::White, None); + /// : + /// let nth = StyledString::new("nth message", Color::Green, None); + /// let msg = SubMessage::ambiguous_new( + /// loc, + /// vec![ + /// first.to_string(), + /// second.to_string(), + /// ..., + /// nth.to_string(), + /// ], + /// hint); + /// /* example + /// ------- + /// :- 1th message + /// :- 2th message + /// : + /// :- nth message + /// `- hint here + /// */ + /// + /// ``` + /// pub fn ambiguous_new(loc: Location, msg: Vec, hint: Option) -> Self { Self { loc, msg, hint } } + /// + /// Used when only Location is fixed. + /// In this case, error position is just modified + /// # Example + /// ``` + /// let sub_msg = SubMessage::only_loc(loc); + /// ``` pub fn only_loc(loc: Location) -> Self { Self { loc, diff --git a/compiler/erg_common/style.rs b/compiler/erg_common/style.rs index 5e686bf2..d67bb1a8 100644 --- a/compiler/erg_common/style.rs +++ b/compiler/erg_common/style.rs @@ -313,6 +313,14 @@ pub struct StyledString { } impl StyledString { + /// + /// # Example + /// ``` + /// let s = String::from("Hello, world"); + /// StyledString::new(s, None, None); + /// let s = "Hello, world"; + /// StyledString::new(s, None, None); + /// ``` pub fn new<'a, S: Into>>( s: S, color: Option, @@ -464,7 +472,18 @@ impl StyledStrings { } } - pub fn is_same_color(&self, color: Color) -> bool { + /// + /// Determine if all strings in Vec are empty + /// Returns False if any string is present. + /// + pub fn is_empty(&self) -> bool { + self.texts + .iter() + .inspect(|x| println!("{:#?}", x)) + .all(|s| s.is_empty()) + } + + fn is_same_color(&self, color: Color) -> bool { if let Some(text) = self.texts.last() { return text.color == Some(color); }