use of com.intellij.json.psi.JsonObject in project intellij-community by JetBrains.
the class JsonSchemaReader method create.
@Nullable
public static JsonSchemaReader create(@NotNull Project project, @NotNull VirtualFile key) {
final PsiFile psiFile = PsiManager.getInstance(project).findFile(key);
if (!(psiFile instanceof JsonFile))
return null;
final List<JsonValue> values = ((JsonFile) psiFile).getAllTopLevelValues();
if (values.size() != 1 || !(values.get(0) instanceof JsonObject))
return null;
return new JsonSchemaReader((JsonObject) values.get(0));
}
use of com.intellij.json.psi.JsonObject in project intellij-community by JetBrains.
the class JsonSchemaInsideSchemaResolver method resolveInSchemaRecursively.
public PsiElement resolveInSchemaRecursively() {
final Ref<PsiElement> ref = new Ref<>();
final JsonSchemaWalker.CompletionSchemesConsumer consumer = new JsonSchemaWalker.CompletionSchemesConsumer() {
@Override
public void consume(boolean isName, @NotNull JsonSchemaObject schema, @NotNull VirtualFile schemaFile, @NotNull List<JsonSchemaWalker.Step> steps) {
if (!ref.isNull())
return;
final PsiFile file = PsiManager.getInstance(myProject).findFile(mySchemaFile);
if (file == null)
return;
final JsonObject jsonObject = schema.getPeerPointer().getElement();
if (jsonObject != null && jsonObject.isValid()) {
if (jsonObject.getParent() instanceof JsonProperty)
ref.set(((JsonProperty) jsonObject.getParent()).getNameElement());
else
ref.set(jsonObject);
}
}
@Override
public void oneOf(boolean isName, @NotNull List<JsonSchemaObject> list, @NotNull VirtualFile schemaFile, @NotNull List<JsonSchemaWalker.Step> steps) {
list.stream().findFirst().ifPresent(object -> consume(isName, object, schemaFile, steps));
}
@Override
public void anyOf(boolean isName, @NotNull List<JsonSchemaObject> list, @NotNull VirtualFile schemaFile, @NotNull List<JsonSchemaWalker.Step> steps) {
list.stream().findFirst().ifPresent(object -> consume(isName, object, schemaFile, steps));
}
};
JsonSchemaService.Impl.getEx(myProject).visitSchemaObject(mySchemaFile, object -> {
JsonSchemaWalker.extractSchemaVariants(myProject, consumer, mySchemaFile, object, true, mySteps, false);
return true;
});
return ref.get();
}
use of com.intellij.json.psi.JsonObject in project intellij-community by JetBrains.
the class JsonBySchemaDocumentationProvider method generateDoc.
@Nullable
@Override
public String generateDoc(PsiElement element, @Nullable PsiElement originalElement) {
final JsonLikePsiWalker walker = JsonSchemaWalker.getWalker(element, myRootSchema);
if (walker == null)
return null;
if (JsonSchemaFileType.INSTANCE.equals(element.getContainingFile().getFileType())) {
final JsonProperty jsonProperty = element instanceof JsonProperty ? (JsonProperty) element : PsiTreeUtil.getParentOfType(element, JsonProperty.class);
if (jsonProperty != null) {
final JsonValue value = jsonProperty.getValue();
if (value instanceof JsonObject) {
final JsonProperty description = ((JsonObject) value).findProperty("description");
if (description != null && description.getValue() instanceof JsonStringLiteral) {
return StringUtil.escapeXml(StringUtil.unquoteString(description.getValue().getText()));
}
}
return null;
}
}
final Ref<String> result = Ref.create();
JsonSchemaWalker.findSchemasForDocumentation(element, walker, new JsonSchemaWalker.CompletionSchemesConsumer() {
@Override
public void consume(boolean isName, @NotNull JsonSchemaObject schema, @NotNull VirtualFile schemaFile, @NotNull List<JsonSchemaWalker.Step> steps) {
result.set(schema.getDescription());
}
@Override
public void oneOf(boolean isName, @NotNull List<JsonSchemaObject> list, @NotNull VirtualFile schemaFile, @NotNull List<JsonSchemaWalker.Step> steps) {
//todo?
}
@Override
public void anyOf(boolean isName, @NotNull List<JsonSchemaObject> list, @NotNull VirtualFile schemaFile, @NotNull List<JsonSchemaWalker.Step> steps) {
//todo?
}
}, myRootSchema, mySchemaFile);
return result.get();
}
use of com.intellij.json.psi.JsonObject in project intellij-community by JetBrains.
the class JsonPsiUtilTest method checkAddProperty.
private void checkAddProperty(@NotNull String before, @NotNull String after, final boolean first) {
getCustomCodeStyleSettings().OBJECT_WRAPPING = CommonCodeStyleSettings.DO_NOT_WRAP;
myFixture.configureByText(JsonFileType.INSTANCE, before);
final PsiElement atCaret = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
final JsonObject jsonObject = PsiTreeUtil.getParentOfType(atCaret, JsonObject.class);
assertNotNull(jsonObject);
WriteCommandAction.runWriteCommandAction(getProject(), () -> {
JsonPsiUtil.addProperty(jsonObject, new JsonElementGenerator(getProject()).createProperty("foo", "null"), first);
});
myFixture.checkResult(after);
}
Aggregations