Skip to content

Commit

Permalink
feat(codemodel): enhance ClassStructure and MethodStructure formatting
Browse files Browse the repository at this point in the history
…#14

Update ClassStructure and MethodStructure in codemodel to improve code formatting. Add methods to get field names and method signatures in ClassStructure. Refactor initialization of classContext in MethodStructure. Make fanInOut property public in MethodStructure.
  • Loading branch information
phodal committed Jun 17, 2024
1 parent 4be010b commit 07488b0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.phodal.shirecore.codemodel.model

import com.intellij.openapi.application.runReadAction
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiReference
import com.phodal.shirecore.codemodel.MethodStructureProvider
import com.phodal.shirecore.codemodel.VariableStructureProvider
import com.phodal.shirecore.codemodel.base.FormatableElement

class ClassStructure(
Expand All @@ -15,5 +18,39 @@ class ClassStructure(
val annotations: List<String> = mutableListOf(),
val usages: List<PsiReference> = emptyList(),

Check warning on line 19 in core/src/main/kotlin/com/phodal/shirecore/codemodel/model/ClassStructure.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused symbol

Property "usages" is never used
) : FormatableElement(root, text, name) {
private fun getFieldNames(): List<String> = fields.mapNotNull {
VariableStructureProvider.from(it, false, false, false)?.shortFormat()

Check notice on line 22 in core/src/main/kotlin/com/phodal/shirecore/codemodel/model/ClassStructure.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Boolean literal argument without parameter name

Boolean literal argument without a parameter name

Check notice on line 22 in core/src/main/kotlin/com/phodal/shirecore/codemodel/model/ClassStructure.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Boolean literal argument without parameter name

Boolean literal argument without a parameter name

Check notice on line 22 in core/src/main/kotlin/com/phodal/shirecore/codemodel/model/ClassStructure.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Boolean literal argument without parameter name

Boolean literal argument without a parameter name
}

private fun getMethodSignatures(): List<String> = methods.mapNotNull {
MethodStructureProvider.from(it, false, gatherUsages = false)?.signature
}

override fun format(): String {
val className = name ?: "_"
val classFields = getFieldNames().joinToString(separator = "\n ")
val superClasses = when {
superClasses.isNullOrEmpty() -> ""
else -> " : ${superClasses.joinToString(separator = ", ")}"
}
val methodSignatures = getMethodSignatures()
.filter { it.isNotBlank() }.joinToString(separator = "\n ") { method ->
"+ $method"
}

val filePath = displayName ?: runReadAction { root.containingFile?.virtualFile?.path }
val annotations = if (annotations.isEmpty()) {
""
} else {
"\n'" + annotations.joinToString(separator = ", ")
}

return """
|'package: $filePath$annotations
|class $className$superClasses {
| $classFields
| $methodSignatures
|}
""".trimMargin()
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.phodal.shirecore.codemodel.model

import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiReference
import com.phodal.shirecore.codemodel.ClassStructureProvider
Expand All @@ -18,17 +17,12 @@ class MethodStructure(
val paramNames: List<String> = emptyList(),

Check warning on line 17 in core/src/main/kotlin/com/phodal/shirecore/codemodel/model/MethodStructure.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused symbol

Property "paramNames" is never used
val includeClassContext: Boolean = false,

Check warning on line 18 in core/src/main/kotlin/com/phodal/shirecore/codemodel/model/MethodStructure.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Constructor parameter is never used as a property

Constructor parameter is never used as a property

Check notice on line 18 in core/src/main/kotlin/com/phodal/shirecore/codemodel/model/MethodStructure.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Class member can have 'private' visibility

Property 'includeClassContext' could be private
val usages: List<PsiReference> = emptyList(),

Check notice on line 19 in core/src/main/kotlin/com/phodal/shirecore/codemodel/model/MethodStructure.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Class member can have 'private' visibility

Property 'usages' could be private
private val fanInOut: List<PsiElement> = emptyList(),
val fanInOut: List<PsiElement> = emptyList(),

Check notice on line 20 in core/src/main/kotlin/com/phodal/shirecore/codemodel/model/MethodStructure.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Class member can have 'private' visibility

Property 'fanInOut' could be private
) : FormatableElement(root, text, name) {
private val classContext: ClassStructure?
private val project: Project = root.project

init {
classContext = if (includeClassContext && enclosingClass != null) {
ClassStructureProvider.from(enclosingClass, false)
} else {
null
}
private val classContext: ClassStructure? = if (includeClassContext && enclosingClass != null) {
ClassStructureProvider.from(enclosingClass, false)
} else {
null
}

override fun format(): String {
Expand Down Expand Up @@ -64,7 +58,7 @@ class MethodStructure(
val context: ClassStructure = ClassStructureProvider.from(it, false) ?: return@forEach
val element = context.root

if (!isInProject(element.containingFile?.virtualFile!!, project)) {
if (!isInProject(element.containingFile?.virtualFile!!, root.project)) {
return@forEach
}

Expand Down

0 comments on commit 07488b0

Please sign in to comment.