Skip to content

Commit 5b89fc5

Browse files
committed
feat(goland): add GoMethodContextBuilder and GoPsiUtil
- Added GoMethodContextBuilder class to build method context for Go functions and methods. - Added GoPsiUtil object to find related types in Go function or method declarations. - Updated cc.unitmesh.goland.xml to include GoMethodContextBuilder as a method context builder for Go language.
1 parent 5ff85e0 commit 5b89fc5

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package cc.unitmesh.go.context
2+
3+
import cc.unitmesh.devti.context.MethodContext
4+
import cc.unitmesh.devti.context.builder.MethodContextBuilder
5+
import com.goide.psi.GoFunctionOrMethodDeclaration
6+
import com.intellij.psi.PsiElement
7+
import com.intellij.psi.PsiNameIdentifierOwner
8+
9+
class GoMethodContextBuilder : MethodContextBuilder {
10+
override fun getMethodContext(
11+
psiElement: PsiElement,
12+
includeClassContext: Boolean,
13+
gatherUsages: Boolean
14+
): MethodContext? {
15+
16+
if (psiElement !is GoFunctionOrMethodDeclaration) {
17+
return null
18+
}
19+
20+
val functionSignature = psiElement.signature?.text
21+
val returnType = psiElement.signature?.resultType?.text
22+
val languages = psiElement.language.displayName
23+
val returnTypeText = returnType
24+
val parameterNames = psiElement.signature?.parameters?.parameterDeclarationList?.mapNotNull {
25+
it.paramDefinitionList.firstOrNull()?.text
26+
}.orEmpty()
27+
28+
return MethodContext(
29+
psiElement, psiElement.text, psiElement.name!!, functionSignature, null, languages,
30+
returnTypeText, parameterNames, includeClassContext, emptyList()
31+
)
32+
33+
}
34+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package cc.unitmesh.go.util
2+
3+
import com.goide.psi.GoFunctionOrMethodDeclaration
4+
import com.goide.psi.GoTypeList
5+
import com.goide.psi.GoTypeSpec
6+
import com.intellij.openapi.roots.ProjectFileIndex
7+
import com.intellij.psi.PsiElement
8+
9+
object GoPsiUtil {
10+
fun findRelatedTypes(declaration: GoFunctionOrMethodDeclaration): List<GoTypeSpec> {
11+
val signature = declaration.signature ?: return emptyList()
12+
13+
val parameterTypes = signature.parameters.parameterDeclarationList
14+
.mapNotNull { it.type }
15+
16+
val resultTypes = when (val resultType = signature.resultType) {
17+
is GoTypeList -> resultType.typeList
18+
else -> listOf(resultType)
19+
}
20+
21+
val mentionedTypes = parameterTypes + resultTypes
22+
23+
val genericTypes = mentionedTypes
24+
.flatMap { it.typeArguments?.types ?: emptyList() }
25+
26+
val relatedTypes = genericTypes + mentionedTypes
27+
28+
return relatedTypes
29+
.mapNotNull { it.resolve(declaration) as? GoTypeSpec }
30+
.filter { isProjectContent(it) }
31+
}
32+
33+
private fun isProjectContent(element: PsiElement): Boolean {
34+
val virtualFile = element.containingFile.virtualFile ?: return true
35+
return ProjectFileIndex.getInstance(element.project).isInContent(virtualFile)
36+
}
37+
}

goland/src/main/resources/cc.unitmesh.goland.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
</dependencies>
66

77
<extensions defaultExtensionNs="cc.unitmesh">
8+
<methodContextBuilder language="go" implementationClass="cc.unitmesh.go.context.GoMethodContextBuilder"/>
9+
810
<chatContextProvider implementation="cc.unitmesh.go.provider.GoVersionChatContextProvider"/>
911
</extensions>
1012
</idea-plugin>

0 commit comments

Comments
 (0)