Skip to content

Commit

Permalink
First try at replacing partials lines
Browse files Browse the repository at this point in the history
Concerns #1
  • Loading branch information
killercup committed Jul 9, 2016
1 parent dd49d9f commit b883480
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,14 +288,30 @@ fn apply_suggestion(suggestion: &Suggestion) -> Result<(), ProgramError> {
.join("\n"));
new_content.push_str("\n");

// TODO(killercup): Replace sections of lines only
new_content.push_str(&indent((suggestion.line_range.start.column - 1) as u32,
suggestion.replacement.trim()));
// Parts of line before replacement
new_content.push_str(&file_content.lines()
.nth(suggestion.line_range.start.line - 1)
.unwrap_or("")
.chars()
.take(suggestion.line_range.start.column - 1)
.collect::<String>());

// Insert new content! Finally!
new_content.push_str(&suggestion.replacement);

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

// Parts of line after replacement
new_content.push_str(&file_content.lines()
.nth(suggestion.line_range.end.line - 1)
.unwrap_or("")
.chars()
.skip(suggestion.line_range.end.column)
.collect::<String>());

// Add the lines after the section we want to replace
new_content.push_str("\n");
new_content.push_str(&file_content.lines()
Expand Down

0 comments on commit b883480

Please sign in to comment.