style: reformat

This commit is contained in:
Kagura 2024-11-01 10:05:16 +08:00
parent 0e461e1419
commit 008af1f7e3
7 changed files with 47 additions and 46 deletions

View file

@ -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()

View file

@ -95,20 +95,19 @@ class CParser(content: String, paramNameList: List<String>) {
}
}
}
}
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))
}
}
}

View file

@ -34,7 +34,7 @@ fun generateGraph(relations: Map<String, List<Relation>>, 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<String, List<Relation>>, crossLabelPaths: List<
}
fun parseRelation(funcName: String, parse: String): Map<String, List<Relation>>{
fun parseRelation(funcName: String, parse: String): Map<String, List<Relation>> {
val list = mutableListOf<Relation>()
val added = mutableListOf<Int>()
parse.split('\n').forEach { line ->
val split = line.split("->")
if (split.size < 2){
if (split.size < 2) {
return@forEach
}
for (i in 0..<split.lastIndex){
val hash = "${split[i]}->${split[i+1]}".hashCode()
if (hash !in added){
list.add(Relation(split[i],split[i+1]))
for (i in 0..<split.lastIndex) {
val hash = "${split[i]}->${split[i + 1]}".hashCode()
if (hash !in added) {
list.add(Relation(split[i], split[i + 1]))
added.add(hash)
}
}
@ -73,9 +73,9 @@ fun List<CFunction>.getInvokeGraph(): String {
val relations = mutableMapOf<String, List<Relation>>()
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() })
}

View file

@ -22,7 +22,7 @@ fun List<TraceTree>.findNode(name: String): TraceTree? {
fun List<TraceTree>.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<TraceTree>.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<String, List<TraceTree>>.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")
}
}

View file

@ -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 显示不了
},
)

View file

@ -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{
} 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))
}

View file

@ -9,7 +9,7 @@ fun getDefineList(defList: Map<String, List<String>>): String =
.replace("{", "")
.replace("}", "") // 解决左边和右边的括号
.replace("=", ":def")
.replace("], ","]\n")
.replace("], ", "]\n")
fun getUseList(useList: Map<String, List<String>>): 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")
}