package com.haskforce.psi.impl;

import com.haskforce.HaskellLanguage;
import com.haskforce.psi.HaskellConid;
import com.haskforce.psi.HaskellFile;
import com.haskforce.psi.HaskellQvarid;
import com.haskforce.psi.HaskellTycls;
import com.haskforce.psi.HaskellTycon;
import com.haskforce.psi.HaskellVarid;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFileFactory;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * Performs creation of element types.
 */
public class HaskellElementFactory {
    @Nullable
    public static HaskellVarid createVaridFromText(@NotNull Project project, @NotNull String text) {
        PsiElement e = createExpressionFromText(project, text + " = 2").getFirstChild();
        if (e instanceof HaskellVarid) return (HaskellVarid) e;
        return null;
    }

    @NotNull
    public static HaskellQvarid createQvaridFromText(@NotNull Project project, @NotNull String text) {
        return ((HaskellQvarid) (createExpressionFromText(project, text + " = 2")).getFirstChild());
    }

    @Nullable
    public static HaskellConid createConidFromText(@NotNull Project project, @NotNull String text) {
        PsiElement e = createExpressionFromText(project, text + " = 2").getFirstChild();
        if (e instanceof HaskellConid) return (HaskellConid) e;
        return null;
    }

    @NotNull
    public static HaskellTycon createTyconFromText(@NotNull Project project, @NotNull String text) {
        return ((HaskellTycon) (createExpressionFromText(project, text + " = 2")).getFirstChild());
    }

    @NotNull
    public static HaskellTycls createTyclsFromText(@NotNull Project project, @NotNull String text) {
        return ((HaskellTycls) (createExpressionFromText(project, text + " = 2")).getFirstChild());
    }

    @NotNull
    public static PsiElement createExpressionFromText(@NotNull Project project, @NotNull String text) {
        HaskellFile fileFromText = createFileFromText(project, text);
        return fileFromText.getFirstChild().getFirstChild();
    }

    @NotNull
    private static HaskellFile createFileFromText(@NotNull Project project, @NotNull String text) {
        return (HaskellFile) PsiFileFactory.getInstance(project).createFileFromText("A.hs", HaskellLanguage.INSTANCE, text);
    }
}

@@ -589,16 +589,98 @@
 ctory %7B%0A
+    /**%0A     * Takes a name and returns a Psi node of that name, or null.%0A     */%0A
     @Nul
@@ -770,36 +770,36 @@
 @NotNull String 
-text
+name
 ) %7B%0A        PsiE
@@ -839,37 +839,47 @@
 xt(project, 
-text + %22 = 2%22
+name + %22uniq = %22 + name
 ).getFirstCh
@@ -969,32 +969,114 @@
 rn null;%0A    %7D%0A%0A
+    /**%0A     * Takes a name and returns a Psi node of that name, or null.%0A     */%0A
     @NotNull%0A   
@@ -1159,36 +1159,36 @@
 @NotNull String 
-text
+name
 ) %7B%0A        retu
@@ -1238,37 +1238,47 @@
 xt(project, 
-text + %22 = 2%22
+name + %22uniq = %22 + name
 )).getFirstC
@@ -1289,24 +1289,106 @@
 ());%0A    %7D%0A%0A
+    /**%0A     * Takes a name and returns a Psi node of that name, or null.%0A     */%0A
     @Nullabl
@@ -1474,36 +1474,36 @@
 @NotNull String 
-text
+name
 ) %7B%0A        PsiE
@@ -1547,29 +1547,39 @@
 roject, 
-text + %22 = 2%22
+name + %22uniq = %22 + name
 ).getFir
@@ -1673,32 +1673,114 @@
 rn null;%0A    %7D%0A%0A
+    /**%0A     * Takes a name and returns a Psi node of that name, or null.%0A     */%0A
     @NotNull%0A   
@@ -1861,36 +1861,36 @@
 @NotNull String 
-text
+name
 ) %7B%0A        retu
@@ -1939,37 +1939,47 @@
 xt(project, 
-text + %22 = 2%22
+name + %22uniq = %22 + name
 )).getFirstC
@@ -1986,32 +1986,114 @@
 hild());%0A    %7D%0A%0A
+    /**%0A     * Takes a name and returns a Psi node of that name, or null.%0A     */%0A
     @NotNull%0A   
@@ -2174,36 +2174,36 @@
 @NotNull String 
-text
+name
 ) %7B%0A        retu
@@ -2260,21 +2260,31 @@
 ct, 
-text + %22 = 2%22
+name + %22uniq = %22 + name
 )).g
@@ -2299,32 +2299,123 @@
 hild());%0A    %7D%0A%0A
+    /**%0A     * Takes an expression in text and returns a Psi tree of that program.%0A     */%0A
     @NotNull%0A   
@@ -2499,36 +2499,36 @@
 @NotNull String 
-text
+name
 ) %7B%0A        Hask
@@ -2578,20 +2578,20 @@
 roject, 
-text
+name
 );%0A     
@@ -2593,22 +2593,32 @@
         
-return
+PsiElement rhs =
  fileFro
@@ -2654,24 +2654,210 @@
 tChild()
-;%0A    %7D%0A
+.getLastChild();%0A        PsiElement nodeOfInterest = rhs.getLastChild().getLastChild().getLastChild();%0A        return nodeOfInterest;%0A    %7D%0A%0A    /**%0A     * Create a file containing text.%0A     */
 %0A    @No

Update element renaming to work with the updated parser.