Search in sources :

Example 1 with XbaseInterpreter

use of org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter in project xtext-eclipse by eclipse.

the class ReplAutoEdit method getConfiguredInterpreter.

protected XbaseInterpreter getConfiguredInterpreter(XtextResource resource) {
    XbaseInterpreter interpreter2 = interpreterProvider.get();
    ResourceSet set = resource.getResourceSet();
    ClassLoader cl = getClass().getClassLoader();
    if (set instanceof XtextResourceSet) {
        Object context = ((XtextResourceSet) set).getClasspathURIContext();
        if (context instanceof IJavaProject) {
            try {
                final IJavaProject jp = (IJavaProject) context;
                // String[] runtimeClassPath =
                // JavaRuntime.computeDefaultRuntimeClassPath(jp);
                // URL[] urls = new URL[runtimeClassPath.length];
                // for (int i = 0; i < urls.length; i++) {
                // urls[i] = new URL(runtimeClassPath[i]);
                // }
                // cl = new URLClassLoader(urls);
                IClasspathEntry[] classpath = jp.getResolvedClasspath(true);
                final IWorkspaceRoot root = jp.getProject().getWorkspace().getRoot();
                Set<URL> urls = newHashSet();
                for (int i = 0; i < classpath.length; i++) {
                    final IClasspathEntry entry = classpath[i];
                    if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                        IPath outputLocation = entry.getOutputLocation();
                        if (outputLocation == null) {
                            outputLocation = jp.getOutputLocation();
                        }
                        IFolder folder = root.getFolder(outputLocation);
                        if (folder.exists()) {
                            urls.add(new URL(folder.getRawLocationURI().toASCIIString() + "/"));
                        }
                    } else if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                        IPath outputLocation = entry.getOutputLocation();
                        if (outputLocation == null) {
                            IProject project = (IProject) jp.getProject().getWorkspace().getRoot().getContainerForLocation(entry.getPath());
                            IJavaProject javaProject = JavaCore.create(project);
                            outputLocation = javaProject.getOutputLocation();
                        }
                        IFolder folder = root.getFolder(outputLocation);
                        if (folder.exists()) {
                            urls.add(new URL(folder.getRawLocationURI().toASCIIString() + "/"));
                        }
                    } else {
                        urls.add(entry.getPath().toFile().toURI().toURL());
                    }
                }
                cl = new URLClassLoader(urls.toArray(new URL[urls.size()]));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    interpreter2.setClassLoader(cl);
    return interpreter2;
}
Also used : IPath(org.eclipse.core.runtime.IPath) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) XbaseInterpreter(org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) URL(java.net.URL) IProject(org.eclipse.core.resources.IProject) BadLocationException(org.eclipse.jface.text.BadLocationException) IJavaProject(org.eclipse.jdt.core.IJavaProject) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) IFolder(org.eclipse.core.resources.IFolder)

Example 2 with XbaseInterpreter

use of org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter in project xtext-eclipse by eclipse.

the class ReplAutoEdit method computeResultText.

protected String computeResultText(final IDocument document, final DocumentCommand command, XtextResource resource) throws BadLocationException {
    if (resource.getContents().isEmpty())
        return null;
    if (!(resource.getContents().get(0) instanceof Model))
        return null;
    Model model = (Model) resource.getContents().get(0);
    if (model.getBlock() == null)
        return null;
    EList<XExpression> expressions = model.getBlock().getExpressions();
    if (expressions.isEmpty())
        return null;
    XExpression expression = expressions.get(expressions.size() - 1);
    if (expression == null) {
        return null;
    }
    ICompositeNode node = NodeModelUtils.getNode(expression);
    if (node == null || document.getLineOfOffset(command.offset) + 1 != node.getEndLine())
        return null;
    List<Issue> issues = validator.validate(resource, CheckMode.NORMAL_AND_FAST, CancelIndicator.NullImpl);
    if (issues.stream().anyMatch(i -> i.getSeverity() == Severity.ERROR)) {
        return null;
    }
    XbaseInterpreter xbaseInterpreter = getConfiguredInterpreter(resource);
    IEvaluationResult result = xbaseInterpreter.evaluate(model.getBlock(), new DefaultEvaluationContext(), new CancelIndicator() {

        private long stopAt = System.currentTimeMillis() + 2000;

        @Override
        public boolean isCanceled() {
            return System.currentTimeMillis() > stopAt;
        }
    });
    if (result == null)
        return null;
    String value = "" + result.getResult();
    if (result.getException() != null)
        value = "threw " + result.getException().getClass().getSimpleName();
    LightweightTypeReference expressionType = typeResolver.resolveTypes(expression).getActualType(expression);
    if (expressionType != null) {
        String newText = command.text + "// " + value + " (" + expressionType.getSimpleName() + ")" + command.text;
        return newText;
    }
    return command.text + "// " + value;
}
Also used : Issue(org.eclipse.xtext.validation.Issue) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) IEvaluationResult(org.eclipse.xtext.xbase.interpreter.IEvaluationResult) DefaultEvaluationContext(org.eclipse.xtext.xbase.interpreter.impl.DefaultEvaluationContext) XbaseInterpreter(org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter) Model(org.eclipse.xtext.purexbase.pureXbase.Model) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) XExpression(org.eclipse.xtext.xbase.XExpression) CancelIndicator(org.eclipse.xtext.util.CancelIndicator)

Aggregations

XbaseInterpreter (org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter)2 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 IFolder (org.eclipse.core.resources.IFolder)1 IProject (org.eclipse.core.resources.IProject)1 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)1 IPath (org.eclipse.core.runtime.IPath)1 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)1 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)1 IJavaProject (org.eclipse.jdt.core.IJavaProject)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)1 Model (org.eclipse.xtext.purexbase.pureXbase.Model)1 XtextResourceSet (org.eclipse.xtext.resource.XtextResourceSet)1 CancelIndicator (org.eclipse.xtext.util.CancelIndicator)1 Issue (org.eclipse.xtext.validation.Issue)1 XExpression (org.eclipse.xtext.xbase.XExpression)1 IEvaluationResult (org.eclipse.xtext.xbase.interpreter.IEvaluationResult)1 DefaultEvaluationContext (org.eclipse.xtext.xbase.interpreter.impl.DefaultEvaluationContext)1 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)1