Search in sources :

Example 56 with XtextResource

use of org.eclipse.xtext.resource.XtextResource in project xtext-xtend by eclipse.

the class ImportOrganizerTest method assertIsOrganizedTo.

protected void assertIsOrganizedTo(final CharSequence model, final CharSequence expected, final boolean validate) {
    try {
        Assert.assertFalse(expected.toString().contains("$"));
        final XtendFile xtendFile = this.file(model.toString(), validate);
        Resource _eResource = xtendFile.eResource();
        final List<ReplaceRegion> changes = this.importOrganizer.getOrganizedImportChanges(((XtextResource) _eResource));
        final StringBuilder builder = new StringBuilder(model);
        final Function1<ReplaceRegion, Integer> _function = (ReplaceRegion it) -> {
            return Integer.valueOf(it.getOffset());
        };
        final List<ReplaceRegion> sortedChanges = IterableExtensions.<ReplaceRegion, Integer>sortBy(changes, _function);
        ReplaceRegion lastChange = null;
        for (final ReplaceRegion it : sortedChanges) {
            {
                if (((lastChange != null) && (lastChange.getEndOffset() > it.getOffset()))) {
                    Assert.fail(((("Overlapping text edits: " + lastChange) + " and ") + it));
                }
                lastChange = it;
            }
        }
        List<ReplaceRegion> _reverse = ListExtensions.<ReplaceRegion>reverse(sortedChanges);
        for (final ReplaceRegion it_1 : _reverse) {
            int _offset = it_1.getOffset();
            int _offset_1 = it_1.getOffset();
            int _length = it_1.getLength();
            int _plus = (_offset_1 + _length);
            builder.replace(_offset, _plus, it_1.getText());
        }
        Assert.assertEquals(expected.toString(), builder.toString());
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource)

Example 57 with XtextResource

use of org.eclipse.xtext.resource.XtextResource in project xtext-xtend by eclipse.

the class InsertionOffsetTest method checkFieldInsertionOffset.

protected void checkFieldInsertionOffset(final CharSequence model) {
    try {
        final String modelAsString = model.toString();
        final int caretOffset = modelAsString.replace("$", "").indexOf("|");
        final int expectedOffset = modelAsString.replace("|", "").indexOf("$");
        XtendTypeDeclaration _head = IterableExtensions.<XtendTypeDeclaration>head(this.parseHelper.parse(modelAsString.replace("|", "").replace("$", "")).getXtendTypes());
        final XtendClass xtendClass = ((XtendClass) _head);
        Resource _eResource = xtendClass.eResource();
        final XtextResource xtextResource = ((XtextResource) _eResource);
        final EObject caretElement = this._eObjectAtOffsetHelper.resolveContainedElementAt(xtextResource, caretOffset);
        Assert.assertEquals(expectedOffset, this._insertionOffsets.getNewFieldInsertOffset(caretElement, xtendClass));
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : XtendClass(org.eclipse.xtend.core.xtend.XtendClass) EObject(org.eclipse.emf.ecore.EObject) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource)

Example 58 with XtextResource

use of org.eclipse.xtext.resource.XtextResource in project xtext-xtend by eclipse.

the class InsertionOffsetTest method checkMethodInsertionOffset.

protected void checkMethodInsertionOffset(final CharSequence model) {
    try {
        final String modelAsString = model.toString();
        final int caretOffset = modelAsString.replace("$", "").indexOf("|");
        final int expectedOffset = modelAsString.replace("|", "").indexOf("$");
        XtendTypeDeclaration _head = IterableExtensions.<XtendTypeDeclaration>head(this.parseHelper.parse(modelAsString.replace("|", "").replace("$", "")).getXtendTypes());
        final XtendClass xtendClass = ((XtendClass) _head);
        Resource _eResource = xtendClass.eResource();
        final XtextResource xtextResource = ((XtextResource) _eResource);
        final EObject caretElement = this._eObjectAtOffsetHelper.resolveContainedElementAt(xtextResource, caretOffset);
        Assert.assertEquals(expectedOffset, this._insertionOffsets.getNewMethodInsertOffset(caretElement, xtendClass));
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : XtendClass(org.eclipse.xtend.core.xtend.XtendClass) EObject(org.eclipse.emf.ecore.EObject) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource)

Example 59 with XtextResource

use of org.eclipse.xtext.resource.XtextResource in project xtext-xtend by eclipse.

the class HyperlinkingTest method testReturnTypeLabel.

@Test
public void testReturnTypeLabel() throws Exception {
    String modelAsString = "class Baz {def Baz meth(Baz baz, String str) { var vv='' x = baz j = str vv=j x.meth(baz,str) } var x = new Baz var j = '' }";
    XtextResource resource = (XtextResource) testHelper.xtendFile("Baz", modelAsString).eResource();
    int indexOf_x_FieldRef = modelAsString.indexOf("x");
    IHyperlink[] hyperlinks = hyperlinkHelper.createHyperlinksByOffset(resource, indexOf_x_FieldRef, true);
    assertEquals(2, hyperlinks.length);
    assertEquals("Open Declaration - x : Baz", hyperlinks[0].getHyperlinkText());
    assertEquals("Open Field Type - Baz", hyperlinks[1].getHyperlinkText());
    int indexOf_Baz_ParamRef = modelAsString.indexOf("baz", indexOf_x_FieldRef);
    hyperlinks = hyperlinkHelper.createHyperlinksByOffset(resource, indexOf_Baz_ParamRef, true);
    assertEquals(2, hyperlinks.length);
    assertEquals("Open Declaration - Baz baz", hyperlinks[0].getHyperlinkText());
    assertEquals("Open Parameter Type - Baz", hyperlinks[1].getHyperlinkText());
    int indexOf_j_FieldRef = modelAsString.indexOf("j", indexOf_Baz_ParamRef);
    hyperlinks = hyperlinkHelper.createHyperlinksByOffset(resource, indexOf_j_FieldRef, true);
    assertEquals(2, hyperlinks.length);
    assertEquals("Open Declaration - j : String", hyperlinks[0].getHyperlinkText());
    assertEquals("Open Field Type - String", hyperlinks[1].getHyperlinkText());
    assertEquals("String", ((JdtHyperlink) hyperlinks[1]).getJavaElement().getElementName());
    int indexOf_vv_VarRef = modelAsString.indexOf("vv", indexOf_j_FieldRef);
    hyperlinks = hyperlinkHelper.createHyperlinksByOffset(resource, indexOf_vv_VarRef, true);
    assertEquals(2, hyperlinks.length);
    assertEquals("Open Declaration - String vv", hyperlinks[0].getHyperlinkText());
    assertEquals("Open Variable Type - String", hyperlinks[1].getHyperlinkText());
    int indexOf_meth_MethodeCall = modelAsString.indexOf("meth", indexOf_vv_VarRef);
    hyperlinks = hyperlinkHelper.createHyperlinksByOffset(resource, indexOf_meth_MethodeCall, true);
    assertEquals(2, hyperlinks.length);
    assertEquals("Open Declaration - meth(Baz, String) : Baz", hyperlinks[0].getHyperlinkText());
    assertEquals("Open Return Type - Baz", hyperlinks[1].getHyperlinkText());
}
Also used : IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) XtextResource(org.eclipse.xtext.resource.XtextResource) JdtHyperlink(org.eclipse.xtext.common.types.xtext.ui.JdtHyperlink) Test(org.junit.Test)

Example 60 with XtextResource

use of org.eclipse.xtext.resource.XtextResource in project xtext-xtend by eclipse.

the class HyperlinkingTest method testOpenInferredTypeOnReference.

@Test
public void testOpenInferredTypeOnReference() throws Exception {
    String modelAsString = "class Baz { def void foo() { var myVar='' println(myVar) } }";
    XtextResource resource = (XtextResource) testHelper.xtendFile("Baz", modelAsString).eResource();
    int indexOf_x_FieldRef = modelAsString.indexOf("myVar)");
    IHyperlink[] hyperlinks = hyperlinkHelper.createHyperlinksByOffset(resource, indexOf_x_FieldRef, true);
    assertEquals(2, hyperlinks.length);
    assertEquals("Open Declaration - String myVar", hyperlinks[0].getHyperlinkText());
    assertEquals("Open Variable Type - String", hyperlinks[1].getHyperlinkText());
}
Also used : IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) XtextResource(org.eclipse.xtext.resource.XtextResource) Test(org.junit.Test)

Aggregations

XtextResource (org.eclipse.xtext.resource.XtextResource)627 Test (org.junit.Test)367 Resource (org.eclipse.emf.ecore.resource.Resource)107 EObject (org.eclipse.emf.ecore.EObject)99 XtextResourceSet (org.eclipse.xtext.resource.XtextResourceSet)67 StringInputStream (org.eclipse.xtext.util.StringInputStream)67 URI (org.eclipse.emf.common.util.URI)62 Diagnostic (org.eclipse.emf.common.util.Diagnostic)55 IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)55 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)46 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)40 Grammar (org.eclipse.xtext.Grammar)32 IUnitOfWork (org.eclipse.xtext.util.concurrent.IUnitOfWork)31 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)30 IFile (org.eclipse.core.resources.IFile)29 Issue (org.eclipse.xtext.validation.Issue)29 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)28 List (java.util.List)26 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)26 INode (org.eclipse.xtext.nodemodel.INode)23