fix: instant apply for search and replace - vs code and jetbrains

This commit is contained in:
Dallin Romney 2025-10-27 18:43:09 -07:00
parent ab214e3abc
commit d227a571bb
10 changed files with 199 additions and 28 deletions

View file

@ -70,6 +70,7 @@ import {
import { MCPManagerSingleton } from "./context/mcp/MCPManagerSingleton";
import { performAuth, removeMCPAuth } from "./context/mcp/MCPOauth";
import { setMdmLicenseKey } from "./control-plane/mdm/mdm";
import { myersDiff } from "./diff/myers";
import { ApplyAbortManager } from "./edit/applyAbortManager";
import { streamDiffLines } from "./edit/streamDiffLines";
import { shouldIgnore } from "./indexing/shouldIgnore";
@ -796,6 +797,10 @@ export class Core {
);
});
on("getDiffLines", (msg) => {
return myersDiff(msg.data.oldContent, msg.data.newContent);
});
on("cancelApply", async (msg) => {
const abortManager = ApplyAbortManager.getInstance();
abortManager.clear(); // for now abort all streams

View file

@ -241,6 +241,7 @@ export type ToCoreFromIdeOrWebviewProtocol = {
AsyncGenerator<ChatMessage, PromptLog>,
];
streamDiffLines: [StreamDiffLinesPayload, AsyncGenerator<DiffLine>];
getDiffLines: [{ oldContent: string; newContent: string }, DiffLine[]];
"llm/compileChat": [
{ messages: ChatMessage[]; options: LLMFullCompletionOptions },
CompiledMessagesResult,

View file

@ -122,6 +122,7 @@ class MessageTypes {
"llm/listModels",
"llm/compileChat",
"streamDiffLines",
"getDiffLines",
"chatDescriber/describe",
"conversation/compact",
"stats/getTokensPerDay",

View file

@ -83,17 +83,7 @@ class ApplyToFileHandler(
val diffStreamHandler = createDiffStreamHandler(editorUtils.editor, startLine, endLine)
diffStreamService.register(diffStreamHandler, editorUtils.editor)
// Stream the diffs between current and new content
// For search/replace, we pass the new content as "input" and current as "highlighted"
diffStreamHandler.streamDiffLinesToEditor(
input = newContent, // The new content (full rewrite)
prefix = "", // No prefix since we're rewriting the whole file
highlighted = currentContent, // Current file content
suffix = "", // No suffix since we're rewriting the whole file
modelTitle = null, // No model needed for search/replace instant apply
includeRulesInSystemMessage = false, // No LLM involved, just diff generation
isApply = true
)
diffStreamHandler.instantApplyMyersDiff(currentContent, newContent)
}
private fun notifyStreamStarted() {

View file

@ -3,6 +3,7 @@ package com.github.continuedev.continueintellijextension.editor
import com.github.continuedev.continueintellijextension.ApplyState
import com.github.continuedev.continueintellijextension.ApplyStateStatus
import com.github.continuedev.continueintellijextension.StreamDiffLinesPayload
import com.github.continuedev.continueintellijextension.GetDiffLinesPayload
import com.github.continuedev.continueintellijextension.browser.ContinueBrowserService.Companion.getBrowser
import com.github.continuedev.continueintellijextension.services.ContinuePluginService
import com.intellij.openapi.application.ApplicationManager
@ -131,6 +132,111 @@ class DiffStreamHandler(
}
}
fun instantApplyDiffLines(
currentContent: String,
newContent: String
) {
isRunning = true
sendUpdate(ApplyStateStatus.STREAMING)
project.service<ContinuePluginService>().coreMessenger?.request(
"getDiffLines",
GetDiffLinesPayload(
oldContent=currentContent,
newContent=newContent
),
null
) { response ->
if (!isRunning) return@request
val diffLines = response as List<Map<String, Any>>
ApplicationManager.getApplication().invokeLater {
WriteCommandAction.runWriteCommandAction(project) {
applyAllDiffLines(diffLines)
createDiffBlocksFromDiffLines(diffLines)
cleanupProgressHighlighters()
if (diffBlocks.isEmpty()) {
setClosed()
} else {
sendUpdate(ApplyStateStatus.DONE)
}
onFinish()
}
}
}
}
private fun applyAllDiffLines(diffLines: List<Map<String, Any>>) {
var currentLine = startLine
diffLines.forEach { line ->
val type = getDiffLineType(line["type"] as String)
val text = line["line"] as String
when (type) {
DiffLineType.OLD -> {
// Delete line
val document = editor.document
val start = document.getLineStartOffset(currentLine)
val end = document.getLineEndOffset(currentLine)
document.deleteString(start, if (currentLine < document.lineCount - 1) end + 1 else end)
}
DiffLineType.NEW -> {
// Insert line
val offset = editor.document.getLineStartOffset(currentLine)
editor.document.insertString(offset, text + "\n")
currentLine++
}
DiffLineType.SAME -> {
currentLine++
}
}
}
}
private fun createDiffBlocksFromDiffLines(diffLines: List<Map<String, Any>>) {
var currentBlock: VerticalDiffBlock? = null
var currentLine = startLine
diffLines.forEach { line ->
val type = getDiffLineType(line["type"] as String)
val text = line["line"] as String
when (type) {
DiffLineType.OLD -> {
if (currentBlock == null) {
currentBlock = createDiffBlock()
currentBlock!!.startLine = currentLine
}
currentBlock!!.deletedLines.add(text)
}
DiffLineType.NEW -> {
if (currentBlock == null) {
currentBlock = createDiffBlock()
currentBlock!!.startLine = currentLine
}
currentBlock!!.addedLines.add(text)
currentLine++
}
DiffLineType.SAME -> {
if (currentBlock != null) {
currentBlock!!.onLastDiffLine()
currentBlock = null
}
currentLine++
}
}
}
// Handle last block if it doesn't end with SAME
currentBlock?.onLastDiffLine()
}
private fun initUnfinishedRangeHighlights() {
val editorUtils = EditorUtils(editor)
val unfinishedKey = editorUtils.createTextAttributesKey("CONTINUE_DIFF_UNFINISHED_LINE", 0x20888888)

View file

@ -310,6 +310,11 @@ data class StreamDiffLinesPayload(
val isApply: Boolean
)
data class GetDiffLinesPayload(
val oldContent: String,
val newContent: String,
)
data class AcceptOrRejectDiffPayload(
val filepath: String? = null,
val streamId: String? = null

View file

@ -58,13 +58,9 @@ export class ApplyManager {
// Currently `isSearchAndReplace` will always provide a full file rewrite
// as the contents of `text`, so we can just instantly apply
if (isSearchAndReplace) {
const diffLinesGenerator = generateLines(
myersDiff(activeTextEditor.document.getText(), text),
);
await this.verticalDiffManager.streamDiffLines(
diffLinesGenerator,
true,
await this.verticalDiffManager.instantApplyDiff(
originalFileContent,
text,
streamId,
toolCallId,
);

View file

@ -11,6 +11,7 @@ import {
import type { ApplyState, DiffLine } from "core";
import type { VerticalDiffCodeLens } from "./manager";
import { getFirstChangedLine } from "./util";
export interface VerticalDiffHandlerOptions {
input?: string;
@ -211,7 +212,7 @@ export class VerticalDiffHandler implements vscode.Disposable {
// Scroll to the first diff
const scrollToLine =
this.getFirstChangedLine(myersDiffs) ?? this.startLine;
getFirstChangedLine(myersDiffs, this.startLine) ?? this.startLine;
const range = new vscode.Range(scrollToLine, 0, scrollToLine, 0);
this.editor.revealRange(range, vscode.TextEditorRevealType.Default);
@ -605,13 +606,4 @@ export class VerticalDiffHandler implements vscode.Disposable {
/**
* Gets the first line number that was changed in a diff
*/
private getFirstChangedLine(diff: DiffLine[]): number | null {
for (let i = 0; i < diff.length; i++) {
const item = diff[i];
if (item.type === "old" || item.type === "new") {
return this.startLine + i;
}
}
return null;
}
}

View file

@ -10,12 +10,14 @@ import EditDecorationManager from "../../quickEdit/EditDecorationManager";
import { handleLLMError } from "../../util/errorHandling";
import { VsCodeWebviewProtocol } from "../../webviewProtocol";
import { myersDiff } from "core/diff/myers";
import { ApplyAbortManager } from "core/edit/applyAbortManager";
import { EDIT_MODE_STREAM_ID } from "core/edit/constants";
import { stripImages } from "core/util/messageContent";
import { getLastNPathParts } from "core/util/uri";
import { editOutcomeTracker } from "../../extension/EditOutcomeTracker";
import { VerticalDiffHandler, VerticalDiffHandlerOptions } from "./handler";
import { getFirstChangedLine } from "./util";
export interface VerticalDiffCodeLens {
start: number;
@ -293,6 +295,65 @@ export class VerticalDiffManager {
}
}
async instantApplyDiff(
oldContent: string,
newContent: string,
streamId: string,
toolCallId?: string,
) {
vscode.commands.executeCommand("setContext", "continue.diffVisible", true);
const editor = vscode.window.activeTextEditor;
if (!editor) {
return;
}
const fileUri = editor.document.uri.toString();
const myersDiffs = myersDiff(oldContent, newContent);
const diffHandler = this.createVerticalDiffHandler(
fileUri,
0,
editor.document.lineCount - 1,
{
instant: true,
onStatusUpdate: (status, numDiffs, fileContent) =>
void this.webviewProtocol.request("updateApplyState", {
streamId,
status,
numDiffs,
fileContent,
filepath: fileUri,
toolCallId,
}),
streamId,
},
);
if (!diffHandler) {
console.warn("Issue occurred while creating vertical diff handler");
return;
}
await diffHandler.reapplyWithMyersDiff(myersDiffs);
const scrollToLine = getFirstChangedLine(myersDiffs, 0) ?? 0;
const range = new vscode.Range(scrollToLine, 0, scrollToLine, 0);
editor.revealRange(range, vscode.TextEditorRevealType.Default);
this.enableDocumentChangeListener();
await this.webviewProtocol.request("updateApplyState", {
streamId,
status: "done",
numDiffs: this.fileUriToCodeLens.get(fileUri)?.length ?? 0,
fileContent: editor.document.getText(),
filepath: fileUri,
toolCallId,
});
}
async streamEdit({
input,
llm,

View file

@ -0,0 +1,14 @@
import { DiffLine } from "core";
export function getFirstChangedLine(
diff: DiffLine[],
startLine: number,
): number | null {
for (let i = 0; i < diff.length; i++) {
const item = diff[i];
if (item.type === "old" || item.type === "new") {
return startLine + i;
}
}
return null;
}