<commit_before>/*
 * 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.codeInsight.intention.impl.BaseIntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiNameIdentifierOwner;
import com.intellij.psi.SmartPointerManager;
import com.intellij.psi.SmartPsiElementPointer;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import consulo.annotations.RequiredReadAction;
import consulo.annotations.RequiredWriteAction;
import consulo.csharp.ide.highlight.CSharpHighlightContext;
import consulo.csharp.ide.highlight.check.CompilerCheck;
import consulo.csharp.lang.doc.psi.CSharpDocRoot;
import consulo.csharp.lang.psi.CSharpContextUtil;
import consulo.csharp.lang.psi.CSharpFileFactory;
import consulo.csharp.lang.psi.CSharpReferenceExpressionEx;
import consulo.csharp.module.extension.CSharpLanguageVersion;
import consulo.dotnet.psi.DotNetExpression;
import consulo.dotnet.psi.DotNetModifierListOwner;

/**
 * @author VISTALL
 * @since 31.12.14
 */
public class CS0120 extends CompilerCheck<CSharpReferenceExpressionEx>
{
	public static class ReplaceQualifierByTypeFix extends BaseIntentionAction
	{
		private SmartPsiElementPointer<CSharpReferenceExpressionEx> myReferenceExpressionPointer;

		public ReplaceQualifierByTypeFix(CSharpReferenceExpressionEx referenceExpressionEx)
		{
			myReferenceExpressionPointer = SmartPointerManager.getInstance(referenceExpressionEx.getProject()).createSmartPsiElementPointer(referenceExpressionEx);
		}

		@NotNull
		@Override
		@RequiredReadAction
		public String getText()
		{
			CSharpReferenceExpressionEx element = myReferenceExpressionPointer.getElement();
			if(element == null)
			{
				throw new IllegalArgumentException();
			}

			PsiElement resolvedElement = element.resolve();
			if(resolvedElement == null)
			{
				return "";
			}
			return "Replace qualifier by '" + formatElement(resolvedElement.getParent()) + "'";
		}

		@NotNull
		@Override
		public String getFamilyName()
		{
			return "C#";
		}

		@Override
		@RequiredReadAction
		public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file)
		{
			CSharpReferenceExpressionEx element = myReferenceExpressionPointer.getElement();
			return element != null && element.resolve() != null && element.getQualifier() != null;
		}

		@Override
		@RequiredWriteAction
		public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException
		{
			CSharpReferenceExpressionEx element = myReferenceExpressionPointer.getElement();
			if(element == null)
			{
				return;
			}

			PsiElement qualifier = element.getQualifier();
			if(qualifier == null)
			{
				return;
			}
			PsiElement resolvedElement = element.resolve();
			if(resolvedElement == null)
			{
				return;
			}

			PsiElement parent = resolvedElement.getParent();

			assert parent instanceof PsiNameIdentifierOwner;

			DotNetExpression expression = CSharpFileFactory.createExpression(project, ((PsiNameIdentifierOwner) parent).getName());

			qualifier.replace(expression);
		}
	}

	@RequiredReadAction
	@Nullable
	@Override
	public HighlightInfoFactory checkImpl(@NotNull CSharpLanguageVersion languageVersion, @NotNull CSharpHighlightContext highlightContext, @NotNull CSharpReferenceExpressionEx element)
	{
		PsiElement referenceElement = element.getReferenceElement();
		if(referenceElement == null)
		{
			return null;
		}

		PsiElement resolvedElement = element.resolve();
		if(!(resolvedElement instanceof DotNetModifierListOwner))
		{
			return null;
		}

		if(PsiTreeUtil.getParentOfType(element, CSharpDocRoot.class) != null)
		{
			return null;
		}

		CSharpContextUtil.ContextType parentContextType = CSharpContextUtil.getParentContextTypeForReference(element);

		CSharpContextUtil.ContextType contextForResolved = CSharpContextUtil.getContextForResolved(resolvedElement);
		if(parentContextType == CSharpContextUtil.ContextType.STATIC && contextForResolved.isAllowInstance())
		{
			return newBuilder(referenceElement, formatElement(resolvedElement));
		}
		else if(contextForResolved == CSharpContextUtil.ContextType.STATIC && !parentContextType.isAllowStatic())
		{
			return newBuilderImpl(CS0176.class, referenceElement, formatElement(resolvedElement)).addQuickFix(new ReplaceQualifierByTypeFix(element));
		}
		return null;
	}
}<commit_msg>use util<commit_after>/*
 * 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.codeInsight.intention.impl.BaseIntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiNameIdentifierOwner;
import com.intellij.psi.SmartPointerManager;
import com.intellij.psi.SmartPsiElementPointer;
import com.intellij.util.IncorrectOperationException;
import consulo.annotations.RequiredReadAction;
import consulo.annotations.RequiredWriteAction;
import consulo.csharp.ide.highlight.CSharpHighlightContext;
import consulo.csharp.ide.highlight.check.CompilerCheck;
import consulo.csharp.lang.doc.CSharpDocUtil;
import consulo.csharp.lang.psi.CSharpContextUtil;
import consulo.csharp.lang.psi.CSharpFileFactory;
import consulo.csharp.lang.psi.CSharpReferenceExpressionEx;
import consulo.csharp.module.extension.CSharpLanguageVersion;
import consulo.dotnet.psi.DotNetExpression;
import consulo.dotnet.psi.DotNetModifierListOwner;

/**
 * @author VISTALL
 * @since 31.12.14
 */
public class CS0120 extends CompilerCheck<CSharpReferenceExpressionEx>
{
	public static class ReplaceQualifierByTypeFix extends BaseIntentionAction
	{
		private SmartPsiElementPointer<CSharpReferenceExpressionEx> myReferenceExpressionPointer;

		public ReplaceQualifierByTypeFix(CSharpReferenceExpressionEx referenceExpressionEx)
		{
			myReferenceExpressionPointer = SmartPointerManager.getInstance(referenceExpressionEx.getProject()).createSmartPsiElementPointer(referenceExpressionEx);
		}

		@NotNull
		@Override
		@RequiredReadAction
		public String getText()
		{
			CSharpReferenceExpressionEx element = myReferenceExpressionPointer.getElement();
			if(element == null)
			{
				throw new IllegalArgumentException();
			}

			PsiElement resolvedElement = element.resolve();
			if(resolvedElement == null)
			{
				return "";
			}
			return "Replace qualifier by '" + formatElement(resolvedElement.getParent()) + "'";
		}

		@NotNull
		@Override
		public String getFamilyName()
		{
			return "C#";
		}

		@Override
		@RequiredReadAction
		public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file)
		{
			CSharpReferenceExpressionEx element = myReferenceExpressionPointer.getElement();
			return element != null && element.resolve() != null && element.getQualifier() != null;
		}

		@Override
		@RequiredWriteAction
		public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException
		{
			CSharpReferenceExpressionEx element = myReferenceExpressionPointer.getElement();
			if(element == null)
			{
				return;
			}

			PsiElement qualifier = element.getQualifier();
			if(qualifier == null)
			{
				return;
			}
			PsiElement resolvedElement = element.resolve();
			if(resolvedElement == null)
			{
				return;
			}

			PsiElement parent = resolvedElement.getParent();

			assert parent instanceof PsiNameIdentifierOwner;

			DotNetExpression expression = CSharpFileFactory.createExpression(project, ((PsiNameIdentifierOwner) parent).getName());

			qualifier.replace(expression);
		}
	}

	@RequiredReadAction
	@Nullable
	@Override
	public HighlightInfoFactory checkImpl(@NotNull CSharpLanguageVersion languageVersion, @NotNull CSharpHighlightContext highlightContext, @NotNull CSharpReferenceExpressionEx element)
	{
		PsiElement referenceElement = element.getReferenceElement();
		if(referenceElement == null)
		{
			return null;
		}

		PsiElement resolvedElement = element.resolve();
		if(!(resolvedElement instanceof DotNetModifierListOwner))
		{
			return null;
		}

		if(CSharpDocUtil.isInsideDoc(element))
		{
			return null;
		}

		CSharpContextUtil.ContextType parentContextType = CSharpContextUtil.getParentContextTypeForReference(element);

		CSharpContextUtil.ContextType contextForResolved = CSharpContextUtil.getContextForResolved(resolvedElement);
		if(parentContextType == CSharpContextUtil.ContextType.STATIC && contextForResolved.isAllowInstance())
		{
			return newBuilder(referenceElement, formatElement(resolvedElement));
		}
		else if(contextForResolved == CSharpContextUtil.ContextType.STATIC && !parentContextType.isAllowStatic())
		{
			return newBuilderImpl(CS0176.class, referenceElement, formatElement(resolvedElement)).addQuickFix(new ReplaceQualifierByTypeFix(element));
		}
		return null;
	}
}
{'fraction_non_alphanumeric': 0.08804051421893261, 'fraction_numerical': 0.00506427736657577, 'mean_word_length': 5.544933078393881, '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': '2370360', 'n_tokens_mistral': 3205, 'n_tokens_neox': 2796, 'n_words': 611}