<commit_before>/*
 * Copyright 2013-2014 must-be.org
 *
 * 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.mustbe.consulo.csharp.ide.resolve;

import org.jetbrains.annotations.NotNull;
import org.mustbe.consulo.csharp.lang.psi.CSharpCallArgumentList;
import org.mustbe.consulo.csharp.lang.psi.impl.source.CSharpArrayAccessExpressionImpl;
import org.mustbe.consulo.csharp.lang.psi.impl.source.CSharpLikeMethodDeclarationImplUtil;
import org.mustbe.consulo.dotnet.psi.DotNetVirtualImplementOwner;
import com.intellij.openapi.util.TextRange;
import com.intellij.patterns.StandardPatterns;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiNameIdentifierOwner;
import com.intellij.psi.PsiReference;
import com.intellij.psi.PsiReferenceBase;
import com.intellij.psi.PsiReferenceContributor;
import com.intellij.psi.PsiReferenceProvider;
import com.intellij.psi.PsiReferenceRegistrar;
import com.intellij.util.ProcessingContext;
import lombok.val;

/**
 * @author VISTALL
 * @since 04.09.14
 */
public class CSharpPsiReferenceContributor extends PsiReferenceContributor
{
	@Override
	public void registerReferenceProviders(PsiReferenceRegistrar psiReferenceRegistrar)
	{
		psiReferenceRegistrar.registerReferenceProvider(StandardPatterns.psiElement(CSharpCallArgumentList.class).withParent
				(CSharpArrayAccessExpressionImpl.class), new PsiReferenceProvider()
		{
			@NotNull
			@Override
			public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext processingContext)
			{
				CSharpArrayAccessExpressionImpl parent = (CSharpArrayAccessExpressionImpl) element.getParent();
				PsiElement callable = parent.resolveToCallable();
				if(callable == null)
				{
					return PsiReference.EMPTY_ARRAY;
				}
				return new PsiReference[]{
						new PsiReferenceBase.Immediate<PsiElement>(element, new TextRange(0, element.getTextLength()), callable)
				};
			}
		});

		psiReferenceRegistrar.registerReferenceProvider(StandardPatterns.psiElement(DotNetVirtualImplementOwner.class), new PsiReferenceProvider()
		{
			@NotNull
			@Override
			public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context)
			{
				PsiElement nameIdentifier = ((PsiNameIdentifierOwner) element).getNameIdentifier();
				if(nameIdentifier == null)
				{
					return PsiReference.EMPTY_ARRAY;
				}

				val resultPair = CSharpLikeMethodDeclarationImplUtil.resolveVirtualImplementation((DotNetVirtualImplementOwner) element, element);
				switch(resultPair.getFirst())
				{
					case FOUND:
						int textOffset = nameIdentifier.getStartOffsetInParent();
						PsiReferenceBase.Immediate<PsiElement> psiElementImmediate = new PsiReferenceBase.Immediate<PsiElement>(element, new TextRange(textOffset, textOffset + nameIdentifier.getTextLength()), resultPair.getSecond());

						return new PsiReference[]{psiElementImmediate};
				}

				return PsiReference.EMPTY_ARRAY;
			}
		});<commit_msg>handle rename for private impl<commit_after>/*
 * Copyright 2013-2014 must-be.org
 *
 * 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.mustbe.consulo.csharp.ide.resolve;

import org.jetbrains.annotations.NotNull;
import org.mustbe.consulo.csharp.lang.psi.CSharpCallArgumentList;
import org.mustbe.consulo.csharp.lang.psi.impl.source.CSharpArrayAccessExpressionImpl;
import org.mustbe.consulo.csharp.lang.psi.impl.source.CSharpLikeMethodDeclarationImplUtil;
import org.mustbe.consulo.dotnet.psi.DotNetVirtualImplementOwner;
import com.intellij.openapi.util.TextRange;
import com.intellij.patterns.StandardPatterns;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiNameIdentifierOwner;
import com.intellij.psi.PsiNamedElement;
import com.intellij.psi.PsiReference;
import com.intellij.psi.PsiReferenceBase;
import com.intellij.psi.PsiReferenceContributor;
import com.intellij.psi.PsiReferenceProvider;
import com.intellij.psi.PsiReferenceRegistrar;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.ProcessingContext;
import lombok.val;

/**
 * @author VISTALL
 * @since 04.09.14
 */
public class CSharpPsiReferenceContributor extends PsiReferenceContributor
{
	@Override
	public void registerReferenceProviders(PsiReferenceRegistrar psiReferenceRegistrar)
	{
		psiReferenceRegistrar.registerReferenceProvider(StandardPatterns.psiElement(CSharpCallArgumentList.class).withParent
				(CSharpArrayAccessExpressionImpl.class), new PsiReferenceProvider()
		{
			@NotNull
			@Override
			public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext processingContext)
			{
				CSharpArrayAccessExpressionImpl parent = (CSharpArrayAccessExpressionImpl) element.getParent();
				PsiElement callable = parent.resolveToCallable();
				if(callable == null)
				{
					return PsiReference.EMPTY_ARRAY;
				}
				return new PsiReference[]{
						new PsiReferenceBase.Immediate<PsiElement>(element, new TextRange(0, element.getTextLength()), callable)
				};
			}
		});

		psiReferenceRegistrar.registerReferenceProvider(StandardPatterns.psiElement(DotNetVirtualImplementOwner.class), new PsiReferenceProvider()
		{
			@NotNull
			@Override
			public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context)
			{
				PsiElement nameIdentifier = ((PsiNameIdentifierOwner) element).getNameIdentifier();
				if(nameIdentifier == null)
				{
					return PsiReference.EMPTY_ARRAY;
				}

				val resultPair = CSharpLikeMethodDeclarationImplUtil.resolveVirtualImplementation((DotNetVirtualImplementOwner) element, element);
				switch(resultPair.getFirst())
				{
					case FOUND:
						int textOffset = nameIdentifier.getStartOffsetInParent();
						PsiReferenceBase.Immediate<PsiElement> psiElementImmediate = new PsiReferenceBase.Immediate<PsiElement>(element,
								new TextRange(textOffset, textOffset + nameIdentifier.getTextLength()), resultPair.getSecond())
						{
							@Override
							public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException
							{
								PsiElement element1 = getElement();
								if(element1 instanceof PsiNamedElement)
								{
									return ((PsiNamedElement) element1).setName(newElementName);
								}
								return element1;
							}
						};

						return new PsiReference[]{psiElementImmediate};
				}

				return PsiReference.EMPTY_ARRAY;
			}
		});
{'fraction_non_alphanumeric': 0.08591757282856759, 'fraction_numerical': 0.005638340716874748, 'mean_word_length': 5.116584564860427, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '6207061', 'n_tokens_mistral': 2426, 'n_tokens_neox': 2063, 'n_words': 446}