Merge pull request #2470 from continuedev/pe/jb-inline-hint-tooltip-updates
jb: further inline tooltip ux improvements
This commit is contained in:
commit
8289cf62d3
3 changed files with 222 additions and 193 deletions
|
|
@ -178,8 +178,9 @@ fun openInlineEdit(project: Project?, editor: Editor) {
|
|||
val comboBoxRef = Ref<JComboBox<String>>()
|
||||
|
||||
fun onEnter() {
|
||||
val selectedModelStrippedOfCaret = (comboBoxRef.get().selectedItem as String).removeSuffix(" ▾")
|
||||
customPanelRef.get().enter()
|
||||
diffStreamHandler.run(textArea.text, prefix, highlighted, suffix, comboBoxRef.get().selectedItem as String)
|
||||
diffStreamHandler.run(textArea.text, prefix, highlighted, suffix, selectedModelStrippedOfCaret)
|
||||
}
|
||||
|
||||
val panel =
|
||||
|
|
@ -200,31 +201,44 @@ fun openInlineEdit(project: Project?, editor: Editor) {
|
|||
viewport?.dispatchEvent(ComponentEvent(viewport, ComponentEvent.COMPONENT_RESIZED))
|
||||
|
||||
// Add key listener to text area
|
||||
textArea.addKeyListener(object : KeyAdapter() {
|
||||
override fun keyPressed(e: KeyEvent) {
|
||||
if (e.keyCode == KeyEvent.VK_ESCAPE) {
|
||||
diffStreamService.reject(editor)
|
||||
|
||||
// Re-highlight the selected text
|
||||
selectionModel.setSelection(start, end)
|
||||
} else if (e.keyCode == KeyEvent.VK_ENTER) {
|
||||
if (e.modifiersEx == KeyEvent.SHIFT_DOWN_MASK) {
|
||||
textArea.document.insertString(textArea.caretPosition, "\n", null)
|
||||
} else if (e.modifiersEx == 0) {
|
||||
onEnter()
|
||||
e.consume()
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// Listen for key typed events after diff streaming is finished
|
||||
textArea.addKeyListener(object : KeyAdapter() {
|
||||
override fun keyTyped(e: KeyEvent) {
|
||||
if (customPanelRef.get().isFinished) {
|
||||
customPanelRef.get().setup()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
override fun keyPressed(e: KeyEvent) {
|
||||
when (e.keyCode) {
|
||||
KeyEvent.VK_ESCAPE -> {
|
||||
diffStreamService.reject(editor)
|
||||
selectionModel.setSelection(start, end)
|
||||
}
|
||||
|
||||
KeyEvent.VK_ENTER -> {
|
||||
when (e.modifiersEx) {
|
||||
KeyEvent.SHIFT_DOWN_MASK -> {
|
||||
textArea.document.insertString(textArea.caretPosition, "\n", null)
|
||||
}
|
||||
|
||||
0 -> {
|
||||
onEnter()
|
||||
e.consume()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// We need this because backspace/delete is not registering properly on keyPressed for an unknown reason
|
||||
override fun keyReleased(e: KeyEvent) {
|
||||
if (e.keyCode == KeyEvent.VK_BACK_SPACE || e.keyCode == KeyEvent.VK_DELETE) {
|
||||
if (customPanelRef.get().isFinished) {
|
||||
customPanelRef.get().setup()
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// Listen for changes to textarea line count
|
||||
|
|
@ -304,20 +318,27 @@ class CustomPanel(
|
|||
}
|
||||
|
||||
private fun createCloseButton(): JComponent {
|
||||
return CustomButton("X") { onCancel() }.apply {
|
||||
return JLabel("X").apply {
|
||||
foreground = Color(128, 128, 128, 128)
|
||||
background = Color(0, 0, 0, 0)
|
||||
font = UIUtil.getFontWithFallback("Arial", Font.BOLD, 10)
|
||||
border = EmptyBorder(2, 6, 2, 6)
|
||||
toolTipText = "`esc` to cancel"
|
||||
isOpaque = false
|
||||
|
||||
addMouseListener(object : MouseAdapter() {
|
||||
override fun mouseEntered(e: MouseEvent) {
|
||||
foreground = Color(128, 128, 128, 255)
|
||||
cursor = Cursor(Cursor.HAND_CURSOR)
|
||||
}
|
||||
|
||||
override fun mouseExited(e: MouseEvent) {
|
||||
foreground = Color(128, 128, 128, 128)
|
||||
cursor = Cursor.getDefaultCursor()
|
||||
}
|
||||
|
||||
override fun mouseClicked(e: MouseEvent) {
|
||||
onCancel()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
@ -353,8 +374,8 @@ class CustomPanel(
|
|||
comboBoxRef.set(dropdown)
|
||||
|
||||
val rightButton = CustomButton("⏎ Enter") { onEnter() }.apply {
|
||||
background = JBColor(0x33999998.toInt(), 0x33999998.toInt())
|
||||
foreground = Color.WHITE
|
||||
background = JBColor(0x999998.toInt(), 0x999998.toInt())
|
||||
foreground = JBColor(0xF5F5F5.toInt(), 0xF5F5F5.toInt())
|
||||
border = EmptyBorder(2, 6, 2, 6)
|
||||
}
|
||||
|
||||
|
|
@ -393,13 +414,15 @@ class CustomPanel(
|
|||
}
|
||||
|
||||
val leftButton = CustomButton("Reject (${getAltKeyLabel()}⇧N)") { onReject() }.apply {
|
||||
background = Color(239, 68, 68) // text-red-500
|
||||
foreground = Color.WHITE
|
||||
background = JBColor(0x30FF0000.toInt(), 0x30FF0000.toInt())
|
||||
foreground = JBColor(0xF5F5F5.toInt(), 0xF5F5F5.toInt())
|
||||
|
||||
}
|
||||
|
||||
val rightButton = CustomButton("Accept (${getAltKeyLabel()}⇧Y)") { onAccept() }.apply {
|
||||
background = Color(34, 197, 94) // text-green-500
|
||||
foreground = Color.WHITE
|
||||
background = JBColor(0x3000FF00.toInt(), 0x3000FF00.toInt())
|
||||
foreground = JBColor(0xF5F5F5.toInt(), 0xF5F5F5.toInt())
|
||||
|
||||
}
|
||||
|
||||
val rightPanel = JPanel(MigLayout("insets 0, fillx")).apply {
|
||||
|
|
@ -517,9 +540,8 @@ class CustomButton(text: String, onClick: () -> Unit) : JLabel(text, CENTER) {
|
|||
}
|
||||
})
|
||||
|
||||
// verticalAlignment = CENTER
|
||||
font = UIUtil.getFontWithFallback("Arial", Font.PLAIN, 11)
|
||||
border = EmptyBorder(2, 4, 2, 4)
|
||||
border = EmptyBorder(2, 6, 2, 6)
|
||||
}
|
||||
|
||||
override fun paintComponent(g: Graphics) {
|
||||
|
|
@ -537,7 +559,13 @@ class CustomButton(text: String, onClick: () -> Unit) : JLabel(text, CENTER) {
|
|||
cornerRadius.toFloat()
|
||||
)
|
||||
if (isHovered) {
|
||||
g2.color = background.brighter()
|
||||
val brightenFactor = 1.1f
|
||||
val brighterColor = Color(
|
||||
(background.red * brightenFactor).toInt(),
|
||||
(background.green * brightenFactor).toInt(),
|
||||
(background.blue * brightenFactor).toInt()
|
||||
)
|
||||
g2.color = brighterColor
|
||||
} else {
|
||||
g2.color = background
|
||||
}
|
||||
|
|
@ -599,23 +627,26 @@ class TransparentArrowButtonUI : BasicComboBoxUI() {
|
|||
val defaultBackground = globalScheme.defaultBackground
|
||||
comboBox.background = defaultBackground
|
||||
|
||||
// Custom renderer to add the down caret symbol
|
||||
comboBox.renderer = object : DefaultListCellRenderer() {
|
||||
override fun getListCellRendererComponent(
|
||||
list: JList<*>?,
|
||||
value: Any?,
|
||||
index: Int,
|
||||
isSelected: Boolean,
|
||||
cellHasFocus: Boolean
|
||||
): Component {
|
||||
val component = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus)
|
||||
if (component is JLabel && index == -1) { // -1 indicates it's the selected item in the ComboBox
|
||||
text = "$text ▾"
|
||||
}
|
||||
foreground = Color(156, 163, 175) // text-gray-400
|
||||
background = comboBox.background
|
||||
isOpaque = false
|
||||
return component
|
||||
// Modify the ComboBoxModel to include the down symbol
|
||||
val originalModel = comboBox.model
|
||||
comboBox.model = object : ComboBoxModel<Any> {
|
||||
override fun getSize(): Int = originalModel.size
|
||||
override fun getElementAt(index: Int): Any? = originalModel.getElementAt(index)
|
||||
override fun setSelectedItem(anItem: Any?) {
|
||||
originalModel.selectedItem = anItem
|
||||
}
|
||||
|
||||
override fun getSelectedItem(): Any? {
|
||||
val item = originalModel.selectedItem
|
||||
return "$item ▾"
|
||||
}
|
||||
|
||||
override fun addListDataListener(l: ListDataListener?) {
|
||||
originalModel.addListDataListener(l)
|
||||
}
|
||||
|
||||
override fun removeListDataListener(l: ListDataListener?) {
|
||||
originalModel.removeListDataListener(l)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,43 +14,24 @@ import java.util.*
|
|||
import javax.swing.JButton
|
||||
import java.awt.geom.RoundRectangle2D
|
||||
|
||||
class StyledButton(text: String, foregroundColor: Color, backgroundColor: Color) : JButton(text) {
|
||||
class StyledButton(text: String) : JButton(text) {
|
||||
private var isHovered = false
|
||||
private val borderColor: Color
|
||||
private val hoverBackgroundColor: Color
|
||||
private val hoverForegroundColor: Color
|
||||
private val originalForegroundColor: Color
|
||||
private val editorBackground: Color
|
||||
|
||||
init {
|
||||
cursor = Cursor(Cursor.HAND_CURSOR)
|
||||
isOpaque = false
|
||||
border = null
|
||||
isContentAreaFilled = false
|
||||
isFocusPainted = false
|
||||
border = null
|
||||
foreground = foregroundColor
|
||||
background = Color(backgroundColor.red, backgroundColor.green, backgroundColor.blue, 0)
|
||||
cursor = Cursor(Cursor.HAND_CURSOR)
|
||||
|
||||
// Store the original foreground color
|
||||
originalForegroundColor = foregroundColor
|
||||
|
||||
// Calculate colors for different states
|
||||
borderColor = Color(foregroundColor.red, foregroundColor.green, foregroundColor.blue, 40) // More subtle border
|
||||
hoverBackgroundColor = Color(backgroundColor.red, backgroundColor.green, backgroundColor.blue, 40)
|
||||
hoverForegroundColor = Color(
|
||||
foregroundColor.red,
|
||||
foregroundColor.green,
|
||||
foregroundColor.blue,
|
||||
160
|
||||
)
|
||||
|
||||
// Get the editor font and its size
|
||||
val scheme = EditorColorsManager.getInstance().globalScheme
|
||||
val editorFont = scheme.getFont(EditorFontType.PLAIN)
|
||||
val editorFontSize = editorFont.size
|
||||
|
||||
// Set the button's font to be slightly smaller than the editor
|
||||
font = font.deriveFont(editorFontSize.toFloat() * 0.75f)
|
||||
|
||||
editorBackground = scheme.defaultBackground
|
||||
|
||||
addMouseListener(object : MouseAdapter() {
|
||||
override fun mouseEntered(e: MouseEvent) {
|
||||
isHovered = true
|
||||
|
|
@ -71,22 +52,19 @@ class StyledButton(text: String, foregroundColor: Color, backgroundColor: Color)
|
|||
|
||||
val width = width.toFloat()
|
||||
val height = height.toFloat()
|
||||
val arc = 4f
|
||||
val arc = 6f
|
||||
|
||||
// Draw button background
|
||||
g2.color = if (isHovered) hoverBackgroundColor else background
|
||||
// Draw semi-transparent background
|
||||
g2.color = editorBackground
|
||||
g2.fill(RoundRectangle2D.Float(0f, 0f, width, height, arc, arc))
|
||||
|
||||
// Draw border
|
||||
g2.color = if (isHovered) hoverForegroundColor else borderColor
|
||||
g2.color = if (isHovered) foreground else foreground.darker()
|
||||
g2.stroke = BasicStroke(1f)
|
||||
g2.draw(RoundRectangle2D.Float(0.5f, 0.5f, width - 1f, height - 1f, arc, arc))
|
||||
|
||||
g2.dispose()
|
||||
|
||||
// Set text color
|
||||
foreground = if (isHovered) hoverForegroundColor else originalForegroundColor
|
||||
|
||||
super.paintComponent(g)
|
||||
g2.dispose()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -95,23 +73,13 @@ class ToolTipComponent(editor: Editor, x: Int, y: Int) :
|
|||
JBPanel<ToolTipComponent>() {
|
||||
private var addToChatButton: StyledButton
|
||||
private var editButton: StyledButton
|
||||
private var backgroundColor: Color
|
||||
private var foregroundColor: Color
|
||||
|
||||
init {
|
||||
layout = null // Remove the FlowLayout
|
||||
|
||||
val globalScheme = EditorColorsManager.getInstance().globalScheme
|
||||
|
||||
backgroundColor = globalScheme.getColor(EditorColors.SELECTION_BACKGROUND_COLOR)
|
||||
?: globalScheme.defaultBackground
|
||||
|
||||
foregroundColor = Color(
|
||||
globalScheme.defaultForeground.red,
|
||||
globalScheme.defaultForeground.green,
|
||||
globalScheme.defaultForeground.blue,
|
||||
200
|
||||
)
|
||||
// Make the background transparent
|
||||
isOpaque = false
|
||||
background = Color(0, 0, 0, 0)
|
||||
|
||||
val cmdCtrlChar =
|
||||
if (System.getProperty("os.name").lowercase(Locale.getDefault()).contains("mac")) "⌘" else "Ctrl"
|
||||
|
|
@ -122,8 +90,8 @@ class ToolTipComponent(editor: Editor, x: Int, y: Int) :
|
|||
val componentHorizontalPadding = 4
|
||||
val buttonMargin = 4
|
||||
|
||||
addToChatButton = StyledButton("Chat (${cmdCtrlChar}J)", foregroundColor, backgroundColor)
|
||||
editButton = StyledButton("Edit (${cmdCtrlChar}I)", foregroundColor, backgroundColor)
|
||||
addToChatButton = StyledButton("Chat (${cmdCtrlChar}J)")
|
||||
editButton = StyledButton("Edit (${cmdCtrlChar}I)")
|
||||
|
||||
addToChatButton.addActionListener { e: ActionEvent? ->
|
||||
focusContinueInput(editor.project)
|
||||
|
|
@ -157,9 +125,5 @@ class ToolTipComponent(editor: Editor, x: Int, y: Int) :
|
|||
// Center the component on the provided y coordinate
|
||||
val yPosition = y - (totalHeight / 2)
|
||||
setBounds(x, yPosition, totalWidth, totalHeight)
|
||||
|
||||
// Make the background transparent
|
||||
isOpaque = false
|
||||
background = Color(0, 0, 0, 0)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,24 +12,42 @@ import com.intellij.openapi.editor.LogicalPosition
|
|||
import com.intellij.openapi.editor.SelectionModel
|
||||
import com.intellij.openapi.editor.event.SelectionEvent
|
||||
import com.intellij.openapi.editor.event.SelectionListener
|
||||
import com.intellij.openapi.fileEditor.FileDocumentManager
|
||||
import com.intellij.openapi.project.DumbAware
|
||||
import com.intellij.openapi.util.TextRange
|
||||
import com.intellij.openapi.vfs.VirtualFile
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlin.math.max
|
||||
import kotlin.math.min
|
||||
import com.intellij.openapi.editor.Document
|
||||
|
||||
class ContinuePluginSelectionListener(
|
||||
private val ideProtocolClient: IdeProtocolClient,
|
||||
private val coroutineScope: CoroutineScope
|
||||
) : SelectionListener, DumbAware {
|
||||
private val debouncer = Debouncer(100L, coroutineScope)
|
||||
private val debouncer = Debouncer(100, coroutineScope)
|
||||
private var toolTipComponents: ArrayList<ToolTipComponent> = ArrayList()
|
||||
|
||||
override fun selectionChanged(e: SelectionEvent) {
|
||||
debouncer.debounce { handleSelection(e) }
|
||||
}
|
||||
|
||||
private var toolTipComponents: ArrayList<ToolTipComponent> = ArrayList()
|
||||
private fun handleSelection(e: SelectionEvent) {
|
||||
ApplicationManager.getApplication().runReadAction {
|
||||
val editor = e.editor
|
||||
val model: SelectionModel = editor.selectionModel
|
||||
val selectedText = model.selectedText
|
||||
|
||||
if (shouldRemoveTooltip(selectedText, editor)) {
|
||||
removeExistingTooltips(editor)
|
||||
return@runReadAction
|
||||
}
|
||||
|
||||
updateTooltip(editor, model)
|
||||
}
|
||||
}
|
||||
|
||||
private fun shouldRemoveTooltip(selectedText: String?, editor: Editor): Boolean {
|
||||
return selectedText.isNullOrEmpty() ||
|
||||
!service<ContinueExtensionSettings>().continueState.displayEditorTooltip ||
|
||||
EditorUtils().isTerminal(editor)
|
||||
}
|
||||
|
||||
private fun removeExistingTooltips(editor: Editor, onComplete: () -> Unit = {}) {
|
||||
ApplicationManager.getApplication().invokeLater {
|
||||
|
|
@ -43,98 +61,114 @@ class ContinuePluginSelectionListener(
|
|||
}
|
||||
}
|
||||
|
||||
private fun handleSelection(e: SelectionEvent) {
|
||||
ApplicationManager.getApplication().runReadAction {
|
||||
val editor = e.editor
|
||||
val model: SelectionModel = editor.selectionModel
|
||||
val selectedText = model.selectedText
|
||||
private fun updateTooltip(editor: Editor, model: SelectionModel) {
|
||||
removeExistingTooltips(editor) {
|
||||
ApplicationManager.getApplication().invokeLater {
|
||||
val document = editor.document
|
||||
val (startLine, endLine, isFullLineSelection) = getSelectionInfo(model, document)
|
||||
val selectionTopY = calculateSelectionTopY(editor, startLine, endLine, isFullLineSelection)
|
||||
val tooltipX = calculateTooltipX(editor, document, startLine, endLine, isFullLineSelection)
|
||||
|
||||
// If selected text is empty, remove the tooltip
|
||||
if (selectedText.isNullOrEmpty()) {
|
||||
removeExistingTooltips(editor)
|
||||
return@runReadAction
|
||||
}
|
||||
|
||||
// Allow user to disable editor tooltip
|
||||
// Note that we still check for empty selected text before this
|
||||
val extensionSettingsService = service<ContinueExtensionSettings>()
|
||||
if (extensionSettingsService.continueState.displayEditorTooltip == false) {
|
||||
return@runReadAction
|
||||
}
|
||||
|
||||
// Don't display in the terminal
|
||||
if (EditorUtils().isTerminal(editor)) {
|
||||
return@runReadAction
|
||||
}
|
||||
|
||||
val document = editor.document
|
||||
val startOffset = model.selectionStart
|
||||
val endOffset = model.selectionEnd
|
||||
val startLine = document.getLineNumber(startOffset)
|
||||
val endLine = document.getLineNumber(endOffset)
|
||||
val virtualFile: VirtualFile? =
|
||||
FileDocumentManager.getInstance().getFile(document)
|
||||
|
||||
|
||||
removeExistingTooltips(editor) {
|
||||
ApplicationManager.getApplication().invokeLater {
|
||||
editor.contentComponent.layout = null
|
||||
|
||||
val startOffset = model.selectionStart
|
||||
val endOffset = model.selectionEnd
|
||||
var startLine = document.getLineNumber(startOffset)
|
||||
val endLine = document.getLineNumber(endOffset)
|
||||
|
||||
// Find the first non-empty line
|
||||
var nonEmptyLine = -1
|
||||
for (line in startLine..endLine) {
|
||||
val lineStartOffset = document.getLineStartOffset(line)
|
||||
val lineEndOffset = document.getLineEndOffset(line)
|
||||
val lineText = document.getText(TextRange(lineStartOffset, lineEndOffset)).trim()
|
||||
if (lineText.isNotEmpty()) {
|
||||
nonEmptyLine = line
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// If no non-empty line found, don't render anything
|
||||
if (nonEmptyLine == -1) {
|
||||
return@invokeLater
|
||||
}
|
||||
|
||||
// Get the text of the first non-empty line
|
||||
val lineStartOffset = document.getLineStartOffset(nonEmptyLine)
|
||||
val lineEndOffset = document.getLineEndOffset(nonEmptyLine)
|
||||
val lineText = document.getText(TextRange(lineStartOffset, lineEndOffset))
|
||||
|
||||
// Calculate the position 20px to the right of the end of the line
|
||||
val endOfLinePos = LogicalPosition(nonEmptyLine, lineText.length)
|
||||
val endOfLineX = editor.logicalPositionToXY(endOfLinePos).x
|
||||
val tooltipX = endOfLineX + 40
|
||||
|
||||
// Calculate the Y position (vertically centered on the line)
|
||||
val lineTopY = editor.logicalPositionToXY(LogicalPosition(nonEmptyLine, 0)).y
|
||||
val lineHeight = editor.lineHeight
|
||||
val y = lineTopY + (lineHeight / 2)
|
||||
|
||||
// Check if x is out of bounds
|
||||
val maxEditorWidth = editor.contentComponent.width
|
||||
val maxToolTipWidth = 600
|
||||
val x = max(0, min(tooltipX, maxEditorWidth - maxToolTipWidth))
|
||||
|
||||
val toolTipComponent = ToolTipComponent(editor, x, y)
|
||||
toolTipComponents.add(toolTipComponent)
|
||||
editor.contentComponent.add(toolTipComponent)
|
||||
|
||||
editor.contentComponent.revalidate()
|
||||
editor.contentComponent.repaint()
|
||||
if (tooltipX != null) {
|
||||
addToolTipComponent(editor, tooltipX, selectionTopY)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun getSelectionInfo(model: SelectionModel, document: Document): Triple<Int, Int, Boolean> {
|
||||
val startOffset = model.selectionStart
|
||||
val endOffset = model.selectionEnd
|
||||
val startLine = document.getLineNumber(startOffset)
|
||||
val endLine = document.getLineNumber(endOffset)
|
||||
|
||||
val isFullLineSelection = startOffset == document.getLineStartOffset(startLine) &&
|
||||
(endOffset == document.getLineEndOffset(endLine - 1) || endOffset == document.getLineStartOffset(endLine))
|
||||
|
||||
val adjustedEndLine = if (isFullLineSelection && endLine > startLine) endLine - 1 else endLine
|
||||
|
||||
return Triple(startLine, adjustedEndLine, isFullLineSelection)
|
||||
}
|
||||
|
||||
private fun calculateSelectionTopY(
|
||||
editor: Editor,
|
||||
startLine: Int,
|
||||
endLine: Int,
|
||||
isFullLineSelection: Boolean
|
||||
): Int {
|
||||
return if (startLine == endLine || isFullLineSelection) {
|
||||
val lineTopY = editor.logicalPositionToXY(LogicalPosition(startLine, 0)).y
|
||||
lineTopY + (editor.lineHeight / 2)
|
||||
} else {
|
||||
editor.logicalPositionToXY(LogicalPosition(startLine, 0)).y
|
||||
}
|
||||
}
|
||||
|
||||
private fun calculateTooltipX(
|
||||
editor: Editor,
|
||||
document: Document,
|
||||
startLine: Int,
|
||||
endLine: Int,
|
||||
isFullLineSelection: Boolean
|
||||
): Int? {
|
||||
fun isLineEmpty(lineNumber: Int): Boolean {
|
||||
val lineStartOffset = document.getLineStartOffset(lineNumber)
|
||||
val lineEndOffset = document.getLineEndOffset(lineNumber)
|
||||
return document.getText(TextRange(lineStartOffset, lineEndOffset)).trim().isEmpty()
|
||||
}
|
||||
|
||||
fun getLineEndX(lineNumber: Int): Int {
|
||||
val lineStartOffset = document.getLineStartOffset(lineNumber)
|
||||
val lineEndOffset = document.getLineEndOffset(lineNumber)
|
||||
val lineText = document.getText(TextRange(lineStartOffset, lineEndOffset)).trimEnd()
|
||||
val endOfLinePos = LogicalPosition(lineNumber, lineText.length)
|
||||
return editor.logicalPositionToXY(endOfLinePos).x
|
||||
}
|
||||
|
||||
val offset = 40
|
||||
|
||||
// If only one line is selected and it's empty, return null
|
||||
if (startLine == endLine && isLineEmpty(startLine) && !isFullLineSelection) {
|
||||
return null
|
||||
}
|
||||
|
||||
// Find the topmost non-empty line within the selection
|
||||
var topNonEmptyLine = startLine
|
||||
while (topNonEmptyLine <= endLine && isLineEmpty(topNonEmptyLine)) {
|
||||
topNonEmptyLine++
|
||||
}
|
||||
|
||||
// If all lines in the selection are empty, return null
|
||||
if (topNonEmptyLine > endLine) {
|
||||
return null
|
||||
}
|
||||
|
||||
// Always display inline if the selection is a single line
|
||||
if (isFullLineSelection || startLine == endLine) {
|
||||
return getLineEndX(topNonEmptyLine) + offset
|
||||
}
|
||||
|
||||
// Check the line above the start of the selection (if it exists)
|
||||
val lineAboveSelection = maxOf(0, startLine - 1)
|
||||
|
||||
// Get x-coordinates
|
||||
val xCoordTopNonEmpty = getLineEndX(topNonEmptyLine)
|
||||
val xCoordLineAbove = getLineEndX(lineAboveSelection)
|
||||
|
||||
// Use the maximum of the two x-coordinates
|
||||
val baseXCoord = maxOf(xCoordTopNonEmpty, xCoordLineAbove)
|
||||
|
||||
// Calculate the final x-coordinate
|
||||
return baseXCoord + offset
|
||||
}
|
||||
|
||||
private fun addToolTipComponent(editor: Editor, tooltipX: Int, selectionTopY: Int) {
|
||||
val toolTipComponent = ToolTipComponent(editor, tooltipX, selectionTopY)
|
||||
toolTipComponents.add(toolTipComponent)
|
||||
editor.contentComponent.add(toolTipComponent)
|
||||
editor.contentComponent.revalidate()
|
||||
editor.contentComponent.repaint()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue