Skip to content

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Muscraft
Copy link
Member

This partially addresses #208 by making Snippet::origin use Origin internally. This makes the distinction a little clearer: you should use Snippet::origin if you are using a Snippet and Origin on its own if you need the file details.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should use Snippet::origin if you are using a Snippet and Origin on its own if you need the file details.

maybe that should be put in some docs?

Copy link
Member Author

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

Copy link
Contributor

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')?

Comment on lines +210 to 217
pub fn origin(mut self, origin: impl Into<Origin<'a>>) -> Self {
self.origin = Some(origin.into());
self
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_primary is respected unless the Origin is the "primary origin" for a Group (I think).

line is respected if it is set.

char_column is respected if line is set; if line is not set, it is overridden.

src/snippet.rs Outdated
Comment on lines 387 to 400
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)
}
}

impl<'a> Origin<'a> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I tend to put trait impls after the regular functions as they tend to be more of the focus for a type

src/snippet.rs Outdated
Comment on lines 203 to 207
/// If just a location is passed (i.e. a string) in the line and column
/// numbers are automatically inferred. If you want to explicitly set the
/// line and column you can create an [`Origin`] and pass it in.
///
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// If just a location is passed (i.e. a string) in the line and column
/// numbers are automatically inferred. If you want to explicitly set the
/// line and column you can create an [`Origin`] and pass it in.
///
/// 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`]
Comment on lines +388 to +391
/// 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like good context to put on Origin::char_column

/// <div class="warning">
///
/// This will only be respected if [`Origin::line]` is also set
///
/// </div>
src/snippet.rs Outdated
Comment on lines 384 to 386
/// This should be used if you want to display the file details by
/// itself, or you want to set the line number and column explicitly.
/// Otherwise, it is better to use [`Snippet::origin`].
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be clarified that you mean that this is a parallel item to Snippet (can't remember all of the terms and not digging in to provide a suggestion atm)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was referring to [Element::Origin]

@Muscraft Muscraft force-pushed the use-origin branch 3 times, most recently from 30d349a to 960ef89 Compare June 25, 2025 23:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants