From c703152ebfbcbc82bd59995dea7de78e21acafbe Mon Sep 17 00:00:00 2001 From: Pavel Dolgov Date: Mon, 12 Sep 2016 18:12:42 +0300 Subject: [PATCH] Javafx: Support resolution of resource keys (IDEA-100213) --- .../javaFX/fxml/JavaFXHighlightingTest.java | 4 ++- .../plugins/javaFX/fxml/JavaFXRenameTest.java | 7 ++++ .../javaFX/fxml/JavaFxCompletionTest.java | 15 ++++++++ plugins/javaFX/javaFX.iml | 1 + .../fxml/refs/FxmlReferencesContributor.java | 6 ++++ ...vaFxResourcePropertyReferenceProvider.java | 35 +++++++++++++++++++ .../testData/completion/resourceProperty.fxml | 5 +++ .../completion/resourcePropertyManyFiles.fxml | 5 +++ ...ibute.fxml => resourceKeyInAttribute.fxml} | 2 +- .../testData/rename/resourceProperty.fxml | 5 +++ .../rename/resourceProperty.properties | 1 + .../rename/resourceProperty_after.fxml | 5 +++ .../rename/resourceProperty_after.properties | 1 + 13 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/refs/JavaFxResourcePropertyReferenceProvider.java create mode 100644 plugins/javaFX/testData/completion/resourceProperty.fxml create mode 100644 plugins/javaFX/testData/completion/resourcePropertyManyFiles.fxml rename plugins/javaFX/testData/highlighting/{resourceIdInFxAttribute.fxml => resourceKeyInAttribute.fxml} (75%) create mode 100644 plugins/javaFX/testData/rename/resourceProperty.fxml create mode 100644 plugins/javaFX/testData/rename/resourceProperty.properties create mode 100644 plugins/javaFX/testData/rename/resourceProperty_after.fxml create mode 100644 plugins/javaFX/testData/rename/resourceProperty_after.properties diff --git a/plugins/javaFX/javaFX-CE/testSrc/org/jetbrains/plugins/javaFX/fxml/JavaFXHighlightingTest.java b/plugins/javaFX/javaFX-CE/testSrc/org/jetbrains/plugins/javaFX/fxml/JavaFXHighlightingTest.java index fc82a64c504d..b4123606a8da 100644 --- a/plugins/javaFX/javaFX-CE/testSrc/org/jetbrains/plugins/javaFX/fxml/JavaFXHighlightingTest.java +++ b/plugins/javaFX/javaFX-CE/testSrc/org/jetbrains/plugins/javaFX/fxml/JavaFXHighlightingTest.java @@ -490,7 +490,9 @@ public class JavaFXHighlightingTest extends AbstractJavaFXTestCase { myFixture.testHighlighting(true, true, true, superclass + ".java"); } - public void testResourceIdInFxAttribute() throws Exception { + public void testResourceKeyInAttribute() throws Exception { + myFixture.addFileToProject("messages.properties", "string.key=My text\n" + + "double.key=123.456\n"); doTest(); } diff --git a/plugins/javaFX/javaFX-CE/testSrc/org/jetbrains/plugins/javaFX/fxml/JavaFXRenameTest.java b/plugins/javaFX/javaFX-CE/testSrc/org/jetbrains/plugins/javaFX/fxml/JavaFXRenameTest.java index 592432817f71..888446423da7 100644 --- a/plugins/javaFX/javaFX-CE/testSrc/org/jetbrains/plugins/javaFX/fxml/JavaFXRenameTest.java +++ b/plugins/javaFX/javaFX-CE/testSrc/org/jetbrains/plugins/javaFX/fxml/JavaFXRenameTest.java @@ -219,6 +219,13 @@ public class JavaFXRenameTest extends AbstractJavaFXRenameTest { doTestProperty("newName", "model.Data", false); } + public void testResourceProperty() { + myFixture.configureByFiles(getTestName(true) + ".fxml", getTestName(true) + ".properties"); + myFixture.renameElementAtCaret("new.name"); + myFixture.checkResultByFile(getTestName(true) + "_after.fxml"); + myFixture.checkResultByFile(getTestName(true) + ".properties", getTestName(true) + "_after.properties", false); + } + public void doTestProperty(String name, boolean isBoolean) throws Exception { doTestProperty(name, null, isBoolean); } diff --git a/plugins/javaFX/javaFX-CE/testSrc/org/jetbrains/plugins/javaFX/fxml/JavaFxCompletionTest.java b/plugins/javaFX/javaFX-CE/testSrc/org/jetbrains/plugins/javaFX/fxml/JavaFxCompletionTest.java index 03bb60e93cee..41ded06c516e 100644 --- a/plugins/javaFX/javaFX-CE/testSrc/org/jetbrains/plugins/javaFX/fxml/JavaFxCompletionTest.java +++ b/plugins/javaFX/javaFX-CE/testSrc/org/jetbrains/plugins/javaFX/fxml/JavaFxCompletionTest.java @@ -330,6 +330,21 @@ public class JavaFxCompletionTest extends LightFixtureCompletionTestCase { } } + public void testResourceProperty() { + myFixture.addFileToProject("messages.properties", "double.key=123.456\n" + + "string.key=Some text\n"); + configureAndComplete(); + assertSameElements(myFixture.getLookupElementStrings(), "double.key", "string.key"); + } + + + public void testResourcePropertyManyFiles() { + myFixture.addFileToProject("messages1.properties", "double.key=123.456\n"); + myFixture.addFileToProject("messages2.properties", "string.key=Some text\n"); + configureAndComplete(); + assertSameElements(myFixture.getLookupElementStrings(), "double.key", "string.key"); + } + private void doTest() throws Exception { doTest(null); } diff --git a/plugins/javaFX/javaFX.iml b/plugins/javaFX/javaFX.iml index 3e4c82d62ae0..0936ae44f5a0 100644 --- a/plugins/javaFX/javaFX.iml +++ b/plugins/javaFX/javaFX.iml @@ -24,6 +24,7 @@ + diff --git a/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/refs/FxmlReferencesContributor.java b/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/refs/FxmlReferencesContributor.java index 7c87fb9758ad..f297e975c585 100644 --- a/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/refs/FxmlReferencesContributor.java +++ b/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/refs/FxmlReferencesContributor.java @@ -114,6 +114,12 @@ public class FxmlReferencesContributor extends PsiReferenceContributor { .and(attributeValueInFxml), new JavaFxLocationReferenceProvider(false, "css")); + registrar.registerReferenceProvider(XmlPatterns.xmlAttributeValue().withValue(string().startsWith("%")) + .withParent(XmlPatterns.xmlAttribute().andNot( + XmlPatterns.xmlAttribute().withName( + FxmlConstants.FX_VALUE, FxmlConstants.FX_CONSTANT, FxmlConstants.FX_FACTORY))) + .and(attributeValueInFxml), new JavaFxResourcePropertyReferenceProvider()); + registrar.registerReferenceProvider(PlatformPatterns.psiElement(XmlProcessingInstruction.class).inVirtualFile(virtualFile().withExtension(JavaFxFileTypeFactory.FXML_EXTENSION)), new ImportReferenceProvider()); diff --git a/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/refs/JavaFxResourcePropertyReferenceProvider.java b/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/refs/JavaFxResourcePropertyReferenceProvider.java new file mode 100644 index 000000000000..8da9bb4643cf --- /dev/null +++ b/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/refs/JavaFxResourcePropertyReferenceProvider.java @@ -0,0 +1,35 @@ +package org.jetbrains.plugins.javaFX.fxml.refs; + +import com.intellij.lang.properties.references.PropertyReference; +import com.intellij.openapi.util.TextRange; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiReference; +import com.intellij.psi.PsiReferenceProvider; +import com.intellij.psi.xml.XmlAttributeValue; +import com.intellij.util.IncorrectOperationException; +import com.intellij.util.ProcessingContext; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * @author Pavel.Dolgov + */ +class JavaFxResourcePropertyReferenceProvider extends PsiReferenceProvider { + @NotNull + @Override + public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) { + if (element instanceof XmlAttributeValue) { + final String value = ((XmlAttributeValue)element).getValue(); + if (value != null && value.startsWith("%") && value.length() > 1) { + return new PsiReference[]{new JavaFxResourcePropertyReference(value.substring(1), (XmlAttributeValue)element)}; + } + } + return PsiReference.EMPTY_ARRAY; + } + + static class JavaFxResourcePropertyReference extends PropertyReference { + public JavaFxResourcePropertyReference(@NotNull String key, @NotNull XmlAttributeValue element) { + super(key, element, null, false, new TextRange(2, key.length() + 2)); // "%key" - shift by 2 because the quote also counts + } + } +} diff --git a/plugins/javaFX/testData/completion/resourceProperty.fxml b/plugins/javaFX/testData/completion/resourceProperty.fxml new file mode 100644 index 000000000000..588f45909b53 --- /dev/null +++ b/plugins/javaFX/testData/completion/resourceProperty.fxml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/plugins/javaFX/testData/completion/resourcePropertyManyFiles.fxml b/plugins/javaFX/testData/completion/resourcePropertyManyFiles.fxml new file mode 100644 index 000000000000..588f45909b53 --- /dev/null +++ b/plugins/javaFX/testData/completion/resourcePropertyManyFiles.fxml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/plugins/javaFX/testData/highlighting/resourceIdInFxAttribute.fxml b/plugins/javaFX/testData/highlighting/resourceKeyInAttribute.fxml similarity index 75% rename from plugins/javaFX/testData/highlighting/resourceIdInFxAttribute.fxml rename to plugins/javaFX/testData/highlighting/resourceKeyInAttribute.fxml index 96dfc950f2be..9491fd6c6b71 100644 --- a/plugins/javaFX/testData/highlighting/resourceIdInFxAttribute.fxml +++ b/plugins/javaFX/testData/highlighting/resourceKeyInAttribute.fxml @@ -8,5 +8,5 @@ "%double.key"/> -