From f63325bf2f2e8ac877e45bd7b201e3c42fe447e2 Mon Sep 17 00:00:00 2001 From: Daniil Ovchinnikov Date: Thu, 29 Mar 2018 17:49:51 +0300 Subject: [PATCH] [platform] extend PsiReference from SymbolReference (IDEA-190162) --- .../src/com/intellij/model/SymbolService.java | 36 +++++++++++++++++++ .../intellij/model/psi/PsiElementSymbol.java | 25 +++++++++++++ .../intellij/psi/PsiPolyVariantReference.java | 32 ++++++++--------- .../src/com/intellij/psi/PsiReference.java | 34 +++++++++--------- .../src/com/intellij/psi/ResolveResult.java | 31 ++++++++-------- .../converters/DelimitedListConverter.java | 2 +- 6 files changed, 109 insertions(+), 51 deletions(-) create mode 100644 platform/core-api/src/com/intellij/model/SymbolService.java create mode 100644 platform/core-api/src/com/intellij/model/psi/PsiElementSymbol.java diff --git a/platform/core-api/src/com/intellij/model/SymbolService.java b/platform/core-api/src/com/intellij/model/SymbolService.java new file mode 100644 index 000000000000..f90d226e146d --- /dev/null +++ b/platform/core-api/src/com/intellij/model/SymbolService.java @@ -0,0 +1,36 @@ +// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package com.intellij.model; + +import com.intellij.model.psi.PsiElementSymbol; +import com.intellij.psi.PsiElement; +import org.jetbrains.annotations.ApiStatus.Experimental; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; + +@Experimental +public interface SymbolService { + + @Contract(pure = true) + @NotNull + static Symbol adaptPsiElement(@NotNull PsiElement element) { + if (element instanceof Symbol) { + return (Symbol)element; + } + else { + return new PsiElementSymbol(element); + } + } + + @Contract(pure = true) + @NotNull + static SymbolResolveResult resolveResult(@NotNull PsiElement element) { + Symbol symbol = adaptPsiElement(element); + return new SymbolResolveResult() { + @NotNull + @Override + public Symbol getTarget() { + return symbol; + } + }; + } +} diff --git a/platform/core-api/src/com/intellij/model/psi/PsiElementSymbol.java b/platform/core-api/src/com/intellij/model/psi/PsiElementSymbol.java new file mode 100644 index 000000000000..6b093bd8c5df --- /dev/null +++ b/platform/core-api/src/com/intellij/model/psi/PsiElementSymbol.java @@ -0,0 +1,25 @@ +// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package com.intellij.model.psi; + +import com.intellij.model.Symbol; +import com.intellij.psi.PsiElement; +import org.jetbrains.annotations.ApiStatus.Experimental; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; + +@Experimental +public final class PsiElementSymbol implements Symbol { + + private final @NotNull PsiElement myElement; + + @Contract(pure = true) + public PsiElementSymbol(@NotNull PsiElement element) { + myElement = element; + } + + @Contract(pure = true) + @NotNull + public PsiElement getElement() { + return myElement; + } +} diff --git a/platform/core-api/src/com/intellij/psi/PsiPolyVariantReference.java b/platform/core-api/src/com/intellij/psi/PsiPolyVariantReference.java index 1e443a3d9a99..f4012c31a7c9 100644 --- a/platform/core-api/src/com/intellij/psi/PsiPolyVariantReference.java +++ b/platform/core-api/src/com/intellij/psi/PsiPolyVariantReference.java @@ -1,22 +1,12 @@ -/* - * 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. - */ +// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.psi; +import com.intellij.model.SymbolResolveResult; +import com.intellij.util.containers.ContainerUtil; import org.jetbrains.annotations.NotNull; +import java.util.Collection; + /** * Inherit this interface if you want the reference to resolve to more than one element, * or if you want to provide resolve result(s) for a superset of valid resolve cases. @@ -36,5 +26,13 @@ public interface PsiPolyVariantReference extends PsiReference { * * @return the array of results for resolving the reference. */ - @NotNull ResolveResult[] multiResolve(boolean incompleteCode); -} \ No newline at end of file + @NotNull + ResolveResult[] multiResolve(boolean incompleteCode); + + @NotNull + @Override + default Collection resolveReference() { + ResolveResult[] results = multiResolve(false); + return ContainerUtil.filter(results, it -> it.getElement() != null); + } +} diff --git a/platform/core-api/src/com/intellij/psi/PsiReference.java b/platform/core-api/src/com/intellij/psi/PsiReference.java index b63e27c2b12c..d50803d3124c 100644 --- a/platform/core-api/src/com/intellij/psi/PsiReference.java +++ b/platform/core-api/src/com/intellij/psi/PsiReference.java @@ -1,20 +1,9 @@ -/* - * Copyright 2000-2018 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. - */ +// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.psi; +import com.intellij.model.SymbolReference; +import com.intellij.model.SymbolResolveResult; +import com.intellij.model.SymbolService; import com.intellij.openapi.util.TextRange; import com.intellij.util.ArrayFactory; import com.intellij.util.ArrayUtil; @@ -22,6 +11,9 @@ import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.Collection; +import java.util.Collections; + /** * A reference to a PSI element. For example, the variable name used in an expression. * The "Go to Declaration" action can be used to go from a reference to the element it references. @@ -35,7 +27,8 @@ import org.jetbrains.annotations.Nullable; * @see PsiReferenceBase * @see PsiReferenceContributor */ -public interface PsiReference { +public interface PsiReference extends SymbolReference { + PsiReference[] EMPTY_ARRAY = new PsiReference[0]; ArrayFactory ARRAY_FACTORY = count -> count == 0 ? EMPTY_ARRAY : new PsiReference[count]; @@ -138,4 +131,11 @@ public interface PsiReference { * @return true if the reference is soft, false otherwise. */ boolean isSoft(); -} \ No newline at end of file + + @NotNull + @Override + default Collection resolveReference() { + PsiElement resolved = resolve(); + return resolved == null ? Collections.emptyList() : Collections.singletonList(SymbolService.resolveResult(resolved)); + } +} diff --git a/platform/core-api/src/com/intellij/psi/ResolveResult.java b/platform/core-api/src/com/intellij/psi/ResolveResult.java index 058f5a357415..09a4f1942e2d 100644 --- a/platform/core-api/src/com/intellij/psi/ResolveResult.java +++ b/platform/core-api/src/com/intellij/psi/ResolveResult.java @@ -1,28 +1,20 @@ -/* - * Copyright 2000-2009 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. - */ +// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.psi; +import com.intellij.model.Symbol; +import com.intellij.model.SymbolResolveResult; +import com.intellij.model.SymbolService; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.Objects; + /** * Represents the result of resolving a {@link com.intellij.psi.PsiPolyVariantReference}. * * @see com.intellij.psi.PsiElementResolveResult */ -public interface ResolveResult { +public interface ResolveResult extends SymbolResolveResult { /** * The empty array of PSI resolve results which can be reused to avoid unnecessary allocations. */ @@ -42,4 +34,11 @@ public interface ResolveResult { * @return true if the resolve encountered no problems */ boolean isValidResult(); + + @NotNull + @Override + default Symbol getTarget() { + PsiElement element = Objects.requireNonNull(getElement(), "Don't access this property on empty results"); + return SymbolService.adaptPsiElement(element); + } } diff --git a/xml/dom-openapi/src/com/intellij/util/xml/converters/DelimitedListConverter.java b/xml/dom-openapi/src/com/intellij/util/xml/converters/DelimitedListConverter.java index 94c14891ef37..c57b269844af 100644 --- a/xml/dom-openapi/src/com/intellij/util/xml/converters/DelimitedListConverter.java +++ b/xml/dom-openapi/src/com/intellij/util/xml/converters/DelimitedListConverter.java @@ -194,7 +194,7 @@ public abstract class DelimitedListConverter extends ResolvingConverter