use of org.eclipse.wst.json.core.document.IJSONModel in project webtools.sourceediting by eclipse.
the class TestUtil method loadModel.
public static IJSONModel loadModel(String json) {
IJSONModel model = (IJSONModel) TestUtil.createModel();
IStructuredDocument structuredDocument = model.getStructuredDocument();
// Load JSON Object
structuredDocument.set(json);
return model;
}
use of org.eclipse.wst.json.core.document.IJSONModel in project webtools.sourceediting by eclipse.
the class JSONFormatUtil method collectJSONNodes.
public List collectJSONNodes(IStructuredModel model, int start, int length) {
List nodes = new ArrayList();
IndexedRegion startNode = model.getIndexedRegion(start);
IndexedRegion endNode = model.getIndexedRegion(start + length - 1);
if (startNode == null || endNode == null) {
return nodes;
}
if (model instanceof IJSONModel && startNode instanceof IJSONNode && endNode instanceof IJSONNode) {
// JSON model
IJSONNode ca = getCommonAncestor((IJSONNode) startNode, (IJSONNode) endNode);
if (ca != null) {
for (IJSONNode node = ca.getFirstChild(); node != null && start + length < ((IndexedRegion) node).getStartOffset(); node = node.getNextSibling()) {
if (start < ((IndexedRegion) node).getEndOffset()) {
nodes.add(node);
}
}
}
}
return nodes;
}
use of org.eclipse.wst.json.core.document.IJSONModel in project webtools.sourceediting by eclipse.
the class JFaceNodeContentProvider method getElements.
public Object[] getElements(Object object) {
// The root is usually an instance of an JSONStructuredModel in
// which case we want to extract the document.
Object topNode = object;
if (object instanceof IJSONModel) {
topNode = ((IJSONModel) object).getDocument();
}
IJFaceNodeAdapter adapter = getAdapter(topNode);
if (adapter != null) {
return adapter.getElements(topNode);
}
return new Object[0];
}
use of org.eclipse.wst.json.core.document.IJSONModel in project webtools.sourceediting by eclipse.
the class AbstractJSONSourceFormatter method getLineDelimiter.
String getLineDelimiter(IJSONNode node) {
IJSONModel model = node != null ? node.getOwnerDocument().getModel() : null;
IStructuredDocument structuredDocument = model != null ? model.getStructuredDocument() : null;
return structuredDocument != null ? structuredDocument.getLineDelimiter() : // $NON-NLS-1$
"\n";
}
use of org.eclipse.wst.json.core.document.IJSONModel in project webtools.sourceediting by eclipse.
the class FormatProcessorJSON method formatModel.
@Override
public void formatModel(IStructuredModel structuredModel, int start, int length) {
JSONFormatUtil formatUtil = JSONFormatUtil.getInstance();
if (structuredModel instanceof IJSONModel) {
// BUG102822 take advantage of IDocumentExtension4
IDocumentExtension4 docExt4 = null;
if (structuredModel.getStructuredDocument() instanceof IDocumentExtension4) {
docExt4 = (IDocumentExtension4) structuredModel.getStructuredDocument();
}
DocumentRewriteSession rewriteSession = null;
try {
DocumentRewriteSessionType rewriteType = (length > MAX_SMALL_FORMAT_SIZE) ? DocumentRewriteSessionType.UNRESTRICTED : DocumentRewriteSessionType.UNRESTRICTED_SMALL;
rewriteSession = (docExt4 == null || docExt4.getActiveRewriteSession() != null) ? null : docExt4.startRewriteSession(rewriteType);
IJSONDocument doc = ((IJSONModel) structuredModel).getDocument();
IndexedRegion startRegion = ((IJSONModel) structuredModel).getIndexedRegion(start);
IndexedRegion endRegion = ((IJSONModel) structuredModel).getIndexedRegion(start + length);
if (startRegion != null && endRegion != null) {
start = startRegion.getStartOffset();
int offset;
if (endRegion instanceof IJSONPair) {
offset = endRegion.getEndOffset();
IStructuredDocumentRegion nextRegion = structuredModel.getStructuredDocument().getRegionAtCharacterOffset(offset + 1);
if (nextRegion.getType() == JSONRegionContexts.JSON_COMMA) {
offset = nextRegion.getEndOffset();
}
} else {
offset = endRegion.getEndOffset();
}
int end = offset - start;
IJSONSourceFormatter formatter = JSONSourceFormatterFactory.getInstance().getSourceFormatter(doc);
StringBuilder buf = formatter.format(doc, new Region(start, end));
if (buf != null) {
formatUtil.replaceSource(doc.getModel(), start, end, buf.toString());
}
}
} finally {
// BUG102822 take advantage of IDocumentExtension4
if (docExt4 != null && rewriteSession != null)
docExt4.stopRewriteSession(rewriteSession);
}
}
}
Aggregations