Skip to content

[IJ Plugin] Fix a crash when invoking the 'missing @link' quickfix #6402

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

Merged
merged 2 commits into from
Mar 4, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import com.intellij.codeInspection.ProblemsHolder
import com.intellij.lang.jsgraphql.psi.GraphQLArrayValue
import com.intellij.lang.jsgraphql.psi.GraphQLDirective
import com.intellij.lang.jsgraphql.psi.GraphQLElementFactory
import com.intellij.lang.jsgraphql.psi.GraphQLFile
import com.intellij.lang.jsgraphql.psi.GraphQLVisitor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElementVisitor
Expand All @@ -60,7 +61,8 @@ class ApolloMissingGraphQLDefinitionImportInspection : LocalInspectionTool() {
highlightType: ProblemHighlightType,
) {
if (directiveElement.name !in definitions.directives().map { it.name }) return
val message = if (highlightType == ProblemHighlightType.WEAK_WARNING) "inspection.missingGraphQLDefinitionImport.reportText.warning" else "inspection.missingGraphQLDefinitionImport.reportText.error"
val message =
if (highlightType == ProblemHighlightType.WEAK_WARNING) "inspection.missingGraphQLDefinitionImport.reportText.warning" else "inspection.missingGraphQLDefinitionImport.reportText.error"
if (!directiveElement.isImported(definitionsUrl)) {
val typeKind = ApolloBundle.message("inspection.missingGraphQLDefinitionImport.reportText.directive")
holder.registerProblem(
Expand Down Expand Up @@ -123,13 +125,16 @@ private class ImportDefinitionQuickFix(
val linkDirective = schemaFiles.flatMap { it.linkDirectives(definitionsUrl) }.firstOrNull()

if (linkDirective == null) {
val linkDirectiveSchemaExtension = createLinkDirectiveSchemaExtension(project, setOf(element.nameForImport), definitions, definitionsUrl)
val extraSchemaFile = schemaFiles.firstOrNull { it.name == "extra.graphqls" }
val linkDirectiveSchemaExtension =
createLinkDirectiveSchemaExtension(project, setOf(element.nameForImport), definitions, definitionsUrl)
val extraSchemaFile = (element.containingFile as? GraphQLFile)?.takeIf { it.name == "extra.graphqls" }
?: schemaFiles.firstOrNull { it.name == "extra.graphqls" }
if (extraSchemaFile == null) {
GraphQLElementFactory.createFile(project, linkDirectiveSchemaExtension.text).also {
// Save the file to the project
it.name = "extra.graphqls"
schemaFiles.first().containingDirectory!!.add(it)
val directory = schemaFiles.firstOrNull()?.containingDirectory ?: element.containingFile.containingDirectory ?: return
directory.add(it)

// There's a new schema file, reload the configuration
project.gradleToolingModelService.triggerFetchToolingModels()
Expand All @@ -140,7 +145,8 @@ private class ImportDefinitionQuickFix(
}
} else {
val importedNames = buildSet {
addAll(linkDirective.arguments!!.argumentList.firstOrNull { it.name == "import" }?.value?.cast<GraphQLArrayValue>()?.valueList.orEmpty().map { it.text.unquoted() })
addAll(linkDirective.arguments!!.argumentList.firstOrNull { it.name == "import" }?.value?.cast<GraphQLArrayValue>()?.valueList.orEmpty()
.map { it.text.unquoted() })
add(element.nameForImport)
}
linkDirective.replace(createLinkDirective(project, importedNames, definitions, definitionsUrl))
Expand Down
Loading