Skip to content

Commit

Permalink
address comments and typos
Browse files Browse the repository at this point in the history
  • Loading branch information
nbacquey committed Dec 2, 2024
1 parent 30c029e commit 8ea857b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
35 changes: 22 additions & 13 deletions topiary-core/src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ use crate::{

/// When you remove a block of text from the input, it changes the positions of every subsequent character.
/// This is what this Diff instance does.
impl Diff<InputSection, FormatterError> for Position {
fn substract(self: &mut Self, other: &InputSection) -> FormatterResult<()> {
impl Diff<InputSection> for Position {
type ErrorType = FormatterError;

fn subtract(self: &mut Self, other: &InputSection) -> FormatterResult<()> {
if *self <= other.start {
// The point is before the removed block: nothing happens.
Ok(())
Expand Down Expand Up @@ -47,15 +49,17 @@ impl Diff<InputSection, FormatterError> for Position {
} else {
// The point is within the removed block: fail, as the point will not exist
// in the new version of the input.
Err(FormatterError::Internal("Tried to substract a section from a point it contains".into(), None))
Err(FormatterError::Internal("Tried to subtract a section from a point it contains".into(), None))
}
}
}

impl Diff<InputSection, FormatterError> for InputSection {
fn substract(self: &mut Self, other: &Self) -> FormatterResult<()> {
self.start.substract(other)?;
self.end.substract(other)
impl Diff<InputSection> for InputSection {
type ErrorType = FormatterError;

fn subtract(self: &mut Self, other: &Self) -> FormatterResult<()> {
self.start.subtract(other)?;
self.end.subtract(other)
}
}

Expand All @@ -79,7 +83,7 @@ fn find_comments<'a>(node: Node<'a>, input: &str, comments: & mut Vec<AnchoredCo

/// The section of code to which a comment refers. We also remember whether the comment
/// is positioned before or after the section.
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Debug)]
pub enum Commented {
/// The code section is before the comment, as in:
/// ```
Expand All @@ -101,14 +105,16 @@ pub enum Commented {
CommentedAfter(InputSection),
}

impl Diff<InputSection, FormatterError> for Commented {
fn substract(self: &mut Self, other: &InputSection) -> FormatterResult<()> {
impl Diff<InputSection> for Commented {
type ErrorType = FormatterError;

fn subtract(self: &mut Self, other: &InputSection) -> FormatterResult<()> {
match self {
Commented::CommentedBefore(section) => {
section.substract(other)
section.subtract(other)
},
Commented::CommentedAfter(section) => {
section.substract(other)
section.subtract(other)
},
}
}
Expand Down Expand Up @@ -234,6 +240,9 @@ pub fn extract_comments<'a>(
// 1) remove the comment from the input,
// 2) register an InputEdit to modify the tree,
// 3) edit the following anchors to account for the removed comment.
//
// The order is reversed so that all InputEdits can be applied in succession:
// one will not affect the others.
for index in (0..anchors.len()).rev() {
let AnchoredComment{comment, ..} = &anchors[index];
// 1)
Expand All @@ -251,7 +260,7 @@ pub fn extract_comments<'a>(
// 3)
for following_index in index..anchors.len() {
let AnchoredComment { commented: mut following_anchor, .. } = &anchors[following_index];
following_anchor.substract(&comment.into());
following_anchor.subtract(&comment.into());
}
}
for edit in edits {
Expand Down
8 changes: 5 additions & 3 deletions topiary-core/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ impl From<&Node<'_>> for InputSection {
}
}

/// A generic trait to substract stuff from other stuff. The function can be partial.
/// A generic trait to subtract stuff from other stuff. The function can be partial.
/// In practice, it will be used to update text positions within the input,
/// when removing parts of it.
pub trait Diff<T,E> {
fn substract(self: &mut Self, other: &T) -> Result<(), E>;
pub trait Diff<T> {
type ErrorType;

fn subtract(self: &mut Self, other: &T) -> Result<(), Self::ErrorType>;
}

0 comments on commit 8ea857b

Please sign in to comment.