Search in sources :

Example 21 with ITextReplacement

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

the class FixedContentFormatter method createTextEdit.

@Override
protected TextEdit createTextEdit(List<ITextReplacement> replacements) {
    if (replacements.isEmpty()) {
        return new ReplaceEdit(0, 0, "");
    }
    ITextRegionAccess regionAccess = replacements.get(0).getTextRegionAccess();
    String newDocument = regionAccess.getRewriter().renderToString(replacements);
    return new ReplaceEdit(0, regionAccess.regionForDocument().getLength(), newDocument);
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit)

Example 22 with ITextReplacement

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

the class FormatterFacade method format.

public String format(final String xtendCode) {
    try {
        final XtextResourceSet resourceSet = new XtextResourceSet();
        Resource _createResource = this.resourceFactory.createResource(URI.createURI("synthetic://to-be-formatted.xtend"));
        final XtextResource resource = ((XtextResource) _createResource);
        EList<Resource> _resources = resourceSet.getResources();
        _resources.add(resource);
        StringInputStream _stringInputStream = new StringInputStream(xtendCode);
        resource.load(_stringInputStream, CollectionLiterals.<Object, Object>emptyMap());
        final ITextRegionAccess regionAccess = this.regionAccessBuilder.get().forNodeModel(resource).create();
        FormatterRequest _formatterRequest = new FormatterRequest();
        final Procedure1<FormatterRequest> _function = (FormatterRequest it) -> {
            it.setAllowIdentityEdits(false);
            it.setTextRegionAccess(regionAccess);
            it.setPreferences(TypedPreferenceValues.castOrWrap(this.cfgProvider.getPreferenceValues(resource)));
        };
        FormatterRequest request = ObjectExtensions.<FormatterRequest>operator_doubleArrow(_formatterRequest, _function);
        List<ITextReplacement> replacements = this.formatter.format(request);
        return regionAccess.getRewriter().renderToString(replacements);
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) StringInputStream(org.eclipse.xtext.util.StringInputStream) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource) FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest)

Example 23 with ITextReplacement

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

the class RegionDiffFormatter method format.

public List<ITextReplacement> format(ITextRegionAccess regions) {
    IFormatter2 fmt = getFormatter();
    if (regions instanceof ITextRegionAccessDiff) {
        ITextRegionAccessDiff diff = (ITextRegionAccessDiff) regions;
        FormatterRequest request = createFormatterRequest(diff);
        List<ITextReplacement> replacements = fmt.format(request);
        List<ITextReplacement> merged = mergeReplacements(diff, replacements);
        return merged;
    } else {
        FormatterRequest request = createFormatterRequest(regions);
        List<ITextReplacement> replacements = fmt.format(request);
        return replacements;
    }
}
Also used : ITextRegionAccessDiff(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccessDiff) IFormatter2(org.eclipse.xtext.formatting2.IFormatter2) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest)

Example 24 with ITextReplacement

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

the class RelatedXtextResourceUpdater method applyChange.

@Override
public void applyChange(Deltas deltas, IAcceptor<IEmfResourceChange> changeAcceptor) {
    XtextResource res = (XtextResource) lifecycleManager.openAndApplyReferences(getResourceSet(), getResource());
    if (!referenceUpdater.isAffected(deltas, getResource())) {
        return;
    }
    ITextRegionAccess base = textRegionBuilderProvider.get().forNodeModel(res).create();
    ITextRegionDiffBuilder rewriter = new StringBasedTextRegionAccessDiffBuilder(base);
    ReferenceUpdaterContext context = new ReferenceUpdaterContext(deltas, rewriter, getResource());
    referenceUpdater.update(context);
    if (!context.getModifications().isEmpty()) {
        ChangeRecorder rec = new ChangeRecorder(res);
        for (Runnable run : context.getModifications()) {
            run.run();
        }
        ChangeDescription recording = rec.endRecording();
        ResourceSet rs = res.getResourceSet();
        ResourceSetRecording tree = changeTreeProvider.createChangeTree(rs, Collections.emptyList(), recording);
        ResourceRecording recordedResource = tree.getRecordedResource(res);
        if (recordedResource != null) {
            serializer.serializeChanges(recordedResource, rewriter);
        }
    }
    for (IUpdatableReference upd : context.getUpdatableReferences()) {
        referenceUpdater.updateReference(rewriter, upd);
    }
    ITextRegionAccessDiff rewritten = rewriter.create();
    List<ITextReplacement> rep = formatter.format(rewritten);
    TextDocumentChange change = new TextDocumentChange(rewritten, getResource().getUri(), rep);
    changeAcceptor.accept(change);
}
Also used : ResourceRecording(org.eclipse.xtext.ide.serializer.impl.ChangeTreeProvider.ResourceRecording) ITextRegionAccessDiff(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccessDiff) ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) StringBasedTextRegionAccessDiffBuilder(org.eclipse.xtext.formatting2.regionaccess.internal.StringBasedTextRegionAccessDiffBuilder) 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) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) ChangeRecorder(org.eclipse.emf.ecore.change.util.ChangeRecorder) ChangeDescription(org.eclipse.emf.ecore.change.ChangeDescription) ITextRegionDiffBuilder(org.eclipse.xtext.formatting2.regionaccess.ITextRegionDiffBuilder)

Example 25 with ITextReplacement

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

the class FormattingService method format.

/**
 * @since 2.14
 */
public List<TextEdit> format(final XtextResource resource, final Document document, final int offset, final int length, final FormattingOptions options) {
    String indent = this.indentationInformation.getIndentString();
    if ((options != null)) {
        boolean _isInsertSpaces = options.isInsertSpaces();
        if (_isInsertSpaces) {
            indent = Strings.padEnd("", options.getTabSize(), ' ');
        }
    }
    if ((this.formatter2Provider != null)) {
        final MapBasedPreferenceValues preferences = new MapBasedPreferenceValues();
        preferences.put("indentation", indent);
        TextRegion _textRegion = new TextRegion(offset, length);
        final List<ITextReplacement> replacements = this.format2(resource, _textRegion, preferences);
        final Function1<ITextReplacement, TextEdit> _function = (ITextReplacement r) -> {
            return this.toTextEdit(document, r.getReplacementText(), r.getOffset(), r.getLength());
        };
        return IterableExtensions.<TextEdit>toList(ListExtensions.<ITextReplacement, TextEdit>map(replacements, _function));
    } else {
        return CollectionLiterals.<TextEdit>newArrayList();
    }
}
Also used : TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) TextEdit(org.eclipse.lsp4j.TextEdit) MapBasedPreferenceValues(org.eclipse.xtext.preferences.MapBasedPreferenceValues)

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