Skip to content

Commit f7aee83

Browse files
committed
fix: try to six string type issue
1 parent e0d9d89 commit f7aee83

File tree

3 files changed

+120
-33
lines changed

3 files changed

+120
-33
lines changed

java/src/main/kotlin/cc/unitmesh/idea/context/JavaContextCollectionUtilsKt.kt

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package cc.unitmesh.idea.context
22

33
import cc.unitmesh.devti.context.SimpleClassStructure
44
import com.intellij.psi.*
5+
import com.intellij.psi.impl.source.PsiClassReferenceType
56
import com.intellij.psi.search.GlobalSearchScope
67
import com.intellij.psi.search.SearchScope
78
import com.intellij.psi.search.searches.MethodReferencesSearch
89
import com.intellij.psi.search.searches.ReferencesSearch
10+
import org.jetbrains.kotlin.psi.psiUtil.contains
911

1012
object JavaContextCollectionUtilsKt {
1113
fun findUsages(nameIdentifierOwner: PsiNameIdentifierOwner): List<PsiReference> {
@@ -31,31 +33,37 @@ object JavaContextCollectionUtilsKt {
3133
*
3234
* For example, if a BlogPost class includes a Comment class, and the Comment class includes a User class, then the resulting tree will be:
3335
*
36+
* ```
3437
* parent: BlogPost Psi
35-
* child: I'd
36-
* child: Comment psi
37-
* child: User psi
38+
* child: id
39+
* child: Comment
40+
* child: User
41+
* child: name
42+
*```
3843
*/
39-
fun dataStructure(clazz: PsiClass): HashMap<String, SimpleClassStructure> {
40-
val classTree = HashMap<String, SimpleClassStructure>()
41-
buildSimpleClassStructure(clazz, classTree)
42-
return classTree
44+
fun dataStructure(clazz: PsiClass): SimpleClassStructure {
45+
val project = clazz.project
46+
val searchScope = GlobalSearchScope.allScope(project) as SearchScope
47+
return createSimpleStructure(clazz ,searchScope)
4348
}
4449

45-
private fun buildSimpleClassStructure(clazz: PsiClass, classTree: HashMap<String, SimpleClassStructure>) {
50+
private fun createSimpleStructure(clazz: PsiClass, searchScope: SearchScope): SimpleClassStructure {
4651
val fields = clazz.fields
47-
val children = fields.mapNotNull {
48-
if (it.type is PsiClass) {
49-
val childSimpleClassStructure = SimpleClassStructure(it.name, it.type.canonicalText, emptyList())
50-
buildSimpleClassStructure(it.type as PsiClass, classTree)
51-
childSimpleClassStructure
52-
} else {
53-
SimpleClassStructure(it.name, it.type.canonicalText, emptyList())
52+
val children = fields.mapNotNull { field ->
53+
when {
54+
field.type is PsiClassReferenceType -> {
55+
val classStructure =
56+
(field.type as PsiClassType).resolve()?.let { createSimpleStructure(it, searchScope) } ?: return@mapNotNull null
57+
classStructure.builtIn = false
58+
classStructure
59+
}
60+
61+
else -> {
62+
SimpleClassStructure(field.name, field.type.presentableText, emptyList(), builtIn = true)
63+
}
5464
}
5565
}
5666

57-
val classSimpleClassStructure =
58-
SimpleClassStructure(clazz.name ?: "Unknown", clazz.qualifiedName ?: "Unknown", children)
59-
classTree[clazz.name ?: "Unknown"] = classSimpleClassStructure
67+
return SimpleClassStructure(clazz.name ?: "", clazz.name ?: "", children)
6068
}
6169
}

java/src/test/kotlin/cc/unitmesh/idea/context/JavaContextCollectionUtilsKtTest.kt

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,33 @@ class SimpleClassStructureTest : LightJavaCodeInsightFixtureTestCase() {
1616
Comment comment;
1717
}
1818
class Comment {
19+
long id;
1920
User user;
2021
}
2122
class User {
23+
long id;
2224
String name;
2325
}
2426
"""
2527

2628
val psiFile = fileFactory.createFileFromText(JavaLanguage.INSTANCE, javaCode)
2729
val classes = PsiTreeUtil.findChildrenOfType(psiFile, PsiClass::class.java)
2830

29-
val blogpost = classes.filter { it.name == "BlogPost" }.first()
30-
val tree = JavaContextCollectionUtilsKt.dataStructure(blogpost)
31-
TestCase.assertEquals(tree["BlogPost"]?.toString(), "class BlogPost {\n" +
31+
val blogpost = classes.first { it.name == "BlogPost" }
32+
val structure = JavaContextCollectionUtilsKt.dataStructure(blogpost)
33+
TestCase.assertEquals(structure.children.size, 2)
34+
TestCase.assertEquals(structure.toString(), "class BlogPost {\n" +
35+
" id: long\n" +
36+
" Comment: Comment\n" +
37+
"}\n" +
38+
"\n" +
39+
"class Comment {\n" +
40+
" id: long\n" +
41+
" User: User\n" +
42+
"}\n" +
43+
"\n" +
44+
"class User {\n" +
3245
" id: long\n" +
33-
" comment: Comment\n" +
3446
"}\n")
3547
}
3648
}
Lines changed: 78 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,88 @@
11
package cc.unitmesh.devti.context
22

3-
data class SimpleClassStructure(val fieldName: String, val fieldType: String, val children: List<SimpleClassStructure>) {
4-
override fun toString(): String {
5-
return buildPumlRepresentation(this)
3+
data class SimpleClassStructure(
4+
val fieldName: String,
5+
var fieldType: String,
6+
val children: List<SimpleClassStructure>,
7+
var builtIn: Boolean = false
8+
) {
9+
val childPuml: MutableMap<String, String> = mutableMapOf()
10+
11+
12+
/**
13+
* Returns a PlantUML string representation of the class structure
14+
* for example:
15+
* ```
16+
* class BlogPost {
17+
* long id;
18+
* Comment comment;
19+
* }
20+
* class Comment {
21+
* User user;
22+
* }
23+
* class User {
24+
* String name;
25+
* }
26+
*```
27+
*
28+
* will be represented as:
29+
*
30+
* ```puml
31+
* class BlogPost {
32+
* long id;
33+
* Comment comment;
34+
*}
35+
*```
36+
*/
37+
private fun simplePuml(simpleClassStructure: SimpleClassStructure): String {
38+
return "class ${simpleClassStructure.fieldType} {\n" +
39+
simpleClassStructure.children.joinToString("\n") { " ${it.fieldName}: ${it.fieldType}" } +
40+
"\n}\n"
641
}
742

8-
private fun buildPumlRepresentation(node: SimpleClassStructure, indent: String = ""): String {
9-
val stringBuilder = StringBuilder()
1043

11-
stringBuilder.append("class ${node.fieldName} {\n")
44+
/**
45+
* Returns a PlantUML string representation of the class structure.
46+
*
47+
* This method generates a PlantUML string representation of the class structure based on the current object and its child objects. The resulting string is built using the buildPuml() method and the childPuml map. The resulting string will be a tree-like structure that shows the relationships between the classes.
48+
*
49+
* @return the PlantUML string representation of the class structure
50+
*
51+
* For example, if a BlogPost class includes a Comment class, and the Comment class includes a User class, then the resulting tree will be:
52+
*
53+
* ```puml
54+
* class BlogPost {
55+
* id: long
56+
* comment: Comment
57+
*}
58+
*
59+
* class Comment {
60+
* user: User
61+
* }
62+
*
63+
* class User {
64+
* name: String
65+
* }
66+
*```
67+
*/
68+
override fun toString(): String {
69+
val puml = StringBuilder()
70+
puml.append(simplePuml(this))
1271

13-
for (child in node.children) {
14-
stringBuilder.append(" ${child.fieldName}: ${child.fieldType}\n")
72+
createChildPuml(children)
73+
74+
childPuml.forEach {
75+
puml.append("\n")
76+
puml.append(it.value)
1577
}
1678

17-
stringBuilder.append("}\n")
79+
return puml.toString()
80+
}
1881

19-
return stringBuilder.toString()
82+
private fun createChildPuml(data: List<SimpleClassStructure>) {
83+
data.filter { !it.builtIn }.forEach {
84+
childPuml[it.fieldType] = simplePuml(it)
85+
createChildPuml(it.children)
86+
}
2087
}
21-
}
88+
}

0 commit comments

Comments
 (0)