use of com.intellij.json.psi.JsonProperty in project intellij-community by JetBrains.
the class JsonPropertyNameReference method isReferenceTo.
@Override
public boolean isReferenceTo(PsiElement element) {
if (!(element instanceof JsonProperty)) {
return false;
}
// May reference to the property with the same name for compatibility with JavaScript JSON support
final JsonProperty otherProperty = (JsonProperty) element;
final PsiElement selfResolve = resolve();
return otherProperty.getName().equals(getCanonicalText()) && selfResolve != otherProperty;
}
use of com.intellij.json.psi.JsonProperty 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.JsonProperty 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.JsonProperty in project intellij-community by JetBrains.
the class JsonPsiUtilTest method testGetOtherSiblingPropertyNames.
public void testGetOtherSiblingPropertyNames() throws Exception {
myFixture.configureByText(JsonFileType.INSTANCE, "{\"firs<caret>t\" : 1, \"second\" : 2}");
PsiElement atCaret = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
JsonProperty property = PsiTreeUtil.getParentOfType(atCaret, JsonProperty.class);
assertNotNull(property);
assertEquals(Collections.singleton("second"), JsonPsiUtil.getOtherSiblingPropertyNames(property));
}
use of com.intellij.json.psi.JsonProperty in project intellij-community by JetBrains.
the class JsonStringPropertyInsertHandler method handleInsert.
@Override
public void handleInsert(InsertionContext context, LookupElement item) {
PsiElement element = context.getFile().findElementAt(context.getStartOffset());
JsonStringLiteral literal = PsiTreeUtil.getParentOfType(element, JsonStringLiteral.class, false);
if (literal == null)
return;
JsonProperty property = ObjectUtils.tryCast(literal.getParent(), JsonProperty.class);
if (property == null)
return;
final TextRange toDelete;
String textToInsert = "";
TextRange literalRange = literal.getTextRange();
if (literal.getValue().equals(myNewValue)) {
toDelete = new TextRange(literalRange.getEndOffset(), literalRange.getEndOffset());
} else {
toDelete = literalRange;
textToInsert = StringUtil.wrapWithDoubleQuote(myNewValue);
}
int newCaretOffset = literalRange.getStartOffset() + 1 + myNewValue.length();
boolean showAutoPopup = false;
if (property.getNameElement().equals(literal)) {
if (property.getValue() == null) {
textToInsert += ":\"\"";
// "package<caret offset>":"<new caret offset>"
newCaretOffset += 3;
if (needCommaAfter(property)) {
textToInsert += ",";
}
showAutoPopup = true;
}
}
context.getDocument().replaceString(toDelete.getStartOffset(), toDelete.getEndOffset(), textToInsert);
context.getEditor().getCaretModel().moveToOffset(newCaretOffset);
reformat(context, toDelete.getStartOffset(), toDelete.getStartOffset() + textToInsert.length());
if (showAutoPopup) {
AutoPopupController.getInstance(context.getProject()).autoPopupMemberLookup(context.getEditor(), null);
}
}
Aggregations