Search in sources :

Example 21 with TextRegion

use of org.eclipse.xtext.util.TextRegion in project xtext-core by eclipse.

the class NodeModelStreamer method feedTokenStream.

@Override
public ITextRegion feedTokenStream(ITokenStream out, ICompositeNode in, int offset, int length) throws IOException {
    List<INode> nodes = getLeafs(in, offset, offset + length);
    if (nodes.isEmpty())
        return new TextRegion(in.getOffset(), 0);
    if (out instanceof ITokenStreamExtension)
        ((ITokenStreamExtension) out).init(findRootRuleForRegion(nodes.get(0)));
    boolean lastIsTokenOrComment = false;
    for (INode node : nodes) {
        boolean currentIsTokenOrComment = tokenUtil.isCommentNode(node) || tokenUtil.isToken(node);
        if (lastIsTokenOrComment && currentIsTokenOrComment)
            writeHiddenEmpty(out);
        lastIsTokenOrComment = currentIsTokenOrComment;
        if (node instanceof ILeafNode) {
            ILeafNode leaf = (ILeafNode) node;
            if (leaf.isHidden())
                writeHidden(out, leaf);
            else
                writeSemantic(out, leaf);
        } else if (node instanceof ICompositeNode)
            writeSemantic(out, (ICompositeNode) node);
    }
    out.flush();
    int rStart = nodes.get(0).getOffset();
    int rLength = nodes.get(nodes.size() - 1).getEndOffset() - rStart;
    return new TextRegion(rStart, rLength);
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) ITokenStreamExtension(org.eclipse.xtext.parsetree.reconstr.ITokenStreamExtension)

Example 22 with TextRegion

use of org.eclipse.xtext.util.TextRegion in project xtext-core by eclipse.

the class FormattableDocumentTest method shouldFormat_02.

@Test(expected = IllegalStateException.class)
public void shouldFormat_02() {
    final Procedure1<GenericFormatterTestRequest> _function = (GenericFormatterTestRequest it) -> {
        Collection<ITextRegion> _regions = it.getRequest().getRegions();
        TextRegion _textRegion = new TextRegion(0, 6);
        _regions.add(_textRegion);
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("idlist");
        it.setToBeFormatted(_builder);
        it.setFormatter(new GenericFormatter() {

            @Override
            protected void format(final EObject model, final ITextRegionExtensions regionAccess, final IFormattableDocument document) {
                throw new IllegalStateException("this method should never be called");
            }

            @Override
            public boolean shouldFormat(final Object obj, final IFormattableDocument document) {
                return true;
            }
        });
        StringConcatenation _builder_1 = new StringConcatenation();
        _builder_1.append("idlist");
        it.setExpectation(_builder_1);
    };
    this._genericFormatterTester.assertFormatted(_function);
}
Also used : GenericFormatterTestRequest(org.eclipse.xtext.formatting2.internal.GenericFormatterTestRequest) GenericFormatter(org.eclipse.xtext.formatting2.internal.GenericFormatter) TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) EObject(org.eclipse.emf.ecore.EObject) IFormattableDocument(org.eclipse.xtext.formatting2.IFormattableDocument) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Collection(java.util.Collection) EObject(org.eclipse.emf.ecore.EObject) ITextRegionExtensions(org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions) Test(org.junit.Test)

Example 23 with TextRegion

use of org.eclipse.xtext.util.TextRegion in project xtext-core by eclipse.

the class FormattableDocumentTest method aroundDocument.

@Test
public void aroundDocument() {
    final Procedure1<GenericFormatterTestRequest> _function = (GenericFormatterTestRequest it) -> {
        Collection<ITextRegion> _regions = it.getRequest().getRegions();
        TextRegion _textRegion = new TextRegion(0, 6);
        _regions.add(_textRegion);
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("idlist");
        it.setToBeFormatted(_builder);
        final GenericFormatter<IDList> _function_1 = new GenericFormatter<IDList>() {

            @Override
            protected void format(final IDList model, @Extension final ITextRegionExtensions regions, @Extension final IFormattableDocument document) {
                final Procedure1<IHiddenRegionFormatter> _function = (IHiddenRegionFormatter it_1) -> {
                    it_1.setSpace("!");
                };
                document.surround(regions.regionFor(model).keyword("idlist"), _function);
            }
        };
        it.setFormatter(_function_1);
        StringConcatenation _builder_1 = new StringConcatenation();
        _builder_1.append("!idlist!");
        it.setExpectation(_builder_1);
    };
    this._genericFormatterTester.assertFormatted(_function);
}
Also used : GenericFormatterTestRequest(org.eclipse.xtext.formatting2.internal.GenericFormatterTestRequest) TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) IHiddenRegionFormatter(org.eclipse.xtext.formatting2.IHiddenRegionFormatter) IFormattableDocument(org.eclipse.xtext.formatting2.IFormattableDocument) IDList(org.eclipse.xtext.formatting2.internal.formattertestlanguage.IDList) ITextRegionExtensions(org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions) GenericFormatter(org.eclipse.xtext.formatting2.internal.GenericFormatter) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Collection(java.util.Collection) Test(org.junit.Test)

Example 24 with TextRegion

use of org.eclipse.xtext.util.TextRegion in project xtext-core by eclipse.

the class AbstractTraceRegionToString method render.

protected List<String> render(final AbstractTraceRegionToString.File file, final int width) {
    try {
        String _xifexpression = null;
        if ((file.uri == null)) {
            _xifexpression = this.getLocalText();
        } else {
            _xifexpression = this.getRemoteText(file.uri);
        }
        final String text = _xifexpression;
        ITextRegion _elvis = null;
        ITextRegion _xifexpression_1 = null;
        if ((file.uri == null)) {
            _xifexpression_1 = this.getLocalFrame();
        } else {
            _xifexpression_1 = this.getRemoteFrame(file.uri);
        }
        if (_xifexpression_1 != null) {
            _elvis = _xifexpression_1;
        } else {
            int _length = text.length();
            TextRegion _textRegion = new TextRegion(0, _length);
            _elvis = _textRegion;
        }
        final ITextRegion frame = _elvis;
        final Function1<AbstractTraceRegionToString.Insert, Boolean> _function = (AbstractTraceRegionToString.Insert it) -> {
            return Boolean.valueOf(((it.offset >= frame.getOffset()) && (it.offset <= (frame.getOffset() + frame.getLength()))));
        };
        final Iterable<AbstractTraceRegionToString.Insert> inframe = IterableExtensions.<AbstractTraceRegionToString.Insert>filter(file.inserts, _function);
        final Function<AbstractTraceRegionToString.Insert, Integer> _function_1 = (AbstractTraceRegionToString.Insert it) -> {
            return Integer.valueOf(it.offset);
        };
        final Function1<Map.Entry<Integer, Collection<AbstractTraceRegionToString.Insert>>, Integer> _function_2 = (Map.Entry<Integer, Collection<AbstractTraceRegionToString.Insert>> it) -> {
            return it.getKey();
        };
        final List<Map.Entry<Integer, Collection<AbstractTraceRegionToString.Insert>>> offsets = IterableExtensions.<Map.Entry<Integer, Collection<AbstractTraceRegionToString.Insert>>, Integer>sortBy(IterableExtensions.<Map.Entry<Integer, Collection<AbstractTraceRegionToString.Insert>>>toList(Multimaps.<Integer, AbstractTraceRegionToString.Insert>index(inframe, _function_1).asMap().entrySet()), _function_2);
        int last = frame.getOffset();
        final StringBuilder result = new StringBuilder();
        for (final Map.Entry<Integer, Collection<AbstractTraceRegionToString.Insert>> e : offsets) {
            {
                final Integer offset = e.getKey();
                final String insert = this.render(e.getValue(), width);
                final String prefix = text.substring(last, (offset).intValue());
                result.append(prefix);
                result.append(insert);
                last = (offset).intValue();
            }
        }
        int _offset = frame.getOffset();
        int _length_1 = frame.getLength();
        final int end = (_offset + _length_1);
        if ((last < end)) {
            result.append(text.substring(last, end));
        }
        String _string = result.toString();
        StringReader _stringReader = new StringReader(_string);
        return CharStreams.readLines(_stringReader);
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) StringReader(java.io.StringReader) Collection(java.util.Collection) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 25 with TextRegion

use of org.eclipse.xtext.util.TextRegion in project xtext-xtend by eclipse.

the class XtendHighlightingCalculatorTest method addPosition.

@Override
public void addPosition(int offset, int length, String... ids) {
    if (logEnabled) {
        System.out.print("acceptor.addPosition(" + (offset - 22) + ", " + length);
        for (String id : ids) {
            System.out.print(", \"" + id + "\"");
        }
        System.out.println(");");
    }
    assertTrue("length = " + length, length >= 0);
    TextRegion region = new TextRegion(offset, length);
    assertEquals(1, ids.length);
    // I deactivated the following check as it is to strict/to simple in the face of the new fine-grained coloring
    // assertFalse(region.toString() + " is not contained in " + expectedRegions, expectedRegions.isEmpty());
    Collection<String> expectedIds = expectedRegions.get(region);
    if (strictMode && expectedIds.isEmpty())
        fail("No Region for (offset: " + offset + ", length : " + length + ", id : " + ids[0] + ") expected.");
    if (expectedIds.size() > 0)
        assertTrue("expected: " + expectedRegions.toString() + " but was: " + region + " (" + ids[0] + ")", expectedIds.contains(ids[0]));
    if (expectedIds.contains(ids[0]))
        expectedRegions.remove(region, ids[0]);
    Collection<String> unexpectedIds = explicitNotExpectedRegions.get(region);
    assertFalse("unexpected highlighting as position: " + region + " (" + ids[0] + ")", unexpectedIds.contains(ids[0]));
}
Also used : TextRegion(org.eclipse.xtext.util.TextRegion)

Aggregations

TextRegion (org.eclipse.xtext.util.TextRegion)41 ITextRegion (org.eclipse.xtext.util.ITextRegion)28 Test (org.junit.Test)19 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)6 Collection (java.util.Collection)5 StringReader (java.io.StringReader)3 CommonToken (org.antlr.runtime.CommonToken)3 IFile (org.eclipse.core.resources.IFile)3 IFormattableDocument (org.eclipse.xtext.formatting2.IFormattableDocument)3 GenericFormatter (org.eclipse.xtext.formatting2.internal.GenericFormatter)3 GenericFormatterTestRequest (org.eclipse.xtext.formatting2.internal.GenericFormatterTestRequest)3 ITextRegionExtensions (org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions)3 SourceRelativeURI (org.eclipse.xtext.generator.trace.SourceRelativeURI)3 IEclipseTrace (org.eclipse.xtext.ui.generator.trace.IEclipseTrace)3 EObject (org.eclipse.emf.ecore.EObject)2 ITextSelection (org.eclipse.jface.text.ITextSelection)2 AbstractElement (org.eclipse.xtext.AbstractElement)2 ILocationData (org.eclipse.xtext.generator.trace.ILocationData)2 ILocationInResource (org.eclipse.xtext.generator.trace.ILocationInResource)2 TraceRegionToStringTester (org.eclipse.xtext.generator.trace.TraceRegionToStringTester)2