use of org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess in project xtext-core by eclipse.
the class LineRegion method getNextLine.
@Override
public ILineRegion getNextLine() {
ITextRegionAccess access = getTextRegionAccess();
int start = getEndOffset() + 1;
String text = access.regionForDocument().getText();
while (true) {
if (start >= text.length())
return null;
char c = text.charAt(start);
if (c == '\n' || c == '\r')
start++;
else
break;
}
int end = text.indexOf('\n', start);
if (end > 0) {
if (text.charAt(end - 1) == '\r')
end = end - 1;
} else
end = text.length();
return new LineRegion(access, start, end - start);
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess in project xtext-core by eclipse.
the class AbstractFormatter2 method isInRequestedRange.
protected boolean isInRequestedRange(EObject obj) {
Collection<ITextRegion> regions = request.getRegions();
if (regions.isEmpty())
return true;
ITextRegionAccess access = request.getTextRegionAccess();
IEObjectRegion objRegion = access.regionForEObject(obj);
if (objRegion == null)
return false;
IHiddenRegion previousHidden = objRegion.getPreviousHiddenRegion();
IHiddenRegion nextHidden = objRegion.getNextHiddenRegion();
int objOffset = previousHidden != null ? previousHidden.getOffset() : 0;
int objEnd = nextHidden != null ? nextHidden.getEndOffset() : access.regionForRootEObject().getEndOffset();
for (ITextRegion region : regions) {
int regionOffset = region.getOffset();
int regionEnd = regionOffset + region.getLength();
if (regionOffset <= objEnd && regionEnd >= objOffset)
return true;
}
return false;
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess in project xtext-xtend by eclipse.
the class RichStringFormatter method setSpace.
protected void setSpace(final IFormattableDocument doc, final int offset, final int length, final String space) {
IHiddenRegionFormatting _createHiddenRegionFormatting = doc.getFormatter().createHiddenRegionFormatting();
final Procedure1<IHiddenRegionFormatting> _function = (IHiddenRegionFormatting it) -> {
it.setSpace(space);
};
final IHiddenRegionFormatting fmt = ObjectExtensions.<IHiddenRegionFormatting>operator_doubleArrow(_createHiddenRegionFormatting, _function);
AbstractFormatter2 _formatter = doc.getFormatter();
ITextRegionAccess _textRegionAccess = this._iTextRegionExtensions.getTextRegionAccess();
TextSegment _textSegment = new TextSegment(_textRegionAccess, offset, length);
final ITextReplacer replacer = _formatter.createWhitespaceReplacer(_textSegment, fmt);
doc.addReplacer(replacer);
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess 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);
}
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess in project xtext-eclipse by eclipse.
the class FormatterTester method assertAllWhitespaceIsFormatted.
protected void assertAllWhitespaceIsFormatted(ITextRegionAccess access, List<ITextReplacement> replacements) {
List<ITextSegment> expected = Lists.newArrayList();
IHiddenRegion current = access.regionForRootEObject().getPreviousHiddenRegion();
while (current != null) {
expected.addAll(current.getMergedSpaces());
current = current.getNextHiddenRegion();
}
List<ITextSegment> missing = TextRegions.difference(expected, replacements);
if (!missing.isEmpty()) {
TextRegionsToString toString = new TextRegionsToString().setTextRegionAccess(access);
for (ITextSegment region : missing) toString.add(region, region.getClass().getSimpleName());
String msg = "The following regions are not formatted:\n" + toString;
System.err.println(msg);
Assert.fail(msg);
}
}
Aggregations