use of org.eclipse.xtext.util.ReplaceRegion in project xtext-xtend by eclipse.
the class AbstractSmokeTest method testSkipNodesInBetweenWithoutResourceSet.
@Test
public void testSkipNodesInBetweenWithoutResourceSet() throws Exception {
for (String string : smokeTestModels) {
LazyLinkingResource resource = createResource(string);
ICompositeNode rootNode = resource.getParseResult().getRootNode();
ReplaceRegion replaceRegion = null;
for (INode node : rootNode.getAsTreeIterable()) {
int offset = node.getTotalOffset();
int length = node.getTotalLength();
if (replaceRegion == null || replaceRegion.getOffset() != offset || replaceRegion.getLength() != length) {
replaceRegion = new ReplaceRegion(offset, length, "");
StringBuilder builder = new StringBuilder(string);
replaceRegion.applyTo(builder);
doParseAndCheckForSmokeWithoutResourceSet(builder.toString());
}
}
}
}
use of org.eclipse.xtext.util.ReplaceRegion in project xtext-xtend by eclipse.
the class ImportOrganizerTest method assertIsOrganizedTo.
protected void assertIsOrganizedTo(final CharSequence model, final CharSequence expected, final boolean validate) {
try {
Assert.assertFalse(expected.toString().contains("$"));
final XtendFile xtendFile = this.file(model.toString(), validate);
Resource _eResource = xtendFile.eResource();
final List<ReplaceRegion> changes = this.importOrganizer.getOrganizedImportChanges(((XtextResource) _eResource));
final StringBuilder builder = new StringBuilder(model);
final Function1<ReplaceRegion, Integer> _function = (ReplaceRegion it) -> {
return Integer.valueOf(it.getOffset());
};
final List<ReplaceRegion> sortedChanges = IterableExtensions.<ReplaceRegion, Integer>sortBy(changes, _function);
ReplaceRegion lastChange = null;
for (final ReplaceRegion it : sortedChanges) {
{
if (((lastChange != null) && (lastChange.getEndOffset() > it.getOffset()))) {
Assert.fail(((("Overlapping text edits: " + lastChange) + " and ") + it));
}
lastChange = it;
}
}
List<ReplaceRegion> _reverse = ListExtensions.<ReplaceRegion>reverse(sortedChanges);
for (final ReplaceRegion it_1 : _reverse) {
int _offset = it_1.getOffset();
int _offset_1 = it_1.getOffset();
int _length = it_1.getLength();
int _plus = (_offset_1 + _length);
builder.replace(_offset, _plus, it_1.getText());
}
Assert.assertEquals(expected.toString(), builder.toString());
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
use of org.eclipse.xtext.util.ReplaceRegion in project xtext-core by eclipse.
the class CommentAssociationTest method checkReplaceRegion.
private void checkReplaceRegion(Element element, String expectedText, String completeModel) {
Serializer serializer = get(Serializer.class);
ReplaceRegion replacement = serializer.serializeReplacement(element, SaveOptions.defaultOptions());
assertEquals(expectedText, replacement.getText());
assertEquals(completeModel.indexOf(expectedText), replacement.getOffset());
assertEquals(expectedText.length(), replacement.getLength());
}
use of org.eclipse.xtext.util.ReplaceRegion in project xtext-core by eclipse.
the class AbstractPartialParserReplaceTest method replaceAndReparse.
protected void replaceAndReparse(String model, int offset, int length, String change, String expectedReparseRegion) throws Exception {
IParseResult parseResult = getParseResult(model);
PartialParsingPointers parsingPointers = getPartialParser().calculatePartialParsingPointers(parseResult, offset, length);
String reparseRegion = getPartialParser().insertChangeIntoReplaceRegion(parsingPointers.getDefaultReplaceRootNode(), new ReplaceRegion(offset, length, change));
assertEquals(expectedReparseRegion, reparseRegion);
final Wrapper<Boolean> unloaded = Wrapper.wrap(Boolean.FALSE);
getPartialParser().setUnloader(new IReferableElementsUnloader() {
@Override
public void unloadRoot(EObject root) {
unloaded.set(Boolean.TRUE);
}
});
IParseResult partiallyReparse = reparse(parseResult, offset, length, change);
assertTrue("unloaded", unloaded.get());
assertFalse(partiallyReparse.getRootNode().getText(), partiallyReparse.hasSyntaxErrors());
String expectedReparseModel = model.substring(0, offset) + change + model.substring(offset + length);
assertEquals(expectedReparseModel, partiallyReparse.getRootNode().getText());
}
use of org.eclipse.xtext.util.ReplaceRegion in project xtext-core by eclipse.
the class SerializerReplacementCalculationTest method testSerializeReplacement001.
@Test
public void testSerializeReplacement001() throws Exception {
// Given
String textModel = "Hello Xtext!" + System.lineSeparator();
StringBuilder stringBuilder = new StringBuilder(textModel);
Model model = (Model) getModel(textModel);
Greeting greeting = model.getGreetings().get(0);
ReplaceRegion replacement = getSerializer().serializeReplacement(greeting, SaveOptions.defaultOptions());
// When
replacement.applyTo(stringBuilder);
// Then
assertEquals(textModel, stringBuilder.toString());
}
Aggregations