diff --git a/json/src/com/jetbrains/jsonSchema/extension/schema/JsonSchemaDefinitionResolver.java b/json/src/com/jetbrains/jsonSchema/extension/schema/JsonSchemaDefinitionResolver.java index 444c78bd77b8..e2d79ede8a1a 100644 --- a/json/src/com/jetbrains/jsonSchema/extension/schema/JsonSchemaDefinitionResolver.java +++ b/json/src/com/jetbrains/jsonSchema/extension/schema/JsonSchemaDefinitionResolver.java @@ -27,6 +27,7 @@ import com.intellij.psi.PsiFile; import com.intellij.psi.PsiManager; import com.intellij.psi.search.GlobalSearchScope; import com.intellij.util.indexing.FileBasedIndex; +import com.jetbrains.jsonSchema.JsonSchemaFileType; import com.jetbrains.jsonSchema.ide.JsonSchemaService; import com.jetbrains.jsonSchema.impl.*; import org.jetbrains.annotations.NotNull; @@ -73,6 +74,19 @@ public class JsonSchemaDefinitionResolver { return null; } + @Nullable + public PsiElement doResolveInSchemaFile() { + if (!JsonSchemaFileType.INSTANCE.equals(myElement.getContainingFile().getFileType())) return null; + if (myRef == null) initializeName(); + if (myRef == null) return null; + final PsiElement element = resolveInSomeSchema(myRef, myElement.getProject(), null, myElement.getContainingFile().getVirtualFile()); + if (element != null) return element; + if (mySchemaId == null) { + return tryResolveBySchemaObject(); + } + return null; + } + private PsiElement tryResolveBySchemaObject() { if (!(myElement.getParent() instanceof JsonProperty)) return null; final Ref ref = new Ref<>(); diff --git a/json/src/com/jetbrains/jsonSchema/extension/schema/JsonSchemaRefReferenceProvider.java b/json/src/com/jetbrains/jsonSchema/extension/schema/JsonSchemaRefReferenceProvider.java index ae83e0f34fc1..38495a5cbb9c 100644 --- a/json/src/com/jetbrains/jsonSchema/extension/schema/JsonSchemaRefReferenceProvider.java +++ b/json/src/com/jetbrains/jsonSchema/extension/schema/JsonSchemaRefReferenceProvider.java @@ -58,9 +58,10 @@ public class JsonSchemaRefReferenceProvider extends PsiReferenceProvider { if (idx <= 0) return null; id = text.substring(0, idx); ref = text.substring(idx + 1); + return new JsonSchemaDefinitionResolver(getElement(), id).setRef(ref).doResolve(); } - return new JsonSchemaDefinitionResolver(getElement(), id).setRef(ref).doResolve(); + return new JsonSchemaDefinitionResolver(getElement(), null).setRef(ref).doResolveInSchemaFile(); } } } diff --git a/json/tests/test/com/jetbrains/jsonSchema/JsonSchemaCrossReferencesTest.java b/json/tests/test/com/jetbrains/jsonSchema/JsonSchemaCrossReferencesTest.java index 676969a2846d..959b15195fb0 100644 --- a/json/tests/test/com/jetbrains/jsonSchema/JsonSchemaCrossReferencesTest.java +++ b/json/tests/test/com/jetbrains/jsonSchema/JsonSchemaCrossReferencesTest.java @@ -84,12 +84,14 @@ public class JsonSchemaCrossReferencesTest extends CompletionTestCase { instance.addSchema(inherited); + doHighlighting(); complete(); assertStringItems("\"one\"", "\"two\""); LookupImpl lookup = getActiveLookup(); if (lookup != null) lookup.hide(); JsonSchemaService.Impl.get(getProject()).reset(); + doHighlighting(); complete(); assertStringItems("\"one\"", "\"two\""); @@ -157,6 +159,7 @@ public class JsonSchemaCrossReferencesTest extends CompletionTestCase { } private void testSchemaCompletion(VirtualFile moduleFile, final String fileName) { + doHighlighting(); complete(); assertStringItems("\"one\"", "\"two\""); @@ -177,17 +180,20 @@ public class JsonSchemaCrossReferencesTest extends CompletionTestCase { LookupImpl lookup = getActiveLookup(); if (lookup != null) lookup.hide(); + doHighlighting(); complete(); assertStringItems("\"one1\"", "\"two1\""); lookup = getActiveLookup(); if (lookup != null) lookup.hide(); JsonSchemaService.Impl.get(getProject()).reset(); + doHighlighting(); complete(); assertStringItems("\"one1\"", "\"two1\""); } public void testJsonSchemaRefsCrossResolve() throws Exception { + ApplicationManager.getApplication().runWriteAction(() -> myFileTypeManager.associatePattern(JsonSchemaFileType.INSTANCE, "*Schema.json")); configureByFiles(null, BASE_SCHEMA_RESOLVE_PATH + "/referencingSchema.json", BASE_SCHEMA_RESOLVE_PATH + "/localRefSchema.json"); String moduleDir = null; @@ -202,11 +208,6 @@ public class JsonSchemaCrossReferencesTest extends CompletionTestCase { } Assert.assertNotNull(moduleDir); - AreaPicoContainer container = Extensions.getArea(getProject()).getPicoContainer(); - final String key = JsonSchemaMappingsProjectConfiguration.class.getName(); - container.unregisterComponent(key); - container.registerComponentImplementation(key, TestJsonSchemaMappingsProjectConfiguration.class); - final JsonSchemaMappingsProjectConfiguration instance = JsonSchemaMappingsProjectConfiguration.getInstance(getProject()); final JsonSchemaMappingsConfigurationBase.SchemaInfo base = new JsonSchemaMappingsConfigurationBase.SchemaInfo("base", moduleDir + "/localRefSchema.json", false, Collections.emptyList()); @@ -218,8 +219,8 @@ public class JsonSchemaCrossReferencesTest extends CompletionTestCase { instance.addSchema(inherited); try { - ApplicationManager.getApplication().runWriteAction(() -> myFileTypeManager.associatePattern(JsonSchemaFileType.INSTANCE, "*Schema.json")); JsonSchemaService.Impl.get(getProject()).reset(); + doHighlighting(); testIsSchemaFile(moduleFile, "localRefSchema.json"); testIsSchemaFile(moduleFile, "referencingSchema.json"); @@ -233,8 +234,6 @@ public class JsonSchemaCrossReferencesTest extends CompletionTestCase { ApplicationManager.getApplication().runWriteAction(() -> myFileTypeManager.removeAssociatedExtension(JsonSchemaFileType.INSTANCE, "*Schema.json")); } finally { - container.unregisterComponent(key); - container.registerComponentImplementation(key, JsonSchemaMappingsProjectConfiguration.class); } instance.removeSchema(inherited); @@ -276,6 +275,9 @@ public class JsonSchemaCrossReferencesTest extends CompletionTestCase { try { ApplicationManager.getApplication().runWriteAction(() -> myFileTypeManager.associatePattern(JsonSchemaFileType.INSTANCE, "*Schema.json")); + JsonSchemaService.Impl.get(getProject()).reset(); + doHighlighting(); + int offset = myEditor.getCaretModel().getPrimaryCaret().getOffset(); final PsiReference referenceAt = myFile.findReferenceAt(offset); Assert.assertNotNull(referenceAt); @@ -315,6 +317,7 @@ public class JsonSchemaCrossReferencesTest extends CompletionTestCase { instance.addSchema(inherited); JsonSchemaService.Impl.get(getProject()).reset(); + doHighlighting(); ApplicationManager.getApplication().runWriteAction(() -> myFileTypeManager.associatePattern(JsonSchemaFileType.INSTANCE, "*Schema.json")); try { diff --git a/json/tests/test/com/jetbrains/jsonSchema/schemaFile/JsonSchemaFileResolveTest.java b/json/tests/test/com/jetbrains/jsonSchema/schemaFile/JsonSchemaFileResolveTest.java index 8bfb6ae6f54a..eca592bdf86c 100644 --- a/json/tests/test/com/jetbrains/jsonSchema/schemaFile/JsonSchemaFileResolveTest.java +++ b/json/tests/test/com/jetbrains/jsonSchema/schemaFile/JsonSchemaFileResolveTest.java @@ -15,24 +15,28 @@ */ package com.jetbrains.jsonSchema.schemaFile; +import com.intellij.codeInsight.daemon.DaemonAnalyzerTestCase; import com.intellij.openapi.application.PathManager; import com.intellij.openapi.application.ex.PathManagerEx; import com.intellij.openapi.command.WriteCommandAction; -import com.intellij.openapi.extensions.AreaPicoContainer; -import com.intellij.openapi.extensions.Extensions; import com.intellij.openapi.fileTypes.FileTypeManager; import com.intellij.openapi.fileTypes.ex.FileTypeManagerEx; +import com.intellij.openapi.vfs.VfsUtil; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiReference; -import com.intellij.testFramework.fixtures.LightPlatformCodeInsightFixtureTestCase; +import com.intellij.psi.util.PsiTreeUtil; import com.jetbrains.jsonSchema.JsonSchemaFileType; +import com.jetbrains.jsonSchema.JsonSchemaMappingsConfigurationBase; import com.jetbrains.jsonSchema.JsonSchemaMappingsProjectConfiguration; +import com.jetbrains.jsonSchema.ide.JsonSchemaService; import org.junit.Assert; +import java.util.Collections; + /** * @author Irina.Chernushina on 4/1/2016. */ -public class JsonSchemaFileResolveTest extends LightPlatformCodeInsightFixtureTestCase { +public class JsonSchemaFileResolveTest extends DaemonAnalyzerTestCase { private final static String BASE_PATH = "/tests/testData/jsonSchema/schemaFile/resolve"; private FileTypeManager myFileTypeManager; @@ -46,20 +50,26 @@ public class JsonSchemaFileResolveTest extends LightPlatformCodeInsightFixtureTe protected String getTestDataPath() { PathManagerEx.TestDataLookupStrategy strategy = PathManagerEx.guessTestDataLookupStrategy(); if (strategy.equals(PathManagerEx.TestDataLookupStrategy.COMMUNITY)) { - return PathManager.getHomePath() + "/json" + BASE_PATH; + return PathManager.getHomePath() + "/json" + BASE_PATH + "/"; } - return PathManager.getHomePath() + "/community/json" + BASE_PATH; + return PathManager.getHomePath() + "/community/json" + BASE_PATH + "/"; } public void testResolveLocalRef() throws Exception { - AreaPicoContainer container = Extensions.getArea(getProject()).getPicoContainer(); - final String key = JsonSchemaMappingsProjectConfiguration.class.getName(); - container.unregisterComponent(key); - container.registerComponentImplementation(key, TestJsonSchemaMappingsProjectConfiguration.class); - + JsonSchemaMappingsConfigurationBase.SchemaInfo schemaInfo = null; try { WriteCommandAction.runWriteCommandAction(getProject(), () -> myFileTypeManager.associatePattern(JsonSchemaFileType.INSTANCE, "*Schema.json")); - PsiReference position = myFixture.getReferenceAtCaretPosition("localRefSchema.json"); + configureByFile("localRefSchema.json"); + final String path = VfsUtil.getRelativePath(myFile.getVirtualFile(), myProject.getBaseDir()); + schemaInfo = new JsonSchemaMappingsConfigurationBase.SchemaInfo("test", path, false, Collections.emptyList()); + JsonSchemaMappingsProjectConfiguration.getInstance(getProject()).addSchema(schemaInfo); + JsonSchemaService.Impl.get(myProject).reset(); + doHighlighting(); + + final int offset = getEditor().getCaretModel().getCurrentCaret().getOffset(); + final PsiElement atOffset = PsiTreeUtil.findElementOfClassAtOffset(myFile, offset, PsiElement.class, false); + Assert.assertNotNull(atOffset); + PsiReference position = myFile.findReferenceAt(offset); Assert.assertNotNull(position); PsiElement resolve = position.resolve(); Assert.assertNotNull(resolve); @@ -67,29 +77,7 @@ public class JsonSchemaFileResolveTest extends LightPlatformCodeInsightFixtureTe WriteCommandAction.runWriteCommandAction(getProject(), () -> myFileTypeManager.removeAssociatedExtension(JsonSchemaFileType.INSTANCE, "*Schema.json")); } finally { - container.unregisterComponent(key); - container.registerComponentImplementation(key, JsonSchemaMappingsProjectConfiguration.class); - } - } - - public void testResolveExternalRef() throws Exception { - AreaPicoContainer container = Extensions.getArea(getProject()).getPicoContainer(); - final String key = JsonSchemaMappingsProjectConfiguration.class.getName(); - container.unregisterComponent(key); - container.registerComponentImplementation(key, TestJsonSchemaMappingsProjectConfiguration.class); - - try { - WriteCommandAction.runWriteCommandAction(getProject(), () -> myFileTypeManager.associatePattern(JsonSchemaFileType.INSTANCE, "*Schema.json")); - PsiReference position = myFixture.getReferenceAtCaretPosition("localRefSchema.json"); - Assert.assertNotNull(position); - PsiElement resolve = position.resolve(); - Assert.assertNotNull(resolve); - Assert.assertEquals("\"baseEnum\"", resolve.getText()); - - WriteCommandAction.runWriteCommandAction(getProject(), () -> myFileTypeManager.removeAssociatedExtension(JsonSchemaFileType.INSTANCE, "*Schema.json")); - } finally { - container.unregisterComponent(key); - container.registerComponentImplementation(key, JsonSchemaMappingsProjectConfiguration.class); + if (schemaInfo != null) JsonSchemaMappingsProjectConfiguration.getInstance(getProject()).removeSchema(schemaInfo); } } } diff --git a/json/tests/testData/jsonSchema/schemaFile/resolve/localRefSchema.json b/json/tests/testData/jsonSchema/schemaFile/resolve/localRefSchema.json index c053edda979d..e3bb136688ab 100644 --- a/json/tests/testData/jsonSchema/schemaFile/resolve/localRefSchema.json +++ b/json/tests/testData/jsonSchema/schemaFile/resolve/localRefSchema.json @@ -6,7 +6,7 @@ "enum": ["one", "two"] }, "smth": { - "$ref" : "#/properties/baseEnum" + "$ref" : "#/properties/baseEnum" } } } \ No newline at end of file