From c6619425a0807a3b6ed8701b141a6b55f4647d00 Mon Sep 17 00:00:00 2001 From: Koudai Aono Date: Mon, 17 Jul 2023 01:33:59 +0900 Subject: [PATCH] Update v2 external document url (#748) (#749) --- .../PydanticDocumentationLinkProvider.kt | 37 ++++++++++++++----- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/src/com/koxudaxi/pydantic/PydanticDocumentationLinkProvider.kt b/src/com/koxudaxi/pydantic/PydanticDocumentationLinkProvider.kt index 974bd402..cfca34af 100644 --- a/src/com/koxudaxi/pydantic/PydanticDocumentationLinkProvider.kt +++ b/src/com/koxudaxi/pydantic/PydanticDocumentationLinkProvider.kt @@ -8,22 +8,39 @@ import org.jsoup.nodes.Document import java.util.function.Function class PydanticDocumentationLinkProvider : PythonDocumentationLinkProvider { - private val urls = mapOf( - BASE_MODEL_Q_NAME to "https://docs.pydantic.dev/usage/models/#basic-model-usage", - VALIDATOR_Q_NAME to "https://docs.pydantic.dev/usage/validators/", - VALIDATOR_SHORT_Q_NAME to "https://docs.pydantic.dev/usage/validators/", - ROOT_VALIDATOR_Q_NAME to "https://docs.pydantic.dev/usage/validators/#root-validators", - ROOT_VALIDATOR_SHORT_Q_NAME to "https://docs.pydantic.dev/usage/validators/#root-validators", - DATA_CLASS_Q_NAME to "https://docs.pydantic.dev/usage/dataclasses/", - BASE_SETTINGS_Q_NAME to "https://docs.pydantic.dev/usage/settings/", - BASE_CONFIG_Q_NAME to "https://docs.pydantic.dev/usage/model_config/" + private val v1Urls = mapOf( + BASE_MODEL_Q_NAME to "https://docs.pydantic.dev/dev-1/usage/models/#basic-model-usage", + VALIDATOR_Q_NAME to "https://docs.pydantic.dev/dev-1/usage/validators", + VALIDATOR_SHORT_Q_NAME to "https://docs.pydantic.dev/dev-1/usage/validators", + ROOT_VALIDATOR_Q_NAME to "https://docs.pydantic.dev/dev-1/usage/validators/#root-validators", + ROOT_VALIDATOR_SHORT_Q_NAME to "https://docs.pydantic.dev/dev-1/usage/validators/#root-validators", + DATA_CLASS_Q_NAME to "https://docs.pydantic.dev/dev-1/usage/dataclasses/", + BASE_SETTINGS_Q_NAME to "https://docs.pydantic.dev/dev-1/usage/dataclasses/", + BASE_CONFIG_Q_NAME to "https://docs.pydantic.dev/dev-1/usage/model_config/" + ) + private val v2Urls = mapOf( + BASE_MODEL_Q_NAME to "https://docs.pydantic.dev/dev-v2/usage/models/#basic-model-usage", + FIELD_VALIDATOR_Q_NAME to "https://docs.pydantic.dev/dev-v2/usage/validators/#field-validators", + FIELD_VALIDATOR_SHORT_Q_NAME to "https://docs.pydantic.dev/dev-v2/usage/validators/#field-validators", + MODEL_VALIDATOR_Q_NAME to "https://docs.pydantic.dev/dev-v2/usage/validators/#model-validators", + MODEL_VALIDATOR_SHORT_Q_NAME to "https://docs.pydantic.dev/dev-v2/usage/validators/#model-validators", + DATA_CLASS_Q_NAME to "https://docs.pydantic.dev/dev-v2/usage/dataclasses/", + BASE_SETTINGS_Q_NAME to "https://docs.pydantic.dev/dev-v2/api/pydantic_settings/#pydantic_settings.BaseSettings", + BASE_CONFIG_Q_NAME to "https://docs.pydantic.dev/dev-v2/api/config/#pydantic.config.BaseConfig", + CONFIG_DICT_Q_NAME to "https://docs.pydantic.dev/dev-v2/api/config/#pydantic.config.ConfigDict", + CONFIG_DICT_SHORT_Q_NAME to "https://docs.pydantic.dev/dev-v2/api/config/#pydantic.config.ConfigDict", ) override fun getExternalDocumentationUrl(element: PsiElement, originalElement: PsiElement): String? { val qualifiedName = (element as? PyQualifiedNameOwner)?.qualifiedName ?: return null if (!qualifiedName.startsWith("pydantic.")) return null - return urls[qualifiedName] + + val version = PydanticCacheService.getInstance(element.project) + return when { + version.isV2 -> v2Urls[qualifiedName] + else -> v1Urls[qualifiedName] } + } override fun quickDocExtractor(namedElement: PsiNamedElement): Function? { return null