/*
 * Copyright 2011 Jon S Akhtar (Sylvanaar)
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package com.sylvanaar.idea.Lua.lang;

import com.intellij.codeInsight.editorActions.TypedHandlerDelegate;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiComment;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.sylvanaar.idea.Lua.lang.psi.LuaFunctionDefinition;

/**
 * Created by IntelliJ IDEA.
 * User: Jon S Akhtar
 * Date: 1/30/11
 * Time: 6:45 AM
 */
public class LuaTypedInsideBlockDelegate extends TypedHandlerDelegate {
    @Override
    public Result charTyped(char c, final Project project, final Editor editor, final PsiFile file) {
        Document document = editor.getDocument();
        int caretOffset = editor.getCaretModel().getOffset();

        PsiDocumentManager.getInstance(file.getProject()).commitDocument(document);

        PsiElement e = file.findElementAt(caretOffset-1);

        if (!(e instanceof PsiComment)) {

            PsiElement e1 = file.findElementAt(caretOffset);

            // This handles the case where we are already inside parens.
            // for example a(b,c function(|) where | is the cursor
            boolean preserveCloseParen = false;
            if (c == '(' && e1 != null && e1.getText().equals(")")) {
                e = e1;
                c = ')';
                preserveCloseParen = true;
            }

            if (c == ')' && e != null && e.getContext() instanceof LuaFunctionDefinition) {
                document.insertString(e.getTextOffset() + 1, preserveCloseParen ? " end)" : " end");
                return Result.STOP;
            }
        }
        return super.charTyped(c, project, editor, file);
    }
}

@@ -827,24 +827,73 @@
 or.Editor;%0D%0A
+import com.intellij.openapi.fileTypes.FileType;%0D%0A
 import com.i
@@ -922,24 +922,24 @@
 t.Project;%0D%0A
-
 import com.i
@@ -1312,24 +1312,560 @@
 Delegate %7B%0D%0A
+    boolean preserveParen = false;%0D%0A%0D%0A    @Override%0D%0A    public Result beforeCharTyped(char c, Project project, Editor editor, PsiFile file, FileType fileType) %7B%0D%0A        int caretOffset = editor.getCaretModel().getOffset();%0D%0A        PsiElement e1 = file.findElementAt(caretOffset);%0D%0A%0D%0A        preserveParen = (e1 != null && e1.getText().equals(%22)%22));%0D%0A%0D%0A        return super.beforeCharTyped(c, project, editor, file,%0D%0A                fileType);    //To change body of overridden methods use File %7C Settings %7C File Templates.%0D%0A    %7D%0D%0A%0D%0A
     @Overrid
@@ -2332,32 +2332,98 @@
 t(caretOffset);%0D
+%0A//            PsiElement e2 = file.findElementAt(caretOffset+1);%0D
 %0A%0D%0A            /
@@ -2555,57 +2555,8 @@
 or%0D%0A
-            boolean preserveCloseParen = false;%0D%0A
     
@@ -2677,52 +2677,8 @@
 ';%0D%0A
-                preserveCloseParen = true;%0D%0A
     
@@ -2779,24 +2779,24 @@
 inition) %7B%0D%0A
+
             
@@ -2856,13 +2856,8 @@
 erve
-Close
 Pare

fix for extra paren when defining a function