Skip to content

Commit

Permalink
Revert "Account for trailing comments in span handling (#527)"
Browse files Browse the repository at this point in the history
This reverts commit c950bdc.

Fixes #634
Re-open #464
  • Loading branch information
panekj committed Jul 27, 2024
1 parent 990f0b1 commit 4d08035
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 81 deletions.
4 changes: 4 additions & 0 deletions crates/taplo/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## 0 13.2

This comment has been minimized.

Copy link
@rami3l

rami3l Jul 27, 2024

Nit: this should be 0.13.2.


- Revert "Account for trailing comments in span handling (#527)" to fix sorting regression

## 0.13.1

### Fixes
Expand Down
47 changes: 1 addition & 46 deletions crates/taplo/src/dom/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,6 @@ impl Node {
Ok(all.into_iter())
}

/// Determine the portions of the source that correspond to this
/// `Node`.
///
/// Since a symbol may appear in multiple places, a single node
/// may correspond to multiple `TextRange`s.
pub fn text_ranges(&self) -> impl ExactSizeIterator<Item = TextRange> {
let mut ranges = Vec::with_capacity(1);

Expand All @@ -261,20 +256,7 @@ impl Node {
ranges.extend(entry.text_ranges());
}

// consider both v.syntax() and its parent node when
// broadening the range. v.syntax() is only the
// TableHeader, where its parent is the full Table
// syntax node. Take a range which encompasses both.
let syntax_range = match (
v.syntax().map(|n| n.text_range()),
v.syntax().and_then(|h| h.parent()).map(|n| n.text_range()),
) {
(Some(child), Some(parent)) => Some(child.cover(parent)),
(Some(child), None) => Some(child),
(None, Some(parent)) => Some(parent),
_ => None,
};
if let Some(mut r) = syntax_range {
if let Some(mut r) = v.syntax().map(|s| s.text_range()) {
for range in &ranges {
r = r.cover(*range);
}
Expand Down Expand Up @@ -677,30 +659,3 @@ impl From<Invalid> for Node {
Self::Invalid(v)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_dom_find_all_matches_includes_comments() {
let source = r#"
[table]
key = "value" # comment
"#;
let dom = crate::parser::parse(source).into_dom();
let matches = dom
.find_all_matches("table".parse().unwrap(), false)
.unwrap()
.collect::<Vec<_>>();
assert_eq!(1, matches.len());
let (_, node) = matches.first().unwrap();

// Ensure that at least one of the resultant spans includes
// the comment.
let spans = node.text_ranges().map(|r| &source[r]).collect::<Vec<_>>();
assert!(
spans.iter().any(|s| s.contains('#')),
"expected one of {:?} to contain a comment",
&spans
);
}
}
35 changes: 0 additions & 35 deletions crates/taplo/src/tests/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1026,41 +1026,6 @@ foo = [
assert_format!(expected, &formatted);
}

/// See https://github.com/tamasfe/taplo/issues/464
#[test]
fn test_reorder_keys_trailing_comment() {
let src = r#"
[mytable]
a = "a"
c = "c"
b = "b" # ...
"#;

let expected = r#"
[mytable]
a = "a"
b = "b" # ...
c = "c"
"#;

let p = crate::parser::parse(src);
let formatted = crate::formatter::format_with_path_scopes(
p.into_dom(),
Default::default(),
&[],
vec![(
"mytable",
formatter::OptionsIncomplete {
reorder_keys: Some(true),
..Default::default()
},
)],
)
.unwrap();

assert_format!(expected, &formatted);
}

#[test]
fn test_single_comment_no_alignment() {
let src = r#"
Expand Down

0 comments on commit 4d08035

Please sign in to comment.