From 2a635576fccc74cf2bb0841d8d2cf553b01dd884 Mon Sep 17 00:00:00 2001 From: Dmitry Avdeev Date: Mon, 28 Mar 2016 13:56:24 +0300 Subject: [PATCH] IDEA-153402 optimize imports does nothing in new classes --- .../src/META-INF/XmlPlugin.xml | 1 + .../JspxIncludePathReferenceProvider.java | 92 +++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 xml/impl/src/com/intellij/psi/impl/source/resolve/reference/impl/providers/JspxIncludePathReferenceProvider.java diff --git a/platform/platform-resources/src/META-INF/XmlPlugin.xml b/platform/platform-resources/src/META-INF/XmlPlugin.xml index ab46a7c17c77..1a244d0e4dcc 100644 --- a/platform/platform-resources/src/META-INF/XmlPlugin.xml +++ b/platform/platform-resources/src/META-INF/XmlPlugin.xml @@ -313,6 +313,7 @@ + diff --git a/xml/impl/src/com/intellij/psi/impl/source/resolve/reference/impl/providers/JspxIncludePathReferenceProvider.java b/xml/impl/src/com/intellij/psi/impl/source/resolve/reference/impl/providers/JspxIncludePathReferenceProvider.java new file mode 100644 index 000000000000..2f8df4404c20 --- /dev/null +++ b/xml/impl/src/com/intellij/psi/impl/source/resolve/reference/impl/providers/JspxIncludePathReferenceProvider.java @@ -0,0 +1,92 @@ +/* + * Copyright 2000-2016 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 com.intellij.psi.impl.source.resolve.reference.impl.providers; + +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiReference; +import com.intellij.psi.PsiReferenceProvider; +import com.intellij.psi.xml.XmlAttributeValue; +import com.intellij.psi.xml.XmlTag; +import com.intellij.psi.xml.XmlTagValue; +import com.intellij.util.ProcessingContext; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Map; + +/** + * @author ven + */ +public class JspxIncludePathReferenceProvider extends PsiReferenceProvider implements CustomizableReferenceProvider { + @Nullable private Map myOptions; + + @Override + @NotNull + public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull final ProcessingContext context) { + if (element instanceof XmlAttributeValue) { + final XmlAttributeValue attributeValue = ((XmlAttributeValue)element); + + String valueString = attributeValue.getValue(); + if(valueString.indexOf('?') >= 0) + return getReferencesByString(valueString.substring(0, valueString.indexOf('?')), attributeValue, 1); + return getReferencesByString(valueString, attributeValue, 1); + } else if (element instanceof XmlTag) { + final XmlTag tag = ((XmlTag)element); + + final XmlTagValue value = tag.getValue(); + final String text = value.getText(); + String trimmedText = text.trim(); + + return getReferencesByString( + trimmedText, + tag, value.getTextRange().getStartOffset() + text.indexOf(trimmedText) - element.getTextOffset() + ); + } + return PsiReference.EMPTY_ARRAY; + } + + @NotNull + private PsiReference[] getReferencesByString(String str, @NotNull PsiElement position, int offsetInPosition) { + return getFileReferencesFromString(str, position, offsetInPosition, this); + } + + public static PsiReference[] getFileReferencesFromString(final String str, @NotNull PsiElement position, final int offsetInPosition, + PsiReferenceProvider provider) { + return getFileReferenceSet(str, position, offsetInPosition, provider).getAllReferences(); + } + + public static FileReferenceSet getFileReferenceSet(final String str, @NotNull PsiElement position, + final int offsetInPosition, + final PsiReferenceProvider provider) { + return new FileReferenceSet(str, position, offsetInPosition, provider, true) { + @Override + protected boolean useIncludingFileAsContext() { + return false; + } + }; + } + + @Override + public void setOptions(@Nullable Map options) { + myOptions = options; + } + + @Override + @Nullable + public Map getOptions() { + return myOptions; + } +}