use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.
the class JSONModelParser method insertStartArray.
// ---------------------------- JSON Array
protected void insertStartArray(IJSONArray element) {
if (element == null)
return;
if (this.context == null)
return;
insertNode(element);
JSONArrayImpl newElement = (JSONArrayImpl) element;
String type = newElement.getStartStructuredDocumentRegion().getLastRegion().getType();
// demote siblings
IJSONNode parent = this.context.getParentNode();
if (parent == null)
// error
return;
IJSONNode next = this.context.getNextNode();
demoteNodes(element, element, parent, next);
// update context
IJSONNode firstChild = element.getFirstChild();
if (firstChild != null)
this.context.setCurrentNode(firstChild);
else
this.context.setParentNode(element);
}
use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.
the class JSONModelParser method removeNode.
/**
* removeNode method
*
* @param node
* org.w3c.dom.Node
*/
private void removeNode(IJSONNode node) {
if (node == null)
return;
if (this.context == null)
return;
IJSONNode parent = node.getParentNode();
if (parent == null) {
if (node instanceof IJSONValue) {
parent = node.getParentOrPairNode();
if (parent == null) {
return;
}
}
}
IJSONNode next = node.getNextSibling();
IJSONNode prev = node.getPreviousSibling();
// update context
IJSONNode oldParent = this.context.getParentNode();
if (node == oldParent) {
if (next != null)
this.context.setCurrentNode(next);
else
this.context.setParentNode(parent);
} else {
IJSONNode oldNext = this.context.getNextNode();
if (node == oldNext) {
this.context.setCurrentNode(next);
}
}
parent.removeChild(node);
// demote sibling
if (prev != null && prev.getNodeType() == IJSONNode.OBJECT_NODE) {
JSONObjectImpl newElement = (JSONObjectImpl) prev;
if (!newElement.hasEndTag() && !newElement.isEmptyTag() && newElement.isContainer()) {
// find new parent
for (IJSONNode last = newElement.getLastChild(); last != null; last = last.getLastChild()) {
if (last.getNodeType() != IJSONNode.OBJECT_NODE)
break;
JSONObjectImpl lastElement = (JSONObjectImpl) last;
if (lastElement.hasEndTag() || lastElement.isEmptyTag() || !lastElement.isContainer())
break;
newElement = lastElement;
}
IJSONNode lastChild = newElement.getLastChild();
demoteNodes(prev, newElement, parent, next);
// update context
IJSONNode newNext = null;
if (lastChild != null)
newNext = lastChild.getNextSibling();
else
newNext = newElement.getFirstChild();
if (newNext != null)
this.context.setCurrentNode(newNext);
else
this.context.setParentNode(newElement);
}
}
}
use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.
the class JSONNodeImpl method getNodeAt.
/**
* getNodeAt method
*
* @return org.w3c.dom.Node
* @param offset
* int
*/
IJSONNode getNodeAt(int offset) {
IJSONNode parent = this;
IJSONNode child = getFirstChild();
while (child != null) {
if (child.getEndOffset() == offset) {
return child;
}
if (child.getEndOffset() <= offset) {
child = child.getNextSibling();
continue;
}
if (child.getStartOffset() > offset) {
break;
}
IStructuredDocumentRegion startStructuredDocumentRegion = child.getStartStructuredDocumentRegion();
if (startStructuredDocumentRegion != null) {
if (startStructuredDocumentRegion.getEnd() > offset)
return child;
}
// dig more
parent = child;
// JSONNode like JSONObject or JSONArray
if (parent instanceof JSONPairImpl) {
IJSONValue value = ((JSONPairImpl) parent).getValue();
if (value instanceof JSONObjectImpl || value instanceof JSONArrayImpl) {
parent = value;
}
}
child = parent.getFirstChild();
}
return parent;
}
use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.
the class JSONNodeImpl method getIndex.
/**
*/
public int getIndex() {
IJSONNode parent = getParentNode();
if (parent == null)
// error
return -1;
int index = 0;
for (IJSONNode child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
if (child == this)
return index;
index++;
}
// error
return -1;
}
use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.
the class JSONNodeImpl method getStartOffset.
/**
* getStartOffset method
*
* @return int
*/
@Override
public int getStartOffset() {
if (this.flatNode != null)
return this.flatNode.getStart();
JSONNodeImpl prev = (JSONNodeImpl) getPreviousSibling();
if (prev != null)
return prev.getEndOffset();
IJSONNode parent = getParentNode();
if (parent != null && parent.getNodeType() == IJSONNode.OBJECT_NODE) {
JSONObjectImpl element = (JSONObjectImpl) parent;
if (element.hasStartTag())
return element.getStartEndOffset();
return element.getStartOffset();
}
// final fallback to look into first child
JSONNodeImpl child = (JSONNodeImpl) getFirstChild();
while (child != null) {
IStructuredDocumentRegion childStructuredDocumentRegion = child.getStructuredDocumentRegion();
if (childStructuredDocumentRegion != null)
return childStructuredDocumentRegion.getStart();
child = (JSONNodeImpl) child.getFirstChild();
}
return 0;
}
Aggregations