in src/main/com/intellij/lang/jsgraphql/ide/introspection/GraphQLIntrospectionSchemaWriter.kt [55:110]
suspend fun createOrUpdateIntrospectionFile(output: IntrospectionOutput, dir: VirtualFile, fileName: String) {
val header = when (output.format) {
IntrospectionOutputFormat.SDL -> "# This file was generated. Do not edit manually.\n\n"
IntrospectionOutputFormat.JSON -> ""
}
val fileDocumentManager = FileDocumentManager.getInstance()
val psiDocumentManager = PsiDocumentManager.getInstance(project)
try {
val outputFile = withContext(Dispatchers.EDT) {
runUndoTransparentWriteAction {
createOrUpdateSchemaFile(project, dir, FileUtil.toSystemIndependentName(fileName))
}
}
val document = readAndEdtWriteAction {
val document = fileDocumentManager.getDocument(outputFile)
?: throw IOException("Unable to get document for created introspection file: $outputFile")
writeAction {
CommandProcessor.getInstance().withUndoTransparentAction().use {
document.setText(StringUtil.convertLineSeparators(header + output.schemaText))
psiDocumentManager.commitDocument(document)
document
}
}
}
reformatDocumentIfNeeded(psiDocumentManager, outputFile, document)
edtWriteAction {
psiDocumentManager.doPostponedOperationsAndUnblockDocument(document)
psiDocumentManager.commitDocument(document)
fileDocumentManager.saveDocument(document)
}
openSchemaInEditor(project, outputFile)
}
catch (e: CancellationException) {
throw e
}
catch (e: IOException) {
LOG.info(e)
val notification = Notification(
GRAPHQL_NOTIFICATION_GROUP_ID,
GraphQLBundle.message("graphql.notification.error.title"),
GraphQLBundle.message("graphql.notification.unable.to.create.file", fileName, dir.path),
NotificationType.ERROR
)
addShowQueryErrorDetailsAction(project, notification, e)
Notifications.Bus.notify(notification)
}
catch (e: Exception) {
LOG.error(e)
}
}