From 09574d72b807ae4d2c782f0d094d33686a8615b7 Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Thu, 6 Mar 2014 20:46:05 +0400 Subject: [PATCH] Translate JetSimpleNameReference to Kotlin --- .../references/JetSimpleNameReference.java | 102 ------------------ .../references/JetSimpleNameReference.kt | 78 ++++++++++++++ 2 files changed, 78 insertions(+), 102 deletions(-) delete mode 100644 idea/src/org/jetbrains/jet/plugin/references/JetSimpleNameReference.java create mode 100644 idea/src/org/jetbrains/jet/plugin/references/JetSimpleNameReference.kt diff --git a/idea/src/org/jetbrains/jet/plugin/references/JetSimpleNameReference.java b/idea/src/org/jetbrains/jet/plugin/references/JetSimpleNameReference.java deleted file mode 100644 index e24a61c5a14..00000000000 --- a/idea/src/org/jetbrains/jet/plugin/references/JetSimpleNameReference.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Copyright 2010-2013 JetBrains s.r.o. - * - * 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.jetbrains.jet.plugin.references; - -import com.intellij.openapi.project.Project; -import com.intellij.openapi.util.TextRange; -import com.intellij.psi.PsiElement; -import com.intellij.psi.tree.IElementType; -import com.intellij.psi.util.PsiTreeUtil; -import com.intellij.util.IncorrectOperationException; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.jet.lang.psi.*; -import org.jetbrains.jet.lang.psi.psiUtil.PsiUtilPackage; -import org.jetbrains.jet.lang.resolve.name.FqName; -import org.jetbrains.jet.lexer.JetTokens; -import org.jetbrains.jet.plugin.codeInsight.CodeInsightPackage; -import org.jetbrains.jet.plugin.codeInsight.ShortenReferences; -import org.jetbrains.jet.plugin.refactoring.RefactoringPackage; - -public class JetSimpleNameReference extends JetSimpleReference { - public JetSimpleNameReference(@NotNull JetSimpleNameExpression jetSimpleNameExpression) { - super(jetSimpleNameExpression); - } - - @NotNull - @Override - public TextRange getRangeInElement() { - return new TextRange(0, getElement().getTextLength()); - } - - @Override - public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException { - IElementType type = getExpression().getReferencedNameElementType(); - Project project = getExpression().getProject(); - PsiElement element; - if (JetTokens.FIELD_IDENTIFIER == type) { - element = JetPsiFactory.createFieldIdentifier(project, newElementName); - } - else if (JetTokens.LABEL_IDENTIFIER == type) { - element = JetPsiFactory.createClassLabel(project, newElementName); - } - else { - element = JetPsiFactory.createNameIdentifier(project, newElementName); - } - return getExpression().getReferencedNameElement().replace(element); - } - - // By default reference binding is delayed - @NotNull - @Override - public PsiElement bindToElement(@NotNull PsiElement element) { - return bindToElement(element, false); - } - - @NotNull - public PsiElement bindToElement(@NotNull PsiElement element, boolean bindImmediately) { - FqName fqName = PsiUtilPackage.getFqName(element); - return fqName != null ? bindToFqName(fqName, bindImmediately) : getExpression(); - } - - @NotNull - public PsiElement bindToFqName(@NotNull FqName fqName, boolean forceImmediateBinding) { - JetSimpleNameExpression currentExpression = getExpression(); - - JetElement qualifier = RefactoringPackage.changeQualifiedName(currentExpression, fqName); - JetSimpleNameExpression newExpression = (JetSimpleNameExpression) PsiUtilPackage.getQualifiedElementSelector(qualifier); - assert newExpression != null : "No selector in qualified element"; - - //noinspection unchecked - boolean needToShorten = - PsiTreeUtil.getParentOfType(currentExpression, JetImportDirective.class, JetPackageDirective.class) == null; - if (needToShorten) { - if (forceImmediateBinding) { - ShortenReferences.instance$.process(PsiUtilPackage.getOutermostNonInterleavingQualifiedElement(newExpression)); - } - else { - CodeInsightPackage.addElementToShorteningWaitSet(newExpression.getProject(), newExpression); - } - } - - return newExpression; - } - - @Override - public String toString() { - return JetSimpleNameReference.class.getSimpleName() + ": " + getExpression().getText(); - } -} diff --git a/idea/src/org/jetbrains/jet/plugin/references/JetSimpleNameReference.kt b/idea/src/org/jetbrains/jet/plugin/references/JetSimpleNameReference.kt new file mode 100644 index 00000000000..cc7a3ddbeaf --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/references/JetSimpleNameReference.kt @@ -0,0 +1,78 @@ +/* + * Copyright 2010-2014 JetBrains s.r.o. + * + * 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.jetbrains.jet.plugin.references + +import com.intellij.openapi.util.TextRange +import com.intellij.psi.PsiElement +import com.intellij.psi.util.PsiTreeUtil +import org.jetbrains.jet.lang.psi.* +import org.jetbrains.jet.lang.resolve.name.FqName +import org.jetbrains.jet.lexer.JetTokens +import org.jetbrains.jet.plugin.codeInsight.ShortenReferences +import org.jetbrains.jet.lang.psi.psiUtil.getFqName +import org.jetbrains.jet.plugin.refactoring.changeQualifiedName +import org.jetbrains.jet.lang.psi.psiUtil.getQualifiedElementSelector +import org.jetbrains.jet.lang.psi.psiUtil.getOutermostNonInterleavingQualifiedElement +import org.jetbrains.jet.plugin.codeInsight.addElementToShorteningWaitSet + +public class JetSimpleNameReference( + jetSimpleNameExpression: JetSimpleNameExpression +) : JetSimpleReference(jetSimpleNameExpression) { + + override fun getRangeInElement(): TextRange = TextRange(0, getElement().getTextLength()) + + public override fun handleElementRename(newElementName: String?): PsiElement? { + if (newElementName == null) return null; + + val project = expression.getProject() + val element = when (expression.getReferencedNameElementType()) { + JetTokens.FIELD_IDENTIFIER -> JetPsiFactory.createFieldIdentifier(project, newElementName) + JetTokens.LABEL_IDENTIFIER -> JetPsiFactory.createClassLabel(project, newElementName) + else -> JetPsiFactory.createNameIdentifier(project, newElementName) + } + + return expression.getReferencedNameElement().replace(element) + } + + // By default reference binding is delayed + override fun bindToElement(element: PsiElement): PsiElement = bindToElement(element, false) + + public fun bindToElement(element: PsiElement, bindImmediately: Boolean): PsiElement { + return element.getFqName()?.let { fqName -> bindToFqName(fqName, bindImmediately) } ?: expression + } + + public fun bindToFqName(fqName: FqName, forceImmediateBinding: Boolean): PsiElement { + val newExpression = expression.changeQualifiedName(fqName).getQualifiedElementSelector() as JetSimpleNameExpression + + val needToShorten = + PsiTreeUtil.getParentOfType(expression, javaClass(), javaClass()) == null + if (needToShorten) { + if (forceImmediateBinding) { + ShortenReferences.process(newExpression.getOutermostNonInterleavingQualifiedElement()) + } + else { + newExpression.getProject().addElementToShorteningWaitSet(newExpression) + } + } + + return newExpression + } + + override fun toString(): String { + return javaClass().getSimpleName() + ": " + expression.getText() + } +}