use of org.eclipse.wst.json.core.document.IJSONDocument 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.document.IJSONDocument in project webtools.sourceediting by eclipse.
the class DelegatingSourceValidator method validate.
/**
* Calls a delegate validator getting and updates it's list of
* ValidationMessages with a good squiggle offset and length.
*
* @param helper
* loads an object.
* @param reporter
* Is an instance of an IReporter interface, which is used for
* interaction with the user.
*/
public void validate(IValidationContext helper, IReporter reporter) throws ValidationException {
String[] delta = helper.getURIs();
if (delta.length > 0) {
// get the file, model and document:
IFile file = getFile(delta[0]);
IJSONModel jsonModel = null;
if (file != null)
jsonModel = getModelForResource(file);
// some problem occurred, abort
if (jsonModel == null)
return;
try {
IJSONDocument document = jsonModel.getDocument();
// store the text in a byte array; make a full copy to ease
// any threading problems
byte[] byteArray;
try {
byteArray = jsonModel.getStructuredDocument().get().getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
// Not likely to happen
byteArray = jsonModel.getStructuredDocument().get().getBytes();
}
if (isDelegateValidatorEnabled(file)) {
IValidator validator = getDelegateValidator();
if (validator != null) {
// Validate the file:
IValidationContext vHelper = new MyHelper(new ByteArrayInputStream(byteArray), file);
MyReporter vReporter = new MyReporter();
if (validator instanceof IValidatorJob) {
((IValidatorJob) validator).validateInJob(vHelper, vReporter);
} else {
validator.validate(vHelper, vReporter);
}
List messages = vReporter.list;
// set the offset and length
updateValidationMessages(messages, document, reporter);
}
}
} finally {
if (jsonModel != null) {
jsonModel.releaseFromRead();
}
}
}
}
use of org.eclipse.wst.json.core.document.IJSONDocument in project webtools.sourceediting by eclipse.
the class AbstractJSONSourceFormatter method appendDelimBefore.
protected void appendDelimBefore(IJSONNode node, CompoundRegion toAppend, StringBuilder source) {
if (node == null || source == null)
return;
if (isCleanup() && !getCleanupStrategy(node).isFormatSource())
// for not formatting case on cleanup action
return;
String delim = getLineDelimiter(node);
boolean needIndent = !(node instanceof IJSONDocument);
if (toAppend == null) {
source.append(delim);
source.append(getIndent(node));
if (needIndent)
source.append(getIndentString());
} else {
String type = toAppend.getType();
if (type == JSONRegionContexts.JSON_COMMENT) {
RegionIterator it = new RegionIterator(toAppend.getDocumentRegion(), toAppend.getTextRegion());
it.prev();
ITextRegion prev = it.prev();
int[] result = null;
if (prev == null || (prev.getType() == JSONRegionContexts.WHITE_SPACE && (result = TextUtilities.indexOf(DefaultLineTracker.DELIMITERS, it.getStructuredDocumentRegion().getText(prev), 0))[0] >= 0)) {
// Collapse to one empty line if there's more than one.
if (result != null) {
int offset = result[0] + DefaultLineTracker.DELIMITERS[result[1]].length();
if (offset < it.getStructuredDocumentRegion().getText(prev).length()) {
if (TextUtilities.indexOf(DefaultLineTracker.DELIMITERS, it.getStructuredDocumentRegion().getText(prev), offset)[0] >= 0) {
source.append(delim);
}
}
source.append(delim);
source.append(getIndent(node));
if (needIndent)
source.append(getIndentString());
}
} else if (prev.getType() == JSONRegionContexts.JSON_COMMENT) {
String fullText = toAppend.getDocumentRegion().getFullText(prev);
String trimmedText = toAppend.getDocumentRegion().getText(prev);
// $NON-NLS-1$
String whiteSpaces = "";
if (fullText != null && trimmedText != null)
whiteSpaces = fullText.substring(trimmedText.length());
int[] delimiterFound = TextUtilities.indexOf(DefaultLineTracker.DELIMITERS, whiteSpaces, 0);
if (delimiterFound[0] != -1) {
source.append(delim);
} else {
appendSpaceBefore(node, toAppend.getText(), source);
/*
* If two comments can't be adjusted in one
* line(combined length exceeds line width), a tab is
* also appended along with next line delimiter , we
* need to remove that.
*/
if (source.toString().endsWith(getIndentString())) {
source.delete((source.length() - getIndentString().length()), source.length());
}
}
} else {
appendSpaceBefore(node, toAppend.getText(), source);
}
} else if (type == JSONRegionContexts.JSON_COMMA) {
RegionIterator it = new RegionIterator(toAppend.getDocumentRegion(), toAppend.getTextRegion());
it.prev();
ITextRegion prev = it.prev();
Preferences preferences = JSONCorePlugin.getDefault().getPluginPreferences();
if (prev.getType() == JSONRegionContexts.WHITE_SPACE && TextUtilities.indexOf(DefaultLineTracker.DELIMITERS, it.getStructuredDocumentRegion().getText(prev), 0)[0] >= 0) {
source.append(delim);
source.append(getIndent(node));
if (needIndent)
source.append(getIndentString());
} else if (preferences.getInt(JSONCorePreferenceNames.LINE_WIDTH) > 0 && (!preferences.getBoolean(JSONCorePreferenceNames.WRAPPING_PROHIBIT_WRAP_ON_ATTR) || node.getOwnerDocument().getNodeType() != IJSONNode.PAIR_NODE)) {
int length = getLastLineLength(node, source);
int append = 1;
if (length + append > preferences.getInt(JSONCorePreferenceNames.LINE_WIDTH)) {
source.append(getLineDelimiter(node));
source.append(getIndent(node));
if (needIndent)
source.append(getIndentString());
}
}
} else if (type == JSONRegionContexts.JSON_OBJECT_OPEN || type == JSONRegionContexts.JSON_OBJECT_CLOSE || type == JSONRegionContexts.JSON_ARRAY_OPEN || type == JSONRegionContexts.JSON_ARRAY_CLOSE) {
source.append(delim);
source.append(getIndent(node));
} else {
source.append(delim);
source.append(getIndent(node));
if (needIndent)
source.append(getIndentString());
}
}
}
use of org.eclipse.wst.json.core.document.IJSONDocument in project webtools.sourceediting by eclipse.
the class AbstractJSONSourceFormatter method appendSpaceBefore.
protected void appendSpaceBefore(IJSONNode node, CompoundRegion toAppend, StringBuilder source) {
if (node == null || toAppend == null || source == null)
return;
if (isCleanup() && !getCleanupStrategy(node).isFormatSource())
// for not formatting case on cleanup action
return;
String type = toAppend.getType();
Preferences preferences = JSONCorePlugin.getDefault().getPluginPreferences();
boolean needIndent = !(node instanceof IJSONDocument);
/*if (type == JSONRegionContexts.JSON_COMMENT) {
// check whether previous region is 'S' and has CR-LF
String delim = getLineDelimiter(node);
RegionIterator it = new RegionIterator(
toAppend.getDocumentRegion(), toAppend.getTextRegion());
it.prev();
ITextRegion prev = it.prev();
// bug390904
if (prev.getType() == JSONRegionContexts.JSON_LBRACE
&& TextUtilities
.indexOf(DefaultLineTracker.DELIMITERS,
it.getStructuredDocumentRegion()
.getFullText(prev), 0)[0] > 0) {
source.append(delim);
source.append(getIndent(node));
source.append(getIndentString());
} else if (prev.getType() == JSONRegionContexts.WHITE_SPACE
&& TextUtilities.indexOf(DefaultLineTracker.DELIMITERS, it
.getStructuredDocumentRegion().getText(prev), 0)[0] >= 0) {
source.append(delim);
source.append(getIndent(node));
if (needIndent)
source.append(getIndentString());
} else {
appendSpaceBefore(node, toAppend.getText(), source);
}
}*/
if ((type == JSONRegionContexts.JSON_OBJECT_OPEN || type == JSONRegionContexts.JSON_ARRAY_OPEN) && preferences.getBoolean(JSONCorePreferenceNames.WRAPPING_NEWLINE_ON_OPEN_BRACE)) {
String delim = getLineDelimiter(node);
source.append(delim);
source.append(getIndent(node));
// } else if (type == JSONRegionContexts.JSON_CURLY_BRACE_CLOSE) {
// } else if (type == JSONRegionContexts.JSON_INCLUDES || type ==
// JSONRegionContexts.JSON_DASHMATCH) {
/*} else if (type == JSONRegionContexts.JSON_DECLARATION_SEPARATOR
&& node instanceof IJSONStyleDeclItem) {
int n = preferences
.getInt(JSONCorePreferenceNames.FORMAT_PROP_PRE_DELIM);
// no delimiter case
while (n-- > 0)
source.append(" ");//$NON-NLS-1$
} else if (type == JSONRegionContexts.JSON_DECLARATION_VALUE_OPERATOR
|| type == JSONRegionContexts.JSON_DECLARATION_VALUE_PARENTHESIS_CLOSE) {
if (preferences.getInt(JSONCorePreferenceNames.LINE_WIDTH) > 0
&& (!preferences
.getBoolean(JSONCorePreferenceNames.WRAPPING_PROHIBIT_WRAP_ON_ATTR) || node
.getOwnerDocument().getNodeType() != IJSONNode.STYLEDECLARATION_NODE)) {
int length = getLastLineLength(node, source);
int append = 1;
if (length + append > preferences
.getInt(JSONCorePreferenceNames.LINE_WIDTH)) {
source.append(getLineDelimiter(node));
source.append(getIndent(node));
if (needIndent)
source.append(getIndentString());
}
}
} else if (JSONRegionContexts.JSON_FOREIGN_ELEMENT == type
|| JSONRegionContexts.JSON_DECLARATION_DELIMITER == type) {
return;
*/
} else
appendSpaceBefore(node, toAppend.getText(), source);
}
use of org.eclipse.wst.json.core.document.IJSONDocument 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