Skip to content

Commit 274ebc7

Browse files
committed
feat(context): improve formatting in ClassContext and VariableContext
- In ClassContext, improved the formatting of the shortFormat() method to only return the root text. - In VariableContext, added a new shortFormat() method that returns the root text. - Updated the format() method in VariableContext to include the name of the variable, method context, and class context in the returned string. These changes were made to enhance the readability and clarity of the formatted string representations in the ClassContext and VariableContext classes.
1 parent 79aba96 commit 274ebc7

File tree

7 files changed

+59
-21
lines changed

7 files changed

+59
-21
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public class BlogService {
9797
"""'package: cc.unitmesh.untitled.demo.controller.BlogController
9898
'@Controller
9999
class BlogController {
100-
'variable -> BlogService blogService;
100+
BlogService blogService;
101101
+ public BlogController(BlogService blogService)
102102
+ @PostMapping("/blog") public BlogPost createBlog(CreateBlogDto blogDto)
103103
+ @GetMapping("/blog") public List<BlogPost> getBlog()

kotlin/src/main/kotlin/cc/unitmesh/kotlin/context/KotlinClassContextBuilder.kt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ class KotlinClassContextBuilder : ClassContextBuilder {
2222

2323
val text = psiElement.text
2424
val name = psiElement.name
25-
val ktNamedFunctions = getFunctions(psiElement)
26-
val primaryConstructorFields = getPrimaryConstructorFields(psiElement)
27-
val allFields = ktNamedFunctions + primaryConstructorFields
25+
val functions = getFunctions(psiElement)
26+
val allFields = getPrimaryConstructorFields(psiElement)
2827
val usages =
2928
if (gatherUsages) JavaContextCollection.findUsages(psiElement as PsiNameIdentifierOwner) else emptyList()
3029

@@ -33,7 +32,17 @@ class KotlinClassContextBuilder : ClassContextBuilder {
3332
}
3433

3534
val displayName = psiElement.fqName?.asString() ?: psiElement.name ?: ""
36-
return ClassContext(psiElement, text, name, ktNamedFunctions, allFields, null, usages, displayName = displayName, annotations)
35+
return ClassContext(
36+
psiElement,
37+
text,
38+
name,
39+
functions,
40+
allFields,
41+
null,
42+
usages,
43+
displayName = displayName,
44+
annotations
45+
)
3746
}
3847

3948
companion object {

kotlin/src/test/kotlin/cc/unitmesh/kotlin/context/KotlinClassContextBuilderTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import junit.framework.TestCase
66
import org.jetbrains.kotlin.psi.KtClassOrObject
77
import org.jetbrains.kotlin.psi.KtPsiFactory
88

9-
class KotlinClassContextBuilderTest: LightPlatformTestCase() {
9+
class KotlinClassContextBuilderTest : LightPlatformTestCase() {
1010

1111
fun testShould_return_functions_from_kt_class_or_object() {
1212
val code = """

kotlin/src/test/kotlin/cc/unitmesh/kotlin/provider/KotlinTestDataBuilderTest.kt

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,17 @@ class KotlinTestDataBuilderTest : LightPlatformTestCase() {
8282
assertEquals(outboundData.size, 1)
8383
assertEquals(
8484
outboundData["cc.unitmesh.untitled.demo.controller.UserDTO"],
85-
"'package: cc.unitmesh.untitled.demo.controller.UserDTO\n" +
86-
"class UserDTO {\n" +
87-
" \n" +
88-
" \n" +
89-
"}"
85+
"""
86+
'package: cc.unitmesh.untitled.demo.controller.UserDTO
87+
class UserDTO {
88+
val id: Long? = null
89+
val username: String
90+
val email: String
91+
val name: String
92+
val surname: String? = null
93+
94+
}
95+
""".trimIndent()
9096
)
9197
}
9298

@@ -108,11 +114,17 @@ class KotlinTestDataBuilderTest : LightPlatformTestCase() {
108114
assertEquals(outboundData.size, 1)
109115
assertEquals(
110116
outboundData["cc.unitmesh.untitled.demo.controller.UserDTO"],
111-
"'package: cc.unitmesh.untitled.demo.controller.UserDTO\n" +
112-
"class UserDTO {\n" +
113-
" \n" +
114-
" \n" +
115-
"}"
117+
"""
118+
'package: cc.unitmesh.untitled.demo.controller.UserDTO
119+
class UserDTO {
120+
val id: Long? = null
121+
val username: String
122+
val email: String
123+
val name: String
124+
val surname: String? = null
125+
126+
}
127+
""".trimIndent()
116128
)
117129
}
118130

rust/src/233/test/kotlin/cc/unitmesh/rust/context/RustClassContextBuilderTest.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ class RustClassContextBuilderTest: BasePlatformTestCase() {
4747
result.format(), """
4848
'package: Entry
4949
class Entry {
50-
50+
id: String
51+
embedding: Embedding
52+
embedded: Document
5153
5254
}
5355
""".trimIndent()

src/main/kotlin/cc/unitmesh/devti/context/ClassContext.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class ClassContext(
1818
) : NamedElementContext(root, text, name) {
1919
private fun getFieldNames(): List<String> = fields.mapNotNull {
2020
val variableContextProvider = VariableContextProvider(false, false, false)
21-
variableContextProvider.from(it).format()
21+
variableContextProvider.from(it).shortFormat()
2222
}
2323

2424
private fun getMethodSignatures(): List<String> = methods.mapNotNull {

src/main/kotlin/cc/unitmesh/devti/context/VariableContext.kt

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@ class VariableContext(
1010
override val text: String,
1111
override val name: String?,
1212
val enclosingMethod: PsiElement? = null,
13-
val enclosingClass: PsiElement? = null,
13+
val enclosingClass: PsiElement?= null,
1414
val usages: List<PsiReference> = emptyList(),
1515
val includeMethodContext: Boolean = false,
1616
val includeClassContext: Boolean = false
1717
) : NamedElementContext(
1818
root, text, name
1919
) {
20-
2120
private val methodContext: MethodContext?
2221
private val classContext: ClassContext?
2322

@@ -34,9 +33,25 @@ class VariableContext(
3433
}
3534
}
3635

36+
fun shortFormat(): String {
37+
return root.text
38+
}
39+
40+
/**
41+
* Returns a formatted string representation of the method.
42+
*
43+
* The returned string includes the following information:
44+
* - The name of the method, or "_" if the name is null.
45+
* - The name of the method's context, or "_" if the context is null.
46+
* - The name of the class's context, or "_" if the context is null.
47+
*
48+
* @return A formatted string representation of the method.
49+
*/
3750
override fun format(): String {
3851
return """
39-
'variable -> ${root.text}
52+
var name: ${name ?: "_"}
53+
var method name: ${methodContext?.name ?: "_"}
54+
var class name: ${classContext?.name ?: "_"}
4055
""".trimIndent()
4156
}
4257
}

0 commit comments

Comments
 (0)