/*
 * 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.highlight.check.impl;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import consulo.annotations.RequiredReadAction;
import consulo.csharp.ide.highlight.CSharpHighlightContext;
import consulo.csharp.ide.highlight.check.CompilerCheck;
import consulo.csharp.lang.psi.CSharpFieldDeclaration;
import consulo.csharp.lang.psi.CSharpModifier;
import consulo.csharp.lang.psi.CSharpReferenceExpression;
import consulo.csharp.lang.psi.CSharpTypeDeclaration;
import consulo.csharp.module.extension.CSharpLanguageVersion;
import consulo.dotnet.psi.DotNetExpression;
import consulo.dotnet.psi.DotNetModifierListOwner;
import consulo.dotnet.psi.DotNetType;

/**
 * @author VISTALL
 * @since 02-Nov-17
 */
public class CS0236 extends CompilerCheck<CSharpReferenceExpression>
{
	@RequiredReadAction
	@Nullable
	@Override
	public HighlightInfoFactory checkImpl(@NotNull CSharpLanguageVersion languageVersion, @NotNull CSharpHighlightContext highlightContext, @NotNull CSharpReferenceExpression element)
	{
		CSharpFieldDeclaration fieldDeclaration = PsiTreeUtil.getParentOfType(element, CSharpFieldDeclaration.class);
		if(fieldDeclaration == null)
		{
			return null;
		}

		DotNetExpression initializer = fieldDeclaration.getInitializer();
		if(initializer == null || !initializer.getTextRange().contains(element.getTextRange()))
		{
			return null;
		}

		PsiElement parent = fieldDeclaration.getParent();
		if(!(parent instanceof CSharpTypeDeclaration))
		{
			return null;
		}

		PsiElement target = element.resolve();
		if(PsiTreeUtil.getParentOfType(element, DotNetType.class) != null)
		{
			return null;
		}

		if(target instanceof DotNetModifierListOwner && !((DotNetModifierListOwner) target).hasModifier(CSharpModifier.STATIC) && parent.isEquivalentTo(target.getParent()))
		{
			return newBuilder(element, formatElement(target));
		}
		return null;
	}
}

@@ -2302,24 +2302,67 @@
 ass) != null
+ %7C%7C target instanceof CSharpTypeDeclaration
 )%0A%09%09%7B%0A%09%09%09ret

allow type