Search in sources :

Example 41 with XtextResource

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

the class AbstractXtendTestCase method files.

protected Iterable<XtendFile> files(boolean validate, String... contents) throws Exception {
    XtextResourceSet set = getResourceSet();
    List<XtendFile> result = newArrayList();
    for (String string : contents) {
        String fileName = getFileName(string);
        Resource resource = set.createResource(URI.createURI(fileName + ".xtend"));
        resource.load(new StringInputStream(string), null);
        assertEquals(resource.getErrors().toString(), 0, resource.getErrors().size());
    }
    for (Resource resource : new ArrayList<Resource>(set.getResources())) {
        XtendFile file = (XtendFile) resource.getContents().get(0);
        result.add(file);
    }
    if (validate) {
        for (XtendFile file : result) {
            List<Issue> issues = ((XtextResource) file.eResource()).getResourceServiceProvider().getResourceValidator().validate(file.eResource(), CheckMode.ALL, CancelIndicator.NullImpl);
            assertTrue("Resource contained errors : " + issues.toString(), issues.isEmpty());
        }
    }
    return result;
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) StringInputStream(org.eclipse.xtext.util.StringInputStream) Issue(org.eclipse.xtext.validation.Issue) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) ArrayList(java.util.ArrayList) XtextResource(org.eclipse.xtext.resource.XtextResource)

Example 42 with XtextResource

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

the class AbstractXtendContentAssistBugTest method getResourceFor.

@Override
public XtextResource getResourceFor(InputStream stream) {
    try {
        XtextResourceSet set = get(XtextResourceSet.class);
        XtextResource result = (XtextResource) set.createResource(URI.createURI("Test." + testHelper.getFileExtension()));
        result.load(stream, null);
        initializeTypeProvider(result);
        return result;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) XtextResource(org.eclipse.xtext.resource.XtextResource) CoreException(org.eclipse.core.runtime.CoreException)

Example 43 with XtextResource

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

the class ConvertJavaCode method formatXtendCode.

private String formatXtendCode(IFile xtendFile, final String xtendCode) {
    try {
        XtextResource resource = (XtextResource) createResource(xtendFile, xtendCode);
        ITextRegionAccess regionAccess = regionAccessBuilder.get().forNodeModel(resource).create();
        FormatterRequest request = new FormatterRequest();
        request.setAllowIdentityEdits(false);
        request.setTextRegionAccess(regionAccess);
        request.setPreferences(TypedPreferenceValues.castOrWrap(cfgProvider.getPreferenceValues(resource)));
        List<ITextReplacement> replacements = formatter.format(request);
        String formatted = regionAccess.getRewriter().renderToString(replacements);
        return formatted;
    } catch (Exception e) {
        LOG.error("Formatting step canceled due to an exception.", e);
        return null;
    }
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) XtextResource(org.eclipse.xtext.resource.XtextResource) FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest) CoreException(org.eclipse.core.runtime.CoreException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JavaModelException(org.eclipse.jdt.core.JavaModelException) IOException(java.io.IOException) ExecutionException(org.eclipse.core.commands.ExecutionException)

Example 44 with XtextResource

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

the class ExtractMethodRefactoring method initialize.

public boolean initialize(XtextEditor editor, List<XExpression> selectedExpressions, boolean doSave) {
    if (selectedExpressions.isEmpty() || editor.getDocument() == null)
        return false;
    this.document = editor.getDocument();
    this.doSave = doSave;
    this.editor = editor;
    this.expressions = calculateExpressions(selectedExpressions);
    this.firstExpression = this.expressions.get(0);
    this.originalMethod = EcoreUtil2.getContainerOfType(firstExpression, XtendFunction.class);
    this.lastExpression = this.expressions.get(this.expressions.size() - 1);
    this.resourceURI = EcoreUtil2.getPlatformResourceOrNormalizedURI(firstExpression).trimFragment();
    this.xtendClass = EcoreUtil2.getContainerOfType(firstExpression, XtendClass.class);
    if (xtendClass == null || originalMethod == null)
        return false;
    this.visibility = JvmVisibility.PROTECTED;
    this.isStatic = originalMethod.isStatic();
    this.isExplicitlyDeclareReturnType = true;
    XExpression successorExpression = expressionUtil.findSuccessorExpressionForVariableDeclaration(lastExpression);
    nameUtil.setFeatureScopeContext(successorExpression);
    rewriter = rewriterFactory.create(document, (XtextResource) firstExpression.eResource());
    return true;
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) XtendClass(org.eclipse.xtend.core.xtend.XtendClass) XExpression(org.eclipse.xtext.xbase.XExpression) XtextResource(org.eclipse.xtext.resource.XtextResource)

Example 45 with XtextResource

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

the class XtendQuickfixProvider method specifyTypeExplicitly.

@Fix(IssueCodes.API_TYPE_INFERENCE)
public void specifyTypeExplicitly(Issue issue, IssueResolutionAcceptor acceptor) {
    acceptor.accept(issue, "Infer type", "Infer type", null, new ISemanticModification() {

        @Override
        public void apply(EObject element, IModificationContext context) throws Exception {
            EStructuralFeature featureAfterType = null;
            JvmIdentifiableElement jvmElement = null;
            if (element instanceof XtendFunction) {
                XtendFunction function = (XtendFunction) element;
                if (function.getCreateExtensionInfo() == null) {
                    featureAfterType = XtendPackage.Literals.XTEND_FUNCTION__NAME;
                } else {
                    featureAfterType = XtendPackage.Literals.XTEND_FUNCTION__CREATE_EXTENSION_INFO;
                }
                jvmElement = associations.getDirectlyInferredOperation((XtendFunction) element);
            } else if (element instanceof XtendField) {
                featureAfterType = XtendPackage.Literals.XTEND_FIELD__NAME;
                jvmElement = associations.getJvmField((XtendField) element);
            }
            if (jvmElement != null) {
                LightweightTypeReference type = batchTypeResolver.resolveTypes(element).getActualType(jvmElement);
                INode node = Iterables.getFirst(NodeModelUtils.findNodesForFeature(element, featureAfterType), null);
                if (node == null) {
                    throw new IllegalStateException("Could not determine node for " + element);
                }
                if (type == null) {
                    throw new IllegalStateException("Could not determine type for " + element);
                }
                ReplacingAppendable appendable = appendableFactory.create(context.getXtextDocument(), (XtextResource) element.eResource(), node.getOffset(), 0);
                appendable.append(type);
                appendable.append(" ");
                appendable.commitChanges();
            }
        }
    });
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) INode(org.eclipse.xtext.nodemodel.INode) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) ISemanticModification(org.eclipse.xtext.ui.editor.model.edit.ISemanticModification) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) XtextResource(org.eclipse.xtext.resource.XtextResource) ReplacingAppendable(org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable) CoreException(org.eclipse.core.runtime.CoreException) BadLocationException(org.eclipse.jface.text.BadLocationException) XtendField(org.eclipse.xtend.core.xtend.XtendField) EObject(org.eclipse.emf.ecore.EObject) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

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