Elffuss BraidHub

kikocis/continue public

feat: JB support for search and replace
Changesete841ee848161a20aaabd2af514de0f671bcd63b1b2e1b7e46a78f7ce0d34a9c8
AuthorPatrick Erichsen <patrick.a.erichsen@gmail.com> · 2025-06-30 13:37 -07:00
CommitterPatrick Erichsen <patrick.a.erichsen@gmail.com> · 2025-06-30 13:37 -07:00
Parents62c4c3e6fad7

Browse the files at this changeset

4 files changed

modified extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/continue/ApplyToFileHandler.kt +50 −28
@@ -44,6 +44,12 @@
4444 return
4545 }
4646
47+ // Handle search/replace mode
48+ if (params.isSearchReplace == true) {
49+ handleSearchReplace(editorUtils)
50+ return
51+ }
52+
4753 // Get the LLM configuration for applying edits
4854 val llm = fetchApplyLLMConfig() ?: run {
4955 ide.showToast(ToastType.ERROR, "Failed to fetch model configuration")
@@ -54,6 +60,40 @@
5460 setupAndStreamDiffs(editorUtils, llm)
5561 }
5662
63+ /**
64+ * Handle search and replace operations
65+ *
66+ * For search/replace, we always get a full file rewrite of the contents.
67+ * We just need to generate diff lines between the current content and new content,
68+ * then stream them through the existing infrastructure.
69+ */
70+ private fun handleSearchReplace(editorUtils: EditorUtils) {
71+ // Clear all diff blocks before running the diff stream
72+ diffStreamService.reject(editorUtils.editor)
73+
74+ // Get current file content and the new content (full rewrite)
75+ val currentContent = editorUtils.getDocumentText()
76+ val newContent = params.text
77+
78+ // Work with the entire document
79+ val startLine = 0
80+ val endLine = editorUtils.getLineCount() - 1
81+ // Create and register the diff stream handler
82+ val diffStreamHandler = createDiffStreamHandler(editorUtils.editor, startLine, endLine)
83+ diffStreamService.register(diffStreamHandler, editorUtils.editor)
84+
85+ // Stream the diffs between current and new content
86+ // For search/replace, we pass the new content as "input" and current as "highlighted"
87+ diffStreamHandler.streamDiffLinesToEditor(
88+ input = newContent, // The new content (full rewrite)
89+ prefix = "", // No prefix since we're rewriting the whole file
90+ highlighted = currentContent, // Current file content
91+ suffix = "", // No suffix since we're rewriting the whole file
92+ modelTitle = null, // No model needed for search/replace instant apply
93+ includeRulesInSystemMessage = false // No LLM involved, just diff generation
94+ )
95+ }
96+
5797 private fun notifyStreamStarted() {
5898 sendApplyStateUpdate(ApplyStateStatus.STREAMING)
5999 }
@@ -61,10 +101,8 @@
61101 private fun notifyStreamClosed(numDiffs: Int? = 0) {
62102 sendApplyStateUpdate(ApplyStateStatus.CLOSED, numDiffs)
63103 }
64-
65104 private fun sendApplyStateUpdate(
66- status: ApplyStateStatus,
67- numDiffs: Int? = null
105+ status: ApplyStateStatus, numDiffs: Int? = null
68106 ) {
69107 val payload = ApplyState(
70108 streamId = params.streamId,
@@ -82,16 +120,11 @@
82120 return try {
83121 suspendCancellableCoroutine { continuation ->
84122 continuePluginService.coreMessenger?.request(
85- "config/getSerializedProfileInfo",
86- null,
87- null
123+ "config/getSerializedProfileInfo", null, null
88124 ) { response ->
89125 try {
90126 val selectedModels = response.castNestedOrNull<Map<String, Any>>(
91- "content",
92- "result",
93- "config",
94- "selectedModelByRole"
127+ "content", "result", "config", "selectedModelByRole"
95128 )
96129
97130 // If "apply" role model is not found, try "chat" role
@@ -143,9 +176,7 @@
143176 }
144177
145178 private fun createDiffStreamHandler(
146- editor: Editor,
147- startLine: Int,
148- endLine: Int
179+ editor: Editor, startLine: Int, endLine: Int
149180 ): DiffStreamHandler {
150181 return DiffStreamHandler(
151182 project,
@@ -164,28 +195,19 @@
164195 * Factory method to create and execute a new handler for a single apply-to-file operation
165196 */
166197 suspend fun apply(
167- project: Project,
168- continuePluginService: ContinuePluginService,
169- ide: IDE,
170- params: ApplyToFileParams
198+ project: Project, continuePluginService: ContinuePluginService, ide: IDE, params: ApplyToFileParams
171199 ) {
172- val editorUtils = if (EditorUtils.editorFileExist(params.filepath))
173- EditorUtils.getOrOpenEditor(project, params.filepath)
174- else
175- EditorUtils.getEditorByCreateFile(project, params.filepath)
200+ val editorUtils =
201+ if (EditorUtils.editorFileExist(params.filepath)) EditorUtils.getOrOpenEditor(project, params.filepath)
202+ else EditorUtils.getEditorByCreateFile(project, params.filepath)
176203
177204 val diffStreamService = project.getService(DiffStreamService::class.java)
178205
179206 val handler = ApplyToFileHandler(
180- project,
181- continuePluginService,
182- ide,
183- params,
184- editorUtils,
185- diffStreamService
207+ project, continuePluginService, ide, params, editorUtils, diffStreamService
186208 )
187209
188210 handler.handleApplyToFile()
189211 }
190212 }
191-}
No newline at the end of the file
213+}
modified extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/DiffStreamHandler.kt +1 −1
@@ -96,7 +96,7 @@
9696 prefix: String,
9797 highlighted: String,
9898 suffix: String,
99- modelTitle: String,
99+ modelTitle: String?,
100100 includeRulesInSystemMessage: Boolean
101101 ) {
102102 isRunning = true
modified extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/EditorUtils.kt +7 −2
@@ -46,6 +46,13 @@
4646 return editor.document.lineCount
4747 }
4848
49+ /**
50+ * Gets the full text content of the document
51+ */
52+ fun getDocumentText(): String {
53+ return editor.document.text
54+ }
55+
4956 /**
5057 * Inserts text at the specified position in the document
5158 */
@@ -62,7 +69,6 @@
6269 insertTextAtPos(0, text)
6370 }
6471
65-
6672 /**
6773 * Scrolls the editor to make the specified line visible
6874 */
@@ -165,7 +171,6 @@
165171 }
166172 }
167173 }
168-
169174 val editor = FileEditorManager.getInstance(project).selectedTextEditor ?: return null
170175 return EditorUtils(editor)
171176 }
modified extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/protocol/ideWebview.kt +2 −2
@@ -8,10 +8,10 @@
88 val text: String,
99 val streamId: String,
1010 val filepath: String?,
11- val toolCallId: String?
11+ val toolCallId: String?,
12+ val isSearchReplace: Boolean? = null
1213 )
1314
1415 data class InsertAtCursorParams(
1516 val text: String
1617 )
17-