Skip to content

Commit

Permalink
Update v2 external document url (#748) (#749)
Browse files Browse the repository at this point in the history
  • Loading branch information
koxudaxi authored Jul 16, 2023
1 parent 04aa57a commit c661942
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions src/com/koxudaxi/pydantic/PydanticDocumentationLinkProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Document, String>? {
return null
Expand Down

0 comments on commit c661942

Please sign in to comment.