use of org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess in project xtext-eclipse by eclipse.
the class FormatterTester method createRegionAccess.
protected ITextRegionAccess createRegionAccess(XtextResource resource, FormatterTestRequest req) {
boolean useSerializer = req.isUseSerializer() && !req.isAllowSyntaxErrors();
if (req.isUseNodeModel() && useSerializer) {
ITextRegionAccess nmRegions = createRegionAccessViaNodeModel(resource);
ITextRegionAccess serRegions = createRegionAccessViaSerializer(resource);
String nmString = toString(nmRegions);
String serString = toString(serRegions);
Assert.assertEquals(nmString, serString);
return nmRegions;
} else if (req.isUseNodeModel()) {
ITextRegionAccess nmRegions = createRegionAccessViaNodeModel(resource);
return nmRegions;
} else if (useSerializer) {
ITextRegionAccess serRegions = createRegionAccessViaSerializer(resource);
return serRegions;
} else
throw new IllegalStateException("Can't format anything when using neither NodeModel nor Serializer.");
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess in project xtext-eclipse by eclipse.
the class ContentFormatter method initRequest.
protected void initRequest(IXtextDocument document, IRegion region, XtextResource resource, FormatterRequest request) {
ITextRegion textRegion = new TextRegion(region.getOffset(), region.getLength());
request.setAllowIdentityEdits(false);
request.setFormatUndefinedHiddenRegionsOnly(false);
request.setRegions(singletonList(textRegion));
ITextRegionAccess tokenAccess = regionBuilder.forNodeModel(resource).create();
IPreferenceValues preferenceValues = preferencesProvider.getPreferenceValues(resource);
request.setPreferences(TypedPreferenceValues.castOrWrap(preferenceValues));
request.setTextRegionAccess(tokenAccess);
if (tokenAccess.hasSyntaxError())
request.setExceptionHandler(ExceptionAcceptor.IGNORING);
else
request.setExceptionHandler(ExceptionAcceptor.LOGGING);
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess in project xtext-xtend by eclipse.
the class RichStringFormatter method setNewLines.
protected void setNewLines(final IFormattableDocument doc, final int offset, final int length, final int indentationIncrease, final int indentationDecrease, final int newLines) {
IHiddenRegionFormatting _createHiddenRegionFormatting = doc.getFormatter().createHiddenRegionFormatting();
final Procedure1<IHiddenRegionFormatting> _function = (IHiddenRegionFormatting it) -> {
it.setIndentationIncrease(Integer.valueOf(indentationIncrease));
it.setIndentationDecrease(Integer.valueOf(indentationDecrease));
it.setNewLinesMin(Integer.valueOf(newLines));
it.setNewLinesDefault(Integer.valueOf(newLines));
it.setNewLinesMax(Integer.valueOf(newLines));
};
final IHiddenRegionFormatting fmt = ObjectExtensions.<IHiddenRegionFormatting>operator_doubleArrow(_createHiddenRegionFormatting, _function);
AbstractFormatter2 _formatter = doc.getFormatter();
ITextRegionAccess _textRegionAccess = this._iTextRegionExtensions.getTextRegionAccess();
TextSegment _textSegment = new TextSegment(_textRegionAccess, offset, length);
final ITextReplacer replacer = _formatter.createWhitespaceReplacer(_textSegment, fmt);
doc.addReplacer(replacer);
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess in project xtext-xtend by eclipse.
the class RichStringFormatter method _format.
protected void _format(final RichString richString, final IFormattableDocument doc) {
RichString _containerOfType = EcoreUtil2.<RichString>getContainerOfType(richString.eContainer(), RichString.class);
boolean _tripleNotEquals = (_containerOfType != null);
if (_tripleNotEquals) {
return;
}
boolean _hasSyntaxError = this.hasSyntaxError(this._iTextRegionExtensions.regionForEObject(richString));
if (_hasSyntaxError) {
return;
}
ITextRegionAccess _textRegionAccess = this._iTextRegionExtensions.getTextRegionAccess();
final RichStringToLineModel impl = new RichStringToLineModel(_textRegionAccess, richString);
DefaultIndentationHandler _defaultIndentationHandler = new DefaultIndentationHandler();
this.factory.richStringProcessor.process(richString, impl, _defaultIndentationHandler);
impl.finish();
EList<XExpression> _expressions = richString.getExpressions();
for (final XExpression e : _expressions) {
this.format(e, doc);
}
final List<Line> lines = impl.getModel().getLines();
final boolean canIndent = ((!lines.isEmpty()) && StringExtensions.isNullOrEmpty(IterableExtensions.<Line>last(lines).getContent()));
for (final Line line : lines) {
int _rootIndentLenght = impl.getModel().getRootIndentLenght();
boolean _greaterThan = (_rootIndentLenght > 0);
if (_greaterThan) {
int _xifexpression = (int) 0;
if ((canIndent && Objects.equal(line, IterableExtensions.<Line>head(lines)))) {
_xifexpression = 1;
} else {
_xifexpression = 0;
}
final int increaseIndentationChange = _xifexpression;
int _xifexpression_1 = (int) 0;
if ((canIndent && Objects.equal(line, IterableExtensions.<Line>last(lines)))) {
_xifexpression_1 = 1;
} else {
_xifexpression_1 = 0;
}
final int decraseIndentationChange = _xifexpression_1;
int _xifexpression_2 = (int) 0;
boolean _isLeadingSemanticNewLine = line.isLeadingSemanticNewLine();
if (_isLeadingSemanticNewLine) {
int _offset = line.getOffset();
int _newLineCharCount = line.getNewLineCharCount();
_xifexpression_2 = (_offset + _newLineCharCount);
} else {
_xifexpression_2 = line.getOffset();
}
final int nloffset = _xifexpression_2;
final int i = Math.min(line.getIndentLength(), impl.getModel().getRootIndentLenght());
int _xifexpression_3 = (int) 0;
boolean _isLeadingSemanticNewLine_1 = line.isLeadingSemanticNewLine();
if (_isLeadingSemanticNewLine_1) {
_xifexpression_3 = i;
} else {
int _newLineCharCount_1 = line.getNewLineCharCount();
_xifexpression_3 = (_newLineCharCount_1 + i);
}
final int nllength = _xifexpression_3;
boolean _isLeadingSemanticNewLine_2 = line.isLeadingSemanticNewLine();
if (_isLeadingSemanticNewLine_2) {
this.setNewLines(doc, nloffset, nllength, increaseIndentationChange, decraseIndentationChange, 0);
} else {
this.setNewLines(doc, nloffset, nllength, increaseIndentationChange, decraseIndentationChange, 1);
}
boolean _isEmpty = line.getChunks().isEmpty();
boolean _not = (!_isEmpty);
if (_not) {
final int offset = (nloffset + nllength);
int _indentLength = line.getIndentLength();
int _rootIndentLenght_1 = impl.getModel().getRootIndentLenght();
final int length = (_indentLength - _rootIndentLenght_1);
final Function1<Chunk, CharSequence> _function = (Chunk chunk) -> {
CharSequence _switchResult = null;
boolean _matched = false;
if (chunk instanceof SemanticWhitespace) {
_matched = true;
_switchResult = ((SemanticWhitespace) chunk).getText();
}
if (!_matched) {
if (chunk instanceof TemplateWhitespace) {
_matched = true;
_switchResult = doc.getFormatter().<String>getPreference(FormatterPreferenceKeys.indentation);
}
}
return _switchResult;
};
final String text = IterableExtensions.join(ListExtensions.<Chunk, CharSequence>map(line.getChunks(), _function));
this.setSpace(doc, offset, length, text);
}
}
}
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess in project xtext-xtend by eclipse.
the class ConvertJavaCode method formatXtendCode.
private String formatXtendCode(IFile xtendFile, final String xtendCode) {
try {
XtextResource resource = (XtextResource) createResource(xtendFile, xtendCode);
ITextRegionAccess regionAccess = regionAccessBuilder.get().forNodeModel(resource).create();
FormatterRequest request = new FormatterRequest();
request.setAllowIdentityEdits(false);
request.setTextRegionAccess(regionAccess);
request.setPreferences(TypedPreferenceValues.castOrWrap(cfgProvider.getPreferenceValues(resource)));
List<ITextReplacement> replacements = formatter.format(request);
String formatted = regionAccess.getRewriter().renderToString(replacements);
return formatted;
} catch (Exception e) {
LOG.error("Formatting step canceled due to an exception.", e);
return null;
}
}
Aggregations