Search in sources :

Example 1 with ILineRegion

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);
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) ILineRegion(org.eclipse.xtext.formatting2.regionaccess.ILineRegion)

Example 2 with ILineRegion

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));
        }
    }
}
Also used : ILineRegion(org.eclipse.xtext.formatting2.regionaccess.ILineRegion) TextRegionAccessToString(org.eclipse.xtext.formatting2.debug.TextRegionAccessToString)

Example 3 with ILineRegion

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;
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) ILineRegion(org.eclipse.xtext.formatting2.regionaccess.ILineRegion) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment)

Example 4 with ILineRegion

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;
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) ILineRegion(org.eclipse.xtext.formatting2.regionaccess.ILineRegion) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment)

Example 5 with ILineRegion

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;
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) IComment(org.eclipse.xtext.formatting2.regionaccess.IComment) ILineRegion(org.eclipse.xtext.formatting2.regionaccess.ILineRegion) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment)

Aggregations

ILineRegion (org.eclipse.xtext.formatting2.regionaccess.ILineRegion)8 ITextRegionAccess (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess)5 ITextSegment (org.eclipse.xtext.formatting2.regionaccess.ITextSegment)3 TextRegionAccessToString (org.eclipse.xtext.formatting2.debug.TextRegionAccessToString)2 IComment (org.eclipse.xtext.formatting2.regionaccess.IComment)1