Skip to content

Commit

Permalink
Merge pull request #187 from smallcloudai/fix/open-chat-selects-code
Browse files Browse the repository at this point in the history
Fix: Open Chat instruction selects range of code in active editor
  • Loading branch information
alashchev17 authored Oct 28, 2024
2 parents cf20e82 + c8b4dc3 commit 95cb519
Showing 1 changed file with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.intellij.openapi.wm.ToolWindowManager
import com.smallcloud.refactai.Resources
import com.smallcloud.refactai.panes.RefactAIToolboxPaneFactory
import kotlin.io.path.relativeTo
import kotlinx.coroutines.*

class CodeLensAction(
private val editor: Editor,
Expand All @@ -34,6 +35,7 @@ class CodeLensAction(
}.minBy { it.toString().length }
}


return contentMsg
.replace("%CURRENT_FILE%", relativePath?.toString() ?: filePath.toString())
.replace("%CURSOR_LINE%", line1.toString())
Expand All @@ -46,5 +48,19 @@ class CodeLensAction(
RefactAIToolboxPaneFactory.chat?.requestFocus()
RefactAIToolboxPaneFactory.chat?.executeCodeLensCommand(formatMessage(), sendImmediately, openNewTab)
}

// If content is empty, then it's "Open Chat" instruction, selecting range of code in active tab
if (contentMsg.isEmpty()) {
CoroutineScope(Dispatchers.Main).launch {
delay(500)
val pos1 = LogicalPosition(line1, 0)
val pos2 = LogicalPosition(line2, editor.document.getLineEndOffset(line2))

editor.selectionModel.setSelection(
editor.logicalPositionToOffset(pos1),
editor.logicalPositionToOffset(pos2)
)
}
}
}
}

0 comments on commit 95cb519

Please sign in to comment.