use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.
the class AbstractJSONSourceFormatter method getIndent.
protected String getIndent(IJSONNode node) {
if (node == null)
// $NON-NLS-1$
return "";
IJSONNode parent = node.getParentNode();
if (parent == null || parent instanceof IJSONDocument)
// $NON-NLS-1$
return "";
String parentIndent = getIndent(parent);
return parentIndent + getIndentString();
}
use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.
the class JSONModelImpl method getIndexedRegion.
@Override
public IndexedRegion getIndexedRegion(int offset) {
if (this.document == null)
return null;
// search in document children
IJSONNode parent = null;
int length = this.document.getEndOffset();
if (offset * 2 < length) {
// search from the first
IJSONNode child = (IJSONNode) this.document.getFirstChild();
while (child != null) {
if (child.getEndOffset() <= offset) {
child = (IJSONNode) child.getNextSibling();
continue;
}
if (child.getStartOffset() > offset) {
break;
}
IStructuredDocumentRegion startStructuredDocumentRegion = child.getStartStructuredDocumentRegion();
if (startStructuredDocumentRegion != null) {
if (startStructuredDocumentRegion.getEnd() > offset)
return child;
}
IStructuredDocumentRegion endStructuredDocumentRegion = child.getEndStructuredDocumentRegion();
if (endStructuredDocumentRegion != null) {
if (endStructuredDocumentRegion.getStart() <= offset) {
if (child instanceof IJSONPair) {
IJSONValue value = ((IJSONPair) child).getValue();
if (value instanceof IJSONObject || value instanceof IJSONArray) {
if (value.getStartOffset() < offset) {
child = value;
continue;
}
}
}
return child;
}
}
// dig more
parent = child;
if (parent != null && parent.getNodeType() == IJSONNode.PAIR_NODE) {
IJSONPair pair = (IJSONPair) parent;
child = pair.getValue();
} else {
child = (IJSONNode) parent.getFirstChild();
}
}
} else {
// search from the last
IJSONNode child = (IJSONNode) this.document.getLastChild();
while (child != null) {
if (child.getStartOffset() > offset) {
child = (IJSONNode) child.getPreviousSibling();
continue;
}
if (child.getEndOffset() <= offset) {
break;
}
IStructuredDocumentRegion startStructuredDocumentRegion = child.getStartStructuredDocumentRegion();
if (startStructuredDocumentRegion != null) {
if (startStructuredDocumentRegion.getEnd() > offset)
return child;
}
IStructuredDocumentRegion endStructuredDocumentRegion = child.getEndStructuredDocumentRegion();
if (endStructuredDocumentRegion != null) {
if (endStructuredDocumentRegion.getStart() <= offset) {
if (child instanceof IJSONPair) {
IJSONValue value = ((IJSONPair) child).getValue();
if (value instanceof IJSONObject || value instanceof IJSONArray) {
if (value.getStartOffset() < offset) {
child = value;
continue;
}
}
}
return child;
}
}
// dig more
parent = child;
if (parent != null && parent.getNodeType() == IJSONNode.PAIR_NODE) {
IJSONPair pair = (IJSONPair) parent;
child = pair.getValue();
} else {
child = (IJSONNode) parent.getLastChild();
}
}
}
return parent != null ? parent : document.getFirstChild();
}
use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.
the class JSONModelNotifierImpl method childReplaced.
/**
* childReplaced method
*
* @param parentNode
* org.w3c.dom.Node
* @param newChild
* org.w3c.dom.Node
* @param oldChild
* org.w3c.dom.Node
*/
@Override
public void childReplaced(IJSONNode parentNode, IJSONNode newChild, IJSONNode oldChild) {
if (parentNode == null)
return;
IJSONNode notifier = (IJSONNode) parentNode;
int type = INodeNotifier.CHANGE;
if (newChild == null)
type = INodeNotifier.REMOVE;
else if (oldChild == null)
type = INodeNotifier.ADD;
int offset = notifier.getStartOffset();
notify(notifier, type, oldChild, oldChild, newChild, offset);
structureChanged(notifier);
}
use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.
the class JSONModelNotifierImpl method pairReplaced.
/**
* attrReplaced method
*
* @param element
* org.w3c.dom.Element
* @param newAttr
* org.w3c.dom.IJSONNode
* @param oldAttr
* org.w3c.dom.IJSONNode
*/
public void pairReplaced(IJSONObject element, IJSONPair newAttr, IJSONPair oldAttr) {
if (element == null)
return;
IJSONNode attr = null;
IJSONValue oldValue = null;
IJSONValue newValue = null;
if (oldAttr != null) {
attr = oldAttr;
oldValue = oldAttr.getValue();
}
if (newAttr != null) {
attr = newAttr;
newValue = newAttr.getValue();
}
IJSONNode notifier = (IJSONNode) element;
int offset = notifier.getStartOffset();
notify(notifier, INodeNotifier.CHANGE, attr, oldValue, newValue, offset);
propertyChanged(notifier);
}
use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.
the class Validator method getProperties.
private Set<String> getProperties(IJSONNode node) {
Set<String> properties = new HashSet<String>();
IJSONNode child = node.getFirstChild();
while (child != null) {
if (child instanceof IJSONPair) {
IJSONPair pair = (IJSONPair) child;
if (pair.getName() != null) {
properties.add(pair.getName());
}
}
child = child.getNextSibling();
}
return properties;
}
Aggregations