feat: make deletion line highlight wider
This commit is contained in:
parent
d1267877f4
commit
10a5ec18a9
2 changed files with 90 additions and 75 deletions
|
|
@ -9,6 +9,7 @@ import com.intellij.openapi.components.ServiceManager
|
|||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.editor.colors.EditorFontType
|
||||
import com.intellij.openapi.editor.colors.TextAttributesKey
|
||||
import com.intellij.openapi.editor.markup.EffectType
|
||||
import com.intellij.openapi.editor.markup.HighlighterLayer
|
||||
import com.intellij.openapi.editor.markup.RangeHighlighter
|
||||
import com.intellij.openapi.editor.markup.TextAttributes
|
||||
|
|
@ -27,13 +28,13 @@ enum class DiffLineType {
|
|||
}
|
||||
|
||||
class DiffStreamHandler(
|
||||
private val project: Project,
|
||||
private val editor: Editor,
|
||||
private val textArea: JTextArea,
|
||||
private val startLine: Int,
|
||||
private val endLine: Int,
|
||||
private val onClose: () -> Unit,
|
||||
private val onFinish: () -> Unit
|
||||
private val project: Project,
|
||||
private val editor: Editor,
|
||||
private val textArea: JTextArea,
|
||||
private val startLine: Int,
|
||||
private val endLine: Int,
|
||||
private val onClose: () -> Unit,
|
||||
private val onFinish: () -> Unit
|
||||
) {
|
||||
// Text attributes keys
|
||||
private val greenKey = run {
|
||||
|
|
@ -86,9 +87,11 @@ class DiffStreamHandler(
|
|||
fun setup() {
|
||||
// Highlight the range with unfinished color
|
||||
for (i in startLine..endLine) {
|
||||
val highlighter = editor.markupModel.addLineHighlighter(unfinishedKey, min(
|
||||
val highlighter = editor.markupModel.addLineHighlighter(
|
||||
unfinishedKey, min(
|
||||
i, editor.document.lineCount - 1
|
||||
), HighlighterLayer.FIRST)
|
||||
), HighlighterLayer.LAST
|
||||
)
|
||||
unfinishedHighlighters.add(highlighter)
|
||||
}
|
||||
}
|
||||
|
|
@ -103,21 +106,18 @@ class DiffStreamHandler(
|
|||
// Insert red highlighted code between the real lines in the editor
|
||||
if (deletionBufferStartLine != -1 && deletionsBuffer.isNotEmpty()) {
|
||||
val component = JTextArea().apply {
|
||||
text = deletionsBuffer.joinToString(" \n")
|
||||
text = deletionsBuffer.joinToString("\n")
|
||||
isEditable = false
|
||||
background = JBColor(0x30FF0000.toInt(), 0x30FF0000.toInt())
|
||||
foreground = JBColor.GRAY
|
||||
border = BorderFactory.createEmptyBorder(0, 0, 0, 0)
|
||||
lineWrap = false
|
||||
wrapStyleWord = false
|
||||
columns = Int.MAX_VALUE // This ensures the component takes up the full width
|
||||
margin = java.awt.Insets(0, -100, 0, 0)
|
||||
|
||||
font = editor.colorsScheme.getFont(EditorFontType.PLAIN)
|
||||
preferredSize = java.awt.Dimension(editor.contentComponent.width, preferredSize.height)
|
||||
}
|
||||
|
||||
val disposable = editorComponentInlaysManager.insert(deletionBufferStartLine, component, true)
|
||||
|
||||
if (disposable != null) {
|
||||
deletionInlays.add(disposable)
|
||||
}
|
||||
|
|
@ -131,53 +131,59 @@ class DiffStreamHandler(
|
|||
private fun handleDiffLine(type: DiffLineType, line: String) {
|
||||
println("DiffStreamHandler: handleDiffLine: $currentLine, $type, $line")
|
||||
try {
|
||||
when (type) {
|
||||
DiffLineType.SAME -> {
|
||||
insertDeletionBuffer()
|
||||
currentLine++
|
||||
}
|
||||
DiffLineType.NEW -> {
|
||||
// Insert new line
|
||||
if (currentLine == editor.document.lineCount) {
|
||||
editor.document.insertString(editor.document.textLength, "\n")
|
||||
}
|
||||
val offset = editor.document.getLineStartOffset(currentLine)
|
||||
editor.document.insertString(offset, line + "\n")
|
||||
|
||||
// Highlight the new line green
|
||||
editor.markupModel.addLineHighlighter(greenKey, currentLine, HighlighterLayer.LAST)
|
||||
|
||||
insertDeletionBuffer()
|
||||
|
||||
currentLine++
|
||||
changeCount++
|
||||
}
|
||||
DiffLineType.OLD -> {
|
||||
// Remove old line
|
||||
deleteLineAt(currentLine)
|
||||
|
||||
// Add to deletions buffer
|
||||
deletionsBuffer.add(line)
|
||||
if (deletionBufferStartLine == -1) {
|
||||
deletionBufferStartLine = currentLine
|
||||
when (type) {
|
||||
DiffLineType.SAME -> {
|
||||
insertDeletionBuffer()
|
||||
currentLine++
|
||||
}
|
||||
|
||||
changeCount++
|
||||
}
|
||||
}
|
||||
DiffLineType.NEW -> {
|
||||
// Insert new line
|
||||
if (currentLine == editor.document.lineCount) {
|
||||
editor.document.insertString(editor.document.textLength, "\n")
|
||||
}
|
||||
val offset = editor.document.getLineStartOffset(currentLine)
|
||||
editor.document.insertString(offset, line + "\n")
|
||||
|
||||
// Highlight the current line
|
||||
if (currentLineHighlighter != null) {
|
||||
editor.markupModel.removeHighlighter(currentLineHighlighter!!)
|
||||
}
|
||||
currentLineHighlighter = editor.markupModel.addLineHighlighter(currentLineKey, min(currentLine, editor.document.lineCount - 1), HighlighterLayer.LAST)
|
||||
// Highlight the new line green
|
||||
editor.markupModel.addLineHighlighter(greenKey, currentLine, HighlighterLayer.LAST)
|
||||
|
||||
// Remove the unfinished highlighter top line
|
||||
if (type != DiffLineType.OLD) {
|
||||
if (unfinishedHighlighters.isNotEmpty()) {
|
||||
editor.markupModel.removeHighlighter(unfinishedHighlighters.removeAt(0))
|
||||
insertDeletionBuffer()
|
||||
|
||||
currentLine++
|
||||
changeCount++
|
||||
}
|
||||
|
||||
DiffLineType.OLD -> {
|
||||
// Remove old line
|
||||
deleteLineAt(currentLine)
|
||||
|
||||
// Add to deletions buffer
|
||||
deletionsBuffer.add(line)
|
||||
if (deletionBufferStartLine == -1) {
|
||||
deletionBufferStartLine = currentLine
|
||||
}
|
||||
|
||||
changeCount++
|
||||
}
|
||||
}
|
||||
|
||||
// Highlight the current line
|
||||
if (currentLineHighlighter != null) {
|
||||
editor.markupModel.removeHighlighter(currentLineHighlighter!!)
|
||||
}
|
||||
currentLineHighlighter = editor.markupModel.addLineHighlighter(
|
||||
currentLineKey,
|
||||
min(currentLine, editor.document.lineCount - 1),
|
||||
HighlighterLayer.LAST
|
||||
)
|
||||
|
||||
// Remove the unfinished highlighter top line
|
||||
if (type != DiffLineType.OLD) {
|
||||
if (unfinishedHighlighters.isNotEmpty()) {
|
||||
editor.markupModel.removeHighlighter(unfinishedHighlighters.removeAt(0))
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
println("Error handling diff line: $currentLine, $type, $line, $e.message")
|
||||
}
|
||||
|
|
@ -232,7 +238,7 @@ class DiffStreamHandler(
|
|||
}
|
||||
}
|
||||
|
||||
fun run(input : String, prefix : String, highlighted : String, suffix : String, modelTitle : String) {
|
||||
fun run(input: String, prefix: String, highlighted: String, suffix: String, modelTitle: String) {
|
||||
// Undo changes
|
||||
resetState()
|
||||
|
||||
|
|
@ -243,18 +249,20 @@ class DiffStreamHandler(
|
|||
|
||||
// Request diff stream from core
|
||||
val continuePluginService = ServiceManager.getService(
|
||||
this.project,
|
||||
ContinuePluginService::class.java
|
||||
this.project,
|
||||
ContinuePluginService::class.java
|
||||
)
|
||||
val virtualFile = FileDocumentManager.getInstance().getFile(editor.document)
|
||||
continuePluginService.coreMessenger?.request("streamDiffLines", mapOf(
|
||||
continuePluginService.coreMessenger?.request(
|
||||
"streamDiffLines", mapOf(
|
||||
"input" to input,
|
||||
"prefix" to prefix,
|
||||
"highlighted" to highlighted,
|
||||
"suffix" to suffix,
|
||||
"language" to virtualFile?.fileType?.name,
|
||||
"modelTitle" to modelTitle
|
||||
), null) { response ->
|
||||
), null
|
||||
) { response ->
|
||||
if (!running) {
|
||||
return@request
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,8 +14,10 @@ import com.intellij.util.concurrency.annotations.RequiresEdt
|
|||
import com.intellij.util.ui.JBUI
|
||||
import java.awt.Dimension
|
||||
import java.awt.Font
|
||||
import java.awt.Insets
|
||||
import java.awt.event.ComponentAdapter
|
||||
import java.awt.event.ComponentEvent
|
||||
import javax.swing.BorderFactory
|
||||
import javax.swing.JComponent
|
||||
import javax.swing.ScrollPaneConstants
|
||||
import kotlin.math.ceil
|
||||
|
|
@ -53,16 +55,20 @@ class EditorComponentInlaysManager(val editor: EditorImpl, private val onlyOneIn
|
|||
val offset = editor.document.getLineEndOffset(lineIndex)
|
||||
|
||||
return EditorEmbeddedComponentManager.getInstance()
|
||||
.addComponent(editor, wrappedComponent,
|
||||
EditorEmbeddedComponentManager.Properties(EditorEmbeddedComponentManager.ResizePolicy.none(),
|
||||
null,
|
||||
true,
|
||||
showAbove,
|
||||
0,
|
||||
offset))?.also {
|
||||
managedInlays[wrappedComponent] = it
|
||||
Disposer.register(it, Disposable { managedInlays.remove(wrappedComponent) })
|
||||
}
|
||||
.addComponent(
|
||||
editor, wrappedComponent,
|
||||
EditorEmbeddedComponentManager.Properties(
|
||||
EditorEmbeddedComponentManager.ResizePolicy.none(),
|
||||
null,
|
||||
true,
|
||||
showAbove,
|
||||
0,
|
||||
offset
|
||||
)
|
||||
)?.also {
|
||||
managedInlays[wrappedComponent] = it
|
||||
Disposer.register(it, Disposable { managedInlays.remove(wrappedComponent) })
|
||||
}
|
||||
}
|
||||
|
||||
private inner class ComponentWrapper(private val component: JComponent) : JBScrollPane(component) {
|
||||
|
|
@ -79,12 +85,12 @@ class EditorComponentInlaysManager(val editor: EditorImpl, private val onlyOneIn
|
|||
|
||||
component.addComponentListener(object : ComponentAdapter() {
|
||||
override fun componentResized(e: ComponentEvent) =
|
||||
dispatchEvent(ComponentEvent(component, ComponentEvent.COMPONENT_RESIZED))
|
||||
dispatchEvent(ComponentEvent(component, ComponentEvent.COMPONENT_RESIZED))
|
||||
})
|
||||
}
|
||||
|
||||
override fun getPreferredSize(): Dimension {
|
||||
return Dimension(editorWidthWatcher.editorTextWidth, component.preferredSize.height)
|
||||
return Dimension(editor.contentComponent.width, component.preferredSize.height)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -107,7 +113,7 @@ class EditorComponentInlaysManager(val editor: EditorImpl, private val onlyOneIn
|
|||
|
||||
val scrollbarFlip = editor.scrollPane.getClientProperty(JBScrollPane.Flip::class.java)
|
||||
verticalScrollbarFlipped =
|
||||
scrollbarFlip == JBScrollPane.Flip.HORIZONTAL || scrollbarFlip == JBScrollPane.Flip.BOTH
|
||||
scrollbarFlip == JBScrollPane.Flip.HORIZONTAL || scrollbarFlip == JBScrollPane.Flip.BOTH
|
||||
}
|
||||
|
||||
override fun componentResized(e: ComponentEvent) = updateWidthForAllInlays()
|
||||
|
|
@ -126,7 +132,8 @@ class EditorComponentInlaysManager(val editor: EditorImpl, private val onlyOneIn
|
|||
}
|
||||
|
||||
private fun calcWidth(): Int {
|
||||
val visibleEditorTextWidth = editor.scrollPane.viewport.width - getVerticalScrollbarWidth() - getGutterTextGap()
|
||||
val visibleEditorTextWidth =
|
||||
editor.scrollPane.viewport.width - getVerticalScrollbarWidth() - getGutterTextGap()
|
||||
return min(max(visibleEditorTextWidth, 0), maximumEditorTextWidth)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue