From 1cde5021be22317fa10bc5462a6026ca2eb92200 Mon Sep 17 00:00:00 2001 From: Pavel Dolgov Date: Fri, 8 Apr 2016 13:00:01 +0300 Subject: [PATCH] Javafx: Fix highlighting and completion for css file names in FXML stylesheets property, both attribute and tag. Tests added (IDEA-154244) --- .../javaFX/fxml/JavaFXHighlightingTest.java | 12 +++++ .../javaFX/fxml/JavaFxCompletionTest.java | 12 +++++ .../plugins/javaFX/fxml/FxmlConstants.java | 3 +- .../fxml/refs/FxmlReferencesContributor.java | 14 ++++-- .../refs/JavaFxLocationReferenceProvider.java | 44 ++++++++++--------- .../multipleStylesheetsAttribute.fxml | 4 ++ .../multipleStylesheetsAttribute_after.fxml | 4 ++ .../completion/multipleStylesheetsTag.fxml | 9 ++++ .../multipleStylesheetsTag_after.fxml | 9 ++++ .../multipleStylesheetsAttribute.fxml | 4 ++ .../highlighting/multipleStylesheetsTag.fxml | 10 +++++ 11 files changed, 100 insertions(+), 25 deletions(-) create mode 100644 plugins/javaFX/testData/completion/multipleStylesheetsAttribute.fxml create mode 100644 plugins/javaFX/testData/completion/multipleStylesheetsAttribute_after.fxml create mode 100644 plugins/javaFX/testData/completion/multipleStylesheetsTag.fxml create mode 100644 plugins/javaFX/testData/completion/multipleStylesheetsTag_after.fxml create mode 100644 plugins/javaFX/testData/highlighting/multipleStylesheetsAttribute.fxml create mode 100644 plugins/javaFX/testData/highlighting/multipleStylesheetsTag.fxml 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 83b2f7c0901f..d419f44313e1 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 @@ -432,6 +432,18 @@ public class JavaFXHighlightingTest extends AbstractJavaFXTestCase { doTest(); } + public void testMultipleStylesheetsAttribute() throws Exception { + myFixture.addFileToProject("mystyle.css", ".myStyle {}"); + myFixture.addFileToProject("very/deeply/located/small.css", ".small {}"); + doTest(); + } + + public void testMultipleStylesheetsTag() throws Exception { + myFixture.addFileToProject("mystyle.css", ".myStyle {}"); + myFixture.addFileToProject("very/deeply/located/small.css", ".small {}"); + doTest(); + } + private void doTest() throws Exception { myFixture.testHighlighting(false, false, false, getTestName(true) + ".fxml"); } 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 692fe0c91879..03bb60e93cee 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 @@ -271,6 +271,18 @@ public class JavaFxCompletionTest extends LightFixtureCompletionTestCase { assertSameElements(myFixture.getLookupElementStrings(), "MY_TEXT", "SUPER_TEXT"); } + public void testMultipleStylesheetsAttribute() throws Exception { + myFixture.addFileToProject("mystyle.css", ".myStyle {}"); + myFixture.addFileToProject("very/deeply/located/small.css", ".small {}"); + doTest(); + } + + public void testMultipleStylesheetsTag() throws Exception { + myFixture.addFileToProject("mystyle.css", ".myStyle {}"); + myFixture.addFileToProject("very/deeply/located/small.css", ".small {}"); + doTest(); + } + private void configureAndComplete(final String... extraFiles) { final String fxmlFileName = getTestName(true) + ".fxml"; if (extraFiles.length != 0) { diff --git a/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/FxmlConstants.java b/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/FxmlConstants.java index 91276f94f793..b4e75c0b4f72 100644 --- a/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/FxmlConstants.java +++ b/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/FxmlConstants.java @@ -45,7 +45,8 @@ public class FxmlConstants { @NonNls public static final String STYLE_CLASS = "styleClass"; @NonNls public static final String STYLESHEETS = "stylesheets"; - @NonNls public static final String URL = "url"; + @NonNls public static final String URL_ATTR = "url"; + @NonNls public static final String URL_TAG = "URL"; @NonNls public static final String SOURCE = "source"; 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 bc1e087a5e5a..f5ef7a919337 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 @@ -91,7 +91,7 @@ public class FxmlReferencesContributor extends PsiReferenceContributor { new JavaFxEventHandlerReferenceProvider()); registrar.registerReferenceProvider(XmlPatterns.xmlAttributeValue().withValue(string().startsWith("@")).and(attributeValueInFxml), - new JavaFxLocationReferenceProvider()); + new JavaFxLocationReferenceProvider(), PsiReferenceRegistrar.LOWER_PRIORITY); registrar.registerReferenceProvider(XmlPatterns.xmlAttributeValue().withValue(string().startsWith("$")) .withParent(XmlPatterns.xmlAttribute() @@ -99,13 +99,21 @@ public class FxmlReferencesContributor extends PsiReferenceContributor { .and(attributeValueInFxml), new JavaFxComponentIdReferenceProvider()); - registrar.registerReferenceProvider(XmlPatterns.xmlAttributeValue().withParent(XmlPatterns.xmlAttribute().withName(FxmlConstants.URL)) - .andNot(XmlPatterns.xmlAttributeValue().withValue(string().matches("^http(s)?://.*"))) + registrar.registerReferenceProvider(XmlPatterns.xmlAttributeValue() + .withParent(XmlPatterns.xmlAttribute().withName(FxmlConstants.URL_ATTR)) + .andNot(XmlPatterns.xmlAttributeValue().withValue(string().matches("^https?://.*"))) .and(attributeValueInFxml), new JavaFxLocationReferenceProvider(false, "png", "jpg", "gif", "bmp")); registrar.registerReferenceProvider(XmlPatterns.xmlAttributeValue().withParent(XmlPatterns.xmlAttribute().withName(FxmlConstants.STYLESHEETS)).and(attributeValueInFxml), new JavaFxLocationReferenceProvider(true, "css")); + registrar.registerReferenceProvider(XmlPatterns.xmlAttributeValue().withValue(string().startsWith("@")) + .withParent(XmlPatterns.xmlAttribute().withName(FxmlConstants.VALUE) + .withParent(XmlPatterns.xmlTag().withName(FxmlConstants.URL_TAG) + .withParent(XmlPatterns.xmlTag().withName(FxmlConstants.STYLESHEETS)))) + .and(attributeValueInFxml), + new JavaFxLocationReferenceProvider(false, "css")); + 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/JavaFxLocationReferenceProvider.java b/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/refs/JavaFxLocationReferenceProvider.java index ddfba6a6aaa9..3ab923b61f11 100644 --- a/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/refs/JavaFxLocationReferenceProvider.java +++ b/plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/refs/JavaFxLocationReferenceProvider.java @@ -18,6 +18,7 @@ package org.jetbrains.plugins.javaFX.fxml.refs; import com.intellij.openapi.fileTypes.FileType; import com.intellij.openapi.fileTypes.FileTypeManager; import com.intellij.openapi.util.Condition; +import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.*; import com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReferenceSet; @@ -55,31 +56,32 @@ class JavaFxLocationReferenceProvider extends PsiReferenceProvider { public PsiReference[] getReferencesByElement(@NotNull final PsiElement element, @NotNull ProcessingContext context) { final String value = ((XmlAttributeValue)element).getValue(); - if (value.startsWith("@")) { - return new FileReferenceSet(value.substring(1), element, 2, null, true).getAllReferences(); + if (mySupportCommaInValue && value.contains(",")) { + int startIdx = 0; + List refs = new ArrayList(); + while (true) { + int endIdx = value.indexOf(',', startIdx); + final String item = endIdx >= 0 ? value.substring(startIdx, endIdx) : value.substring(startIdx); + Collections.addAll(refs, collectRefs(element, item, startIdx + 1)); + if (endIdx < 0) { + break; + } + startIdx = endIdx + 1; + } + return refs.toArray(PsiReference.EMPTY_ARRAY); } else { - if (mySupportCommaInValue && value.contains(",")) { - int startIdx = 0; - int endIdx = 0; - List refs = new ArrayList(); - while (true) { - endIdx = value.indexOf(",", startIdx); - Collections.addAll(refs, collectRefs(element, endIdx >= 0 ? value.substring(startIdx, endIdx) : value.substring(startIdx), startIdx + 1, myAcceptedFileTypes)); - startIdx = endIdx + 1; - if (endIdx < 0) { - break; - } - } - return refs.toArray(new PsiReference[refs.size()]); - } else { - return collectRefs(element, value, 1, myAcceptedFileTypes); - } + return collectRefs(element, value, 1); } } - private static PsiReference[] collectRefs(@NotNull PsiElement element, String value, final int startInElement, final FileType... acceptedFileTypes) { - final FileReferenceSet set = new FileReferenceSet(value, element, startInElement, null, true){ + private PsiReference[] collectRefs(@NotNull PsiElement element, String value, int startInElement) { + final int atSignIndex = value.indexOf('@'); + if (atSignIndex >= 0 && (atSignIndex == 0 || StringUtil.trimLeading(value).startsWith("@"))) { + value = value.substring(atSignIndex + 1); + startInElement += atSignIndex + 1; + } + final FileReferenceSet set = new FileReferenceSet(value, element, startInElement, null, true) { @Override protected Condition getReferenceCompletionFilter() { return new Condition() { @@ -89,7 +91,7 @@ class JavaFxLocationReferenceProvider extends PsiReferenceProvider { final VirtualFile virtualFile = item.getVirtualFile(); if (virtualFile == null) return false; final FileType fileType = virtualFile.getFileType(); - return ArrayUtilRt.find(acceptedFileTypes, fileType) >= 0; + return ArrayUtilRt.find(myAcceptedFileTypes, fileType) >= 0; } }; } diff --git a/plugins/javaFX/testData/completion/multipleStylesheetsAttribute.fxml b/plugins/javaFX/testData/completion/multipleStylesheetsAttribute.fxml new file mode 100644 index 000000000000..8f1d2a9e97ab --- /dev/null +++ b/plugins/javaFX/testData/completion/multipleStylesheetsAttribute.fxml @@ -0,0 +1,4 @@ + + + \ No newline at end of file diff --git a/plugins/javaFX/testData/completion/multipleStylesheetsAttribute_after.fxml b/plugins/javaFX/testData/completion/multipleStylesheetsAttribute_after.fxml new file mode 100644 index 000000000000..13522f478441 --- /dev/null +++ b/plugins/javaFX/testData/completion/multipleStylesheetsAttribute_after.fxml @@ -0,0 +1,4 @@ + + + \ No newline at end of file diff --git a/plugins/javaFX/testData/completion/multipleStylesheetsTag.fxml b/plugins/javaFX/testData/completion/multipleStylesheetsTag.fxml new file mode 100644 index 000000000000..8defdda14bf2 --- /dev/null +++ b/plugins/javaFX/testData/completion/multipleStylesheetsTag.fxml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/plugins/javaFX/testData/completion/multipleStylesheetsTag_after.fxml b/plugins/javaFX/testData/completion/multipleStylesheetsTag_after.fxml new file mode 100644 index 000000000000..84f3105cb817 --- /dev/null +++ b/plugins/javaFX/testData/completion/multipleStylesheetsTag_after.fxml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/plugins/javaFX/testData/highlighting/multipleStylesheetsAttribute.fxml b/plugins/javaFX/testData/highlighting/multipleStylesheetsAttribute.fxml new file mode 100644 index 000000000000..7f08b33b0b42 --- /dev/null +++ b/plugins/javaFX/testData/highlighting/multipleStylesheetsAttribute.fxml @@ -0,0 +1,4 @@ + +unknown.css, @very/deeply/located/small.css"> + \ No newline at end of file diff --git a/plugins/javaFX/testData/highlighting/multipleStylesheetsTag.fxml b/plugins/javaFX/testData/highlighting/multipleStylesheetsTag.fxml new file mode 100644 index 000000000000..627d63b4250c --- /dev/null +++ b/plugins/javaFX/testData/highlighting/multipleStylesheetsTag.fxml @@ -0,0 +1,10 @@ + + + + + + unknown.css"/> + + + \ No newline at end of file