Search in sources :

Example 36 with TextRegion

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

the class EObjectAtOffsetTest method testGetCrossReferenceNode.

@Test
public void testGetCrossReferenceNode() throws Exception {
    String firstPart = "foo Foo1=X.Y foo Foo2=X.Y zonk bar Bar1 Foo1=X.Y";
    String modelAsString = firstPart + ",Foo2=X.Y";
    XtextResource resource = getResourceFromString(modelAsString);
    INode node = eObjectAtOffsetHelper.getCrossReferenceNode(resource, new TextRegion(firstPart.length(), 0));
    assertEquals(firstPart.length() - 8, node.getOffset());
    assertEquals(8, node.getLength());
    assertNull(eObjectAtOffsetHelper.getCrossReferenceNode(resource, new TextRegion(firstPart.length(), 1)));
    node = eObjectAtOffsetHelper.getCrossReferenceNode(resource, new TextRegion(firstPart.length() + 1, 0));
    assertEquals(firstPart.length() + 1, node.getOffset());
    assertEquals(8, node.getLength());
    node = eObjectAtOffsetHelper.getCrossReferenceNode(resource, new TextRegion(firstPart.length() + 1, 5));
    assertEquals(firstPart.length() + 1, node.getOffset());
    assertEquals(8, node.getLength());
    node = eObjectAtOffsetHelper.getCrossReferenceNode(resource, new TextRegion(firstPart.length() + 1, 8));
    assertEquals(firstPart.length() + 1, node.getOffset());
    assertEquals(8, node.getLength());
    assertNull(eObjectAtOffsetHelper.getCrossReferenceNode(resource, new TextRegion(firstPart.length() + 1, 9)));
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) TextRegion(org.eclipse.xtext.util.TextRegion) Test(org.junit.Test)

Example 37 with TextRegion

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

the class TraceRegionToStringTest method twoFrame2.

@Test
public void twoFrame2() {
    final TraceRegionToStringTester tester = new TraceRegionToStringTester();
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("foo bar");
    _builder.newLine();
    tester.setLocalText(_builder.toString());
    TextRegion _textRegion = new TextRegion(2, 4);
    tester.setLocalFrame(_textRegion);
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("baz buz");
    _builder_1.newLine();
    tester.setRemote1(_builder_1.toString());
    SourceRelativeURI _uri1 = tester.getUri1();
    TraceRegionToStringTester.Location _location = new TraceRegionToStringTester.Location(1, 5, _uri1);
    TraceRegionToStringTester.Region _region = new TraceRegionToStringTester.Region(0, 7, Collections.<ILocationData>unmodifiableList(CollectionLiterals.<ILocationData>newArrayList(_location)));
    final Procedure1<TraceRegionToStringTester.Region> _function = (TraceRegionToStringTester.Region it) -> {
        TraceRegionToStringTester.Location _location_1 = new TraceRegionToStringTester.Location(1, 5);
        it.addChild(2, 1, Collections.<ILocationData>unmodifiableList(CollectionLiterals.<ILocationData>newArrayList(_location_1)));
        TraceRegionToStringTester.Location _location_2 = new TraceRegionToStringTester.Location(1, 5);
        it.addChild(5, 1, Collections.<ILocationData>unmodifiableList(CollectionLiterals.<ILocationData>newArrayList(_location_2)));
    };
    TraceRegionToStringTester.Region _doubleArrow = ObjectExtensions.<TraceRegionToStringTester.Region>operator_doubleArrow(_region, _function);
    tester.setTrace(_doubleArrow);
    StringConcatenation _builder_2 = new StringConcatenation();
    _builder_2.append("---- local1 ---- | ---- remote1 ----");
    _builder_2.newLine();
    _builder_2.append("[1[o]1] b[2[a]2] | b[1,2[az bu]2,1]z");
    _builder_2.newLine();
    _builder_2.append("------------------------------------");
    _builder_2.newLine();
    _builder_2.append("1: D 2-1 Region -> Location[1,5]");
    _builder_2.newLine();
    _builder_2.append("2: D 5-1 Region -> Location[1,5]");
    _builder_2.newLine();
    this.operator_tripleEquals(tester, _builder_2);
}
Also used : TextRegion(org.eclipse.xtext.util.TextRegion) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) TextRegion(org.eclipse.xtext.util.TextRegion) TraceRegionToStringTester(org.eclipse.xtext.generator.trace.TraceRegionToStringTester) SourceRelativeURI(org.eclipse.xtext.generator.trace.SourceRelativeURI) ILocationData(org.eclipse.xtext.generator.trace.ILocationData) Test(org.junit.Test)

Example 38 with TextRegion

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

the class TraceRegionToStringTest method twoFrame1.

@Test
public void twoFrame1() {
    final TraceRegionToStringTester tester = new TraceRegionToStringTester();
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("foo bar");
    _builder.newLine();
    tester.setLocalText(_builder.toString());
    TextRegion _textRegion = new TextRegion(1, 5);
    tester.setLocalFrame(_textRegion);
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("baz buz");
    _builder_1.newLine();
    tester.setRemote1(_builder_1.toString());
    SourceRelativeURI _uri1 = tester.getUri1();
    TraceRegionToStringTester.Location _location = new TraceRegionToStringTester.Location(1, 5, _uri1);
    TraceRegionToStringTester.Region _region = new TraceRegionToStringTester.Region(0, 7, Collections.<ILocationData>unmodifiableList(CollectionLiterals.<ILocationData>newArrayList(_location)));
    final Procedure1<TraceRegionToStringTester.Region> _function = (TraceRegionToStringTester.Region it) -> {
        TraceRegionToStringTester.Location _location_1 = new TraceRegionToStringTester.Location(1, 5);
        it.addChild(2, 1, Collections.<ILocationData>unmodifiableList(CollectionLiterals.<ILocationData>newArrayList(_location_1)));
        TraceRegionToStringTester.Location _location_2 = new TraceRegionToStringTester.Location(1, 5);
        it.addChild(5, 1, Collections.<ILocationData>unmodifiableList(CollectionLiterals.<ILocationData>newArrayList(_location_2)));
    };
    TraceRegionToStringTester.Region _doubleArrow = ObjectExtensions.<TraceRegionToStringTester.Region>operator_doubleArrow(_region, _function);
    tester.setTrace(_doubleArrow);
    StringConcatenation _builder_2 = new StringConcatenation();
    _builder_2.append("---- local1 ----- | ---- remote1 ----");
    _builder_2.newLine();
    _builder_2.append("o[1[o]1] b[2[a]2] | b[1,2[az bu]2,1]z");
    _builder_2.newLine();
    _builder_2.append("-------------------------------------");
    _builder_2.newLine();
    _builder_2.append("1: D 2-1 Region -> Location[1,5]");
    _builder_2.newLine();
    _builder_2.append("2: D 5-1 Region -> Location[1,5]");
    _builder_2.newLine();
    this.operator_tripleEquals(tester, _builder_2);
}
Also used : TextRegion(org.eclipse.xtext.util.TextRegion) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) TextRegion(org.eclipse.xtext.util.TextRegion) TraceRegionToStringTester(org.eclipse.xtext.generator.trace.TraceRegionToStringTester) SourceRelativeURI(org.eclipse.xtext.generator.trace.SourceRelativeURI) ILocationData(org.eclipse.xtext.generator.trace.ILocationData) Test(org.junit.Test)

Example 39 with TextRegion

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

the class XtextResource method update.

public void update(int offset, int replacedTextLength, String newText) {
    if (!isLoaded()) {
        throw new IllegalStateException("You can't update an unloaded resource.");
    }
    try {
        isUpdating = true;
        IParseResult oldParseResult = parseResult;
        ReplaceRegion replaceRegion = new ReplaceRegion(new TextRegion(offset, replacedTextLength), newText);
        IParseResult newParseResult;
        ParserRule oldEntryPoint = NodeModelUtils.getEntryParserRule(oldParseResult.getRootNode());
        if (entryPoint == null || entryPoint == oldEntryPoint) {
            newParseResult = parser.reparse(oldParseResult, replaceRegion);
        } else {
            StringBuilder builder = new StringBuilder(oldParseResult.getRootNode().getText());
            replaceRegion.applyTo(builder);
            newParseResult = parser.parse(entryPoint, new StringReader(builder.toString()));
        }
        updateInternalState(oldParseResult, newParseResult);
    } finally {
        isUpdating = false;
    }
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) TextRegion(org.eclipse.xtext.util.TextRegion) ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) StringReader(java.io.StringReader) IParseResult(org.eclipse.xtext.parser.IParseResult)

Example 40 with TextRegion

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

the class FormattableDocumentTest method shouldFormat.

@Test
public void shouldFormat() {
    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 false;
            }
        });
        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)

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