Skip to content

Commit 18d754f

Browse files
committed
clean up util functions
1 parent 4825236 commit 18d754f

File tree

3 files changed

+30
-49
lines changed

3 files changed

+30
-49
lines changed

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/webview/BrowserConnector.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,6 @@ class BrowserConnector(
364364
val enrichedNode = (node as ObjectNode).apply {
365365
set<JsonNode>("params", enrichedParams)
366366
}
367-
368367
handleChat(AmazonQChatServer.insertToCursorPosition, enrichedNode)
369368
}
370369

plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/AmazonQLanguageClientImpl.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -570,8 +570,8 @@ class AmazonQLanguageClientImpl(private val project: Project) : AmazonQLanguageC
570570
)
571571
}
572572

573-
override fun applyEdit(params: ApplyWorkspaceEditParams): CompletableFuture<ApplyWorkspaceEditResponse> {
574-
return CompletableFuture.supplyAsync(
573+
override fun applyEdit(params: ApplyWorkspaceEditParams): CompletableFuture<ApplyWorkspaceEditResponse> =
574+
CompletableFuture.supplyAsync(
575575
{
576576
try {
577577
LspEditorUtil.applyWorkspaceEdit(project, params.edit)
@@ -583,7 +583,6 @@ class AmazonQLanguageClientImpl(private val project: Project) : AmazonQLanguageC
583583
},
584584
ApplicationManager.getApplication()::invokeLater
585585
)
586-
}
587586

588587
private fun refreshVfs(path: String) {
589588
val currPath = Paths.get(path)

plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/util/LspEditorUtil.kt

Lines changed: 28 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package software.aws.toolkits.jetbrains.services.amazonq.lsp.util
55

66
import com.intellij.openapi.application.runReadAction
77
import com.intellij.openapi.command.WriteCommandAction
8+
import com.intellij.openapi.editor.Document
89
import com.intellij.openapi.editor.Editor
910
import com.intellij.openapi.editor.LogicalPosition
1011
import com.intellij.openapi.fileEditor.FileDocumentManager
@@ -15,6 +16,7 @@ import com.intellij.openapi.vfs.VirtualFile
1516
import com.intellij.openapi.vfs.VirtualFileManager
1617
import org.eclipse.lsp4j.Position
1718
import org.eclipse.lsp4j.Range
19+
import org.eclipse.lsp4j.TextEdit
1820
import org.eclipse.lsp4j.WorkspaceEdit
1921
import software.aws.toolkits.core.utils.getLogger
2022
import software.aws.toolkits.core.utils.warn
@@ -88,55 +90,36 @@ object LspEditorUtil {
8890
edit.documentChanges?.forEach { change ->
8991
if (change.isLeft) {
9092
val textDocumentEdit = change.left
91-
val file = VirtualFileManager.getInstance().findFileByUrl(textDocumentEdit.textDocument.uri)
92-
file?.let {
93-
val document = FileDocumentManager.getInstance().getDocument(it)
94-
val editor = FileEditorManager.getInstance(project).getSelectedEditor(it)?.let { fileEditor ->
95-
if (fileEditor is com.intellij.openapi.fileEditor.TextEditor) fileEditor.editor else null
96-
}
97-
document?.let { doc ->
98-
textDocumentEdit.edits.forEach { textEdit ->
99-
val startOffset = if (editor != null) {
100-
editor.logicalPositionToOffset(LogicalPosition(textEdit.range.start.line, textEdit.range.start.character))
101-
} else {
102-
doc.getLineStartOffset(textEdit.range.start.line) + textEdit.range.start.character
103-
}
104-
val endOffset = if (editor != null) {
105-
editor.logicalPositionToOffset(LogicalPosition(textEdit.range.end.line, textEdit.range.end.character))
106-
} else {
107-
doc.getLineStartOffset(textEdit.range.end.line) + textEdit.range.end.character
108-
}
109-
doc.replaceString(startOffset, endOffset, textEdit.newText)
110-
}
111-
}
112-
}
113-
}
114-
} ?: edit.changes?.forEach { (uri, textEdits) ->
115-
val file = VirtualFileManager.getInstance().findFileByUrl(uri)
116-
file?.let {
117-
val document = FileDocumentManager.getInstance().getDocument(it)
118-
val editor = FileEditorManager.getInstance(project).getSelectedEditor(it)?.let { fileEditor ->
119-
if (fileEditor is com.intellij.openapi.fileEditor.TextEditor) fileEditor.editor else null
120-
}
121-
document?.let { doc ->
122-
textEdits.forEach { textEdit ->
123-
val startOffset = if (editor != null) {
124-
editor.logicalPositionToOffset(LogicalPosition(textEdit.range.start.line, textEdit.range.start.character))
125-
} else {
126-
doc.getLineStartOffset(textEdit.range.start.line) + textEdit.range.start.character
127-
}
128-
val endOffset = if (editor != null) {
129-
editor.logicalPositionToOffset(LogicalPosition(textEdit.range.end.line, textEdit.range.end.character))
130-
} else {
131-
doc.getLineStartOffset(textEdit.range.end.line) + textEdit.range.end.character
132-
}
133-
doc.replaceString(startOffset, endOffset, textEdit.newText)
134-
}
135-
}
93+
applyEditsToFile(project, textDocumentEdit.textDocument.uri, textDocumentEdit.edits)
13694
}
13795
}
96+
97+
edit.changes?.forEach { (uri, textEdits) ->
98+
applyEditsToFile(project, uri, textEdits)
99+
}
138100
}
139101
}
140102

103+
private fun applyEditsToFile(project: Project, uri: String, textEdits: List<TextEdit>) {
104+
val file = VirtualFileManager.getInstance().findFileByUrl(uri) ?: return
105+
val document = FileDocumentManager.getInstance().getDocument(file) ?: return
106+
val editor = FileEditorManager.getInstance(project).getSelectedEditor(file)?.let {
107+
if (it is com.intellij.openapi.fileEditor.TextEditor) it.editor else null
108+
}
109+
110+
textEdits.forEach { textEdit ->
111+
val startOffset = calculateOffset(editor, document, textEdit.range.start)
112+
val endOffset = calculateOffset(editor, document, textEdit.range.end)
113+
document.replaceString(startOffset, endOffset, textEdit.newText)
114+
}
115+
}
116+
117+
private fun calculateOffset(editor: Editor?, document: Document, position: Position): Int =
118+
if (editor != null) {
119+
editor.logicalPositionToOffset(LogicalPosition(position.line, position.character))
120+
} else {
121+
document.getLineStartOffset(position.line) + position.character
122+
}
123+
141124
private val LOG = getLogger<LspEditorUtil>()
142125
}

0 commit comments

Comments
 (0)