/*
 * 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 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;
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;

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

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

	@Override
	@RequiredDispatchThread
	public void handleInsert(InsertionContext context, LookupElement item)
	{
		new CSharpParenthesesInsertHandler(myDeclaration).handleInsert(context, item);

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

		// for void method we always insert semicolon
		if(!(myDeclaration instanceof CSharpConstructorDeclaration) && DotNetTypeRefUtil.isVmQNameEqual(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, CSharpLocalVariable.class);
			if(parent != null && parent.getNextSibling() instanceof PsiErrorElement)
			{
				TailType.SEMICOLON.processTail(context.getEditor(), context.getTailOffset());
			}
		}
	}
}

@@ -643,16 +643,350 @@
 ndler;%0A%0A
+import com.intellij.codeInsight.TailType;%0Aimport com.intellij.codeInsight.completion.InsertHandler;%0Aimport com.intellij.codeInsight.completion.InsertionContext;%0Aimport com.intellij.codeInsight.lookup.LookupElement;%0Aimport com.intellij.psi.PsiElement;%0Aimport com.intellij.psi.PsiErrorElement;%0Aimport com.intellij.psi.util.PsiTreeUtil;%0A
 import c
@@ -1483,342 +1483,8 @@
 til;
-%0Aimport com.intellij.codeInsight.TailType;%0Aimport com.intellij.codeInsight.completion.InsertHandler;%0Aimport com.intellij.codeInsight.completion.InsertionContext;%0Aimport com.intellij.codeInsight.lookup.LookupElement;%0Aimport com.intellij.psi.PsiElement;%0Aimport com.intellij.psi.PsiErrorElement;%0Aimport com.intellij.psi.util.PsiTreeUtil;
 %0A%0A/*
@@ -2710,16 +2710,237 @@
 pl.class
+);%0A%09%09%09if(parent != null && parent.getNextSibling() instanceof PsiErrorElement)%0A%09%09%09%7B%0A%09%09%09%09TailType.SEMICOLON.processTail(context.getEditor(), context.getTailOffset());%0A%09%09%09%7D%0A%0A%09%09%09parent = PsiTreeUtil.getParentOfType(elementAt
 , CSharp

fix not appending semicolon