Search in sources :

Example 1 with Result

use of org.eclipse.xtext.xbase.testing.CompilationTestHelper.Result in project xtext-xtend by eclipse.

the class XtendCompilerTester method compile.

public void compile(CharSequence source, final IAcceptor<CompilationResult> acceptor) {
    try {
        compilationTestHelper.configureFreshWorkspace();
        compilationTestHelper.compile(source, new IAcceptor<CompilationTestHelper.Result>() {

            @Override
            public void accept(final Result t) {
                acceptor.accept(new CompilationResult() {

                    @Override
                    public Map<String, String> getGeneratedCode() {
                        return t.getGeneratedCode();
                    }

                    @Override
                    public String getGeneratedCode(String typeName) {
                        return t.getGeneratedCode(typeName);
                    }

                    @Override
                    public String getSingleGeneratedCode() {
                        return t.getSingleGeneratedCode();
                    }

                    @Override
                    public Class<?> getCompiledClass() {
                        return t.getCompiledClass();
                    }

                    @Override
                    public Class<?> getCompiledClass(String className) {
                        return t.getCompiledClass(className);
                    }

                    @Override
                    public List<? extends Problem> getProblems(Element element) {
                        return getTransformationContext().getProblems(element);
                    }

                    @Override
                    public List<? extends Problem> getAllProblems() {
                        Resource resource = compilationUnitImpl.getXtendFile().eResource();
                        Iterable<EObjectDiagnosticImpl> issues = Iterables.filter(Iterables.concat(resource.getErrors(), resource.getWarnings()), EObjectDiagnosticImpl.class);
                        List<Problem> result = Lists.newArrayList(Iterables.transform(issues, new Function<EObjectDiagnosticImpl, Problem>() {

                            @Override
                            public Problem apply(EObjectDiagnosticImpl diag) {
                                ProblemImpl result = new ProblemImpl(diag.getCode(), diag.getMessage(), translateSeverity(diag.getSeverity()));
                                return result;
                            }

                            private org.eclipse.xtend.lib.macro.services.Problem.Severity translateSeverity(Severity severity) {
                                switch(severity) {
                                    case ERROR:
                                        return Problem.Severity.ERROR;
                                    case WARNING:
                                        return Problem.Severity.WARNING;
                                    case INFO:
                                        return Problem.Severity.INFO;
                                    case IGNORE:
                                        return Problem.Severity.IGNORE;
                                    default:
                                        throw new IllegalArgumentException(String.valueOf(severity));
                                }
                            }
                        }));
                        return result;
                    }

                    @Override
                    public Map<String, CharSequence> getAllGeneratedResources() {
                        return t.getAllGeneratedResources();
                    }

                    CompilationUnitImpl compilationUnitImpl;

                    {
                        compilationUnitImpl = compilationUnitProvider.get();
                        XtendFile xtendFile = (XtendFile) t.getResourceSet().getResources().get(0).getContents().get(0);
                        compilationUnitImpl.setXtendFile(xtendFile);
                    }

                    @Override
                    public CompilationUnit getCompilationUnit() {
                        return compilationUnitImpl;
                    }

                    @Override
                    public TransformationContext getTransformationContext() {
                        TransformationContextImpl transformationContextImpl = new TransformationContextImpl();
                        transformationContextImpl.setUnit(compilationUnitImpl);
                        return transformationContextImpl;
                    }
                });
            }
        });
    } catch (IOException e) {
        Exceptions.sneakyThrow(e);
    }
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) Element(org.eclipse.xtend.lib.macro.declaration.Element) Resource(org.eclipse.emf.ecore.resource.Resource) Severity(org.eclipse.xtext.diagnostics.Severity) IOException(java.io.IOException) Result(org.eclipse.xtext.xbase.testing.CompilationTestHelper.Result) Function(com.google.common.base.Function) EObjectDiagnosticImpl(org.eclipse.xtext.validation.EObjectDiagnosticImpl) ProblemImpl(org.eclipse.xtend.core.macro.declaration.ProblemImpl) Problem(org.eclipse.xtend.lib.macro.services.Problem) TransformationContextImpl(org.eclipse.xtend.core.macro.TransformationContextImpl)

Aggregations

Function (com.google.common.base.Function)1 IOException (java.io.IOException)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 TransformationContextImpl (org.eclipse.xtend.core.macro.TransformationContextImpl)1 CompilationUnitImpl (org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl)1 ProblemImpl (org.eclipse.xtend.core.macro.declaration.ProblemImpl)1 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)1 Element (org.eclipse.xtend.lib.macro.declaration.Element)1 Problem (org.eclipse.xtend.lib.macro.services.Problem)1 Severity (org.eclipse.xtext.diagnostics.Severity)1 EObjectDiagnosticImpl (org.eclipse.xtext.validation.EObjectDiagnosticImpl)1 Result (org.eclipse.xtext.xbase.testing.CompilationTestHelper.Result)1