feat: direct apply based on Dice-Sørensen coefficient
Some checks are pending
Build / Build (push) Waiting to run
Build / Verify Plugin (push) Blocked by required conditions

This commit is contained in:
Carl-Robert Linnupuu 2025-05-28 22:00:12 +01:00
parent 3e0c9d407b
commit e260ac4854
2 changed files with 24 additions and 0 deletions

View file

@ -3,11 +3,14 @@ package ee.carlrobert.codegpt.toolwindow.chat.editor.header
import com.intellij.icons.AllIcons
import com.intellij.openapi.actionSystem.*
import com.intellij.openapi.actionSystem.toolbarLayout.ToolbarLayoutStrategy
import com.intellij.openapi.application.runUndoTransparentWriteAction
import com.intellij.openapi.components.service
import com.intellij.openapi.editor.ex.EditorEx
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.JBMenuItem
import com.intellij.openapi.ui.JBPopupMenu
import com.intellij.openapi.vfs.readText
import com.intellij.openapi.vfs.writeText
import com.intellij.ui.AnimatedIcon
import com.intellij.ui.components.JBLabel
import com.intellij.util.ui.JBUI
@ -15,6 +18,7 @@ import ee.carlrobert.codegpt.CodeGPTBundle
import ee.carlrobert.codegpt.toolwindow.chat.editor.actions.*
import ee.carlrobert.codegpt.toolwindow.chat.editor.state.EditorStateManager
import ee.carlrobert.codegpt.util.EditorUtil
import ee.carlrobert.codegpt.util.StringUtil
import javax.swing.JPanel
class DefaultHeaderPanel(config: HeaderConfig) : HeaderPanel(config) {
@ -69,6 +73,15 @@ class DefaultHeaderPanel(config: HeaderConfig) : HeaderPanel(config) {
?: EditorUtil.getSelectedEditor(project)?.virtualFile
?: throw IllegalStateException("Virtual file is null")
val directApplyThreshold = 0.85
val coefficient = StringUtil.getDiceCoefficient(editor.document.text, file.readText())
if (coefficient > directApplyThreshold) {
runUndoTransparentWriteAction {
file.writeText(editor.document.text)
}
return
}
setLoading()
project.service<EditorStateManager>()
.getCodeEditsAsync(editor.document.text, file, editor)

View file

@ -23,6 +23,17 @@ object StringUtil {
return completionLine
}
fun getDiceCoefficient(s1: String, s2: String): Double {
fun bigrams(str: String): Set<String> =
if (str.length < 2) emptySet()
else str.windowed(2).toSet()
val bigrams1 = bigrams(s1)
val bigrams2 = bigrams(s2)
val intersection = bigrams1.intersect(bigrams2).size
return (2.0 * intersection) / (bigrams1.size + bigrams2.size)
}
fun String.extractUntilNewline(): String {
val index = this.indexOf('\n')
if (index == -1) {