use of org.eclipse.xtext.formatting2.regionaccess.ILineRegion in project xtext-core by eclipse.
the class LineRegion method getPreviousLine.
@Override
public ILineRegion getPreviousLine() {
ITextRegionAccess access = getTextRegionAccess();
int end = getOffset() - 1;
String text = access.regionForDocument().getText();
while (true) {
if (end < 0)
return null;
char c = text.charAt(end);
if (c == '\n' || c == '\r')
end--;
else
break;
}
int start = text.lastIndexOf('\n', end);
if (start < 0)
start = 0;
return new LineRegion(access, start, end - start);
}
use of org.eclipse.xtext.formatting2.regionaccess.ILineRegion in project xtext-core by eclipse.
the class RegionAccessBuilderTest method assertLinesAreConsistent.
private void assertLinesAreConsistent(final ITextRegionAccess access) {
final Function1<ILineRegion, String> _function = (ILineRegion it) -> {
int _offset = it.getOffset();
String _plus = (Integer.valueOf(_offset) + ":");
int _length = it.getLength();
return (_plus + Integer.valueOf(_length));
};
final Set<String> lines = IterableExtensions.<String>toSet(ListExtensions.<ILineRegion, String>map(access.regionForDocument().getLineRegions(), _function));
final String text = access.regionForDocument().getText();
for (int i = 0; (i < text.length()); i++) {
{
final ILineRegion line = access.regionForLineAtOffset(i);
int _offset = line.getOffset();
String _plus = (Integer.valueOf(_offset) + ":");
int _length = line.getLength();
final String lineStr = (_plus + Integer.valueOf(_length));
Assert.assertTrue(lines.contains(lineStr));
}
}
}
use of org.eclipse.xtext.formatting2.regionaccess.ILineRegion in project xtext-core by eclipse.
the class TextRegionsInTextToString method getFrame.
public ITextSegment getFrame() {
if (this.frame != null)
return this.frame;
ITextRegionAccess access = getTextRegionAccess();
if (access != null) {
ITextSegment impactRegion = TextRegions.merge(this.items);
List<ILineRegion> expandToLines = TextRegions.expandToLines(impactRegion, getLeadingLines(), getTrailingLines());
return TextRegions.merge(expandToLines);
}
return null;
}
use of org.eclipse.xtext.formatting2.regionaccess.ILineRegion in project xtext-core by eclipse.
the class TextRegionsWithTitleToString method getFrame.
public ITextSegment getFrame() {
if (this.frame != null)
return this.frame;
ITextRegionAccess access = getTextRegionAccess();
if (access != null) {
List<ITextSegment> segments = Lists.newArrayList();
for (Item item : items) segments.add(item.getRegion());
ITextSegment impactRegion = merge(segments);
List<ILineRegion> expandToLines = expandToLines(impactRegion, getLeadingLines(), getTrailingLines());
return merge(expandToLines);
}
return null;
}
use of org.eclipse.xtext.formatting2.regionaccess.ILineRegion in project xtext-core by eclipse.
the class MultilineCommentReplacer method createReplacements.
@Override
public ITextReplacerContext createReplacements(ITextReplacerContext context) {
if (!multiline)
return context;
IComment comment = getComment();
ITextRegionAccess access = comment.getTextRegionAccess();
List<ILineRegion> lines = comment.getLineRegions();
String oldIndentation = lines.get(0).getIndentation().getText();
String indentationString = context.getIndentationString();
String newIndentation = indentationString + " " + prefix + " ";
for (int i = 1; i < lines.size() - 1; i++) {
ITextSegment line = lines.get(i);
String text = line.getText();
int prefixOffset = prefixOffset(text);
ITextSegment target;
if (prefixOffset >= 0)
target = access.regionForOffset(line.getOffset(), prefixOffset + 1);
else if (text.startsWith(oldIndentation))
target = access.regionForOffset(line.getOffset(), oldIndentation.length());
else
target = access.regionForOffset(line.getOffset(), 0);
context.addReplacement(target.replaceWith(newIndentation));
}
if (lines.size() > 1) {
ILineRegion line = lines.get(lines.size() - 1);
context.addReplacement(line.getIndentation().replaceWith(indentationString + " "));
}
return context;
}
Aggregations