use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.
the class JSONModelParser method setupContext.
/**
* setupContext method
*/
private void setupContext(IStructuredDocumentRegion startStructuredDocumentRegion) {
int offset = startStructuredDocumentRegion.getStart();
if (offset < 0)
return;
JSONNodeImpl root = (JSONNodeImpl) this.context.getRootNode();
if (root == null)
return;
if (offset == 0) {
// at the beginning of document
IJSONNode child = root.getFirstChild();
if (child != null)
this.context.setCurrentNode(child);
else
this.context.setParentNode(root);
return;
}
// Get the JSON Node at the specified position in the document, this
// position is the start offset of the flatNode
JSONNodeImpl node = (JSONNodeImpl) root.getNodeAt(offset);
if (node == null) {
// might be at the end of document
this.context.setParentNode(root);
this.context.setLast();
return;
}
if (node.getStartStructuredDocumentRegion() != null) {
if (offset == node.getStartStructuredDocumentRegion().getStartOffset()) {
// This is a JSONPair and the flatNode might be and update of
// the object key value
this.context.setCurrentNode(node);
return;
} else if (node.getEndStructuredDocumentRegion() != null) {
if (offset == node.getEndStructuredDocumentRegion().getStartOffset()) {
// This is a JSONPair and the flatNode might be an update of
// the pair value
this.context.setCurrentNode(node);
return;
}
}
}
if (node instanceof JSONPairImpl && ((JSONPairImpl) node).getValue() != null) {
if (isJSONValue(startStructuredDocumentRegion.getType())) {
// This is a JSONPair and the flatNode might be the pair value
this.context.setCurrentNode(node);
return;
}
}
// into the children
for (IJSONNode child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
if (offset >= ((JSONNodeImpl) child).getEndOffset()) {
if (child instanceof JSONPairImpl && ((JSONPairImpl) child).getValue() == null) {
if (isJSONValue(startStructuredDocumentRegion.getType())) {
this.context.setCurrentNode(child);
return;
}
} else {
continue;
}
}
this.context.setCurrentNode(child);
return;
}
// The node is tha paret node of the flatNode
this.context.setParentNode(node);
this.context.setLast();
}
use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.
the class JSONPairFormatter method formatChildren.
@Override
protected void formatChildren(IJSONNode node, IRegion region, StringBuilder source) {
IJSONNode child = node.getFirstChild();
int start = region.getOffset();
int end = region.getOffset() + region.getLength();
while (child != null) {
int curEnd = child.getEndOffset();
StringBuilder childSource = null;
boolean toFinish = false;
if (start < curEnd) {
int curStart = child.getStartOffset();
if (curStart < end) {
// append child
IJSONSourceFormatter formatter = (IJSONSourceFormatter) ((INodeNotifier) child).getAdapterFor(IJSONSourceFormatter.class);
if (formatter == null) {
formatter = JSONSourceFormatterFactory.getInstance().getSourceFormatter(child);
}
if (includes(region, curStart, curEnd))
childSource = ((AbstractJSONSourceFormatter) formatter).formatProc(child);
else
childSource = ((AbstractJSONSourceFormatter) formatter).formatProc(child, overlappedRegion(region, curStart, curEnd));
} else
toFinish = true;
}
if (childSource != null) {
source.append(childSource);
}
if (toFinish)
break;
child = child.getNextSibling();
}
}
use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.
the class Validator method validateUniqueItems.
private void validateUniqueItems(IJSONNode node, JsonObject schema, IJSONValue value, JsonValue memberValue, JSONValidationInfo valinfo) {
if (memberValue != null && memberValue.isBoolean() && memberValue.asBoolean()) {
if (value instanceof IJSONArray) {
Set<String> instanceValues = new HashSet<String>();
IJSONNode child = value.getFirstChild();
int instanceSize = 0;
while (child != null) {
instanceSize = instanceSize + 1;
instanceValues.add(JSONUtil.getString(child));
child = child.getNextSibling();
}
;
if (instanceSize != instanceValues.size()) {
int offset = node.getStartOffset();
int line = node.getModel().getStructuredDocument().getLineOfOffset(offset);
valinfo.addMessage("Array has duplicate items", line, 0, offset == 0 ? 1 : offset);
}
}
}
}
use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.
the class JSONHoverProcessor method computeHoverHelp.
/**
* Retrieves documentation to display in the hover help popup.
*
* @return String any documentation information to display <code>null</code>
* if there is nothing to display.
*/
protected String computeHoverHelp(ITextViewer textViewer, int documentPosition) {
String result = null;
IndexedRegion treeNode = ContentAssistUtils.getNodeAt(textViewer, documentPosition);
if (treeNode == null) {
return null;
}
IJSONNode node = (IJSONNode) treeNode;
IJSONNode parentNode = node.getParentNode();
IStructuredDocumentRegion flatNode = ((IStructuredDocument) textViewer.getDocument()).getRegionAtCharacterOffset(documentPosition);
if (flatNode != null) {
ITextRegion region = flatNode.getRegionAtCharacterOffset(documentPosition);
if (region != null) {
result = computeRegionHelp(treeNode, parentNode, flatNode, region);
}
}
return result;
}
use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.
the class JSONModelParser method changeAttrName.
/**
* changeAttrName method
*/
private void changeAttrName(IStructuredDocumentRegion flatNode, ITextRegion region) {
int offset = flatNode.getStart();
if (offset < 0)
return;
JSONNodeImpl root = (JSONNodeImpl) this.context.getRootNode();
if (root == null)
return;
IJSONNode node = root.getNodeAt(offset);
if (node == null)
return;
if (node.getNodeType() != IJSONNode.PAIR_NODE) {
return;
}
JSONPairImpl pair = (JSONPairImpl) node;
String name = flatNode.getText(region);
pair.setName(name);
JSONObjectImpl parentObj = (JSONObjectImpl) node.getParentNode();
if (parentObj != null && parentObj.getParentOrPairNode() instanceof JSONPairImpl) {
JSONPairImpl parentPair = (JSONPairImpl) parentObj.getParentOrPairNode();
if (parentPair.getValue() != parentObj.getParentNode()) {
parentPair.setValue(parentObj);
}
}
}
Aggregations