Search in sources :

Example 46 with XtextResource

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

the class ExtractMethodUserInputPage method createSignaturePreview.

protected void createSignaturePreview(Composite composite) {
    Label previewLabel = new Label(composite, SWT.NONE);
    previewLabel.setText("Method signature preview:");
    GridData gridData = new GridData(SWT.FILL);
    gridData.horizontalSpan = 2;
    previewLabel.setLayoutData(gridData);
    signaturePreview = editorFactory.newEditor(new IEditedResourceProvider() {

        @Override
        public XtextResource createResource() {
            URI resourceURI = EcoreUtil2.getPlatformResourceOrNormalizedURI(refactoring.getXtendClass()).trimFragment();
            IProject project = projectUtil.getProject(resourceURI);
            ResourceSet resourceSet = resourceSetProvider.get(project);
            return (XtextResource) resourceSet.getResource(resourceURI, true);
        }
    }).readOnly().withParent(composite);
    GridData gridData2 = new GridData(GridData.FILL_HORIZONTAL);
    gridData2.horizontalSpan = 2;
    signaturePreview.getViewer().getControl().setLayoutData(gridData2);
    partialEditor = signaturePreview.createPartialEditor(getPartialEditorModelPrefix(), refactoring.getMethodSignature(), getPartialEditorModelSuffix(), true);
}
Also used : IEditedResourceProvider(org.eclipse.xtext.ui.editor.embedded.IEditedResourceProvider) Label(org.eclipse.swt.widgets.Label) GridData(org.eclipse.swt.layout.GridData) XtextResource(org.eclipse.xtext.resource.XtextResource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) URI(org.eclipse.emf.common.util.URI) IProject(org.eclipse.core.resources.IProject)

Example 47 with XtextResource

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

the class XtendFileRenameParticipant method createRenameElementContexts.

@Override
protected List<? extends IRenameElementContext> createRenameElementContexts(Object element) {
    if (super.getNewName().endsWith(".xtend")) {
        IFile file = (IFile) element;
        final IPath filePath = file.getFullPath();
        final IPath newPath = file.getFullPath().removeLastSegments(1).append(getNewName() + ".xtend");
        String className = trimFileExtension(file.getName());
        if (className != null) {
            ResourceSet resourceSet = resourceSetProvider.get(file.getProject());
            URI resourceURI = URI.createPlatformResourceURI(file.getFullPath().toString(), true);
            Resource resource = resourceSet.getResource(resourceURI, true);
            if (resource != null && !resource.getContents().isEmpty()) {
                for (XtendTypeDeclaration type : EcoreUtil2.eAllOfType(resource.getContents().get(0), XtendTypeDeclaration.class)) {
                    if (equal(className, type.getName())) {
                        IRenameElementContext renameElementContext = renameContextFactory.createRenameElementContext(type, null, null, (XtextResource) resource);
                        if (renameElementContext instanceof IChangeRedirector.Aware)
                            ((IChangeRedirector.Aware) renameElementContext).setChangeRedirector(new IChangeRedirector() {

                                @Override
                                public IPath getRedirectedPath(IPath source) {
                                    return source.equals(filePath) ? newPath : source;
                                }
                            });
                        return singletonList(renameElementContext);
                    }
                }
            }
        }
    }
    return super.createRenameElementContexts(element);
}
Also used : IFile(org.eclipse.core.resources.IFile) IChangeRedirector(org.eclipse.xtext.ui.refactoring.IChangeRedirector) IPath(org.eclipse.core.runtime.IPath) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) IRenameElementContext(org.eclipse.xtext.ui.refactoring.ui.IRenameElementContext) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) URI(org.eclipse.emf.common.util.URI)

Example 48 with XtextResource

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

the class ParserTest method testAllGrammarElementsUniqueAfterReparse.

@Test
public void testAllGrammarElementsUniqueAfterReparse() throws Exception {
    String text = "class Foo { def m() { newArrayList() } }";
    XtendClass clazz = clazz(text);
    XtextResource resource = (XtextResource) clazz.eResource();
    resource.update(text.indexOf('m'), 0, "m");
    ICompositeNode root = resource.getParseResult().getRootNode();
    assertSame(root, root.getRootNode());
    Set<EObject> grammarElements = Sets.newHashSet();
    for (INode node : root.getAsTreeIterable()) {
        if (node instanceof ICompositeNode) {
            if (node.getGrammarElement() == null) {
                fail("node without grammar element");
            }
            if (!grammarElements.add(node.getGrammarElement())) {
                fail(node.getGrammarElement().toString());
            }
        }
    }
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) XtendClass(org.eclipse.xtend.core.xtend.XtendClass) EObject(org.eclipse.emf.ecore.EObject) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) XtextResource(org.eclipse.xtext.resource.XtextResource) RichString(org.eclipse.xtend.core.xtend.RichString) Test(org.junit.Test)

Example 49 with XtextResource

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

the class PartialParserTest method testEqualModels_05.

@Test
public void testEqualModels_05() throws Exception {
    String model = "package org.eclipse.xtend.core.tests.smoke\n" + "\n" + "import org.eclipse.emf.ecore.EClass\n" + "import org.eclipse.emf.ecore.EPackage\n" + "import org.eclipse.emf.ecore.EStructuralFeature\n" + "import org.eclipse.emf.ecore.EObject\n" + "\n" + "class Case_6 {\n" + "\n" + "	def dispatch transform(EClass model) {\n" + "		model.ETypeParameters.map(e|transform(e))\n" + "	}\n" + "	 \n" + "	def dispatch transform(EPackage packageDecl) {\n" + "		packageDecl.eContents.map(e | transform(e as EStructuralFeature))\n" + "	}\n" + "	\n" + "	def dispatch transform(EStructuralFeature entity) {\n" + "		val inferredType = null\n" + "		newArrayList";
    XtextResource resource = doTestUpdateAtOffset(model, 524, 0, "(i", "Case_6.xtend");
    compareWithNewResource(resource, model + "(", 525, 1, "i", "Case_6.xtend");
    validateWithoutException(resource);
}
Also used : XtextResource(org.eclipse.xtext.resource.XtextResource) Test(org.junit.Test)

Example 50 with XtextResource

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

the class PartialParserTest method testBug480818.

@Test
public void testBug480818() throws Exception {
    String code = "class Example {\n" + " val greeting = 'hello world!'\n" + " def sayHello() {\n" + "   println(greeting)\n" + "   prinntln(greeting)\n" + " }}";
    XtextResource resource = createResource(code, "Example.xtend");
    String before = EmfFormatter.listToStr(resource.getContents());
    resource.update(code.lastIndexOf("prinntln") + 2, 1, "i");
    // here before bug 480818 was fixed a StackOverflowException occured
    String after = EmfFormatter.listToStr(resource.getContents());
    assertEquals(before, after);
}
Also used : 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