Search in sources :

Example 1 with ITextReplacement

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

the class ChangeSerializerTextEditComposer method collectChanges.

protected void collectChanges(IEmfResourceChange e, List<TextEdit> edits) {
    if (e instanceof ITextDocumentChange && e.getResource() == recording) {
        ITextDocumentChange change = (ITextDocumentChange) e;
        for (ITextReplacement c : change.getReplacements()) {
            ReplaceEdit edit = new ReplaceEdit(c.getOffset(), c.getLength(), c.getReplacementText());
            edits.add(edit);
        }
    }
}
Also used : ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) ITextDocumentChange(org.eclipse.xtext.ide.serializer.ITextDocumentChange)

Example 2 with ITextReplacement

use of org.eclipse.xtext.formatting2.regionaccess.ITextReplacement in project xtext-eclipse 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 IFile file = this.resourceUriConverter.toFile(change.getOldURI());
        boolean _canWrite = this.canWrite(file);
        boolean _not = (!_canWrite);
        if (_not) {
            StringConcatenation _builder = new StringConcatenation();
            _builder.append("Affected file \'");
            IPath _fullPath = file.getFullPath();
            _builder.append(_fullPath);
            _builder.append("\' is read-only");
            this.issues.add(RefactoringIssueAcceptor.Severity.FATAL, _builder.toString());
        }
        this.checkDerived(file);
        final Function1<ITextReplacement, ReplaceEdit> _function = (ITextReplacement replacement) -> {
            int _offset = replacement.getOffset();
            int _length = replacement.getLength();
            String _replacementText = replacement.getReplacementText();
            return new ReplaceEdit(_offset, _length, _replacementText);
        };
        final List<ReplaceEdit> textEdits = ListExtensions.<ITextReplacement, ReplaceEdit>map(change.getReplacements(), _function);
        final MultiTextEdit textEdit = new MultiTextEdit();
        textEdit.addChildren(((TextEdit[]) Conversions.unwrapArray(textEdits, TextEdit.class)));
        final ITextEditor openEditor = this.findOpenEditor(file);
        TextChange _xifexpression = null;
        if ((openEditor == null)) {
            String _lastSegment = change.getOldURI().lastSegment();
            TextFileChange _textFileChange = new TextFileChange(_lastSegment, file);
            final Procedure1<TextFileChange> _function_1 = (TextFileChange it) -> {
                it.setSaveMode(TextFileChange.FORCE_SAVE);
            };
            _xifexpression = ObjectExtensions.<TextFileChange>operator_doubleArrow(_textFileChange, _function_1);
        } else {
            String _name = this.currentChange.getName();
            _xifexpression = new EditorDocumentChange(_name, openEditor, false);
        }
        final TextChange ltkChange = _xifexpression;
        ltkChange.setEdit(textEdit);
        ltkChange.setTextType(change.getOldURI().fileExtension());
        this.addChange(ltkChange);
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IPath(org.eclipse.core.runtime.IPath) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) TextChange(org.eclipse.ltk.core.refactoring.TextChange) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) EditorDocumentChange(org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Example 3 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 4 with ITextReplacement

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

the class N4MFFormatterTest method formatted.

/**
 * @param expectation
 *            the expected formatted code
 * @param resource
 *            the Xtext resource to be formatted
 * @param offset
 *            the optional start offset from where formatting should be applied
 * @param to
 *            the optional to offset to which formatting should be applied
 */
@ParameterParser(syntax = "('from' arg2=OFFSET 'to' arg3=OFFSET)?")
@Xpect
public void formatted(@StringExpectation(whitespaceSensitive = true) IStringExpectation expectation, @ThisResource XtextResource resource, int offset, int to) {
    ICompositeNode rootNode = resource.getParseResult().getRootNode();
    FormatterRequest request = requestProvider.get().setTextRegionAccess(regionAccessBuilderProvider.get().forNodeModel(resource).create());
    int totalLength = rootNode.getTotalLength();
    if (offset >= 0 && to > offset) {
        request.setRegions(Collections.singleton(new TextRegion(offset, to - offset)));
    } else {
        request.setRegions(Collections.singleton(new TextRegion(offset, totalLength - offset)));
    }
    List<ITextReplacement> replacements = formatter.format(request);
    String formatted = request.getTextRegionAccess().getRewriter().renderToString(replacements);
    if (offset >= 0 && to > offset) {
        formatted = formatted.substring(offset, formatted.length() - (totalLength - to));
    } else {
        formatted = formatted.substring(offset);
    }
    if (isUnixEnding()) {
        formatted = formatted.replaceAll("\r\n", "\n");
    } else if (isWindowsEnding()) {
        if (!rootNode.getText().contains("\r\n")) {
            formatted = formatted.replaceAll("\r\n", "\n");
        } else {
            formatted = formatted.replaceAll("(!\r)\n", "\r\n");
        }
    }
    expectation.assertEquals(formatted);
}
Also used : TextRegion(org.eclipse.xtext.util.TextRegion) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest) Xpect(org.eclipse.xpect.runner.Xpect) ParameterParser(org.eclipse.xpect.parameter.ParameterParser)

Example 5 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)

Aggregations

ITextReplacement (org.eclipse.xtext.formatting2.regionaccess.ITextReplacement)37 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)6 List (java.util.List)5 URI (org.eclipse.emf.common.util.URI)5 TextEdit (org.eclipse.lsp4j.TextEdit)5 IFormatter2 (org.eclipse.xtext.formatting2.IFormatter2)5 ArrayList (java.util.ArrayList)4 Range (org.eclipse.lsp4j.Range)4 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)4 IHiddenRegion (org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion)4 ITextDocumentChange (org.eclipse.xtext.ide.serializer.ITextDocumentChange)4 String (java.lang.String)3 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)3 Position (org.eclipse.lsp4j.Position)3