From 2f0ebd944302199e565ffcf7f247e04d02f7a86e Mon Sep 17 00:00:00 2001 From: Daan Vanden Bosch Date: Sat, 24 Apr 2021 13:19:19 +0200 Subject: [PATCH] Added go to symbol to ASM editor. --- .../web/assemblyWorker/AssemblyWorker.kt | 30 +++++++--- .../web/shared/messages/Messages.kt | 15 +++++ .../web/externals/monacoEditor/languages.kt | 58 +++++++++++++++++++ .../web/questEditor/asm/AsmAnalyser.kt | 3 + .../asm/monaco/AsmDefinitionProvider.kt | 9 +-- .../asm/monaco/AsmDocumentSymbolProvider.kt | 32 ++++++++++ .../asm/monaco/MonacoExtensions.kt | 7 --- .../web/questEditor/asm/monaco/MonacoUtils.kt | 18 ++++++ .../web/questEditor/stores/AsmStore.kt | 2 +- 9 files changed, 151 insertions(+), 23 deletions(-) create mode 100644 web/src/main/kotlin/world/phantasmal/web/questEditor/asm/monaco/AsmDocumentSymbolProvider.kt delete mode 100644 web/src/main/kotlin/world/phantasmal/web/questEditor/asm/monaco/MonacoExtensions.kt create mode 100644 web/src/main/kotlin/world/phantasmal/web/questEditor/asm/monaco/MonacoUtils.kt diff --git a/web/assembly-worker/src/main/kotlin/world/phantasmal/web/assemblyWorker/AssemblyWorker.kt b/web/assembly-worker/src/main/kotlin/world/phantasmal/web/assemblyWorker/AssemblyWorker.kt index c090e69d..470bd659 100644 --- a/web/assembly-worker/src/main/kotlin/world/phantasmal/web/assemblyWorker/AssemblyWorker.kt +++ b/web/assembly-worker/src/main/kotlin/world/phantasmal/web/assemblyWorker/AssemblyWorker.kt @@ -112,6 +112,9 @@ class AssemblyWorker(private val sendMessage: (ServerMessage) -> Unit) { is Request.GetDefinition -> getDefinition(message.id, message.lineNo, message.col) + + is Request.GetLabels -> + getLabels(message.id) } } @@ -448,18 +451,23 @@ class AssemblyWorker(private val sendMessage: (ServerMessage) -> Unit) { .filter { label in it.labels } .mapNotNull { segment -> val labelIdx = segment.labels.indexOf(label) + segment.srcLoc.labels.getOrNull(labelIdx)?.toAsmRange() + } + .toList() - segment.srcLoc.labels.getOrNull(labelIdx)?.let { labelSrcLoc -> - AsmRange( - startLineNo = labelSrcLoc.lineNo, - startCol = labelSrcLoc.col, - endLineNo = labelSrcLoc.lineNo, - endCol = labelSrcLoc.col + labelSrcLoc.len, - ) + private fun getLabels(requestId: Int) { + val result = bytecodeIr.segments.asSequence() + .flatMap { segment -> + segment.labels.mapIndexed { labelIdx, label -> + val range = segment.srcLoc.labels.getOrNull(labelIdx)?.toAsmRange() + Label(name = label, range) } } .toList() + sendMessage(Response.GetLabels(requestId, result)) + } + private fun positionInside(lineNo: Int, col: Int, srcLoc: SrcLoc?): Boolean = if (srcLoc == null) { false @@ -470,6 +478,14 @@ class AssemblyWorker(private val sendMessage: (ServerMessage) -> Unit) { @Suppress("RedundantNullableReturnType") // Can return undefined. private fun getLine(lineNo: Int): String? = asm[lineNo - 1] + private fun SrcLoc.toAsmRange(): AsmRange = + AsmRange( + startLineNo = lineNo, + startCol = col, + endLineNo = lineNo, + endCol = col + len, + ) + companion object { private val KEYWORD_REGEX = Regex("""^\s*\.[a-z]+${'$'}""") private val KEYWORD_SUGGESTIONS: List = diff --git a/web/shared/src/commonMain/kotlin/world/phantasmal/web/shared/messages/Messages.kt b/web/shared/src/commonMain/kotlin/world/phantasmal/web/shared/messages/Messages.kt index fd8defc9..e80b2c4f 100644 --- a/web/shared/src/commonMain/kotlin/world/phantasmal/web/shared/messages/Messages.kt +++ b/web/shared/src/commonMain/kotlin/world/phantasmal/web/shared/messages/Messages.kt @@ -48,6 +48,9 @@ sealed class Request : ClientMessage() { @Serializable class GetDefinition(override val id: Int, val lineNo: Int, val col: Int) : Request() + + @Serializable + class GetLabels(override val id: Int) : Request() } @Serializable @@ -98,6 +101,12 @@ sealed class Response : ServerMessage() { override val id: Int, override val result: List, ) : Response>() + + @Serializable + class GetLabels( + override val id: Int, + override val result: List