Search in sources :

Example 26 with ITextReplacement

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);
    }
}
Also used : Position(org.eclipse.lsp4j.Position) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) XtextResource(org.eclipse.xtext.resource.XtextResource) Document(org.eclipse.xtext.ide.server.Document) Range(org.eclipse.lsp4j.Range) URI(org.eclipse.emf.common.util.URI) TextEdit(org.eclipse.lsp4j.TextEdit) List(java.util.List)

Example 27 with ITextReplacement

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
}
Also used : ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) MapBasedPreferenceValues(org.eclipse.xtext.preferences.MapBasedPreferenceValues) XtextResource(org.eclipse.xtext.resource.XtextResource) TextRegionAccessToString(org.eclipse.xtext.formatting2.debug.TextRegionAccessToString) TextRegionsToString(org.eclipse.xtext.formatting2.debug.TextRegionsToString) FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest)

Example 28 with ITextReplacement

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);
}
Also used : ITextRegionRewriter(org.eclipse.xtext.formatting2.regionaccess.ITextRegionRewriter) ITextSegmentDiff(org.eclipse.xtext.formatting2.regionaccess.ITextSegmentDiff) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment) TextRegionAccessToString(org.eclipse.xtext.formatting2.debug.TextRegionAccessToString)

Example 29 with ITextReplacement

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();
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) ITextRegionRewriter(org.eclipse.xtext.formatting2.regionaccess.ITextRegionRewriter) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment)

Example 30 with ITextReplacement

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);
    }
}
Also used : ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment) String(java.lang.String) FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest)

Aggregations

ITextReplacement (org.eclipse.xtext.formatting2.regionaccess.ITextReplacement)35 FormatterRequest (org.eclipse.xtext.formatting2.FormatterRequest)13 ITextSegment (org.eclipse.xtext.formatting2.regionaccess.ITextSegment)12 XtextResource (org.eclipse.xtext.resource.XtextResource)11 ITextRegionAccess (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess)9 TextRegionAccessToString (org.eclipse.xtext.formatting2.debug.TextRegionAccessToString)7 TextRegionsToString (org.eclipse.xtext.formatting2.debug.TextRegionsToString)7 Resource (org.eclipse.emf.ecore.resource.Resource)5 IFormatter2 (org.eclipse.xtext.formatting2.IFormatter2)5 String (java.lang.String)4 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)4 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)4 ITextReplacerContext (org.eclipse.xtext.formatting2.ITextReplacerContext)4 IHiddenRegion (org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion)4 ITextRegion (org.eclipse.xtext.util.ITextRegion)4 URI (org.eclipse.emf.common.util.URI)3 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)3 MapBasedPreferenceValues (org.eclipse.xtext.preferences.MapBasedPreferenceValues)3 Procedure1 (org.eclipse.xtext.xbase.lib.Procedures.Procedure1)3 List (java.util.List)2