[platform] extend PsiReference from SymbolReference (IDEA-190162)

This commit is contained in:
Daniil Ovchinnikov
2019-02-05 18:54:00 +03:00
parent ea604652c6
commit f63325bf2f
6 changed files with 109 additions and 51 deletions
@@ -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;
}
};
}
}
@@ -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;
}
}
@@ -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);
}
@NotNull
ResolveResult[] multiResolve(boolean incompleteCode);
@NotNull
@Override
default Collection<? extends SymbolResolveResult> resolveReference() {
ResolveResult[] results = multiResolve(false);
return ContainerUtil.filter(results, it -> it.getElement() != null);
}
}
@@ -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<PsiReference> 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();
}
@NotNull
@Override
default Collection<? extends SymbolResolveResult> resolveReference() {
PsiElement resolved = resolve();
return resolved == null ? Collections.emptyList() : Collections.singletonList(SymbolService.resolveResult(resolved));
}
}
@@ -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);
}
}
@@ -194,7 +194,7 @@ public abstract class DelimitedListConverter<T> extends ResolvingConverter<List<
return getElement();
}
final String value = getValue();
return resolveReference(convertString(value, myContext), myContext);
return DelimitedListConverter.this.resolveReference(convertString(value, myContext), myContext);
}
@Override