-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
104 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/main/java/opwvhk/intellij/avro_idl/language/AvroIdlJsonUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package opwvhk.intellij.avro_idl.language; | ||
|
||
import com.intellij.codeInsight.lookup.LookupElement; | ||
import com.intellij.json.psi.*; | ||
import com.intellij.openapi.vfs.VirtualFile; | ||
import com.intellij.psi.ElementManipulators; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiFile; | ||
import com.intellij.psi.PsiManager; | ||
import com.intellij.psi.util.PsiTreeUtil; | ||
import opwvhk.intellij.avro_idl.psi.AvroIdlJsonStringLiteral; | ||
import org.apache.avro.Schema; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class AvroIdlJsonUtil { | ||
@NotNull | ||
public static LookupElement createLookupElementForSchemaInJsonFile(@NotNull String namespace, | ||
AvroIdlJsonStringLiteral importedFileReferenceElement, | ||
PsiManager psiManager, | ||
VirtualFile importedFile, Schema schema) { | ||
final PsiFile psiProtocolFile = psiManager.findFile(importedFile); | ||
if (psiProtocolFile instanceof JsonFile) { | ||
final PsiElement[] elements = PsiTreeUtil.collectElements(psiProtocolFile, | ||
element -> isSchemaJsonObject(element, schema)); | ||
if (elements.length > 0) { | ||
return lookupElement(elements[0], schema, namespace); | ||
} | ||
} | ||
return lookupElement(importedFileReferenceElement, schema, namespace); | ||
} | ||
|
||
private static boolean isSchemaJsonObject(@NotNull PsiElement element, @NotNull Schema schema) { | ||
if (!(element instanceof JsonObject)) { | ||
return false; | ||
} | ||
|
||
JsonProperty nameProperty = ((JsonObject) element).findProperty("name"); | ||
if (nameProperty == null) { | ||
return false; | ||
} | ||
JsonValue nameValue = nameProperty.getValue(); | ||
if (!(nameValue instanceof JsonStringLiteral)) { | ||
return false; | ||
} | ||
String name = ElementManipulators.getValueText(nameValue); | ||
|
||
if (name.contains(".")) { | ||
return name.equals(schema.getFullName()); | ||
} else if (name.equals(schema.getName())) { | ||
JsonElement parent = (JsonElement) element; | ||
while (parent != null && !(parent instanceof JsonFile)) { | ||
if (parent instanceof JsonObject) { | ||
final JsonProperty namespaceProperty = ((JsonObject) parent).findProperty("namespace"); | ||
if (namespaceProperty != null) { | ||
final JsonValue namespaceValue = namespaceProperty.getValue(); | ||
return namespaceValue instanceof JsonStringLiteral && | ||
ElementManipulators.getValueText(namespaceValue).equals(schema.getNamespace()); | ||
} | ||
} | ||
parent = (JsonElement) parent.getParent(); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
@NotNull | ||
private static LookupElement lookupElement(@NotNull PsiElement psiElement, @NotNull Schema schema, | ||
@NotNull String currentNamespace) { | ||
final String namespace0 = schema.getNamespace(); | ||
final String namespace = namespace0 == null ? "" : namespace0; | ||
final String schemaName = schema.getName(); | ||
final String schemaFullName = schema.getFullName(); | ||
final LookupElement lookupElement = AvroIdlUtil.lookupElement(psiElement, schemaName, schemaFullName, namespace, | ||
currentNamespace); | ||
lookupElement.putUserData(AvroIdlUtil.IS_ERROR_KEY, schema.getType() == Schema.Type.RECORD && schema.isError()); | ||
return lookupElement; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters