-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Notes: * DexTextArea & OatTextArea are trivial, but I think it results in a cleaner project. * The `oat` package currently contains only OatTextArea, but it would make sense to move the `oat` parsing there too.
- Loading branch information
1 parent
3df4437
commit aa8461d
Showing
7 changed files
with
219 additions
and
135 deletions.
There are no files selected for viewing
130 changes: 130 additions & 0 deletions
130
src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/CodeTextArea.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
* Copyright (C) 2023 Romain Guy | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dev.romainguy.kotlin.explorer | ||
|
||
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea | ||
import java.awt.Graphics | ||
import java.awt.Graphics2D | ||
import javax.swing.event.CaretEvent | ||
import javax.swing.event.CaretListener | ||
|
||
open class CodeTextArea( | ||
private val explorerState: ExplorerState, | ||
jumpRegex: Regex, | ||
addressedRegex: Regex, | ||
private val lineNumberRegex: Regex?, | ||
) : RSyntaxTextArea() { | ||
private val jumpDetector = JumpDetector(jumpRegex, addressedRegex) | ||
private var jumpOffsets: JumpOffsets? = null | ||
private var fullText = "" | ||
|
||
|
||
init { | ||
addCaretListener(::caretUpdate) | ||
} | ||
|
||
final override fun addCaretListener(listener: CaretListener?) { | ||
super.addCaretListener(listener) | ||
} | ||
|
||
override fun setText(text: String) { | ||
fullText = text | ||
refreshText() | ||
} | ||
|
||
fun refreshText() { | ||
val saveCaret = caretPosition | ||
super.setText(fullText.takeIf { explorerState.showLineNumbers } ?: fullText.removeLineLumbers()) | ||
caretPosition = saveCaret | ||
} | ||
|
||
override fun paintComponent(g: Graphics?) { | ||
super.paintComponent(g) | ||
jumpOffsets?.let { jump -> | ||
val padding = 6 | ||
|
||
val bounds1 = modelToView2D(jump.src) | ||
val bounds2 = modelToView2D(jump.dst) | ||
|
||
val x1 = bounds1.x.toInt() - padding | ||
val y1 = (bounds1.y + lineHeight / 2).toInt() | ||
|
||
val x2 = bounds2.x.toInt() - padding | ||
val y2 = (bounds2.y + lineHeight / 2).toInt() | ||
|
||
val x0 = modelToView2D(2).x.toInt() | ||
val g2 = g as Graphics2D | ||
g2.drawLine(x1, y1, x0, y1) | ||
g2.drawLine(x0, y1, x0, y2) | ||
g2.drawLine(x0, y2, x2, y2) | ||
} | ||
} | ||
|
||
private fun caretUpdate(event: CaretEvent) { | ||
val oldJumpOffsets = jumpOffsets | ||
jumpOffsets = null | ||
val srcLine = getLineOfOffset(event.dot) | ||
var start = getLineStartOffset(srcLine) | ||
var end = getLineEndOffset(srcLine) | ||
|
||
val caretLine = document.getText(start, end - start).trimEnd() | ||
val jump = jumpDetector.detectJump(caretLine) | ||
if (jump != null) { | ||
val srcOffset = start + caretLine.countPadding() | ||
|
||
var dstLine = srcLine + jump.direction | ||
while (dstLine in 0..<lineCount) { | ||
start = getLineStartOffset(dstLine) | ||
end = getLineEndOffset(dstLine) | ||
val line = document.getText(start, end - start).trimEnd() | ||
|
||
val addressed = jumpDetector.detectAddressed(line) ?: break | ||
if (addressed == jump.address) { | ||
val dstOffset = start + line.countPadding() | ||
jumpOffsets = JumpOffsets(srcOffset, dstOffset) | ||
} | ||
|
||
dstLine += jump.direction | ||
} | ||
} | ||
|
||
if (jumpOffsets != oldJumpOffsets) { | ||
repaint() | ||
} | ||
} | ||
|
||
private fun String.removeLineLumbers() = | ||
lineNumberRegex?.replace(this) { | ||
val (start, end) = it.groupValues.drop(1) | ||
" ".repeat(start.length) + end | ||
} ?: this | ||
|
||
private data class JumpOffsets(val src: Int, val dst: Int) | ||
|
||
private class Jump(val address: String, val direction: Int) | ||
|
||
private class JumpDetector(private val jumpRegex: Regex, private val addressedRegex: Regex) { | ||
fun detectJump(line: String): Jump? { | ||
val match = jumpRegex.matchEntire(line) ?: return null | ||
return Jump(match.getValue("address"), if (match.getValue("direction") == "+") 1 else -1) | ||
} | ||
|
||
fun detectAddressed(line: String) = addressedRegex.matchEntire(line)?.getValue("address") | ||
} | ||
} | ||
|
||
private fun String.countPadding() = indexOfFirst { it != ' ' } |
128 changes: 0 additions & 128 deletions
128
src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/DexTextArea.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright (C) 2023 Romain Guy | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dev.romainguy.kotlin.explorer | ||
|
||
fun MatchResult.getValue(group: String): String { | ||
return groups[group]?.value ?: throw IllegalStateException("Value of $group not found in $value") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/dex/DexTextArea.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright (C) 2023 Romain Guy | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dev.romainguy.kotlin.explorer.dex | ||
|
||
import dev.romainguy.kotlin.explorer.CodeTextArea | ||
import dev.romainguy.kotlin.explorer.ExplorerState | ||
|
||
private val DexJumpRegex = | ||
Regex("^.{9}[0-9a-fA-F]{4}: .+(?<address>[0-9a-fA-F]{4}) // (?<direction>[+-])[0-9a-fA-F]{4}$") | ||
private val DexAddressedRegex = Regex("^.{9}(?<address>[0-9a-fA-F]{4}): .+$") | ||
private val DexLineNumberRegex = Regex("^(?<lineNumber> +\\d+: )([0-9a-f]{4}: )") | ||
|
||
class DexTextArea(explorerState: ExplorerState) : | ||
CodeTextArea(explorerState, DexJumpRegex, DexAddressedRegex, DexLineNumberRegex) | ||
|
Oops, something went wrong.