Skip to content

Commit f897e25

Browse files
committed
fix to extract comments to stop internal error
refactoring
1 parent ed7c032 commit f897e25

File tree

2 files changed

+87
-11
lines changed

2 files changed

+87
-11
lines changed

src/expr.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,14 +1881,14 @@ pub(crate) fn rewrite_assign_rhs<S: Into<String>, R: Rewrite>(
18811881
rewrite_assign_rhs_with(context, lhs, ex, shape, RhsTactics::Default)
18821882
}
18831883

1884-
pub(crate) fn rewrite_assign_rhs_with<S: Into<String>, R: Rewrite>(
1884+
pub(crate) fn rewrite_assign_rhs_expr<R: Rewrite>(
18851885
context: &RewriteContext<'_>,
1886-
lhs: S,
1886+
lhs: &str,
18871887
ex: &R,
18881888
shape: Shape,
18891889
rhs_tactics: RhsTactics,
1890+
has_rhs_comment: bool,
18901891
) -> Option<String> {
1891-
let lhs = lhs.into();
18921892
let last_line_width = last_line_width(&lhs).saturating_sub(if lhs.contains('\n') {
18931893
shape.indent.width()
18941894
} else {
@@ -1900,13 +1900,25 @@ pub(crate) fn rewrite_assign_rhs_with<S: Into<String>, R: Rewrite>(
19001900
offset: shape.offset + last_line_width + 1,
19011901
..shape
19021902
});
1903-
let rhs = choose_rhs(
1903+
choose_rhs(
19041904
context,
19051905
ex,
19061906
orig_shape,
19071907
ex.rewrite(context, orig_shape),
19081908
rhs_tactics,
1909-
)?;
1909+
has_rhs_comment,
1910+
)
1911+
}
1912+
1913+
pub(crate) fn rewrite_assign_rhs_with<S: Into<String>, R: Rewrite>(
1914+
context: &RewriteContext<'_>,
1915+
lhs: S,
1916+
ex: &R,
1917+
shape: Shape,
1918+
rhs_tactics: RhsTactics,
1919+
) -> Option<String> {
1920+
let lhs = lhs.into();
1921+
let rhs = rewrite_assign_rhs_expr(context, &lhs, ex, shape, rhs_tactics, false)?;
19101922
Some(lhs + &rhs)
19111923
}
19121924

@@ -1916,6 +1928,7 @@ fn choose_rhs<R: Rewrite>(
19161928
shape: Shape,
19171929
orig_rhs: Option<String>,
19181930
rhs_tactics: RhsTactics,
1931+
has_rhs_comment: bool,
19191932
) -> Option<String> {
19201933
match orig_rhs {
19211934
Some(ref new_str)
@@ -1932,13 +1945,14 @@ fn choose_rhs<R: Rewrite>(
19321945
.indent
19331946
.block_indent(context.config)
19341947
.to_string_with_newline(context.config);
1948+
let before_space_str = if has_rhs_comment { "" } else { " " };
19351949

19361950
match (orig_rhs, new_rhs) {
19371951
(Some(ref orig_rhs), Some(ref new_rhs))
19381952
if wrap_str(new_rhs.clone(), context.config.max_width(), new_shape)
19391953
.is_none() =>
19401954
{
1941-
Some(format!(" {}", orig_rhs))
1955+
Some(format!("{}{}", before_space_str, orig_rhs))
19421956
}
19431957
(Some(ref orig_rhs), Some(ref new_rhs))
19441958
if prefer_next_line(orig_rhs, new_rhs, rhs_tactics) =>
@@ -1948,10 +1962,11 @@ fn choose_rhs<R: Rewrite>(
19481962
(None, Some(ref new_rhs)) => Some(format!("{}{}", new_indent_str, new_rhs)),
19491963
(None, None) if rhs_tactics == RhsTactics::AllowOverflow => {
19501964
let shape = shape.infinite_width();
1951-
expr.rewrite(context, shape).map(|s| format!(" {}", s))
1965+
expr.rewrite(context, shape)
1966+
.map(|s| format!("{}{}", before_space_str, s))
19521967
}
19531968
(None, None) => None,
1954-
(Some(orig_rhs), _) => Some(format!(" {}", orig_rhs)),
1969+
(Some(orig_rhs), _) => Some(format!("{}{}", before_space_str, orig_rhs)),
19551970
}
19561971
}
19571972
}

src/items.rs

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ use crate::comment::{
1818
use crate::config::lists::*;
1919
use crate::config::{BraceStyle, Config, IndentStyle, Version};
2020
use crate::expr::{
21-
is_empty_block, is_simple_block_stmt, rewrite_assign_rhs, rewrite_assign_rhs_with, RhsTactics,
21+
is_empty_block, is_simple_block_stmt, rewrite_assign_rhs, rewrite_assign_rhs_expr,
22+
rewrite_assign_rhs_with, RhsTactics,
2223
};
2324
use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
2425
use crate::macros::{rewrite_macro, MacroPosition};
@@ -110,11 +111,71 @@ impl Rewrite for ast::Local {
110111

111112
result.push_str(&infix);
112113

114+
let mut has_comment_between_assign_and_rhs = false;
113115
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+
114168
// 1 = trailing semicolon;
115169
let nested_shape = shape.sub_width(1)?;
116-
117-
result = rewrite_assign_rhs(context, result, &**ex, nested_shape)?;
170+
let rhs = rewrite_assign_rhs_expr(
171+
context,
172+
&result,
173+
&**ex,
174+
nested_shape,
175+
RhsTactics::Default,
176+
has_comment_between_assign_and_rhs,
177+
)?;
178+
result = result + &rhs;
118179
}
119180

120181
result.push(';');

0 commit comments

Comments
 (0)