Search in sources :

Example 6 with ITextReplacement

use of org.eclipse.xtext.formatting2.regionaccess.ITextReplacement in project xtext-core by eclipse.

the class FormatterTestHelper method assertReplacementsAreInRegion.

protected void assertReplacementsAreInRegion(List<ITextReplacement> rep, Collection<ITextRegion> regions, String doc) {
    Set<ITextReplacement> invalid = Sets.newHashSet();
    ALLOWED: for (ITextRegion allowed : regions) for (ITextReplacement r : rep) {
        if (allowed.contains(r))
            continue ALLOWED;
        invalid.add(r);
    }
    if (!invalid.isEmpty()) {
        String visualized = new TextRegionsToString().addAllReplacements(invalid).toString();
        fail("One or more TextReplacements are outside of the allowed region. Region: " + regions, visualized);
    }
}
Also used : ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) ITextRegion(org.eclipse.xtext.util.ITextRegion) TextRegionAccessToString(org.eclipse.xtext.formatting2.debug.TextRegionAccessToString) TextRegionsToString(org.eclipse.xtext.formatting2.debug.TextRegionsToString) TextRegionsToString(org.eclipse.xtext.formatting2.debug.TextRegionsToString)

Example 7 with ITextReplacement

use of org.eclipse.xtext.formatting2.regionaccess.ITextReplacement in project xtext-core by eclipse.

the class RecordingXtextResourceUpdater method applyChange.

@Override
public void applyChange(Deltas deltas, IAcceptor<IEmfResourceChange> changeAcceptor) {
    Resource resource = snapshot.getResource();
    ResourceSet rs = resource.getResourceSet();
    ReferenceUpdaterContext ctx = new ReferenceUpdaterContext(deltas, document);
    if (serializer.isUpdateCrossReferences()) {
        referenceUpdater.update(ctx);
        for (Runnable run : ctx.getModifications()) {
            run.run();
        }
    }
    ChangeDescription recording = recorder.endRecording();
    if (recording != null) {
        List<IResourceSnapshot> snapshots = Collections.singletonList(snapshot);
        ResourceSetRecording tree = changeTreeProvider.createChangeTree(rs, snapshots, recording);
        ResourceRecording recordedResource = tree.getRecordedResource(resource);
        partialSerializer.serializeChanges(recordedResource, document);
    }
    List<IUpdatableReference> updatableReferences = ctx.getUpdatableReferences();
    for (IUpdatableReference upd : updatableReferences) {
        referenceUpdater.updateReference(document, upd);
    }
    ITextRegionAccessDiff rewritten = document.create();
    List<ITextReplacement> rep = formatter.format(rewritten);
    URI oldUri = snapshot.getURI();
    TextDocumentChange change = new TextDocumentChange(rewritten, oldUri, rep);
    changeAcceptor.accept(change);
}
Also used : ResourceRecording(org.eclipse.xtext.ide.serializer.impl.ChangeTreeProvider.ResourceRecording) ITextRegionAccessDiff(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccessDiff) IUpdatableReference(org.eclipse.xtext.ide.serializer.hooks.IUpdatableReference) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) ResourceSetRecording(org.eclipse.xtext.ide.serializer.impl.ChangeTreeProvider.ResourceSetRecording) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) URI(org.eclipse.emf.common.util.URI) IResourceSnapshot(org.eclipse.xtext.ide.serializer.hooks.IResourceSnapshot) ChangeDescription(org.eclipse.emf.ecore.change.ChangeDescription)

Example 8 with ITextReplacement

use of org.eclipse.xtext.formatting2.regionaccess.ITextReplacement in project xtext-core by eclipse.

the class RegionDiffFormatter method mergeReplacements.

protected List<ITextReplacement> mergeReplacements(ITextRegionAccessDiff regions, List<ITextReplacement> rep) {
    ITextRegionRewriter rewriter = regions.getOriginalTextRegionAccess().getRewriter();
    List<ITextReplacement> result = Lists.newArrayList();
    for (ITextSegmentDiff r : regions.getRegionDifferences()) {
        ITextSegment original = r.getOriginalRegion();
        ITextSegment modified = r.getModifiedRegion();
        List<ITextReplacement> local = Lists.newArrayList();
        for (ITextReplacement re : rep) {
            if (modified.contains(re)) {
                local.add(re);
            }
        }
        String text;
        if (local.isEmpty()) {
            text = modified.getText();
        } else {
            text = regions.getRewriter().renderToString(modified, local);
        }
        ITextReplacement replacement = rewriter.createReplacement(original.getOffset(), original.getLength(), text);
        result.add(replacement);
    }
    return result;
}
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)

Example 9 with ITextReplacement

use of org.eclipse.xtext.formatting2.regionaccess.ITextReplacement in project xtext-core by eclipse.

the class CodeActionService method recordWorkspaceEdit.

private WorkspaceEdit recordWorkspaceEdit(final Document doc, final XtextResource resource, final IChangeSerializer.IModification<Resource> mod) {
    try {
        final XtextResourceSet rs = new XtextResourceSet();
        final Resource copy = rs.createResource(resource.getURI());
        String _text = resource.getParseResult().getRootNode().getText();
        StringInputStream _stringInputStream = new StringInputStream(_text);
        copy.load(_stringInputStream, CollectionLiterals.<Object, Object>emptyMap());
        this.serializer.<Resource>addModification(copy, mod);
        final ArrayList<IEmfResourceChange> documentchanges = CollectionLiterals.<IEmfResourceChange>newArrayList();
        this.serializer.applyModifications(CollectionBasedAcceptor.<IEmfResourceChange>of(documentchanges));
        WorkspaceEdit _workspaceEdit = new WorkspaceEdit();
        final Procedure1<WorkspaceEdit> _function = (WorkspaceEdit it) -> {
            Iterable<ITextDocumentChange> _filter = Iterables.<ITextDocumentChange>filter(documentchanges, ITextDocumentChange.class);
            for (final ITextDocumentChange documentchange : _filter) {
                {
                    final Function1<ITextReplacement, TextEdit> _function_1 = (ITextReplacement replacement) -> {
                        TextEdit _textEdit = new TextEdit();
                        final Procedure1<TextEdit> _function_2 = (TextEdit it_1) -> {
                            it_1.setNewText(replacement.getReplacementText());
                            Position _position = doc.getPosition(replacement.getOffset());
                            Position _position_1 = doc.getPosition(replacement.getEndOffset());
                            Range _range = new Range(_position, _position_1);
                            it_1.setRange(_range);
                        };
                        return ObjectExtensions.<TextEdit>operator_doubleArrow(_textEdit, _function_2);
                    };
                    final List<TextEdit> edits = ListExtensions.<ITextReplacement, TextEdit>map(documentchange.getReplacements(), _function_1);
                    it.getChanges().put(documentchange.getNewURI().toString(), edits);
                }
            }
        };
        return ObjectExtensions.<WorkspaceEdit>operator_doubleArrow(_workspaceEdit, _function);
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : Position(org.eclipse.lsp4j.Position) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) ITextDocumentChange(org.eclipse.xtext.ide.serializer.ITextDocumentChange) Range(org.eclipse.lsp4j.Range) StringInputStream(org.eclipse.xtext.util.StringInputStream) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) TextEdit(org.eclipse.lsp4j.TextEdit) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) IEmfResourceChange(org.eclipse.xtext.ide.serializer.IEmfResourceChange)

Example 10 with ITextReplacement

use of org.eclipse.xtext.formatting2.regionaccess.ITextReplacement in project xtext-core by eclipse.

the class TextRegionRewriter method renderToAppendable.

@Override
public void renderToAppendable(ITextSegment input, Iterable<? extends ITextReplacement> rep, Appendable result) throws IOException {
    int offset = input.getOffset();
    String text = input.getText();
    List<ITextReplacement> list = new TextReplacementList<ITextReplacement>(rep);
    Collections.sort(list);
    int lastOffset = 0;
    for (ITextReplacement r : list) {
        result.append(text.subSequence(lastOffset, r.getOffset() - offset));
        result.append(r.getReplacementText());
        lastOffset = (r.getOffset() - offset) + r.getLength();
    }
    if (lastOffset <= text.length())
        result.append(text.subSequence(lastOffset, text.length()));
}
Also used : ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) TextReplacementList(org.eclipse.xtext.formatting2.internal.TextReplacementList)

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