javafx: rename controller field if rename was invoked on source attr of a reference

This commit is contained in:
anna
2013-04-02 13:35:10 +02:00
parent 1e5624fac2
commit 0cb67998fc
7 changed files with 90 additions and 2 deletions
@@ -311,7 +311,7 @@
<referenceProviderType key="Schema references provider"
implementationClass="com.intellij.psi.impl.source.resolve.reference.impl.providers.SchemaReferencesProvider"/>
<renamePsiElementProcessor implementation="com.intellij.refactoring.rename.RenameXmlAttributeProcessor"/>
<renamePsiElementProcessor implementation="com.intellij.refactoring.rename.RenameXmlAttributeProcessor" id="xmlAttribute"/>
<lang.importOptimizer id="XML" language="XML" implementationClass="com.intellij.refactoring.XmlImportOptimizer"/>
<readWriteAccessDetector implementation="com.intellij.codeInsight.highlighting.XmlReadWriteAccessDetector"/>
@@ -18,6 +18,7 @@ package org.jetbrains.plugins.javaFX.fxml;
import com.intellij.codeInsight.TargetElementUtilBase;
import com.intellij.codeInsight.daemon.DaemonAnalyzerTestCase;
import com.intellij.openapi.application.PluginPathManager;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.refactoring.rename.RenameProcessor;
import com.intellij.refactoring.rename.inplace.MemberInplaceRenameHandler;
@@ -51,7 +52,15 @@ public class JavaFXRenameTest extends DaemonAnalyzerTestCase {
public void testCustomComponentTag() throws Exception {
doTest("Foo", true);
}
public void testFromReference() throws Exception {
final String newName = "lbl1";
doTest(newName);
final PsiClass controllerClass = findClass(getTestName(false));
assertNotNull(controllerClass);
assertNotNull(controllerClass.findFieldByName(newName, false));
}
public void testIdWithRefs() throws Exception {
configureByFiles(null, getTestName(true) + ".fxml");
PsiElement element = TargetElementUtilBase
@@ -42,6 +42,7 @@
<packaging.artifactPropertiesProvider implementation="org.jetbrains.plugins.javaFX.packaging.preloader.JavaFxPreloaderArtifactPropertiesProvider"/>
<implicitUsageProvider implementation="org.jetbrains.plugins.javaFX.fxml.codeInsight.inspections.JavaFxImplicitUsageProvider"/>
<referencesSearch implementation="org.jetbrains.plugins.javaFX.fxml.refs.JavaFxControllerFieldSearcher"/>
<renamePsiElementProcessor implementation="org.jetbrains.plugins.javaFX.JavaFxRenameAttributeProcessor" order="before xmlAttribute"/>
</extensions>
<actions>
@@ -0,0 +1,58 @@
/*
* Copyright 2000-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.plugins.javaFX;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiReference;
import com.intellij.psi.search.SearchScope;
import com.intellij.psi.xml.XmlAttribute;
import com.intellij.psi.xml.XmlAttributeValue;
import com.intellij.refactoring.rename.RenameXmlAttributeProcessor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.javaFX.fxml.FxmlConstants;
import org.jetbrains.plugins.javaFX.fxml.JavaFxFileTypeFactory;
import java.util.Map;
/**
* User: anna
* Date: 4/2/13
*/
public class JavaFxRenameAttributeProcessor extends RenameXmlAttributeProcessor {
@Override
public boolean canProcessElement(@NotNull PsiElement element) {
if (element instanceof XmlAttributeValue && JavaFxFileTypeFactory.isFxml(element.getContainingFile())) {
final PsiElement parent = element.getParent();
return parent instanceof XmlAttribute && FxmlConstants.FX_ID.equals(((XmlAttribute)parent).getName());
}
return false;
}
@Override
public void prepareRenaming(PsiElement element, String newName, Map<PsiElement, String> allRenames, SearchScope scope) {
if (element instanceof XmlAttributeValue) {
final XmlAttributeValue refId = (XmlAttributeValue)element;
final PsiReference refIdReference = refId.getReference();
if (refIdReference != null) {
final PsiElement resolveRefId = refIdReference.resolve();
if (resolveRefId instanceof PsiField) {
allRenames.put(resolveRefId, newName);
}
}
}
}
}
@@ -0,0 +1,4 @@
import javafx.control.Label;
public class FromReference {
public Label lb;
}
@@ -0,0 +1,8 @@
<?import javafx.scene.control.Label?>
<?import javafx.layout.*?>
<GridPane xmlns:fx="http://javafx.com/fxml" fx:controller="FromReference">
<fx:define>
<Label fx:id="lb"/>
</fx:define>
<fx:reference source="l<caret>b"/>
</GridPane>
@@ -0,0 +1,8 @@
<?import javafx.scene.control.Label?>
<?import javafx.layout.*?>
<GridPane xmlns:fx="http://javafx.com/fxml" fx:controller="FromReference">
<fx:define>
<Label fx:id="lbl1"/>
</fx:define>
<fx:reference source="lbl1"/>
</GridPane>