use of org.eclipse.xtext.formatting2.regionaccess.ITextReplacement in project xtext-core by eclipse.
the class ChangeConverter method _handleReplacements.
protected void _handleReplacements(final ITextDocumentChange change) {
int _size = change.getReplacements().size();
boolean _greaterThan = (_size > 0);
if (_greaterThan) {
final URI uri = change.getNewURI();
final Function2<Document, XtextResource, List<TextEdit>> _function = (Document document, XtextResource resource) -> {
List<TextEdit> _xblockexpression = null;
{
final Function1<ITextReplacement, TextEdit> _function_1 = (ITextReplacement replacement) -> {
TextEdit _xblockexpression_1 = null;
{
final Position start = document.getPosition(replacement.getOffset());
int _offset = replacement.getOffset();
int _length = replacement.getLength();
int _plus = (_offset + _length);
final Position end = document.getPosition(_plus);
final Range range = new Range(start, end);
String _replacementText = replacement.getReplacementText();
_xblockexpression_1 = new TextEdit(range, _replacementText);
}
return _xblockexpression_1;
};
final List<TextEdit> textEdits = ListExtensions.<ITextReplacement, TextEdit>map(change.getReplacements(), _function_1);
_xblockexpression = this.addTextEdit(uri, ((TextEdit[]) Conversions.unwrapArray(textEdits, TextEdit.class)));
}
return _xblockexpression;
};
this.workspaceManager.<List<TextEdit>>doRead(uri, _function);
}
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextReplacement in project xtext-core by eclipse.
the class FormatterTestHelper method assertFormatted.
public void assertFormatted(FormatterTestRequest req) {
checkNotNull(req);
checkNotNull(req.getToBeFormatted());
FormatterRequest request = req.getRequest();
checkArgument(request.getTextRegionAccess() == null);
String document = req.getToBeFormatted().toString();
XtextResource parsed = parse(document);
if (req.isAllowSyntaxErrors()) {
if (request.getExplicitExceptionHandler() == null) {
request.setExceptionHandler(ExceptionAcceptor.IGNORING);
}
} else {
assertNoSyntaxErrors(parsed);
if (request.getExplicitExceptionHandler() == null) {
request.setExceptionHandler(ExceptionAcceptor.THROWING);
}
}
request.setTextRegionAccess(createRegionAccess(parsed, req));
if (request.getPreferences() == null)
request.setPreferences(new MapBasedPreferenceValues(Maps.<String, String>newLinkedHashMap()));
List<ITextReplacement> replacements = createFormatter(req).format(request);
assertReplacementsAreInRegion(replacements, request.getRegions(), document);
if (!req.isAllowUnformattedWhitespace())
assertAllWhitespaceIsFormatted(request.getTextRegionAccess(), replacements);
String formatted = request.getTextRegionAccess().getRewriter().renderToString(replacements);
Assert.assertEquals(req.getExpectationOrToBeFormatted().toString(), formatted);
// TODO: assert formatting a second time only produces identity replacements
// TODO: assert formatting with undefined whitespace only
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextReplacement in project xtext-core by eclipse.
the class RegionAccessTestHelper method assertDiffs.
public void assertDiffs(final ITextRegionAccessDiff regions) {
final ITextRegionRewriter rewriter = regions.getOriginalTextRegionAccess().getRewriter();
final ArrayList<ITextReplacement> result = Lists.<ITextReplacement>newArrayList();
List<ITextSegmentDiff> _regionDifferences = regions.getRegionDifferences();
for (final ITextSegmentDiff r : _regionDifferences) {
{
final ITextSegment originalRegion = r.getOriginalRegion();
final ITextSegment modifiedRegion = r.getModifiedRegion();
final ITextReplacement replacement = originalRegion.replaceWith(modifiedRegion.getText());
result.add(replacement);
}
}
final String actual = rewriter.renderToString(result);
final String expected = regions.regionForDocument().getText();
Assert.assertEquals(expected, actual);
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextReplacement in project xtext-core by eclipse.
the class TextRegionsWithTitleToString method toString.
@Override
public String toString() {
ITextRegionAccess access = getTextRegionAccess();
ITextSegment frame = getFrame();
if (access == null || frame == null)
return "(null)";
ITextRegionRewriter rewriter = access.getRewriter();
StringBuilder builder = new StringBuilder();
List<ITextReplacement> replacements = Lists.newArrayList();
for (int i = 0; i < this.items.size(); i++) {
Item item = this.items.get(i);
ITextSegment region = item.getRegion();
String regionStr = "offset=" + region.getOffset() + " length=" + region.getLength();
String open = i < BRACKETS_OPEN.length ? BRACKETS_OPEN[i] : "[" + i + "[";
String close = i < BRACKETS_CLOSE.length ? BRACKETS_CLOSE[i] : "]" + i + "]";
builder.append(open + close + ": " + item.getTitle() + " at " + regionStr + "\n");
replacements.add(rewriter.createReplacement(region.getOffset(), 0, open));
replacements.add(rewriter.createReplacement(region.getEndOffset(), 0, close));
}
String vizualized = rewriter.renderToString(frame, replacements);
builder.append(box("document snippet", vizualized));
return builder.toString();
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextReplacement in project xtext-core by eclipse.
the class TextReplacerContext method addReplacement.
@Override
public void addReplacement(ITextReplacement replacement) {
Preconditions.checkNotNull(replacer);
ITextSegment replacerRegion = replacer.getRegion();
FormatterRequest request = document.getRequest();
if (!replacerRegion.contains(replacement)) {
String frameTitle = replacer.getClass().getSimpleName();
ITextSegment frameRegion = replacer.getRegion();
String replacerTitle = replacement.getReplacementText();
RegionsOutsideFrameException exception = new RegionsOutsideFrameException(frameTitle, frameRegion, Tuples.create(replacerTitle, (ITextSegment) replacement));
request.getExceptionHandler().accept(exception);
return;
}
if (!isInRequestedRange(replacement)) {
return;
}
if (!isInUndefinedRegion(replacement)) {
if (request.isFormatUndefinedHiddenRegionsOnly()) {
return;
}
if (!request.allowIdentityEdits() && isIdentityEdit(replacement)) {
return;
}
}
try {
replacements.add(replacement);
} catch (ConflictingRegionsException e) {
request.getExceptionHandler().accept(e);
}
}
Aggregations