use of org.eclipse.wst.json.core.internal.format.JSONFormatUtil in project webtools.sourceediting by eclipse.
the class CleanupProcessorJSON method cleanupModel.
public void cleanupModel(IStructuredModel structuredModel, int start, int length) {
JSONFormatUtil formatUtil = JSONFormatUtil.getInstance();
if (structuredModel instanceof IJSONModel) {
IJSONDocument doc = ((IJSONModel) structuredModel).getDocument();
IJSONSourceFormatter formatter = JSONSourceFormatterFactory.getInstance().getSourceFormatter((INodeNotifier) doc);
StringBuilder buf = formatter.cleanup(doc);
if (buf != null) {
int startOffset = ((IndexedRegion) doc).getStartOffset();
int endOffset = ((IndexedRegion) doc).getEndOffset();
formatUtil.replaceSource(doc.getModel(), startOffset, endOffset - startOffset, buf.toString());
}
}
}
use of org.eclipse.wst.json.core.internal.format.JSONFormatUtil 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