Skip to content

Commit

Permalink
fix: translate tabs in snippet to 4 spaces (#356)
Browse files Browse the repository at this point in the history
This replaces `\t` in code snippets with 4 spaces. If you'd prefer a
number of spaces different than 4 please stop using this cursed
character.

Fixes #355
  • Loading branch information
mfontanini authored Aug 21, 2024
2 parents 9e87136 + d8b9136 commit 778f03e
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/processing/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl<'a> CodePreparer<'a> {
let padding = " ".repeat(horizontal_padding as usize);
let padder = NumberPadder::new(code.visible_lines(self.hidden_line_prefix).count());
for (index, line) in code.visible_lines(self.hidden_line_prefix).enumerate() {
let mut line = line.to_string();
let mut line = line.replace('\t', " ");
let mut prefix = padding.clone();
if code.attributes.line_numbers {
let line_number = index + 1;
Expand Down Expand Up @@ -770,4 +770,11 @@ println!("Hello world");
let code = Snippet { contents, language: SnippetLanguage::Rust, attributes: Default::default() };
assert_eq!(expected, code.executable_contents(Some("# ")));
}

#[test]
fn tabs_in_snippet() {
let snippet = Snippet { contents: "\thi".into(), language: SnippetLanguage::C, attributes: Default::default() };
let lines = CodePreparer::new(&Default::default(), None).prepare(&snippet);
assert_eq!(lines[0].code, " hi\n");
}
}

0 comments on commit 778f03e

Please sign in to comment.