Stop appending newlines to files when applying diffs

This commit is contained in:
Matt Rubens
2024-12-11 12:34:53 -05:00
parent ba8bed90d8
commit 413f10650f
2 changed files with 111 additions and 10 deletions

View File

@@ -124,17 +124,18 @@ export class DiffViewProvider {
edit.delete(document.uri, new vscode.Range(this.streamedLines.length, 0, document.lineCount, 0))
await vscode.workspace.applyEdit(edit)
}
// Add empty last line if original content had one
// Preserve empty last line if original content had one
const hasEmptyLastLine = this.originalContent?.endsWith("\n")
if (hasEmptyLastLine) {
const accumulatedLines = accumulatedContent.split("\n")
if (accumulatedLines[accumulatedLines.length - 1] !== "") {
accumulatedContent += "\n"
}
if (hasEmptyLastLine && !accumulatedContent.endsWith("\n")) {
accumulatedContent += "\n"
}
// Clear all decorations at the end (before applying final edit)
this.fadedOverlayController.clear()
this.activeLineController.clear()
// Apply the final content
const finalEdit = new vscode.WorkspaceEdit()
finalEdit.replace(document.uri, new vscode.Range(0, 0, document.lineCount, 0), accumulatedContent)
await vscode.workspace.applyEdit(finalEdit)
// Clear all decorations at the end (after applying final edit)
this.fadedOverlayController.clear()
this.activeLineController.clear()
}
}
@@ -351,4 +352,4 @@ export class DiffViewProvider {
this.streamedLines = []
this.preDiagnostics = []
}
}
}