Skip to content

Commit ffd854b

Browse files
committed
Handle tabs
1 parent 026f1fb commit ffd854b

File tree

4 files changed

+36
-11
lines changed

4 files changed

+36
-11
lines changed

src/comment.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ use crate::rewrite::RewriteContext;
1010
use crate::shape::{Indent, Shape};
1111
use crate::string::{rewrite_string, StringFormat};
1212
use crate::utils::{
13-
count_newlines, first_line_width, last_line_width, trim_left_preserve_layout, unicode_str_width,
13+
count_newlines, first_line_width, last_line_width, tab_to_spaces, trim_left_preserve_layout,
14+
unicode_str_width,
1415
};
1516
use crate::{ErrorKind, FormattingError};
1617

@@ -1478,20 +1479,27 @@ pub(crate) struct CommentCodeSlices<'a> {
14781479
slice: &'a str,
14791480
ungrouped_code_slices: MultiPeek<UngroupedCommentCodeSlices<'a>>,
14801481
offset: Option<usize>,
1482+
tab_spaces: usize,
14811483
}
14821484

14831485
impl<'a> CommentCodeSlices<'a> {
14841486
pub(crate) fn new(slice: &'a str) -> CommentCodeSlices<'a> {
14851487
CommentCodeSlices {
14861488
slice,
1489+
tab_spaces: 4,
14871490
ungrouped_code_slices: multipeek(UngroupedCommentCodeSlices::new(slice)),
14881491
offset: None,
14891492
}
14901493
}
14911494

1492-
pub(crate) fn with_offset(slice: &'a str, offset: usize) -> CommentCodeSlices<'a> {
1495+
pub(crate) fn with_offset(
1496+
slice: &'a str,
1497+
offset: usize,
1498+
tab_spaces: usize,
1499+
) -> CommentCodeSlices<'a> {
14931500
CommentCodeSlices {
14941501
slice,
1502+
tab_spaces,
14951503
ungrouped_code_slices: multipeek(UngroupedCommentCodeSlices::new(slice)),
14961504
offset: Some(offset).filter(|o| *o != 0),
14971505
}
@@ -1519,7 +1527,7 @@ impl<'a> Iterator for CommentCodeSlices<'a> {
15191527
}
15201528
CodeCharKind::Comment => break,
15211529
CodeCharKind::Normal if s.trim().is_empty() && count_newlines(s) == 0 => {
1522-
let indent_width = s.len();
1530+
let indent_width = tab_to_spaces(s, self.tab_spaces);
15231531
if self.offset.map_or(false, |comment_offset| {
15241532
!(indent_width < comment_offset + 2 && comment_offset < indent_width + 2)
15251533
}) {
@@ -1755,13 +1763,20 @@ if x == 3 {
17551763
let mut iter = CommentCodeSlices::new(input);
17561764

17571765
assert_eq!(
1758-
(CodeCharKind::Normal, 0, r#"
1766+
(
1767+
CodeCharKind::Normal,
1768+
0,
1769+
r#"
17591770
if x == 3 {
17601771
x = 4;
1761-
} "#),
1772+
} "#
1773+
),
1774+
iter.next().unwrap()
1775+
);
1776+
assert_eq!(
1777+
(CodeCharKind::Comment, 26, "// if x == 3\n",),
17621778
iter.next().unwrap()
17631779
);
1764-
assert_eq!((CodeCharKind::Comment, 26, "// if x == 3\n",), iter.next().unwrap());
17651780
assert_eq!(
17661781
(CodeCharKind::Comment, 39, "// end of block\n"),
17671782
iter.next().unwrap()

src/missed_spans.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,11 @@ impl<'a> FmtVisitor<'a> {
186186
let last_line_offset = if last_line_contains_single_line_comment(&self.buffer) {
187187
0
188188
} else {
189-
last_line_width(&self.buffer)
189+
last_line_width(&&self.buffer)
190190
};
191-
for (kind, offset, subslice) in CommentCodeSlices::with_offset(snippet, last_line_offset) {
191+
for (kind, offset, subslice) in
192+
CommentCodeSlices::with_offset(snippet, last_line_offset, self.config.tab_spaces())
193+
{
192194
debug!("{:?}: {:?}", kind, subslice);
193195

194196
let (lf_count, crlf_count, within_file_lines_range) =

src/utils.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,12 @@ fn get_prefix_space_width(config: &Config, s: &str) -> usize {
638638
width
639639
}
640640

641+
pub(crate) fn tab_to_spaces(s: &str, tab_spaces: usize) -> usize {
642+
s.chars()
643+
.map(|s| if s == '\t' { tab_spaces } else { 1 })
644+
.sum()
645+
}
646+
641647
pub(crate) trait NodeIdExt {
642648
fn root() -> Self;
643649
}

src/visitor.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,11 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
251251
} else {
252252
last_line_width(&self.buffer)
253253
};
254-
for (kind, offset, sub_slice) in
255-
CommentCodeSlices::with_offset(self.snippet(span), last_line_offset)
256-
{
254+
for (kind, offset, sub_slice) in CommentCodeSlices::with_offset(
255+
self.snippet(span),
256+
last_line_offset,
257+
self.config.tab_spaces(),
258+
) {
257259
let sub_slice = transform_missing_snippet(config, sub_slice);
258260
debug!("close_block: {:?} {:?} {:?}", kind, offset, sub_slice);
259261

0 commit comments

Comments
 (0)