Skip to content

Commit

Permalink
Migrate text- and workspace- edit operation protocol to the generated…
Browse files Browse the repository at this point in the history
… protocol
  • Loading branch information
mkondratek committed Aug 14, 2024
1 parent f7aa472 commit 61428cb
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 93 deletions.
4 changes: 2 additions & 2 deletions src/main/kotlin/com/sourcegraph/cody/agent/CodyAgentClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import com.sourcegraph.cody.agent.protocol.DebugMessage
import com.sourcegraph.cody.agent.protocol.OpenExternalParams
import com.sourcegraph.cody.agent.protocol.ProtocolTextDocument
import com.sourcegraph.cody.agent.protocol.RemoteRepoFetchState
import com.sourcegraph.cody.agent.protocol.TextDocumentEditParams
import com.sourcegraph.cody.agent.protocol.TextDocumentShowParams
import com.sourcegraph.cody.agent.protocol.UntitledTextDocument
import com.sourcegraph.cody.agent.protocol.WebviewCreateWebviewPanelParams
import com.sourcegraph.cody.agent.protocol.WorkspaceEditParams
import com.sourcegraph.cody.agent.protocol_generated.DisplayCodeLensParams
import com.sourcegraph.cody.agent.protocol_generated.EditTask
import com.sourcegraph.cody.agent.protocol_generated.TextDocumentEditParams
import com.sourcegraph.cody.agent.protocol_generated.WorkspaceEditParams
import com.sourcegraph.cody.ui.NativeWebviewProvider
import java.util.concurrent.CompletableFuture
import org.eclipse.lsp4j.jsonrpc.services.JsonNotification
Expand Down

This file was deleted.

18 changes: 0 additions & 18 deletions src/main/kotlin/com/sourcegraph/cody/agent/protocol/TextEdit.kt

This file was deleted.

This file was deleted.

This file was deleted.

72 changes: 31 additions & 41 deletions src/main/kotlin/com/sourcegraph/cody/edit/EditService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@ import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.openapi.project.Project
import com.sourcegraph.cody.agent.protocol.TextEdit
import com.sourcegraph.cody.agent.protocol.WorkspaceEditParams
import com.sourcegraph.cody.agent.protocol_extensions.toOffset
import com.sourcegraph.cody.agent.protocol_generated.CreateFileOperation
import com.sourcegraph.cody.agent.protocol_generated.DeleteFileOperation
import com.sourcegraph.cody.agent.protocol_generated.DeleteTextEdit
import com.sourcegraph.cody.agent.protocol_generated.EditFileOperation
import com.sourcegraph.cody.agent.protocol_generated.InsertTextEdit
import com.sourcegraph.cody.agent.protocol_generated.RenameFileOperation
import com.sourcegraph.cody.agent.protocol_generated.ReplaceTextEdit
import com.sourcegraph.cody.agent.protocol_generated.TextEdit
import com.sourcegraph.cody.agent.protocol_generated.WorkspaceEditParams
import com.sourcegraph.utils.CodyEditorUtil

@Service(Service.Level.PROJECT)
Expand Down Expand Up @@ -38,31 +45,23 @@ class EditService(val project: Project) {

return WriteCommandAction.runWriteCommandAction<Boolean>(project) {
edits.reversed().all { edit ->
when (edit.type) {
"replace",
"delete" -> {
if (edit.range != null) {
document.replaceString(
edit.range.start.toOffset(document),
edit.range.end.toOffset(document),
edit.value ?: "")
true
} else {
logger.warn("Edit range is null for ${edit.type} operation")
false
}
when (edit) {
is ReplaceTextEdit -> {
document.replaceString(
edit.range.start.toOffset(document), edit.range.end.toOffset(document), edit.value)
true
}
"insert" -> {
if (edit.position != null) {
document.insertString(edit.position.toOffset(document), edit.value ?: "")
true
} else {
logger.warn("Edit position is null for insert operation")
false
}
is DeleteTextEdit -> {
document.deleteString(
edit.range.start.toOffset(document), edit.range.end.toOffset(document))
true
}
is InsertTextEdit -> {
document.insertString(edit.position.toOffset(document), edit.value)
true
}
else -> {
logger.warn("Unknown edit type: ${edit.type}")
logger.warn("Unknown edit type: $edit")
false
}
}
Expand All @@ -73,34 +72,25 @@ class EditService(val project: Project) {
fun performWorkspaceEdit(workspaceEditParams: WorkspaceEditParams): Boolean {
return workspaceEditParams.operations.all { op ->
// TODO: We need to support the file-level operations.
when (op.type) {
"create-file" -> {
when (op) {
is CreateFileOperation -> {
logger.warn("Workspace edit operation created a file: ${op.uri}")
return false
}
"rename-file" -> {
is RenameFileOperation -> {
logger.warn("Workspace edit operation renamed a file: ${op.oldUri} -> ${op.newUri}")
return false
}
"delete-file" -> {
is DeleteFileOperation -> {
logger.warn("Workspace edit operation deleted a file: ${op.uri}")
return false
}
"edit-file" -> {
if (op.edits == null) {
logger.warn("Workspace edit operation has no edits")
return false
} else if (op.uri == null) {
logger.warn("Workspace edit operation has null uri")
return false
} else {
logger.info("Applying workspace edit to a file: ${op.uri}")
performTextEdits(op.uri, op.edits)
}
is EditFileOperation -> {
logger.info("Applying workspace edit to a file: ${op.uri}")
performTextEdits(op.uri, op.edits)
}
else -> {
logger.warn(
"DocumentCommand session received unknown workspace edit operation: ${op.type}")
logger.warn("DocumentCommand session received unknown workspace edit operation: $op")
return false
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class ContextItemTest : BasePlatformTestCase() {

fun `testGetLinkActionText - local file`() {
val linkActionText = localContextItemFile.getLinkActionText(projectPath)
assertEquals("@src/main/java/com/sourcegraph/cody/agent/CodyAgentClient.kt:84", linkActionText)
assertEquals(
"@src/main/java/com/sourcegraph/cody/agent/CodyAgentClient.java:84", linkActionText)
}

fun `testGetLinkActionText - remote file`() {
Expand Down

0 comments on commit 61428cb

Please sign in to comment.