Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle empty lines for languages with semantic whitespace #99

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions crates/core/src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,17 @@ fn pad_snippet(padding: &str, snippet: &str) -> String {
result
}

fn adjust_ranges(substitutions: &mut [(EffectRange, String)], index: usize, delta: isize) {
for (EffectRange { range, .. }, _) in substitutions.iter_mut() {
if range.start >= index {
range.start = (range.start as isize + delta) as usize;
}
if range.end >= index {
range.end = (range.end as isize + delta) as usize;
}
}
}

// in multiline snippets, remove padding from every line equal to the padding of the first line,
// such that the first line is left-aligned.
pub(crate) fn adjust_padding<'a>(
Expand Down Expand Up @@ -136,14 +147,11 @@ pub(crate) fn adjust_padding<'a>(
for line in lines {
result.push('\n');
index += 1;
for (EffectRange { range, .. }, _) in substitutions.iter_mut() {
if range.start >= index {
range.start = (range.start as isize + delta) as usize;
}
if range.end >= index {
range.end = (range.end as isize + delta) as usize;
}
if line.trim().is_empty() {
adjust_ranges(substitutions, index, -(line.len() as isize));
continue;
}
adjust_ranges(substitutions, index, delta);
let line = line.strip_prefix(&padding).ok_or_else(|| {
anyhow!(
"expected line \n{}\n to start with {} spaces, code is either not indented with spaces, or does not consistently indent code blocks",
Expand Down
31 changes: 31 additions & 0 deletions crates/core/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12885,3 +12885,34 @@ fn limit_export_default_match() {
})
.unwrap();
}

#[test]
fn python_support_empty_line() {
run_test_expected(TestArgExpected {
pattern: r#"
|engine marzano(0.1)
|language python
|`class $name: $body` => $body
|"#
.trim_margin()
.unwrap(),
source: r#"
|class MyClass:
| def function(self):
| result = 1 + 1
|
| return result
|"#
.trim_margin()
.unwrap(),
expected: r#"
|def function(self):
| result = 1 + 1
|
| return result
|"#
.trim_margin()
.unwrap(),
})
.unwrap();
}
Loading