Search in sources :

Example 6 with IJSONNode

use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.

the class JSONContentOutlineConfiguration method getFilteredNode.

private Object getFilteredNode(Object o) {
    IJSONNode node = null;
    if (o instanceof IJSONNode) {
        node = (IJSONNode) o;
        if (node.getOwnerPairNode() != null) {
            IJSONPair owner = node.getOwnerPairNode();
            IJSONValue value = owner.getValue();
            if (!(value instanceof IJSONArray)) {
                return node.getOwnerPairNode();
            }
        }
    /*
			 * short nodeType = node.getNodeType(); if (node instanceof
			 * IJSONValue) { while (node != null && !(node instanceof
			 * IJSONStyleDeclItem)) { node = node.getParentNode(); } } else if
			 * (nodeType == IJSONNode.STYLEDECLARATION_NODE) { node =
			 * node.getParentNode(); } else if (nodeType ==
			 * IJSONNode.MEDIALIST_NODE) { node = node.getParentNode(); }
			 */
    }
    return node;
}
Also used : IJSONArray(org.eclipse.wst.json.core.document.IJSONArray) IJSONPair(org.eclipse.wst.json.core.document.IJSONPair) IJSONValue(org.eclipse.wst.json.core.document.IJSONValue) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 7 with IJSONNode

use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.

the class JSONFoldingStrategy method calcNewFoldPosition.

/**
 * @see org.eclipse.wst.sse.ui.internal.projection.AbstractFoldingStrategy#calcNewFoldPosition(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)
 */
protected Position calcNewFoldPosition(IndexedRegion indexedRegion) {
    Position retPos = null;
    // only want to fold regions of the valid type and with a valid range
    if (indexedRegion.getStartOffset() >= 0 && indexedRegion.getLength() >= 0) {
        IJSONNode node = (IJSONNode) indexedRegion;
        IStructuredDocumentRegion startRegion = node.getStartStructuredDocumentRegion();
        IStructuredDocumentRegion endRegion = node.getEndStructuredDocumentRegion();
        // don't fold it
        if (startRegion != null && endRegion != null) {
            if (startRegion.getEndOffset() == endRegion.getStartOffset())
                return null;
            if (endRegion.getEndOffset() >= startRegion.getStartOffset() && endRegion.getEndOffset() <= getDocument().getLength())
                retPos = new JSONObjectFoldingPosition(startRegion, endRegion);
        }
    // else if(startRegion != null && indexedRegion instanceof
    // CommentImpl) {
    // retPos = new JSONCommentFoldingPosition(startRegion);
    // }
    }
    return retPos;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) Position(org.eclipse.jface.text.Position) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 8 with IJSONNode

use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.

the class JSONSourceFormatterFactory method getSourceFormatter.

public IJSONSourceFormatter getSourceFormatter(INodeNotifier target) {
    IJSONNode node = (IJSONNode) target;
    short type = node.getNodeType();
    switch(type) {
        case IJSONNode.DOCUMENT_NODE:
            return JSONDocumentFormatter.getInstance();
        case IJSONNode.OBJECT_NODE:
            return JSONObjectFormatter.getInstance();
        case IJSONNode.ARRAY_NODE:
            return JSONArrayFormatter.getInstance();
        case IJSONNode.PAIR_NODE:
            return JSONPairFormatter.getInstance();
        default:
            return UnknownRuleFormatter.getInstance();
    }
}
Also used : IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 9 with IJSONNode

use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.

the class JSONModelUpdater method insertStructuredDocumentRegion.

/**
 */
private void insertStructuredDocumentRegion(IStructuredDocumentRegion newStructuredDocumentRegion) {
    if (newStructuredDocumentRegion == null)
        // error
        return;
    if (this.parentNode == null)
        // error
        return;
    int newOffset = newStructuredDocumentRegion.getStart();
    int newEnd = newStructuredDocumentRegion.getEnd();
    boolean isEndTag = false;
    // find owner node
    JSONNodeImpl ownerNode = null;
    while (this.parentNode != null) {
        if (this.nextNode != null) {
            IStructuredDocumentRegion nextStructuredDocumentRegion = this.nextNode.getStructuredDocumentRegion();
            if (nextStructuredDocumentRegion != null) {
                int nextOffset = nextStructuredDocumentRegion.getStart();
                if (nextOffset == newOffset) {
                    // found
                    ownerNode = this.nextNode;
                    break;
                }
            // if (this.nextNode.getNodeType() == Node.TEXT_NODE) {
            // int nextEnd = nextStructuredDocumentRegion.getEnd();
            // if (nextOffset < newEnd && nextEnd > newOffset) {
            // ownerNode = this.nextNode;
            // break;
            // }
            // }
            }
            IJSONNode child = this.nextNode.getFirstChild();
            if (child != null) {
                this.parentNode = this.nextNode;
                this.nextNode = (JSONNodeImpl) child;
                continue;
            }
            if (this.nextNode.getNodeType() == IJSONNode.OBJECT_NODE) {
                this.parentNode = this.nextNode;
                this.nextNode = null;
                continue;
            }
            this.nextNode = (JSONNodeImpl) this.nextNode.getNextSibling();
            if (this.nextNode != null)
                continue;
        }
        if (this.parentNode.getNodeType() == IJSONNode.OBJECT_NODE) {
            IJSONObject element = (IJSONObject) this.parentNode;
            IStructuredDocumentRegion endStructuredDocumentRegion = element.getEndStructuredDocumentRegion();
            if (endStructuredDocumentRegion != null) {
                int endOffset = endStructuredDocumentRegion.getStart();
                if (endOffset == newOffset) {
                    // found
                    ownerNode = this.parentNode;
                    isEndTag = true;
                    break;
                }
            }
        }
        this.nextNode = (JSONNodeImpl) this.parentNode.getNextSibling();
        this.parentNode = (JSONNodeImpl) this.parentNode.getParentNode();
    }
    if (ownerNode == null)
        throw new StructuredDocumentRegionManagementException();
    short nodeType = ownerNode.getNodeType();
    if (nodeType == IJSONNode.OBJECT_NODE) {
        JSONObjectImpl element = (JSONObjectImpl) ownerNode;
        if (isEndTag) {
            element.setEndStructuredDocumentRegion(newStructuredDocumentRegion);
        } else {
            element.setStartStructuredDocumentRegion(newStructuredDocumentRegion);
            updateAttrRegions(element, newStructuredDocumentRegion);
        }
    } else
        // if (nodeType == Node.TEXT_NODE) {
        // TextImpl text = (TextImpl) ownerNode;
        // IStructuredDocumentRegion oldStructuredDocumentRegion = text
        // .getStructuredDocumentRegion();
        // if (oldStructuredDocumentRegion == null) {
        // throw new StructuredDocumentRegionManagementException();
        // }
        // int oldOffset = oldStructuredDocumentRegion.getStart();
        // int oldEnd = oldStructuredDocumentRegion.getEnd();
        // if (oldOffset == newOffset && oldEnd == newEnd) {
        // text.setStructuredDocumentRegion(newStructuredDocumentRegion);
        // return;
        // }
        // 
        // if (oldStructuredDocumentRegion instanceof StructuredDocumentRegionProxy) {
        // StructuredDocumentRegionProxy proxy = (StructuredDocumentRegionProxy) oldStructuredDocumentRegion;
        // if (oldEnd > newEnd) {
        // StructuredDocumentRegionContainer container = new StructuredDocumentRegionContainer();
        // if (oldOffset == newOffset) {
        // container
        // .appendStructuredDocumentRegion(newStructuredDocumentRegion);
        // } else {
        // StructuredDocumentRegionProxy newProxy = new StructuredDocumentRegionProxy();
        // newProxy.setOffset(oldOffset);
        // newProxy.setLength(newEnd - oldOffset);
        // newProxy.setStructuredDocumentRegion(newStructuredDocumentRegion);
        // container.appendStructuredDocumentRegion(newProxy);
        // }
        // proxy.setOffset(newEnd);
        // proxy.setLength(oldEnd - newEnd);
        // container.appendStructuredDocumentRegion(proxy);
        // text.setStructuredDocumentRegion(container);
        // } else {
        // proxy.setStructuredDocumentRegion(newStructuredDocumentRegion);
        // 
        // if (oldEnd < newEnd) { // to be shared
        // this.nextNode = (JSONNodeImpl) text.getNextSibling();
        // insertStructuredDocumentRegion(newStructuredDocumentRegion);
        // }
        // }
        // return;
        // }
        // 
        // if (oldStructuredDocumentRegion instanceof StructuredDocumentRegionContainer) {
        // StructuredDocumentRegionContainer container = (StructuredDocumentRegionContainer) oldStructuredDocumentRegion;
        // int count = container.getStructuredDocumentRegionCount();
        // for (int i = 0; i < count; i++) {
        // IStructuredDocumentRegion content = container
        // .getStructuredDocumentRegion(i);
        // if (content == null)
        // continue; // error
        // int offset = content.getStart();
        // int end = content.getEnd();
        // if (end <= newOffset)
        // continue;
        // if (offset == newOffset && end == newEnd) {
        // container.replaceStructuredDocumentRegion(
        // newStructuredDocumentRegion, i);
        // return;
        // }
        // 
        // if (content instanceof StructuredDocumentRegionProxy) {
        // StructuredDocumentRegionProxy proxy = (StructuredDocumentRegionProxy) content;
        // if (end > newEnd) {
        // if (offset == newOffset) {
        // container.insertStructuredDocumentRegion(
        // newStructuredDocumentRegion, i);
        // } else {
        // StructuredDocumentRegionProxy newProxy = new StructuredDocumentRegionProxy();
        // newProxy.setOffset(offset);
        // newProxy.setLength(newEnd - offset);
        // newProxy.setStructuredDocumentRegion(newStructuredDocumentRegion);
        // container.insertStructuredDocumentRegion(
        // newProxy, i);
        // }
        // proxy.setOffset(newEnd);
        // proxy.setLength(end - newEnd);
        // return;
        // } else {
        // proxy.setStructuredDocumentRegion(newStructuredDocumentRegion);
        // if (end == newEnd)
        // return;
        // }
        // }
        // }
        // 
        // if (oldEnd < newEnd) { // to be shared
        // this.nextNode = (JSONNodeImpl) text.getNextSibling();
        // insertStructuredDocumentRegion(newStructuredDocumentRegion);
        // }
        // return;
        // } else {
        // throw new StructuredDocumentRegionManagementException();
        // }
        // } else {
        ownerNode.setStructuredDocumentRegion(newStructuredDocumentRegion);
// }
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IJSONObject(org.eclipse.wst.json.core.document.IJSONObject) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 10 with IJSONNode

use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.

the class JSONModelUpdater method changeStructuredDocumentRegion.

private void changeStructuredDocumentRegion(IStructuredDocumentRegion oldStructuredDocumentRegion) {
    if (oldStructuredDocumentRegion == null)
        // error
        return;
    if (this.parentNode == null)
        // error
        return;
    int oldOffset = oldStructuredDocumentRegion.getStart();
    int oldEnd = oldStructuredDocumentRegion.getEnd();
    boolean isEndTag = false;
    // find owner node
    JSONNodeImpl ownerNode = null;
    while (this.parentNode != null) {
        if (this.nextNode != null) {
            IStructuredDocumentRegion nextStructuredDocumentRegion = this.nextNode.getStructuredDocumentRegion();
            if (nextStructuredDocumentRegion != null) {
                if (nextStructuredDocumentRegion == oldStructuredDocumentRegion) {
                    ownerNode = this.nextNode;
                    break;
                }
                int nextOffset = nextStructuredDocumentRegion.getStart();
                if (nextOffset == oldOffset) {
                    // found
                    ownerNode = this.nextNode;
                    break;
                }
            // if (this.nextNode.getNodeType() == Node.TEXT_NODE) {
            // TextImpl text = (TextImpl) this.nextNode;
            // if (text.hasStructuredDocumentRegion(oldStructuredDocumentRegion)) {
            // ownerNode = this.nextNode;
            // break;
            // }
            // int nextEnd = nextStructuredDocumentRegion.getEnd();
            // if (nextOffset < oldEnd && nextEnd > oldOffset) {
            // ownerNode = this.nextNode;
            // break;
            // }
            // }
            }
            IJSONNode child = this.nextNode.getFirstChild();
            if (child != null) {
                this.parentNode = this.nextNode;
                this.nextNode = (JSONNodeImpl) child;
                continue;
            }
            if (this.nextNode.getNodeType() == IJSONNode.OBJECT_NODE) {
                this.parentNode = this.nextNode;
                this.nextNode = null;
                continue;
            }
            this.nextNode = (JSONNodeImpl) this.nextNode.getNextSibling();
            if (this.nextNode != null)
                continue;
        }
        if (this.parentNode.getNodeType() == IJSONNode.OBJECT_NODE) {
            IJSONObject element = (IJSONObject) this.parentNode;
            IStructuredDocumentRegion endStructuredDocumentRegion = element.getEndStructuredDocumentRegion();
            if (endStructuredDocumentRegion != null) {
                if (endStructuredDocumentRegion == oldStructuredDocumentRegion) {
                    ownerNode = this.parentNode;
                    isEndTag = true;
                    break;
                }
                int endOffset = endStructuredDocumentRegion.getStart();
                if (endOffset == oldOffset) {
                    // found
                    ownerNode = this.parentNode;
                    isEndTag = true;
                    break;
                }
            }
        }
        this.nextNode = (JSONNodeImpl) this.parentNode.getNextSibling();
        this.parentNode = (JSONNodeImpl) this.parentNode.getParentNode();
    }
    if (ownerNode == null)
        throw new StructuredDocumentRegionManagementException();
    short nodeType = ownerNode.getNodeType();
    if (nodeType == IJSONNode.OBJECT_NODE) {
        JSONObjectImpl element = (JSONObjectImpl) ownerNode;
        if (isEndTag) {
            element.setEndStructuredDocumentRegion(oldStructuredDocumentRegion);
        } else {
            element.setStartStructuredDocumentRegion(oldStructuredDocumentRegion);
            updateAttrRegions(element, oldStructuredDocumentRegion);
        }
    } else
        /*if (nodeType == Node.TEXT_NODE) {
			TextImpl text = (TextImpl) ownerNode;

			IStructuredDocumentRegion flatNode = text
					.getStructuredDocumentRegion();
			if (flatNode == oldStructuredDocumentRegion) {
				int newOffset = oldOffset;
				int newEnd = oldEnd;
				if (oldOffset == this.gapOffset) {
					newOffset += this.diff;
				} else {
					newEnd = this.gapOffset;
				}
				int newLength = newEnd - newOffset;
				IStructuredDocumentRegion newStructuredDocumentRegion = new StructuredDocumentRegionProxy(
						newOffset, newLength, oldStructuredDocumentRegion);
				text.setStructuredDocumentRegion(newStructuredDocumentRegion);

				if (oldEnd > newEnd) {
					this.nextNode = (JSONNodeImpl) text.getNextSibling();
					changeStructuredDocumentRegion(oldStructuredDocumentRegion);
				}
				return;
			}

			if (flatNode instanceof StructuredDocumentRegionProxy) {
				StructuredDocumentRegionProxy proxy = (StructuredDocumentRegionProxy) flatNode;
				int offset = proxy.getOffset();
				int end = offset + proxy.getLength();
				if (proxy.getStructuredDocumentRegion() == null) {
					if (offset == oldOffset && end == oldEnd) {
						text.setStructuredDocumentRegion(oldStructuredDocumentRegion);
					} else {
						if (end > oldEnd) {
							StructuredDocumentRegionContainer container = new StructuredDocumentRegionContainer();
							container
									.appendStructuredDocumentRegion(oldStructuredDocumentRegion);
							proxy.setOffset(oldEnd);
							proxy.setLength(end - oldEnd);
							container.appendStructuredDocumentRegion(proxy);
							text.setStructuredDocumentRegion(container);
						} else {
							proxy.setStructuredDocumentRegion(oldStructuredDocumentRegion);

							if (end < oldEnd) { // to be shared
								this.nextNode = (JSONNodeImpl) text
										.getNextSibling();
								changeStructuredDocumentRegion(oldStructuredDocumentRegion);
							}
						}
					}
					return;
				}

				if (offset >= this.gapOffset) {
					proxy.setOffset(offset + this.diff);
					end += this.diff;
				}
				if (end < oldEnd) { // to be shared
					this.nextNode = (JSONNodeImpl) text.getNextSibling();
					changeStructuredDocumentRegion(oldStructuredDocumentRegion);
					return;
				}
			} else if (flatNode instanceof StructuredDocumentRegionContainer) {
				StructuredDocumentRegionContainer container = (StructuredDocumentRegionContainer) flatNode;
				int count = container.getStructuredDocumentRegionCount();
				for (int i = 0; i < count; i++) {
					IStructuredDocumentRegion content = container
							.getStructuredDocumentRegion(i);
					if (content == null)
						continue; // error
					if (content == oldStructuredDocumentRegion) {
						int newOffset = oldOffset;
						int newEnd = oldEnd;
						if (oldOffset == this.gapOffset) {
							newOffset += this.diff;
						} else {
							newEnd = this.gapOffset;
						}
						int newLength = newEnd - newOffset;
						IStructuredDocumentRegion newStructuredDocumentRegion = new StructuredDocumentRegionProxy(
								newOffset, newLength,
								oldStructuredDocumentRegion);
						container.replaceStructuredDocumentRegion(
								newStructuredDocumentRegion, i);

						if (oldEnd > newEnd) { // to be shared
							this.nextNode = (JSONNodeImpl) text.getNextSibling();
							changeStructuredDocumentRegion(oldStructuredDocumentRegion);
						}
						return;
					}

					if (content instanceof StructuredDocumentRegionProxy) {
						StructuredDocumentRegionProxy proxy = (StructuredDocumentRegionProxy) content;
						int offset = proxy.getOffset();
						int end = offset + proxy.getLength();
						if (end <= oldOffset)
							continue;
						if (proxy.getStructuredDocumentRegion() == null) {
							if (offset == oldOffset && end == oldEnd) {
								container.replaceStructuredDocumentRegion(
										oldStructuredDocumentRegion, i);
							} else {
								if (end > oldEnd) {
									container.insertStructuredDocumentRegion(
											oldStructuredDocumentRegion, i);
									proxy.setOffset(oldEnd);
									proxy.setLength(end - oldEnd);
								} else {
									proxy.setStructuredDocumentRegion(oldStructuredDocumentRegion);

									if (end < oldEnd) { // to be shared
										this.nextNode = (JSONNodeImpl) text
												.getNextSibling();
										changeStructuredDocumentRegion(oldStructuredDocumentRegion);
									}
								}
							}
							return;
						}

						if (offset >= this.gapOffset) {
							proxy.setOffset(offset + this.diff);
							end += this.diff;
						}
						if (end < oldEnd) { // to be shared
							this.nextNode = (JSONNodeImpl) text.getNextSibling();
							changeStructuredDocumentRegion(oldStructuredDocumentRegion);
							return;
						}
					}
				}
			} else {
				throw new StructuredDocumentRegionManagementException();
			}
		} else {*/
        ownerNode.setStructuredDocumentRegion(oldStructuredDocumentRegion);
// }
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IJSONObject(org.eclipse.wst.json.core.document.IJSONObject) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Aggregations

IJSONNode (org.eclipse.wst.json.core.document.IJSONNode)56 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)10 IJSONObject (org.eclipse.wst.json.core.document.IJSONObject)9 IJSONPair (org.eclipse.wst.json.core.document.IJSONPair)9 IJSONValue (org.eclipse.wst.json.core.document.IJSONValue)8 IJSONArray (org.eclipse.wst.json.core.document.IJSONArray)4 IJSONDocument (org.eclipse.wst.json.core.document.IJSONDocument)4 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 Iterator (java.util.Iterator)3 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)3 IJSONSchemaProperty (org.eclipse.json.schema.IJSONSchemaProperty)2 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)2 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 List (java.util.List)1