-
Notifications
You must be signed in to change notification settings - Fork 44
refactor: Use Origin for Snippet::origin #221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -164,7 +164,7 @@ pub struct Title<'a> { | |||||||||||||||||
/// A source view [`Element`] in a [`Group`] | ||||||||||||||||||
#[derive(Clone, Debug)] | ||||||||||||||||||
pub struct Snippet<'a, T> { | ||||||||||||||||||
Comment on lines
164
to
166
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
pub(crate) origin: Option<&'a str>, | ||||||||||||||||||
pub(crate) origin: Option<Origin<'a>>, | ||||||||||||||||||
pub(crate) line_start: usize, | ||||||||||||||||||
pub(crate) source: &'a str, | ||||||||||||||||||
pub(crate) markers: Vec<T>, | ||||||||||||||||||
|
@@ -200,15 +200,20 @@ impl<'a, T: Clone> Snippet<'a, T> { | |||||||||||||||||
|
||||||||||||||||||
/// The location of the [`source`][Self::source] (e.g. a path) | ||||||||||||||||||
/// | ||||||||||||||||||
/// If only a location is provided (i.e. a `String`) then the rest of the | ||||||||||||||||||
/// [`Origin`] is inferred (e.g. line and column numbers). To adjust line | ||||||||||||||||||
/// numbers, consider using [`Snippet::line_start`] instead as it will also | ||||||||||||||||||
/// adjust line numbers for the [`Snippet::source`]. | ||||||||||||||||||
/// | ||||||||||||||||||
/// <div class="warning"> | ||||||||||||||||||
/// | ||||||||||||||||||
/// Text passed to this function is considered "untrusted input", as such | ||||||||||||||||||
/// all text is passed through a normalization function. Pre-styled text is | ||||||||||||||||||
/// not allowed to be passed to this function. | ||||||||||||||||||
/// | ||||||||||||||||||
/// </div> | ||||||||||||||||||
pub fn origin(mut self, origin: &'a str) -> Self { | ||||||||||||||||||
self.origin = Some(origin); | ||||||||||||||||||
pub fn origin(mut self, origin: impl Into<Origin<'a>>) -> Self { | ||||||||||||||||||
self.origin = Some(origin.into()); | ||||||||||||||||||
self | ||||||||||||||||||
Comment on lines
+215
to
217
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not too sure how this makes it clearer. Also, wht does uit mean to specify line, char_column, or primary on an Origin in a Snippet? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
|
@@ -375,8 +380,16 @@ impl<'a> Patch<'a> { | |||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
/// The location of the [`Snippet`] (e.g. a path) | ||||||||||||||||||
#[derive(Clone, Debug)] | ||||||||||||||||||
/// The location of the [`Snippet`] (e.g. a path). | ||||||||||||||||||
/// | ||||||||||||||||||
/// This should be used if you want to set the line number and column | ||||||||||||||||||
/// explicitly for a [`Snippet`], or if you need to render a location without | ||||||||||||||||||
/// an accompanying [`Snippet`]. | ||||||||||||||||||
Comment on lines
+383
to
+387
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
/// | ||||||||||||||||||
/// Note: `line` is always respected if set, but `char_column` is only | ||||||||||||||||||
/// respected if `line` has been set. `primary` is respected unless the origin | ||||||||||||||||||
/// is the first one in a [`Group`], in which case it is ignored. | ||||||||||||||||||
Comment on lines
+389
to
+391
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems like good context to put on /// <div class="warning">
///
/// This will only be respected if [`Origin::line]` is also set
///
/// </div>
|
||||||||||||||||||
#[derive(Clone, Debug, Eq, PartialEq)] | ||||||||||||||||||
pub struct Origin<'a> { | ||||||||||||||||||
pub(crate) origin: &'a str, | ||||||||||||||||||
pub(crate) line: Option<usize>, | ||||||||||||||||||
|
@@ -412,17 +425,42 @@ impl<'a> Origin<'a> { | |||||||||||||||||
/// Set the default column to display | ||||||||||||||||||
/// | ||||||||||||||||||
/// Otherwise this will be inferred from the primary [`Annotation`] | ||||||||||||||||||
/// | ||||||||||||||||||
/// <div class="warning"> | ||||||||||||||||||
/// | ||||||||||||||||||
/// When [`Origin`] is passed into [`Snippet::origin`], `char_column` is | ||||||||||||||||||
/// only be respected if [`Origin::line`] is also set. | ||||||||||||||||||
/// | ||||||||||||||||||
/// </div> | ||||||||||||||||||
Comment on lines
+428
to
+434
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should keep this comment There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wait,. isn't this always applicable, not just |
||||||||||||||||||
pub fn char_column(mut self, char_column: usize) -> Self { | ||||||||||||||||||
self.char_column = Some(char_column); | ||||||||||||||||||
self | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
/// <div class="warning"> | ||||||||||||||||||
/// | ||||||||||||||||||
/// When [`Origin`] is passed into [`Snippet::origin`], `primary` is | ||||||||||||||||||
/// respected as long as the first [`Origin`] in a [`Group`]. | ||||||||||||||||||
/// | ||||||||||||||||||
/// </div> | ||||||||||||||||||
pub fn primary(mut self, primary: bool) -> Self { | ||||||||||||||||||
self.primary = primary; | ||||||||||||||||||
self | ||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
impl<'a> From<&'a str> for Origin<'a> { | ||||||||||||||||||
fn from(origin: &'a str) -> Self { | ||||||||||||||||||
Self::new(origin) | ||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
impl<'a> From<&'a String> for Origin<'a> { | ||||||||||||||||||
fn from(origin: &'a String) -> Self { | ||||||||||||||||||
Self::new(origin) | ||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
fn newline_count(body: &str) -> usize { | ||||||||||||||||||
#[cfg(feature = "simd")] | ||||||||||||||||||
{ | ||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe that should be put in some docs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me know what you think of the docs I added
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But when would you use
Snippet::origin(Origin')
?