From 008af1f7e3f46ae98a0265a31d4d91fa2627dce4 Mon Sep 17 00:00:00 2001 From: Kagura Date: Fri, 1 Nov 2024 10:05:16 +0800 Subject: [PATCH] style: reformat --- src/main/kotlin/Main.kt | 29 ++++++++++--------- src/main/kotlin/core/CParser.kt | 11 ++++--- src/main/kotlin/core/GraphvizHelper.kt | 18 ++++++------ src/main/kotlin/core/TraceTree.kt | 6 ++-- src/main/kotlin/ui/TextfieldWithLineNumber.kt | 2 +- src/main/kotlin/utils/OpenBrowser.kt | 17 ++++++----- src/main/kotlin/utils/stringMagic.kt | 10 +++---- 7 files changed, 47 insertions(+), 46 deletions(-) diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index 1e533d7..a6c85c2 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -32,7 +32,6 @@ import utils.* @Composable @Preview fun App() { - var source: SourceFile? = null val showResult = remember { mutableStateOf(false) } var defineList by remember { mutableStateOf("") } var useList by remember { mutableStateOf("") } @@ -51,8 +50,10 @@ fun App() { }) { AlertDialog( title = { - Column(modifier = Modifier.fillMaxWidth(), - horizontalAlignment = Alignment.CenterHorizontally) { + Column( + modifier = Modifier.fillMaxWidth(), + horizontalAlignment = Alignment.CenterHorizontally + ) { Text( text = when (page) { 0 -> "函数定义的变量" @@ -69,8 +70,10 @@ fun App() { } }, text = { - Box(modifier = Modifier.fillMaxWidth() - .fillMaxHeight(0.8f)) { + Box( + modifier = Modifier.fillMaxWidth() + .fillMaxHeight(0.8f) + ) { Text( text = when (page) { 0 -> defineList @@ -104,21 +107,21 @@ fun App() { dismissButton = { TextButton( onClick = { - if (page < 2){ + if (page < 2) { showResult.value = false - }else{ - if (page == 2){ + } else { + if (page == 2) { openGraph(ttGraph) - }else if (page == 3){ + } else if (page == 3) { openGraph(ivGraph) } } } ) { Text( - if (page < 2){ + if (page < 2) { "关闭" - }else{ + } else { "查看图像(在线)" } ) @@ -151,7 +154,6 @@ fun App() { return@TextfieldWithLineNumber } val sf = SourceFile(it) - source = sf try { sf.parseFunction() } catch (e: Exception) { @@ -197,8 +199,7 @@ fun App() { traceTree = traceTreeStr.toString() - funcTraceTree = - getInvokeTraceTreeString(sf) + funcTraceTree = getInvokeTraceTreeString(sf) ttGraph = generateGraph(relations) ivGraph = sf.functions.getInvokeGraph() diff --git a/src/main/kotlin/core/CParser.kt b/src/main/kotlin/core/CParser.kt index d1dba48..7a6a337 100644 --- a/src/main/kotlin/core/CParser.kt +++ b/src/main/kotlin/core/CParser.kt @@ -95,20 +95,19 @@ class CParser(content: String, paramNameList: List) { } } } - } - else { + } else { if (right in defineList) { // 看看有没有使用值 result.add( Sentence( "-", right, getType() ) ) - } else{ // 看是不是有运算符号 - for (op in c_op_list){ - if (op in right){ + } else { // 看是不是有运算符号 + for (op in c_op_list) { + if (op in right) { val splits = right.split(op) splits.forEach { split -> - result.addAll(rightHandSimpleParser(split.trim(),type)) + result.addAll(rightHandSimpleParser(split.trim(), type)) } } } diff --git a/src/main/kotlin/core/GraphvizHelper.kt b/src/main/kotlin/core/GraphvizHelper.kt index 409664d..0655f0b 100644 --- a/src/main/kotlin/core/GraphvizHelper.kt +++ b/src/main/kotlin/core/GraphvizHelper.kt @@ -34,7 +34,7 @@ fun generateGraph(relations: Map>, crossLabelPaths: List< // from 到 to 用虚线 val str = "$fromNode${parts[1]} -> $toNode${parts[0]} [style=dashed];" val hash = str.hashCode() - if (hash !in hashes){ // 不重复 + if (hash !in hashes) { // 不重复 sb.append(str) hashes.add(hash) } @@ -48,18 +48,18 @@ fun generateGraph(relations: Map>, crossLabelPaths: List< } -fun parseRelation(funcName: String, parse: String): Map>{ +fun parseRelation(funcName: String, parse: String): Map> { val list = mutableListOf() val added = mutableListOf() parse.split('\n').forEach { line -> val split = line.split("->") - if (split.size < 2){ + if (split.size < 2) { return@forEach } - for (i in 0...getInvokeGraph(): String { val relations = mutableMapOf>() val crossLabelPaths = StringBuilder() this.forEach { - relations.putAll(parseRelation(it.name,it.getTraceTree().getStringRepr())) + relations.putAll(parseRelation(it.name, it.getTraceTree().getStringRepr())) crossLabelPaths.append(this.getInvokeTree(it.name).getFuncRepr(it.name)) crossLabelPaths.append('\n') } - return generateGraph(relations,crossLabelPaths.split('\n').filter { it.isNotEmpty() }) + return generateGraph(relations, crossLabelPaths.split('\n').filter { it.isNotEmpty() }) } \ No newline at end of file diff --git a/src/main/kotlin/core/TraceTree.kt b/src/main/kotlin/core/TraceTree.kt index 9761827..f1c4dec 100644 --- a/src/main/kotlin/core/TraceTree.kt +++ b/src/main/kotlin/core/TraceTree.kt @@ -22,7 +22,7 @@ fun List.findNode(name: String): TraceTree? { fun List.getStringRepr(): String { fun getChanges(node: TraceTree, currentPath: String = node.name, recTimes: Int = 0): String { - if (recTimes >= 10){ // 防止无限循环 + if (recTimes >= 10) { // 防止无限循环 return "" } @@ -34,7 +34,7 @@ fun List.getStringRepr(): String { } else { // 挨个获取 change for (child in node.changes) { - paths.add(getChanges(child, "$currentPath->${child.name}",recTimes + 1)) + paths.add(getChanges(child, "$currentPath->${child.name}", recTimes + 1)) } } @@ -56,7 +56,7 @@ fun Map>.getFuncRepr(func: String): String { this.forEach { (name, list) -> val listRepr = list.getStringRepr() for (line in listRepr.lines()) { - if (line.startsWith(func)){ // main:x-> 这样的 + if (line.startsWith(func)) { // main:x-> 这样的 result.append("$name:$line\n") } } diff --git a/src/main/kotlin/ui/TextfieldWithLineNumber.kt b/src/main/kotlin/ui/TextfieldWithLineNumber.kt index df3619f..257aa02 100644 --- a/src/main/kotlin/ui/TextfieldWithLineNumber.kt +++ b/src/main/kotlin/ui/TextfieldWithLineNumber.kt @@ -97,7 +97,7 @@ fun TextfieldWithLineNumber( onValueChange = { textFieldValue -> val nbLines = textFieldValue.count { it == '\n' } + 1 if (nbLines != linesText) linesText = nbLines - text = textFieldValue.replace("\t"," ") // \t 显示不了 + text = textFieldValue.replace("\t", " ") // \t 显示不了 }, ) diff --git a/src/main/kotlin/utils/OpenBrowser.kt b/src/main/kotlin/utils/OpenBrowser.kt index f4c93f3..11bf117 100644 --- a/src/main/kotlin/utils/OpenBrowser.kt +++ b/src/main/kotlin/utils/OpenBrowser.kt @@ -17,24 +17,25 @@ fun openBrowser(uri: URI) { "nix" in osName || "nux" in osName -> { try { Runtime.getRuntime().exec("xdg-open $uri") - }catch (_: IOException){ + } catch (_: IOException) { // xdg-open 不存在 - if (File("/run/current-system/sw/bin/xdg-open").exists()){ // nixos + if (File("/run/current-system/sw/bin/xdg-open").exists()) { // nixos Runtime.getRuntime().exec("/run/current-system/sw/bin/kde-open $uri") - }else{ - val clipboard = Toolkit.getDefaultToolkit().systemClipboard + } else { + val clipboard = Toolkit.getDefaultToolkit().systemClipboard val content = StringSelection(uri.toString()) clipboard.setContents(content, null) } } } + else -> desktop.browse(uri) } } -fun openGraph(graph: String){ +fun openGraph(graph: String) { val site = "https://dreampuf.github.io/GraphvizOnline/#" - val data = java.net.URLEncoder.encode(graph,"utf-8") - .replace("+","%20") - openBrowser(URI.create(site+data)) + val data = java.net.URLEncoder.encode(graph, "utf-8") + .replace("+", "%20") + openBrowser(URI.create(site + data)) } \ No newline at end of file diff --git a/src/main/kotlin/utils/stringMagic.kt b/src/main/kotlin/utils/stringMagic.kt index 1aeed00..98b6aa4 100644 --- a/src/main/kotlin/utils/stringMagic.kt +++ b/src/main/kotlin/utils/stringMagic.kt @@ -9,7 +9,7 @@ fun getDefineList(defList: Map>): String = .replace("{", "") .replace("}", "") // 解决左边和右边的括号 .replace("=", ":def") - .replace("], ","]\n") + .replace("], ", "]\n") fun getUseList(useList: Map>): String = @@ -35,15 +35,15 @@ fun getInvokeTraceTreeString(sourceFile: SourceFile): String { val tree = StringBuilder() for (function in sourceFile.functions) { val chain = sourceFile.functions.getInvokeTree(function.name).getFuncRepr(function.name) - if (chain.isNotBlank()){ + if (chain.isNotBlank()) { tree.append("在 ${function.name} 函数中调用过下面的函数:\n") tree.append(chain) tree.append("\n\n") } } return tree.toString() - .replace("[","") - .replace("]","") - .replace(", ","\n") + .replace("[", "") + .replace("]", "") + .replace(", ", "\n") } \ No newline at end of file