Skip to content

Commit

Permalink
add hover functionality while keeping click function
Browse files Browse the repository at this point in the history
currently commented out so that it can be demonstrated.

Signed-off-by: Tim Herzig <[email protected]>
  • Loading branch information
timherzig committed Dec 12, 2023
1 parent 9041d7d commit ea32d5d
Showing 1 changed file with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.intellij.codeInsight.hint.HintUtil
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.event.EditorMouseEvent
import com.intellij.openapi.editor.event.EditorMouseListener
import com.intellij.openapi.editor.event.EditorMouseMotionListener
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.PsiFile
Expand All @@ -22,27 +23,27 @@ import javax.swing.JComponent
class HoverAction(private val editor: Editor, private val result: XMLParser.ResultData) {
fun addHoverAction() {
this.editor.addEditorMouseListener(MouseClick())
// this.editor.addEditorMouseMotionListener(MouseMotion())
}

// Currently not in use, but could be used for hover behaviour
// inner class MouseMotion : EditorMouseMotionListener {
// override fun mouseMoved(event: EditorMouseEvent) {
// showHoverMessage(event.mouseEvent.point)
// }
// }
inner class MouseMotion : EditorMouseMotionListener {
override fun mouseMoved(event: EditorMouseEvent) {
showHoverMessage(event.mouseEvent.point)
}
}

inner class MouseClick : EditorMouseListener {
override fun mouseClicked(event: EditorMouseEvent) {
showHoverMessage(event.mouseEvent.point)
}
}

private fun buildHoverMessage(): String? {
private fun buildHoverMessage(point: Point): String? {
val project: Project = this.editor.project ?: return null
val psiFile: PsiFile = PsiDocumentManager.getInstance(project).getPsiFile(this.editor.document) ?: return null

val offset: Int = this.editor.caretModel.offset
val line: Int = this.editor.caretModel.visualPosition.getLine() + 1
val offset: Int = this.editor.visualPositionToOffset(this.editor.xyToVisualPosition(point))
val line: Int = this.editor.yToVisualLine(point.y) + 1
PsiTreeUtil.findElementOfClassAtOffset(psiFile, offset, psiFile.javaClass, false)

for (r in this.result.mutationResults) {
Expand All @@ -57,7 +58,7 @@ class HoverAction(private val editor: Editor, private val result: XMLParser.Resu
}

fun showHoverMessage(point: Point) {
val message: String = buildHoverMessage() ?: return
val message: String = buildHoverMessage(point) ?: return
val hintManager: HintManagerImpl = HintManagerImpl.getInstanceImpl()
val label: JComponent = HintUtil.createInformationLabel(message, null, null, null)
AccessibleContextUtil.setName(label, "PiTest")
Expand Down

0 comments on commit ea32d5d

Please sign in to comment.