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

preserve newlines when padding snippets #181

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
12 changes: 11 additions & 1 deletion crates/core/src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,22 @@ pub(crate) fn adjust_padding<'a>(
};
let mut result = String::new();
let snippet = &src[range.start as usize..range.end as usize];
let mut lines = snippet.split('\n');
let mut lines = snippet.split('\n').peekable();
// assumes codebase uses spaces for indentation
let delta: isize = (new_padding as isize) - (pad_strip_amount as isize);
let padding = " ".repeat(pad_strip_amount);
let new_padding = " ".repeat(new_padding);
let mut index = offset;
if src[newline_index.unwrap_or_default()..range.start as usize]
.trim()
.is_empty()
&& lines.peek().is_some()
{
result.push('\n');
result.push_str(&new_padding);
adjust_ranges(substitutions, index, new_padding.len() as isize + 1);
}

result.push_str(lines.next().unwrap_or_default());
index += result.len();
for line in lines {
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 @@ -13608,3 +13608,34 @@ remove_unused_imports()"#
})
.unwrap();
}

#[test]
fn preserves_python_blocks() {
run_test_expected({
TestArgExpected {
pattern: r#"
|engine marzano(0.1)
|language python
|
|`def $method($args): $body` => `def $method($args): $body`
|"#
.trim_margin()
.unwrap(),
source: r#"
|def foo(args):
| print("bar")
| print("thing")
|"#
.trim_margin()
.unwrap(),
expected: r#"
|def foo(args):
| print("bar")
| print("thing")
|"#
.trim_margin()
.unwrap(),
}
})
.unwrap();
}
2 changes: 1 addition & 1 deletion vendor/tree-sitter-facade
Loading