Skip to content

Commit

Permalink
feat(toolkit): Highlight comment nodes when clicking on the correspon…
Browse files Browse the repository at this point in the history
…ding trivia in the AST tree
  • Loading branch information
felipebz committed Mar 10, 2024
1 parent 2d22d47 commit d206e03
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package com.felipebz.flr.internal.toolkit

import com.felipebz.flr.api.AstNode
import com.felipebz.flr.api.Trivia
import com.felipebz.flr.toolkit.ConfigurationModel
import com.felipebz.flr.toolkit.ConfigurationProperty
import com.felipebz.flr.xpath.api.AstNodeXPathQuery.Companion.create
Expand Down Expand Up @@ -132,7 +133,7 @@ internal class ToolkitPresenter(private val configurationModel: ConfigurationMod
}
}
view.scrollAstTo(firstAstNode)
view.scrollSourceCodeTo(firstAstNode)
view.scrollSourceCodeTo(firstAstNode?.tokenOrNull)
view.setFocusOnAbstractSyntaxTreeView()
}

Expand Down Expand Up @@ -163,7 +164,20 @@ internal class ToolkitPresenter(private val configurationModel: ConfigurationMod
view.highlightSourceCode(astNode.token, astNode.lastToken)
}
}
view.scrollSourceCodeTo(firstAstNode)

var firstTrivia: Trivia? = null
for (trivia in view.selectedTrivias) {
if (firstTrivia == null) {
firstTrivia = trivia
}
view.highlightSourceCode(trivia.token, trivia.tokens.last())
}

if (firstAstNode != null) {
view.scrollSourceCodeTo(firstAstNode.tokenOrNull)
} else {
view.scrollSourceCodeTo(firstTrivia?.token)
}
}

fun onSymbolSelectionChanged() {
Expand All @@ -175,7 +189,7 @@ internal class ToolkitPresenter(private val configurationModel: ConfigurationMod
}
view.highlightSourceCode(astNode.token, astNode.lastToken)
}
view.scrollSourceCodeTo(firstAstNode)
view.scrollSourceCodeTo(firstAstNode?.tokenOrNull)
}

fun onConfigurationPropertyFocusLost(name: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package com.felipebz.flr.internal.toolkit

import com.felipebz.flr.api.AstNode
import com.felipebz.flr.api.Token
import com.felipebz.flr.api.Trivia
import org.sonar.plugins.plsqlopen.api.symbols.Scope
import java.awt.Point
import java.io.File
Expand Down Expand Up @@ -142,11 +143,11 @@ internal interface ToolkitView {
fun clearSourceCodeHighlights()

/**
* Scroll the source code editor in order to make the given AST node visible.
* Scroll the source code editor in order to make the given token visible.
*
* @param astNode The AST node to make visible, null will lead to a no operation
* @param astNode The token to make visible, null will lead to a no operation
*/
fun scrollSourceCodeTo(astNode: AstNode?)
fun scrollSourceCodeTo(astNode: Token?)

/**
* Disable the XPath evaluate button.
Expand All @@ -172,6 +173,8 @@ internal interface ToolkitView {
*/
val selectedAstNodes: List<AstNode>

val selectedTrivias: List<Trivia>

val selectedSymbolOrScopeTrees: List<AstNode>

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package com.felipebz.flr.internal.toolkit

import com.felipebz.flr.api.AstNode
import com.felipebz.flr.api.Token
import com.felipebz.flr.api.Trivia
import org.sonar.plugins.plsqlopen.api.symbols.Scope
import org.sonar.plugins.plsqlopen.api.symbols.Symbol
import java.awt.*
Expand Down Expand Up @@ -329,11 +330,11 @@ internal class ToolkitViewImpl(@Transient val presenter: ToolkitPresenter) : JFr
}
}

override fun scrollSourceCodeTo(astNode: AstNode?) {
if (astNode != null && astNode.hasToken()) {
override fun scrollSourceCodeTo(token: Token?) {
if (token != null) {
val visibleLines =
sourceCodeEditorPane.visibleRect.height / sourceCodeEditorPane.getFontMetrics(sourceCodeEditorPane.font).height
val line = astNode.token.line + visibleLines / 2
val line = token.line + visibleLines / 2
try {
sourceCodeEditorPane.scrollRectToVisible(toRectangle(sourceCodeEditorPane.modelToView2D(0)))
sourceCodeEditorPane.scrollRectToVisible(
Expand Down Expand Up @@ -398,6 +399,22 @@ internal class ToolkitViewImpl(@Transient val presenter: ToolkitPresenter) : JFr
return acc
}

override val selectedTrivias: List<Trivia>
get() {
val acc = mutableListOf<Trivia>()
val selectedPaths = astTree.selectionPaths
if (selectedPaths != null) {
for (selectedPath in selectedPaths) {
val treeNode = selectedPath.lastPathComponent as DefaultMutableTreeNode
val userObject = treeNode.userObject
if (userObject is Trivia) {
acc.add(userObject)
}
}
}
return acc
}

override val selectedSymbolOrScopeTrees: List<AstNode>
get() {
val acc = mutableListOf<AstNode>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ class ToolkitPresenterTest {
presenter.onXPathEvaluateButtonClick()
verify(view).scrollAstTo(astNode)
verify(view, never()).scrollAstTo(childAstNode)
verify(view).scrollSourceCodeTo(astNode)
verify(view, never()).scrollSourceCodeTo(childAstNode)
verify(view).scrollSourceCodeTo(astNode.tokenOrNull)
verify(view, never()).scrollSourceCodeTo(childAstNode.tokenOrNull)
}

@Test
Expand All @@ -286,7 +286,7 @@ class ToolkitPresenterTest {
verify(view, never()).selectAstNode(any())
verify(view, never()).highlightSourceCode(any(), any())
verify(view).scrollAstTo(null)
verify(view).scrollSourceCodeTo(null as AstNode?)
verify(view).scrollSourceCodeTo(null as Token?)
verify(view).setFocusOnAbstractSyntaxTreeView()
}

Expand Down Expand Up @@ -325,8 +325,8 @@ class ToolkitPresenterTest {
presenter.setView(view)
presenter.onAstSelectionChanged()
verify(view).clearSourceCodeHighlights()
verify(view).scrollSourceCodeTo(firstAstNode)
verify(view, never()).scrollSourceCodeTo(secondAstNode)
verify(view).scrollSourceCodeTo(firstAstNode.tokenOrNull)
verify(view).scrollSourceCodeTo(secondAstNode.tokenOrNull)
}

@Test
Expand Down

0 comments on commit d206e03

Please sign in to comment.