From fe7a688b7b0e70e37f55e3bcfa2b6057cbd5f066 Mon Sep 17 00:00:00 2001 From: Pavel Bodiachevskii Date: Sat, 21 Sep 2024 00:09:18 +0400 Subject: [PATCH] feat(preview panel): switch to AsyncAPISpecificationRecognizer --- .../idea/_core/AsyncAPISchemaRecognizer.kt | 48 ---------- .../preview/AsyncAPIPreviewEditorProvider.kt | 7 +- .../_core/AsyncAPISchemaRecognizerTest.kt | 90 ------------------- 3 files changed, 4 insertions(+), 141 deletions(-) delete mode 100644 src/main/kotlin/com/asyncapi/plugin/idea/_core/AsyncAPISchemaRecognizer.kt delete mode 100644 src/test/kotlin/com/asyncapi/plugin/idea/_core/AsyncAPISchemaRecognizerTest.kt diff --git a/src/main/kotlin/com/asyncapi/plugin/idea/_core/AsyncAPISchemaRecognizer.kt b/src/main/kotlin/com/asyncapi/plugin/idea/_core/AsyncAPISchemaRecognizer.kt deleted file mode 100644 index de5f7aa..0000000 --- a/src/main/kotlin/com/asyncapi/plugin/idea/_core/AsyncAPISchemaRecognizer.kt +++ /dev/null @@ -1,48 +0,0 @@ -package com.asyncapi.plugin.idea._core - -import com.asyncapi.plugin.idea._core.xpath.JsonFileXPath -import com.asyncapi.plugin.idea._core.xpath.YamlFileXPath -import com.intellij.json.JsonFileType -import com.intellij.json.psi.JsonFile -import com.intellij.openapi.components.Service -import com.intellij.openapi.project.Project -import com.intellij.openapi.vfs.VirtualFile -import com.intellij.psi.PsiFile -import com.intellij.psi.PsiManager -import org.jetbrains.yaml.YAMLFileType -import org.jetbrains.yaml.psi.YAMLFile - -@Service -class AsyncAPISchemaRecognizer { - - fun isSchema(project: Project, file: VirtualFile?, useIndex: Boolean = false): Boolean { - file ?: return false - - if (file.fileType !is YAMLFileType && file.fileType !is JsonFileType) { - return false - } - - return if (useIndex) { - /* - TODO: implement. - */ - false - } else { - val psiFile = PsiManager.getInstance(project).findFile(file) - - "2.0.0" == extractAsyncAPIKey(psiFile) - } - } - - private fun extractAsyncAPIKey(file: PsiFile?): String? { - file ?: return null - - val psiXPath = "$.asyncapi" - return when (file) { - is JsonFile -> JsonFileXPath.findText(file as? JsonFile, psiXPath).firstOrNull() - is YAMLFile -> YamlFileXPath.findText(file as? YAMLFile, psiXPath).firstOrNull() - else -> null - } - } - -} \ No newline at end of file diff --git a/src/main/kotlin/com/asyncapi/plugin/idea/extensions/editor/preview/AsyncAPIPreviewEditorProvider.kt b/src/main/kotlin/com/asyncapi/plugin/idea/extensions/editor/preview/AsyncAPIPreviewEditorProvider.kt index 5e39d7a..215bd53 100644 --- a/src/main/kotlin/com/asyncapi/plugin/idea/extensions/editor/preview/AsyncAPIPreviewEditorProvider.kt +++ b/src/main/kotlin/com/asyncapi/plugin/idea/extensions/editor/preview/AsyncAPIPreviewEditorProvider.kt @@ -1,6 +1,6 @@ package com.asyncapi.plugin.idea.extensions.editor.preview -import com.asyncapi.plugin.idea._core.AsyncAPISchemaRecognizer +import com.asyncapi.plugin.idea._core.AsyncAPISpecificationRecognizer import com.intellij.openapi.components.service import com.intellij.openapi.fileEditor.FileDocumentManager import com.intellij.openapi.fileEditor.FileEditor @@ -8,13 +8,14 @@ import com.intellij.openapi.fileEditor.FileEditorPolicy import com.intellij.openapi.fileEditor.WeighedFileEditorProvider import com.intellij.openapi.project.Project import com.intellij.openapi.vfs.VirtualFile +import com.intellij.psi.PsiManager class AsyncAPIPreviewEditorProvider: WeighedFileEditorProvider() { - private val asyncAPISchemaRecognizer = service() + private val asyncAPISpecificationRecognizer = service() override fun accept(project: Project, file: VirtualFile): Boolean { - return asyncAPISchemaRecognizer.isSchema(project, file) + return asyncAPISpecificationRecognizer.isSpecification(PsiManager.getInstance(project).findFile(file)) } override fun createEditor(project: Project, file: VirtualFile): FileEditor { diff --git a/src/test/kotlin/com/asyncapi/plugin/idea/_core/AsyncAPISchemaRecognizerTest.kt b/src/test/kotlin/com/asyncapi/plugin/idea/_core/AsyncAPISchemaRecognizerTest.kt deleted file mode 100644 index 5673911..0000000 --- a/src/test/kotlin/com/asyncapi/plugin/idea/_core/AsyncAPISchemaRecognizerTest.kt +++ /dev/null @@ -1,90 +0,0 @@ -package com.asyncapi.plugin.idea._core - -import com.intellij.json.psi.JsonFile -import com.intellij.lang.Language -import com.intellij.openapi.components.service -import com.intellij.psi.PsiFileFactory -import com.intellij.testFramework.fixtures.BasePlatformTestCase -import junit.framework.TestCase -import org.jetbrains.yaml.YAMLLanguage -import org.jetbrains.yaml.psi.YAMLFile - -/** - * @author Pavel Bodiachevskii - */ -class AsyncAPISchemaRecognizerTest: BasePlatformTestCase() { - - override fun getTestDataPath(): String { - return "src/test/testData" - } - - fun `test - injects correctly`() { - TestCase.assertNotNull(service()) - } - - fun `test - isSchema when file is null`() { - val asyncAPISchemaRecognizer = service() - - TestCase.assertFalse( - "must be false in case of null file.", - asyncAPISchemaRecognizer.isSchema(project, null) - ) - } - - fun `test - isSchema when file is schema and not empty`() { - val asyncAPISchemaRecognizer = service() - - val psiFileFactory = PsiFileFactory.getInstance(project) - TestCase.assertTrue( - "must be true in case of non null, non empty json file with `asyncapi` key provided.", - asyncAPISchemaRecognizer.isSchema( - project, - (psiFileFactory.createFileFromText( - "asyncapi.json", - Language.findLanguageByID("JSON")!!, - this.javaClass.getResource("/asyncapi.json")?.readText() ?: "" - ) as JsonFile).virtualFile - ) - ) - TestCase.assertTrue( - "must be true in case of non null, non empty yaml file with `asyncapi` key provided.", - asyncAPISchemaRecognizer.isSchema( - project, - (psiFileFactory.createFileFromText( - "asyncapi.yaml", - YAMLLanguage.INSTANCE, - this.javaClass.getResource("/asyncapi.yaml")?.readText() ?: "" - ) as YAMLFile).virtualFile - ) - ) - } - - fun `test - isSchema when file is empty`() { - val asyncAPISchemaRecognizer = service() - - val psiFileFactory = PsiFileFactory.getInstance(project) - TestCase.assertFalse( - "must be true in case of non null, non empty json file with `asyncapi` key provided.", - asyncAPISchemaRecognizer.isSchema( - project, - (psiFileFactory.createFileFromText( - "asyncapi.json", - Language.findLanguageByID("JSON")!!, - "" - ) as JsonFile).virtualFile - ) - ) - TestCase.assertFalse( - "must be true in case of non null, non empty yaml file with `asyncapi` key provided.", - asyncAPISchemaRecognizer.isSchema( - project, - (psiFileFactory.createFileFromText( - "asyncapi.yaml", - YAMLLanguage.INSTANCE, - "" - ) as YAMLFile).virtualFile - ) - ) - } - -} \ No newline at end of file