Skip to content

Commit

Permalink
Feature/71 implement hover action (#127)
Browse files Browse the repository at this point in the history
* Add HoverAction class to handle mouse hover events

Signed-off-by: Tim Herzig <[email protected]>

* show message on selected line

bug: message shows when moving away from a position, not when "arriving"
Signed-off-by: Tim Herzig <[email protected]>

* Add hover action to show mutation results

Signed-off-by: Tim Herzig <[email protected]>

* remove unused imports and comments

Signed-off-by: Tim Herzig <[email protected]>

* lint correction

Signed-off-by: Tim Herzig <[email protected]>

* lint correction

Signed-off-by: Tim Herzig <[email protected]>

* kotlin conventions

Signed-off-by: Tim Herzig <[email protected]>

* lint correction

Signed-off-by: Tim Herzig <[email protected]>

* lint correction

Signed-off-by: Tim Herzig <[email protected]>

* add hover functionality while keeping click function

currently commented out so that it can be demonstrated.

Signed-off-by: Tim Herzig <[email protected]>

* comment TODO for collective choice

Signed-off-by: Tim Herzig <[email protected]>

---------

Signed-off-by: Tim Herzig <[email protected]>
  • Loading branch information
timherzig authored Dec 12, 2023
1 parent 4399d3f commit 469432a
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2023 Tim Herzig <[email protected]>

package com.amos.pitmutationmate.pitmutationmate.actions

import com.amos.pitmutationmate.pitmutationmate.reporting.XMLParser
import com.intellij.codeInsight.hint.HintManager
import com.intellij.codeInsight.hint.HintManagerImpl
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
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.ui.LightweightHint
import com.intellij.util.ui.accessibility.AccessibleContextUtil
import java.awt.Point
import javax.swing.JComponent

class HoverAction(private val editor: Editor, private val result: XMLParser.ResultData) {
fun addHoverAction() {
this.editor.addEditorMouseListener(MouseClick())
// TODO: Decide on which action suits the plugin the best --> when choice is made refactor to only use one
// this.editor.addEditorMouseMotionListener(MouseMotion())
}

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(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.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) {
if (r.lineNumber == line) {
val color: String = if (r.detected) "dark-green" else "dark-pink"
return "PiTest: selected offset: $offset, selected line: $line \n" +
"The color of this line is $color"
}
}

return null
}

fun showHoverMessage(point: Point) {
val message: String = buildHoverMessage(point) ?: return
val hintManager: HintManagerImpl = HintManagerImpl.getInstanceImpl()
val label: JComponent = HintUtil.createInformationLabel(message, null, null, null)
AccessibleContextUtil.setName(label, "PiTest")
val hint = LightweightHint(label)
val p: Point = HintManagerImpl.getHintPosition(hint, this.editor, this.editor.xyToVisualPosition(point), 1)
val flags: Int = HintManager.HIDE_BY_ANY_KEY or HintManager.HIDE_BY_TEXT_CHANGE or HintManager.HIDE_BY_SCROLLING
hintManager.showEditorHint(hint, this.editor, p, flags, 0, true, 1)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ abstract class RunConfigurationAction : AnAction() {
if (editor != null) {
// TODO: use actual XML report directories. This currently uses a placeholder test folder
val dir = Paths.get("build", "reports", "pitest", "test", "mutations.xml")
var xmlListener = XMLListener(dir, editor)
val xmlListener = XMLListener(dir, editor)
xmlListener.listen()
val ha: HoverAction = HoverAction(editor, xmlListener.getResult())
ha.addHoverAction()
}

// Update visualisation with mock results
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ class XMLListener(private var dir: Path, private var editor: Editor) {
displayResults()
}

fun getResult(): XMLParser.ResultData {
return this.result
}

private fun loadResults() {
val parser: XMLParser = XMLParser()
result = parser.loadResultsFromXmlReport(this.dir.toString())
this.result = parser.loadResultsFromXmlReport(this.dir.toString())
}

fun displayResults() {
Expand Down

0 comments on commit 469432a

Please sign in to comment.