<commit_before>/*
 * Copyright 2000-2007 JetBrains s.r.o.
 * 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 org.jetbrains.plugins.groovy.lang.completion;

import com.intellij.codeInsight.TailType;
import com.intellij.codeInsight.completion.CompletionContext;
import com.intellij.codeInsight.completion.DefaultInsertHandler;
import com.intellij.codeInsight.completion.LookupData;
import com.intellij.codeInsight.lookup.Lookup;
import com.intellij.codeInsight.lookup.LookupItem;
import com.intellij.openapi.editor.CaretModel;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrThrowStatement;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable;
import org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner;

import java.util.Arrays;

/**
 * @author ven
 */
public class GroovyInsertHandler extends DefaultInsertHandler {
  public void handleInsert(CompletionContext context, int startOffset, LookupData data, LookupItem item, boolean signatureSelected, char completionChar) {
    Object obj = item.getObject();
    if (obj instanceof PsiMethod) {
      PsiMethod method = (PsiMethod) obj;
      PsiParameter[] parameters = method.getParameterList().getParameters();
      Editor editor = context.editor;
      Document document = editor.getDocument();
      if (completionChar == Lookup.REPLACE_SELECT_CHAR) {
        handleOverwrite(editor.getCaretModel().getOffset(), document);
      }

      if (startOffset > 0 && document.getCharsSequence().charAt(startOffset - 1) == '&') return;   //all creation
      CaretModel caretModel = editor.getCaretModel();
      int offset = startOffset + method.getName().length();
      if (parameters.length == 0) {
        if (offset == document.getTextLength() || document.getCharsSequence().charAt(offset) != '(') {
          document.insertString(offset, "()");
        }
        caretModel.moveToOffset(offset + 2);
      } else {
        if (parameters.length == 1 && parameters[0].getType().getCanonicalText().equals("groovy.lang.Closure")) {
          document.insertString(offset, " {}");
          caretModel.moveToOffset(offset + 2);
        } else {
          PsiDocumentManager docManager = PsiDocumentManager.getInstance(method.getProject());
          docManager.commitDocument(document);
          PsiFile psiFile = docManager.getPsiFile(document);
          if (isExpressionStatement(psiFile, startOffset)) {
            document.insertString(offset, " ");
          } else {
            document.insertString(offset, "()");
          }
          caretModel.moveToOffset(offset + 1);
        }
      }
      return;
    } else if (obj instanceof String && !"assert".equals(obj)) {
      Editor editor = context.editor;
      Document document = editor.getDocument();
      if (completionChar == Lookup.REPLACE_SELECT_CHAR) {
        handleOverwrite(editor.getCaretModel().getOffset(), document);
      }

      return;
    }

    addTailType(item);
    super.handleInsert(context, startOffset, data, item, signatureSelected, completionChar);<commit_msg>remove logic for whitespace insertion in toplevel calls - too awkward to use<commit_after>/*
 * Copyright 2000-2007 JetBrains s.r.o.
 * 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 org.jetbrains.plugins.groovy.lang.completion;

import com.intellij.codeInsight.TailType;
import com.intellij.codeInsight.completion.CompletionContext;
import com.intellij.codeInsight.completion.DefaultInsertHandler;
import com.intellij.codeInsight.completion.LookupData;
import com.intellij.codeInsight.lookup.Lookup;
import com.intellij.codeInsight.lookup.LookupItem;
import com.intellij.openapi.editor.CaretModel;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrThrowStatement;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable;
import org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner;

import java.util.Arrays;

/**
 * @author ven
 */
public class GroovyInsertHandler extends DefaultInsertHandler {
  public void handleInsert(CompletionContext context, int startOffset, LookupData data, LookupItem item, boolean signatureSelected, char completionChar) {
    Object obj = item.getObject();
    if (obj instanceof PsiMethod) {
      PsiMethod method = (PsiMethod) obj;
      PsiParameter[] parameters = method.getParameterList().getParameters();
      Editor editor = context.editor;
      Document document = editor.getDocument();
      if (completionChar == Lookup.REPLACE_SELECT_CHAR) {
        handleOverwrite(editor.getCaretModel().getOffset(), document);
      }

      if (startOffset > 0 && document.getCharsSequence().charAt(startOffset - 1) == '&') return;   //all creation
      CaretModel caretModel = editor.getCaretModel();
      int offset = startOffset + method.getName().length();
      if (parameters.length == 0) {
        if (offset == document.getTextLength() || document.getCharsSequence().charAt(offset) != '(') {
          document.insertString(offset, "()");
        }
        caretModel.moveToOffset(offset + 2);
      } else {
        if (parameters.length == 1 && parameters[0].getType().getCanonicalText().equals("groovy.lang.Closure")) {
          document.insertString(offset, " {}");
          caretModel.moveToOffset(offset + 2);
        } else {
          PsiDocumentManager docManager = PsiDocumentManager.getInstance(method.getProject());
          docManager.commitDocument(document);
          PsiFile psiFile = docManager.getPsiFile(document);
          if (false/*isExpressionStatement(psiFile, startOffset)*/) {
            document.insertString(offset, " ");
          } else {
            document.insertString(offset, "()");
          }
          caretModel.moveToOffset(offset + 1);
        }
      }
      return;
    } else if (obj instanceof String && !"assert".equals(obj)) {
      Editor editor = context.editor;
      Document document = editor.getDocument();
      if (completionChar == Lookup.REPLACE_SELECT_CHAR) {
        handleOverwrite(editor.getCaretModel().getOffset(), document);
      }

      return;
    }

    addTailType(item);
    super.handleInsert(context, startOffset, data, item, signatureSelected, completionChar);
{'fraction_non_alphanumeric': 0.11185491723466406, 'fraction_numerical': 0.004868549172346641, 'mean_word_length': 3.8621301775147927, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '5532071', 'n_tokens_mistral': 2379, 'n_tokens_neox': 2272, 'n_words': 498}