use of org.eclipse.wst.json.core.document.IJSONValue 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.IJSONValue 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.IJSONValue in project webtools.sourceediting by eclipse.
the class JSONPairImpl method updateValue.
public void updateValue(IJSONValue value) {
IJSONValue oldValue = this.value;
((JSONValueImpl) value).setParentNode(ownerObject);
((JSONValueImpl) value).setOwnerPairNode(this);
this.value = value;
notify(CHANGE, null, oldValue, this.value, getStartOffset());
}
use of org.eclipse.wst.json.core.document.IJSONValue in project webtools.sourceediting by eclipse.
the class JFaceNodeAdapter method getStyledLabelText.
@Override
public StyledString getStyledLabelText(Object element) {
StyledString styledString = new StyledString();
if (element instanceof IJSONNode) {
IJSONNode node = (IJSONNode) element;
switch(node.getNodeType()) {
case IJSONNode.PAIR_NODE:
IJSONPair pair = ((IJSONPair) node);
String name = pair.getName();
if (name != null) {
styledString.append(name);
String value = pair.getSimpleValue();
if (value != null) {
styledString.append(" : ");
Styler styler = fAdapterFactory.getStyler(pair.getValueRegionType());
styledString.append(value, styler);
}
}
break;
case IJSONNode.VALUE_BOOLEAN_NODE:
case IJSONNode.VALUE_NULL_NODE:
case IJSONNode.VALUE_NUMBER_NODE:
case IJSONNode.VALUE_STRING_NODE:
String value = ((IJSONValue) node).getSimpleValue();
if (value != null) {
styledString.append(" : ");
Styler styler = fAdapterFactory.getStyler(((IJSONValue) node).getValueRegionType());
styledString.append(value, styler);
}
break;
}
}
return styledString;
}
use of org.eclipse.wst.json.core.document.IJSONValue in project webtools.sourceediting by eclipse.
the class AbstractJSONCompletionProposalComputer method computeObjectKeyProposals.
private ContentAssistRequest computeObjectKeyProposals(String matchString, ITextRegion completionRegion, IJSONNode nodeAtOffset, IJSONNode node, CompletionProposalInvocationContext context) {
int documentPosition = context.getInvocationOffset();
ContentAssistRequest contentAssistRequest = null;
IStructuredDocumentRegion sdRegion = getStructuredDocumentRegion(documentPosition);
int replaceLength = 0;
int begin = documentPosition;
if (completionRegion.getType() == JSONRegionContexts.JSON_OBJECT_KEY || completionRegion.getType() == JSONRegionContexts.JSON_UNKNOWN) {
replaceLength = completionRegion.getTextLength();
// value region not the entire container
if (completionRegion instanceof ITextRegionContainer) {
ITextRegion openRegion = ((ITextRegionContainer) completionRegion).getFirstRegion();
ITextRegion closeRegion = ((ITextRegionContainer) completionRegion).getLastRegion();
if (openRegion.getType() != closeRegion.getType()) {
replaceLength = openRegion.getTextLength();
}
}
begin = sdRegion.getStartOffset(completionRegion);
}
if (isPairValue(context, nodeAtOffset)) {
IJSONPair pair = (IJSONPair) nodeAtOffset;
IJSONValue value = pair.getValue();
if (value != null) {
try {
begin = value.getStartOffset();
String valueText = getNodeText(value);
valueText = valueText.trim();
replaceLength = valueText.length();
if (valueText.startsWith(QUOTE)) {
begin = begin + 1;
replaceLength = replaceLength - 1;
}
if (valueText.endsWith(QUOTE)) {
replaceLength = replaceLength - 1;
}
} catch (BadLocationException e) {
// ignore
}
}
} else if (nodeAtOffset instanceof IJSONPair) {
IJSONPair pair = (IJSONPair) nodeAtOffset;
try {
begin = pair.getStartOffset();
String text = getNodeText(pair);
text = text.trim();
replaceLength = pair.getName().length();
if (text.startsWith(QUOTE)) {
begin = begin + 1;
}
} catch (BadLocationException e) {
// ignore
}
}
contentAssistRequest = new ContentAssistRequest(nodeAtOffset, node.getParentNode(), sdRegion, completionRegion, begin, replaceLength, matchString);
addObjectKeyProposals(contentAssistRequest, context);
return contentAssistRequest;
}
Aggregations