/*
 * Copyright 2013-2017 consulo.io
 *
 * 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 consulo.csharp.ide.completion.insertHandler;

import com.intellij.codeInsight.TailType;
import com.intellij.codeInsight.completion.InsertHandler;
import com.intellij.codeInsight.completion.InsertionContext;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiErrorElement;
import com.intellij.psi.util.PsiTreeUtil;
import consulo.annotations.RequiredDispatchThread;
import consulo.csharp.ide.completion.util.CSharpParenthesesInsertHandler;
import consulo.csharp.lang.psi.CSharpCodeFragment;
import consulo.csharp.lang.psi.CSharpConstructorDeclaration;
import consulo.csharp.lang.psi.CSharpLocalVariable;
import consulo.csharp.lang.psi.impl.source.CSharpMethodCallExpressionImpl;
import consulo.dotnet.DotNetTypes;
import consulo.dotnet.psi.DotNetLikeMethodDeclaration;
import consulo.dotnet.resolve.DotNetTypeRefUtil;

/**
 * @author VISTALL
 * @since 08.03.2016
 */
public class CSharpParenthesesWithSemicolonInsertHandler implements InsertHandler<LookupElement>
{
	private PsiElement myDeclaration;

	public CSharpParenthesesWithSemicolonInsertHandler(PsiElement declaration)
	{
		myDeclaration = declaration;
	}

	@Override
	@RequiredDispatchThread
	public void handleInsert(InsertionContext context, LookupElement item)
	{
		boolean isMethodLike = myDeclaration instanceof DotNetLikeMethodDeclaration;
		if(isMethodLike)
		{
			new CSharpParenthesesInsertHandler((DotNetLikeMethodDeclaration) myDeclaration).handleInsert(context, item);
		}

		if(context.getCompletionChar() != '\n' || context.getFile() instanceof CSharpCodeFragment)
		{
			return;
		}

		// for void method we always insert semicolon
		if(isMethodLike && !(myDeclaration instanceof CSharpConstructorDeclaration) && DotNetTypeRefUtil.isVmQNameEqual(((DotNetLikeMethodDeclaration) myDeclaration).getReturnTypeRef(),
				myDeclaration, DotNetTypes.System.Void))
		{
			if(TailType.SEMICOLON.isApplicable(context))
			{
				TailType.SEMICOLON.processTail(context.getEditor(), context.getTailOffset());
			}
		}
		else
		{
			context.commitDocument();
			PsiElement elementAt = context.getFile().findElementAt(context.getStartOffset());
			PsiElement parent = PsiTreeUtil.getParentOfType(elementAt, CSharpMethodCallExpressionImpl.class);
			if(parent != null && parent.getNextSibling() instanceof PsiErrorElement)
			{
				TailType.SEMICOLON.processTail(context.getEditor(), context.getTailOffset());
			}

			parent = PsiTreeUtil.getParentOfType(elementAt, CSharpLocalVariable.class);
			if(parent != null && parent.getNextSibling() instanceof PsiErrorElement)
			{
				TailType.SEMICOLON.processTail(context.getEditor(), context.getTailOffset());
			}
		}
	}
}

@@ -898,91 +898,8 @@
 nt;%0A
-import com.intellij.psi.PsiErrorElement;%0Aimport com.intellij.psi.util.PsiTreeUtil;%0A
 impo
@@ -1135,135 +1135,8 @@
 on;%0A
-import consulo.csharp.lang.psi.CSharpLocalVariable;%0Aimport consulo.csharp.lang.psi.impl.source.CSharpMethodCallExpressionImpl;%0A
 impo
@@ -2434,16 +2434,18 @@
 %7D%0A%09%09%7D%0A%09%09
+/*
 else%0A%09%09%7B
@@ -3079,14 +3079,16 @@
 %09%09%09%7D%0A%09%09%7D
+*/
 %0A%09%7D%0A%7D%0A

disable this part