<commit_before>/*
 * Copyright 2011 <NAME> (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: <NAME>
 * 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);
    }
}<commit_msg>fix for extra paren when defining a function<commit_after>/*
 * Copyright 2011 <NAME> (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.fileTypes.FileType;
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: <NAME>
 * Date: 1/30/11
 * Time: 6:45 AM
 */
public class LuaTypedInsideBlockDelegate extends TypedHandlerDelegate {
    boolean preserveParen = false;

    @Override
    public Result beforeCharTyped(char c, Project project, Editor editor, PsiFile file, FileType fileType) {
        int caretOffset = editor.getCaretModel().getOffset();
        PsiElement e1 = file.findElementAt(caretOffset);

        preserveParen = (e1 != null && e1.getText().equals(")"));

        return super.beforeCharTyped(c, project, editor, file,
                fileType);    //To change body of overridden methods use File | Settings | File Templates.
    }

    @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);
//            PsiElement e2 = file.findElementAt(caretOffset+1);

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

            if (c == ')' && e != null && e.getContext() instanceof LuaFunctionDefinition) {
                document.insertString(e.getTextOffset() + 1, preserveParen ? " end)" : " end");
                return Result.STOP;
            }
        }
        return super.charTyped(c, project, editor, file);
    }
}
{'fraction_non_alphanumeric': 0.10572116274763933, 'fraction_numerical': 0.009072394001110906, 'mean_word_length': 2.917331399564902, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3213200', 'n_tokens_mistral': 1567, 'n_tokens_neox': 1527, 'n_words': 450}