use of org.eclipse.wst.json.core.document.IJSONNode 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.IJSONNode in project webtools.sourceediting by eclipse.
the class JSONModelUpdater method changeStartTag.
/**
* This is a fallback method to regenerate the start tag.
*/
void changeStartTag(IJSONObject element) {
if (element == null)
return;
JSONObjectImpl impl = (JSONObjectImpl) element;
if (!impl.hasStartTag() && !impl.hasEndTag()) {
// need to generate the start and the end tags
IJSONNode parent = element.getParentNode();
if (parent != null) {
replaceChild(parent, element, element);
return;
}
// else error
}
String source = this.generator.generateStartTag(element);
if (source == null)
return;
int length = source.length();
if (length == 0)
return;
int offset = impl.getStartOffset();
int start = offset;
int end = offset;
if (impl.hasStartTag()) {
end = impl.getStartEndOffset();
this.gapStructuredDocumentRegion = impl.getStartStructuredDocumentRegion();
}
impl.setStartStructuredDocumentRegion(new StructuredDocumentRegionProxy(offset, length));
replaceSource(source, start, end);
}
use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.
the class JSONModelUpdater method removeStructuredDocumentRegion.
private void removeStructuredDocumentRegion(IStructuredDocumentRegion oldStructuredDocumentRegion) {
if (oldStructuredDocumentRegion == null)
// error
return;
if (this.parentNode == null)
// error
return;
int gapEnd = this.gapOffset + this.gapLength;
int oldOffset = oldStructuredDocumentRegion.getStart();
int oldEnd = oldStructuredDocumentRegion.getEnd();
if (oldOffset >= this.gapOffset && oldEnd <= gapEnd)
// do nothing
return;
int oldLength = oldEnd - oldOffset;
if (oldOffset >= gapEnd)
oldOffset += this.diff;
// find owner node
JSONNodeImpl ownerNode = null;
JSONObjectImpl ownerEndTag = null;
// TextImpl ownerText = null;
while (this.parentNode != null) {
if (this.nextNode != null) {
if (this.nextNode.getStructuredDocumentRegion() == oldStructuredDocumentRegion) {
ownerNode = this.nextNode;
break;
}
// if (this.nextNode.getNodeType() == Node.TEXT_NODE) {
// TextImpl text = (TextImpl) this.nextNode;
// if (text.hasStructuredDocumentRegion(oldStructuredDocumentRegion)) {
// ownerNode = this.nextNode;
// ownerText = text;
// 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) {
JSONObjectImpl element = (JSONObjectImpl) this.parentNode;
if (element.getEndStructuredDocumentRegion() == oldStructuredDocumentRegion) {
ownerNode = this.parentNode;
ownerEndTag = element;
break;
}
}
this.nextNode = (JSONNodeImpl) this.parentNode.getNextSibling();
this.parentNode = (JSONNodeImpl) this.parentNode.getParentNode();
}
if (ownerNode == null)
throw new StructuredDocumentRegionManagementException();
/*if (ownerText != null) {
IStructuredDocumentRegion flatNode = ownerText
.getStructuredDocumentRegion();
if (flatNode == oldStructuredDocumentRegion) {
IStructuredDocumentRegion newStructuredDocumentRegion = new StructuredDocumentRegionProxy(
oldOffset, oldLength);
ownerText
.setStructuredDocumentRegion(newStructuredDocumentRegion);
return;
}
if (flatNode instanceof StructuredDocumentRegionProxy) {
StructuredDocumentRegionProxy proxy = (StructuredDocumentRegionProxy) flatNode;
if (proxy.getStructuredDocumentRegion() != oldStructuredDocumentRegion) {
throw new StructuredDocumentRegionManagementException();
}
int offset = proxy.getOffset();
int end = offset + proxy.getLength();
if (offset >= this.gapOffset) {
proxy.setOffset(offset + this.diff);
}
proxy.setStructuredDocumentRegion(null);
if (end < oldEnd && (end < this.gapOffset || oldEnd > gapEnd)) { // has
// shared
removeStructuredDocumentRegion(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) {
IStructuredDocumentRegion newStructuredDocumentRegion = new StructuredDocumentRegionProxy(
oldOffset, oldLength);
container.replaceStructuredDocumentRegion(
newStructuredDocumentRegion, i);
return;
}
if (content instanceof StructuredDocumentRegionProxy) {
StructuredDocumentRegionProxy proxy = (StructuredDocumentRegionProxy) content;
if (proxy.getStructuredDocumentRegion() == oldStructuredDocumentRegion) {
int offset = proxy.getOffset();
int end = offset + proxy.getLength();
if (offset >= this.gapOffset) {
proxy.setOffset(offset + this.diff);
}
proxy.setStructuredDocumentRegion(null);
if (end < oldEnd
&& (end < this.gapOffset || oldEnd > gapEnd)) { // has
// shared
removeStructuredDocumentRegion(oldStructuredDocumentRegion);
return;
}
}
}
}
} else {
throw new StructuredDocumentRegionManagementException();
}
} else {*/
IStructuredDocumentRegion newStructuredDocumentRegion = new StructuredDocumentRegionProxy(oldOffset, oldLength);
if (ownerEndTag != null) {
ownerEndTag.setEndStructuredDocumentRegion(newStructuredDocumentRegion);
} else {
ownerNode.setStructuredDocumentRegion(newStructuredDocumentRegion);
}
// }
}
use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.
the class AbstractJSONSourceFormatter method formatChildren.
protected void formatChildren(IJSONNode node, StringBuilder source) {
IJSONNode child = node.getFirstChild();
IJSONNode last = null;
while (child != null) {
// append child
IJSONSourceFormatter formatter = (IJSONSourceFormatter) ((INodeNotifier) child).getAdapterFor(IJSONSourceFormatter.class);
if (formatter == null) {
formatter = JSONSourceFormatterFactory.getInstance().getSourceFormatter(child);
}
StringBuilder childSource = ((AbstractJSONSourceFormatter) formatter).formatProc(child);
source.append(childSource);
last = child;
child = child.getNextSibling();
}
}
use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.
the class AbstractJSONSourceFormatter method formatChildren.
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();
}
}
Aggregations