Skip to content

Commit 1e8b59d

Browse files
committed
refactor(go): optimize GoPsiUtil.getDeclarationName function
- Remove unnecessary imports and unused code. - Simplify the logic of getting the declaration name. - Improve readability and maintainability of the code.
1 parent 3e354df commit 1e8b59d

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

goland/src/main/kotlin/cc/unitmesh/go/context/GoStructContextBuilder.kt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cc.unitmesh.go.context
22

33
import cc.unitmesh.devti.context.ClassContext
44
import cc.unitmesh.devti.context.builder.ClassContextBuilder
5+
import cc.unitmesh.go.util.GoPsiUtil
56
import com.goide.psi.GoMethodDeclaration
67
import com.goide.psi.GoTypeDeclaration
78
import com.goide.psi.GoTypeSpec
@@ -27,13 +28,7 @@ class GoStructContextBuilder : ClassContextBuilder {
2728
val methods = methodPairs.map { it.first }
2829
.filterIsInstance<GoMethodDeclaration>()
2930

30-
val name = when (psiElement) {
31-
is GoTypeSpec -> psiElement.name
32-
is GoTypeDeclaration -> {
33-
psiElement.typeSpecList.first().name
34-
}
35-
else -> null
36-
}
31+
val name = GoPsiUtil.getDeclarationName(psiElement)
3732

3833
return ClassContext(
3934
psiElement, psiElement.text, name, methods, emptyList(), emptyList(), emptyList()

goland/src/main/kotlin/cc/unitmesh/go/context/GoVariableContextBuilder.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ package cc.unitmesh.go.context
33
import cc.unitmesh.devti.context.VariableContext
44
import cc.unitmesh.devti.context.builder.VariableContextBuilder
55
import com.goide.psi.GoVarOrConstDefinition
6-
import com.goide.psi.GoVarOrConstSpec
76
import com.intellij.psi.PsiElement
8-
import com.intellij.psi.util.elementType
97

108
class GoVariableContextBuilder : VariableContextBuilder {
119
override fun getVariableContext(

goland/src/main/kotlin/cc/unitmesh/go/util/GoPsiUtil.kt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
package cc.unitmesh.go.util
22

3-
import com.goide.psi.GoFunctionOrMethodDeclaration
4-
import com.goide.psi.GoTypeList
5-
import com.goide.psi.GoTypeSpec
3+
import com.goide.psi.*
64
import com.intellij.openapi.roots.ProjectFileIndex
75
import com.intellij.psi.PsiElement
86

97
object GoPsiUtil {
8+
fun getDeclarationName(psiElement: PsiElement): String? {
9+
return when (psiElement) {
10+
is GoNamedElement -> psiElement.name
11+
is GoTypeDeclaration -> psiElement.typeSpecList.singleOrNull()?.name
12+
is GoVarOrConstDeclaration<*> -> (psiElement.specList.singleOrNull() as? GoVarOrConstSpec)?.definitionList?.singleOrNull()?.name
13+
is GoVarOrConstSpec<*> -> psiElement.definitionList.singleOrNull()?.name
14+
else -> null
15+
}
16+
}
17+
1018
fun findRelatedTypes(declaration: GoFunctionOrMethodDeclaration): List<GoTypeSpec> {
1119
val signature = declaration.signature ?: return emptyList()
1220

0 commit comments

Comments
 (0)