Skip to content

Commit 68ffa8d

Browse files
committed
Revert "[Should revert?] transfer logic"
This reverts commit 5530066.
1 parent e7c76d6 commit 68ffa8d

File tree

2 files changed

+63
-77
lines changed

2 files changed

+63
-77
lines changed

src/expr.rs

Lines changed: 6 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1883,80 +1883,12 @@ pub(crate) fn rewrite_assign_rhs<S: Into<String>, R: Rewrite>(
18831883

18841884
pub(crate) fn rewrite_assign_rhs_expr<R: Rewrite>(
18851885
context: &RewriteContext<'_>,
1886-
mut lhs: String,
1886+
lhs: &str,
18871887
ex: &R,
1888-
local: Option<&ast::Local>,
18891888
shape: Shape,
18901889
rhs_tactics: RhsTactics,
1890+
has_rhs_comment: bool,
18911891
) -> Option<String> {
1892-
let mut has_rhs_comment = false;
1893-
1894-
if let Some(ast::Local {
1895-
ref pat,
1896-
ref ty,
1897-
ref init,
1898-
ref span,
1899-
..
1900-
}) = local
1901-
{
1902-
let base_span = if let Some(ref ty) = ty {
1903-
mk_sp(ty.span.hi(), span.hi())
1904-
} else {
1905-
mk_sp(pat.span.hi(), span.hi())
1906-
};
1907-
1908-
if let Some(ex) = init {
1909-
if let Some(offset) = context.snippet(base_span).find_uncommented("=") {
1910-
let base_span_lo = base_span.lo();
1911-
1912-
let assign_lo = base_span_lo + BytePos(offset as u32);
1913-
let comment_start_pos = if let Some(ref ty) = ty {
1914-
ty.span.hi()
1915-
} else {
1916-
pat.span.hi()
1917-
};
1918-
let comment_before_assign =
1919-
context.snippet(mk_sp(comment_start_pos, assign_lo)).trim();
1920-
1921-
let assign_hi = base_span_lo + BytePos((offset + 1) as u32);
1922-
let rhs_span_lo = ex.span.lo();
1923-
let comment_end_pos = if ex.attrs.is_empty() {
1924-
rhs_span_lo
1925-
} else {
1926-
let attr_span_lo = ex.attrs.first().unwrap().span.lo();
1927-
// for the case using block
1928-
// ex. let x = { #![my_attr]do_something(); }
1929-
if rhs_span_lo < attr_span_lo {
1930-
rhs_span_lo
1931-
} else {
1932-
attr_span_lo
1933-
}
1934-
};
1935-
let comment_after_assign =
1936-
context.snippet(mk_sp(assign_hi, comment_end_pos)).trim();
1937-
1938-
if !comment_before_assign.is_empty() {
1939-
let pat_shape = shape.offset_left(4)?;
1940-
// 1 = ;
1941-
// let pat_shape = pat_shape.sub_width(1)?;
1942-
let new_indent_str = &pat_shape
1943-
.block_indent(0)
1944-
.to_string_with_newline(context.config);
1945-
lhs = format!("{}{}{}", comment_before_assign, new_indent_str, lhs);
1946-
}
1947-
1948-
if !comment_after_assign.is_empty() {
1949-
let new_indent_str =
1950-
&shape.block_indent(0).to_string_with_newline(context.config);
1951-
lhs.push_str(new_indent_str);
1952-
lhs.push_str(comment_after_assign);
1953-
lhs.push_str(new_indent_str);
1954-
has_rhs_comment = true;
1955-
}
1956-
}
1957-
}
1958-
}
1959-
19601892
let last_line_width = last_line_width(&lhs).saturating_sub(if lhs.contains('\n') {
19611893
shape.indent.width()
19621894
} else {
@@ -1968,15 +1900,14 @@ pub(crate) fn rewrite_assign_rhs_expr<R: Rewrite>(
19681900
offset: shape.offset + last_line_width + 1,
19691901
..shape
19701902
});
1971-
let rhs = choose_rhs(
1903+
choose_rhs(
19721904
context,
19731905
ex,
19741906
orig_shape,
19751907
ex.rewrite(context, orig_shape),
19761908
rhs_tactics,
19771909
has_rhs_comment,
1978-
)?;
1979-
Some(lhs + &rhs)
1910+
)
19801911
}
19811912

19821913
pub(crate) fn rewrite_assign_rhs_with<S: Into<String>, R: Rewrite>(
@@ -1987,7 +1918,8 @@ pub(crate) fn rewrite_assign_rhs_with<S: Into<String>, R: Rewrite>(
19871918
rhs_tactics: RhsTactics,
19881919
) -> Option<String> {
19891920
let lhs = lhs.into();
1990-
rewrite_assign_rhs_expr(context, lhs, ex, None, shape, rhs_tactics)
1921+
let rhs = rewrite_assign_rhs_expr(context, &lhs, ex, shape, rhs_tactics, false)?;
1922+
Some(lhs + &rhs)
19911923
}
19921924

19931925
fn choose_rhs<R: Rewrite>(

src/items.rs

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,71 @@ impl Rewrite for ast::Local {
111111

112112
result.push_str(&infix);
113113

114+
let mut has_comment_between_assign_and_rhs = false;
114115
if let Some(ref ex) = self.init {
116+
let base_span = if let Some(ref ty) = self.ty {
117+
mk_sp(ty.span.hi(), self.span.hi())
118+
} else {
119+
mk_sp(self.pat.span.hi(), self.span.hi())
120+
};
121+
122+
if let Some(offset) = context.snippet(base_span).find_uncommented("=") {
123+
let base_span_lo = base_span.lo();
124+
125+
let assign_lo = base_span_lo + BytePos(offset as u32);
126+
let comment_start_pos = if let Some(ref ty) = self.ty {
127+
ty.span.hi()
128+
} else {
129+
self.pat.span.hi()
130+
};
131+
let comment_before_assign =
132+
context.snippet(mk_sp(comment_start_pos, assign_lo)).trim();
133+
134+
let assign_hi = base_span_lo + BytePos((offset + 1) as u32);
135+
let rhs_span_lo = ex.span.lo();
136+
let comment_end_pos = if ex.attrs.is_empty() {
137+
rhs_span_lo
138+
} else {
139+
let attr_span_lo = ex.attrs.first().unwrap().span.lo();
140+
// for the case using block
141+
// ex. let x = { #![my_attr]do_something(); }
142+
if rhs_span_lo < attr_span_lo {
143+
rhs_span_lo
144+
} else {
145+
attr_span_lo
146+
}
147+
};
148+
let comment_after_assign =
149+
context.snippet(mk_sp(assign_hi, comment_end_pos)).trim();
150+
151+
if !comment_before_assign.is_empty() {
152+
let new_indent_str = &pat_shape
153+
.block_indent(0)
154+
.to_string_with_newline(context.config);
155+
result = format!("{}{}{}", comment_before_assign, new_indent_str, result);
156+
}
157+
158+
if !comment_after_assign.is_empty() {
159+
let new_indent_str =
160+
&shape.block_indent(0).to_string_with_newline(context.config);
161+
result.push_str(new_indent_str);
162+
result.push_str(comment_after_assign);
163+
result.push_str(new_indent_str);
164+
has_comment_between_assign_and_rhs = true;
165+
}
166+
}
167+
115168
// 1 = trailing semicolon;
116169
let nested_shape = shape.sub_width(1)?;
117-
result = rewrite_assign_rhs_expr(
170+
let rhs = rewrite_assign_rhs_expr(
118171
context,
119-
result,
172+
&result,
120173
&**ex,
121-
Some(&self),
122174
nested_shape,
123175
RhsTactics::Default,
176+
has_comment_between_assign_and_rhs,
124177
)?;
178+
result = result + &rhs;
125179
}
126180

127181
result.push(';');

0 commit comments

Comments
 (0)