-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
12 changed files
with
298 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/main/kotlin/com/smallcloud/refactai/code_lens/CodeLensAction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.smallcloud.refactai.code_lens | ||
|
||
import com.intellij.openapi.actionSystem.AnActionEvent | ||
import com.intellij.openapi.editor.Editor | ||
import com.intellij.openapi.editor.LogicalPosition | ||
import com.intellij.openapi.project.DumbAwareAction | ||
import com.intellij.openapi.roots.ProjectRootManager | ||
import com.intellij.openapi.wm.ToolWindowManager | ||
import com.smallcloud.refactai.Resources | ||
import com.smallcloud.refactai.panes.RefactAIToolboxPaneFactory | ||
import kotlin.io.path.relativeTo | ||
|
||
class CodeLensAction( | ||
private val editor: Editor, | ||
private val line1: Int, | ||
private val line2: Int, | ||
private val contentMsg: String, | ||
private val sendImmediately: Boolean, | ||
private val openNewTab: Boolean | ||
) : DumbAwareAction(Resources.Icons.LOGO_RED_16x16) { | ||
override fun actionPerformed(p0: AnActionEvent) { | ||
actionPerformed() | ||
} | ||
|
||
private fun formatMessage(): String { | ||
val pos1 = LogicalPosition(line1, 0) | ||
val text = editor.document.text.slice( | ||
editor.logicalPositionToOffset(pos1) until editor.document.getLineEndOffset(line2) | ||
) | ||
val filePath = editor.virtualFile.toNioPath() | ||
val relativePath = editor.project?.let { | ||
ProjectRootManager.getInstance(it).contentRoots.map { root -> | ||
filePath.relativeTo(root.toNioPath()) | ||
}.minBy { it.toString().length } | ||
} | ||
|
||
return contentMsg | ||
.replace("%CURRENT_FILE%", relativePath?.toString() ?: filePath.toString()) | ||
.replace("%CURSOR_LINE%", line1.toString()) | ||
.replace("%CODE_SELECTION%", text) | ||
} | ||
|
||
fun actionPerformed() { | ||
val chat = editor.project?.let { ToolWindowManager.getInstance(it).getToolWindow("Refact") } | ||
chat?.activate { | ||
RefactAIToolboxPaneFactory.chat?.requestFocus() | ||
RefactAIToolboxPaneFactory.chat?.executeCodeLensCommand(formatMessage(), sendImmediately, openNewTab) | ||
} | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
src/main/kotlin/com/smallcloud/refactai/code_lens/RefactCodeVisionProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package com.smallcloud.refactai.code_lens | ||
|
||
import com.google.gson.Gson | ||
import com.google.gson.JsonObject | ||
import com.intellij.codeInsight.codeVision.* | ||
import com.intellij.codeInsight.codeVision.ui.model.ClickableTextCodeVisionEntry | ||
import com.intellij.openapi.application.runReadAction | ||
import com.intellij.openapi.diagnostic.Logger | ||
import com.intellij.openapi.editor.Editor | ||
import com.intellij.openapi.editor.LogicalPosition | ||
import com.intellij.openapi.util.TextRange | ||
import com.smallcloud.refactai.Resources | ||
import com.smallcloud.refactai.lsp.LSPProcessHolder.Companion.getInstance | ||
import com.smallcloud.refactai.lsp.lspGetCodeLens | ||
import com.smallcloud.refactai.struct.ChatMessage | ||
import kotlin.math.max | ||
|
||
data class CodeLen( | ||
val range: TextRange, | ||
val label: String, | ||
val action: CodeLensAction | ||
) | ||
|
||
class RefactCodeVisionProvider(private val commandKey: String, private val posAfter: String?, private val label: String) : | ||
CodeVisionProvider<Unit> { | ||
override val defaultAnchor: CodeVisionAnchorKind | ||
get() = CodeVisionAnchorKind.Top | ||
override val id: String | ||
get() = "refactai.codelens.$commandKey" | ||
override val name: String | ||
get() = "Refact.ai Hint($label)" | ||
override val relativeOrderings: List<CodeVisionRelativeOrdering> | ||
get() { | ||
return if (posAfter == null) { | ||
listOf(CodeVisionRelativeOrdering.CodeVisionRelativeOrderingFirst) | ||
} else { | ||
listOf(CodeVisionRelativeOrdering.CodeVisionRelativeOrderingAfter("refactai.codelens.$posAfter")) | ||
} | ||
} | ||
|
||
|
||
override fun precomputeOnUiThread(editor: Editor) {} | ||
|
||
private fun getCodeLens(editor: Editor): List<CodeLen> { | ||
val codeLensStr = lspGetCodeLens(editor) | ||
val gson = Gson() | ||
val customization = getInstance(editor.project!!).fetchCustomization() | ||
val codeLensJson = gson.fromJson(codeLensStr, JsonObject::class.java) | ||
val resCodeLenses = mutableListOf<CodeLen>() | ||
if (customization.has("code_lens")) { | ||
val allCodeLenses = customization.get("code_lens").asJsonObject | ||
if (codeLensJson.has("code_lens")) { | ||
val codeLenses = codeLensJson.get("code_lens")!!.asJsonArray | ||
for (codeLens in codeLenses) { | ||
val line1 = max(codeLens.asJsonObject.get("line1").asInt - 1, 0) | ||
val line2 = max(codeLens.asJsonObject.get("line2").asInt - 1, 0) | ||
val range = runReadAction { | ||
return@runReadAction TextRange( | ||
editor.logicalPositionToOffset(LogicalPosition(line1, 0)), | ||
editor.document.getLineEndOffset(line2) | ||
) | ||
} | ||
val value = allCodeLenses.get(commandKey).asJsonObject | ||
val msgs = value.asJsonObject.get("messages").asJsonArray.map { | ||
gson.fromJson(it.asJsonObject, ChatMessage::class.java) | ||
}.toList() | ||
val msg = msgs.find { it.role == "user" } | ||
val sendImmediately = value.asJsonObject.get("auto_submit").asBoolean | ||
val openNewTab = value.asJsonObject.get("new_tab")?.asBoolean ?: true | ||
if (msg != null || msgs.isEmpty()) { | ||
resCodeLenses.add( | ||
CodeLen( | ||
range, | ||
value.asJsonObject.get("label").asString, | ||
CodeLensAction(editor, line1, line2, msg?.content ?: "", sendImmediately, openNewTab) | ||
) | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
return resCodeLenses | ||
} | ||
|
||
override fun computeCodeVision(editor: Editor, uiData: Unit): CodeVisionState { | ||
Logger.getInstance(RefactCodeVisionProvider::class.java).warn("computeCodeVision $commandKey start") | ||
val codeLens = getCodeLens(editor) | ||
return runReadAction { | ||
val result = ArrayList<Pair<TextRange, CodeVisionEntry>>() | ||
Logger.getInstance(RefactCodeVisionProvider::class.java) | ||
.warn("computeCodeVision $commandKey ${codeLens.size}") | ||
for (codeLen in codeLens) { | ||
result.add(codeLen.range to ClickableTextCodeVisionEntry(codeLen.label, id, { event, editor -> | ||
codeLen.action.actionPerformed() | ||
}, Resources.Icons.LOGO_12x12)) | ||
} | ||
return@runReadAction CodeVisionState.Ready(result) | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/kotlin/com/smallcloud/refactai/code_lens/RefactCodeVisionProviderFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.smallcloud.refactai.code_lens | ||
|
||
import com.intellij.codeInsight.codeVision.CodeVisionProvider | ||
import com.intellij.codeInsight.codeVision.CodeVisionProviderFactory | ||
import com.intellij.openapi.project.Project | ||
import com.smallcloud.refactai.lsp.LSPProcessHolder.Companion.getInstance | ||
|
||
class RefactCodeVisionProviderFactory : CodeVisionProviderFactory { | ||
override fun createProviders(project: Project): Sequence<CodeVisionProvider<*>> { | ||
val customization = getInstance(project).fetchCustomization() | ||
if (customization.has("code_lens")) { | ||
val allCodeLenses = customization.get("code_lens").asJsonObject | ||
val allCodeLensKeys = allCodeLenses.keySet().toList() | ||
val providers: MutableList<CodeVisionProvider<*>> = mutableListOf() | ||
for ((idx, key) in allCodeLensKeys.withIndex()) { | ||
val label = allCodeLenses.get(key).asJsonObject.get("label").asString | ||
var posAfter: String? = null | ||
if (idx != 0) { | ||
posAfter = allCodeLensKeys[idx - 1] | ||
} | ||
providers.add(RefactCodeVisionProvider(key, posAfter, label)) | ||
} | ||
return providers.asSequence() | ||
|
||
} | ||
return emptySequence() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.