Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix completion adds superfluous equal to field_name #596

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions src/com/koxudaxi/pydantic/PydanticCompletionContributor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class PydanticCompletionContributor : CompletionContributor() {
context: TypeEvalContext,
pydanticVersion: KotlinVersion?,
config: HashMap<String, Any?>,
withEqual: Boolean
): String

val typeProvider: PydanticTypeProvider = PydanticTypeProvider()
Expand Down Expand Up @@ -110,6 +111,7 @@ class PydanticCompletionContributor : CompletionContributor() {
excludes: HashSet<String>?,
isDataclass: Boolean,
genericTypeMap: Map<PyGenericType, PyType>?,
withEqual: Boolean
) {
val pydanticVersion = PydanticCacheService.getVersion(pyClass.project, typeEvalContext)
getClassVariables(pyClass, typeEvalContext)
Expand All @@ -118,7 +120,7 @@ class PydanticCompletionContributor : CompletionContributor() {
.filter { isValidField(it, typeEvalContext) }
.filter { !isDataclass || isInInit(it) }
.forEach {
val elementName = getLookupNameFromFieldName(it, typeEvalContext, pydanticVersion, config)
val elementName = getLookupNameFromFieldName(it, typeEvalContext, pydanticVersion, config, withEqual)
if (excludes == null || !excludes.contains(elementName)) {
val typeText = getTypeText(pyClass,
typeEvalContext,
Expand Down Expand Up @@ -148,6 +150,7 @@ class PydanticCompletionContributor : CompletionContributor() {
genericTypeMap: Map<PyGenericType, PyType>?,
excludes: HashSet<String>? = null,
isDataclass: Boolean,
trimEqual: Boolean
) {

val newElements: LinkedHashMap<String, LookupElement> = LinkedHashMap()
Expand All @@ -161,7 +164,8 @@ class PydanticCompletionContributor : CompletionContributor() {
config,
excludes,
isDataclass,
genericTypeMap)
genericTypeMap,
!trimEqual)
}

addFieldElement(pyClass,
Expand All @@ -171,11 +175,14 @@ class PydanticCompletionContributor : CompletionContributor() {
config,
excludes,
isDataclass,
genericTypeMap)
genericTypeMap,
!trimEqual)

result.runRemainingContributors(parameters)
{ completionResult ->
completionResult.lookupElement.lookupString
completionResult.lookupElement.lookupString.let {
if (trimEqual) it.trimEnd('=') else it
}
.takeIf { name -> !newElements.containsKey(name) && (excludes == null || !excludes.contains(name)) }
?.let { result.passResult(completionResult) }
}
Expand Down Expand Up @@ -238,8 +245,10 @@ class PydanticCompletionContributor : CompletionContributor() {
context: TypeEvalContext,
pydanticVersion: KotlinVersion?,
config: HashMap<String, Any?>,
withEqual: Boolean
): String {
return "${getFieldName(field, context, config, pydanticVersion)}="
val suffix = if(withEqual) "=" else ""
return "${getFieldName(field, context, config, pydanticVersion)}$suffix"
}

override val icon: Icon = AllIcons.Nodes.Parameter
Expand All @@ -265,7 +274,9 @@ class PydanticCompletionContributor : CompletionContributor() {
.mapNotNull { (it as? PyKeywordArgument)?.name }
.map { "${it}=" }
.toHashSet()

val keyword = parameters.originalPosition?.text
val parameter = parameters.originalPosition?.parent?.text
val hasEqual = parameter?.startsWith("$keyword=") ?: false
addAllFieldElement(
parameters,
result,
Expand All @@ -276,6 +287,7 @@ class PydanticCompletionContributor : CompletionContributor() {
typeProvider.getGenericTypeMap(pyClass, typeEvalContext, pyCallExpression),
definedSet,
pyClass.isPydanticDataclass,
hasEqual
)
}
}
Expand All @@ -286,6 +298,7 @@ class PydanticCompletionContributor : CompletionContributor() {
context: TypeEvalContext,
pydanticVersion: KotlinVersion?,
config: HashMap<String, Any?>,
withEqual: Boolean
): String {
return field.name!!
}
Expand Down Expand Up @@ -320,7 +333,8 @@ class PydanticCompletionContributor : CompletionContributor() {
ellipsis,
config,
typeProvider.getGenericTypeMap(pyClass, typeEvalContext, pyTypedElement as? PyCallExpression),
isDataclass = pyClass.isPydanticDataclass,
isDataclass = pyClass.isPydanticDataclass ,
trimEqual=false,
)
}
}
Expand Down
8 changes: 8 additions & 0 deletions testData/completionv18/insertedArgument.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from pydantic import BaseModel, Field


class A(BaseModel):
abc_efg: str = '123'
abc_xyz: str = '456'

A(abc<caret>_efg='789')
8 changes: 8 additions & 0 deletions testSrc/com/koxudaxi/pydantic/PydanticCompletionV18Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,12 @@ open class PydanticCompletionV18Test : PydanticTestCase(version = "v18") {
)
)
}
fun testInsertedArgument() {
doFieldTest(
listOf(
Pair("abc_efg", "str='123' A"),
Pair("abc_xyz", "str='456' A")
)
)
}
}