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);
}
}
}
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);
}
}
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);
}
}
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);
}
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);
}
Aggregations