package org.metaborg.spoofax.shell.functions;

import org.apache.commons.vfs2.FileObject;
import org.metaborg.core.MetaborgException;
import org.metaborg.core.language.ILanguageImpl;
import org.metaborg.core.project.IProject;
import org.metaborg.spoofax.core.shell.ShellFacet;
import org.metaborg.spoofax.core.syntax.JSGLRParserConfiguration;
import org.metaborg.spoofax.shell.output.FailOrSuccessResult;
import org.metaborg.spoofax.shell.output.IResult;
import org.metaborg.spoofax.shell.output.IResultFactory;
import org.metaborg.spoofax.shell.output.InputResult;

import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;

/**
 * Creates an {@link InputResult} from a given source string.
 */
public class InputFunction extends AbstractSpoofaxFunction<String, InputResult> {

    /**
     * Instantiate a {@link InputFunction}.
     *
     * @param resultFactory
     *            The {@link IResultFactory}.
     * @param project
     *            The {@link IProject} in which this command should operate.
     * @param lang
     *            The {@link ILanguageImpl} to which this command applies.
     */
    @Inject
    public InputFunction(IResultFactory resultFactory, @Assisted IProject project,
                         @Assisted ILanguageImpl lang) {
        super(resultFactory, project, lang);
    }

    @Override
    protected FailOrSuccessResult<InputResult, IResult> applyThrowing(String source)
        throws Exception {
        ShellFacet shellFacet = lang.facet(ShellFacet.class);
        if (shellFacet == null) {
            throw new MetaborgException("Cannot find the shell facet.");
        }

        FileObject file = project.location().resolveFile("temp");
        return FailOrSuccessResult.ofSpoofaxResult(resultFactory
            .createInputResult(lang, file, source,
                               new JSGLRParserConfiguration(shellFacet.getShellStartSymbol())));
    }

}

@@ -176,16 +176,74 @@
 geImpl;%0A
+import org.metaborg.core.language.ResourceExtensionFacet;%0A
 import o
@@ -618,16 +618,60 @@
 esult;%0A%0A
+import com.google.common.collect.Iterables;%0A
 import c
@@ -1739,24 +1739,196 @@
 %0A        %7D%0A%0A
+        ResourceExtensionFacet extensionFacet = lang.facet(ResourceExtensionFacet.class);%0A        String extension = Iterables.getFirst(extensionFacet.extensions(), null);%0A
         File
@@ -1940,16 +1940,28 @@
 t file =
+%0A           
  project
@@ -1990,16 +1990,61 @@
 e(%22temp%22
+ + (extension != null ? %22.%22 + extension : %22%22)
 );%0A     

Set file extension of temporary file to language file extension