Skip to content

Commit

Permalink
feat(context): improve formatting in ClassContext and VariableContext
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
phodal committed Jan 19, 2024
1 parent 79aba96 commit 274ebc7
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 21 deletions.
Expand Up @@ -97,7 +97,7 @@ public class BlogService {
"""'package: cc.unitmesh.untitled.demo.controller.BlogController
'@Controller
class BlogController {
'variable -> BlogService blogService;
BlogService blogService;
+ public BlogController(BlogService blogService)
+ @PostMapping("/blog") public BlogPost createBlog(CreateBlogDto blogDto)
+ @GetMapping("/blog") public List<BlogPost> getBlog()
Expand Down
Expand Up @@ -22,9 +22,8 @@ class KotlinClassContextBuilder : ClassContextBuilder {

val text = psiElement.text
val name = psiElement.name
val ktNamedFunctions = getFunctions(psiElement)
val primaryConstructorFields = getPrimaryConstructorFields(psiElement)
val allFields = ktNamedFunctions + primaryConstructorFields
val functions = getFunctions(psiElement)
val allFields = getPrimaryConstructorFields(psiElement)
val usages =
if (gatherUsages) JavaContextCollection.findUsages(psiElement as PsiNameIdentifierOwner) else emptyList()

Expand All @@ -33,7 +32,17 @@ class KotlinClassContextBuilder : ClassContextBuilder {
}

val displayName = psiElement.fqName?.asString() ?: psiElement.name ?: ""
return ClassContext(psiElement, text, name, ktNamedFunctions, allFields, null, usages, displayName = displayName, annotations)
return ClassContext(
psiElement,
text,
name,
functions,
allFields,
null,
usages,
displayName = displayName,
annotations
)
}

companion object {
Expand Down
Expand Up @@ -6,7 +6,7 @@ import junit.framework.TestCase
import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.psi.KtPsiFactory

class KotlinClassContextBuilderTest: LightPlatformTestCase() {
class KotlinClassContextBuilderTest : LightPlatformTestCase() {

fun testShould_return_functions_from_kt_class_or_object() {
val code = """
Expand Down
Expand Up @@ -82,11 +82,17 @@ class KotlinTestDataBuilderTest : LightPlatformTestCase() {
assertEquals(outboundData.size, 1)
assertEquals(
outboundData["cc.unitmesh.untitled.demo.controller.UserDTO"],
"'package: cc.unitmesh.untitled.demo.controller.UserDTO\n" +
"class UserDTO {\n" +
" \n" +
" \n" +
"}"
"""
'package: cc.unitmesh.untitled.demo.controller.UserDTO
class UserDTO {
val id: Long? = null
val username: String
val email: String
val name: String
val surname: String? = null
}
""".trimIndent()
)
}

Expand All @@ -108,11 +114,17 @@ class KotlinTestDataBuilderTest : LightPlatformTestCase() {
assertEquals(outboundData.size, 1)
assertEquals(
outboundData["cc.unitmesh.untitled.demo.controller.UserDTO"],
"'package: cc.unitmesh.untitled.demo.controller.UserDTO\n" +
"class UserDTO {\n" +
" \n" +
" \n" +
"}"
"""
'package: cc.unitmesh.untitled.demo.controller.UserDTO
class UserDTO {
val id: Long? = null
val username: String
val email: String
val name: String
val surname: String? = null
}
""".trimIndent()
)
}

Expand Down
Expand Up @@ -47,7 +47,9 @@ class RustClassContextBuilderTest: BasePlatformTestCase() {
result.format(), """
'package: Entry
class Entry {
id: String
embedding: Embedding
embedded: Document
}
""".trimIndent()
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/cc/unitmesh/devti/context/ClassContext.kt
Expand Up @@ -18,7 +18,7 @@ class ClassContext(
) : NamedElementContext(root, text, name) {
private fun getFieldNames(): List<String> = fields.mapNotNull {
val variableContextProvider = VariableContextProvider(false, false, false)
variableContextProvider.from(it).format()
variableContextProvider.from(it).shortFormat()
}

private fun getMethodSignatures(): List<String> = methods.mapNotNull {
Expand Down
21 changes: 18 additions & 3 deletions src/main/kotlin/cc/unitmesh/devti/context/VariableContext.kt
Expand Up @@ -10,14 +10,13 @@ class VariableContext(
override val text: String,
override val name: String?,
val enclosingMethod: PsiElement? = null,
val enclosingClass: PsiElement? = null,
val enclosingClass: PsiElement?= null,
val usages: List<PsiReference> = emptyList(),
val includeMethodContext: Boolean = false,
val includeClassContext: Boolean = false
) : NamedElementContext(
root, text, name
) {

private val methodContext: MethodContext?
private val classContext: ClassContext?

Expand All @@ -34,9 +33,25 @@ class VariableContext(
}
}

fun shortFormat(): String {
return root.text
}

/**
* Returns a formatted string representation of the method.
*
* The returned string includes the following information:
* - The name of the method, or "_" if the name is null.
* - The name of the method's context, or "_" if the context is null.
* - The name of the class's context, or "_" if the context is null.
*
* @return A formatted string representation of the method.
*/
override fun format(): String {
return """
'variable -> ${root.text}
var name: ${name ?: "_"}
var method name: ${methodContext?.name ?: "_"}
var class name: ${classContext?.name ?: "_"}
""".trimIndent()
}
}

0 comments on commit 274ebc7

Please sign in to comment.