Skip to content
This repository was archived by the owner on Nov 24, 2023. It is now read-only.

Commit b883480

Browse files
committed
First try at replacing partials lines
Concerns #1
1 parent dd49d9f commit b883480

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/main.rs

+19-3
Original file line numberDiff line numberDiff line change
@@ -288,14 +288,30 @@ fn apply_suggestion(suggestion: &Suggestion) -> Result<(), ProgramError> {
288288
.join("\n"));
289289
new_content.push_str("\n");
290290

291-
// TODO(killercup): Replace sections of lines only
292-
new_content.push_str(&indent((suggestion.line_range.start.column - 1) as u32,
293-
suggestion.replacement.trim()));
291+
// Parts of line before replacement
292+
new_content.push_str(&file_content.lines()
293+
.nth(suggestion.line_range.start.line - 1)
294+
.unwrap_or("")
295+
.chars()
296+
.take(suggestion.line_range.start.column - 1)
297+
.collect::<String>());
298+
299+
// Insert new content! Finally!
300+
new_content.push_str(&suggestion.replacement);
294301

302+
// TODO(killercup): better handling of trailing semicolons
295303
if suggestion.text.trim().ends_with(';') && !suggestion.replacement.trim().ends_with(';') {
296304
new_content.push_str(";");
297305
}
298306

307+
// Parts of line after replacement
308+
new_content.push_str(&file_content.lines()
309+
.nth(suggestion.line_range.end.line - 1)
310+
.unwrap_or("")
311+
.chars()
312+
.skip(suggestion.line_range.end.column)
313+
.collect::<String>());
314+
299315
// Add the lines after the section we want to replace
300316
new_content.push_str("\n");
301317
new_content.push_str(&file_content.lines()

0 commit comments

Comments
 (0)