From 7eb40ee159d018b8065b588da112e1df82dcbd8a Mon Sep 17 00:00:00 2001 From: UNV Date: Wed, 4 Mar 2026 02:54:57 +0300 Subject: [PATCH] Replacing StringUtil.escapeXml with more optimized XmlStringUtil.escapeText. Some refactoring. --- .../CSharpDocumentationProvider.java | 800 ++++++++---------- .../impl/ide/highlight/check/impl/CC0001.java | 726 ++++++++-------- .../ParameterPresentationBuilder.java | 79 +- .../impl/CSharpExpressionTypeProvider.java | 70 +- 4 files changed, 757 insertions(+), 918 deletions(-) diff --git a/csharp-impl/src/main/java/consulo/csharp/impl/ide/documentation/CSharpDocumentationProvider.java b/csharp-impl/src/main/java/consulo/csharp/impl/ide/documentation/CSharpDocumentationProvider.java index 46189c87d..ec54e3219 100644 --- a/csharp-impl/src/main/java/consulo/csharp/impl/ide/documentation/CSharpDocumentationProvider.java +++ b/csharp-impl/src/main/java/consulo/csharp/impl/ide/documentation/CSharpDocumentationProvider.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package consulo.csharp.impl.ide.documentation; import consulo.annotation.access.RequiredReadAction; @@ -37,7 +36,6 @@ import consulo.language.psi.PsiFile; import consulo.language.psi.PsiManager; import consulo.language.psi.PsiNamedElement; -import consulo.language.util.ModuleUtilCore; import consulo.module.Module; import consulo.module.content.ProjectFileIndex; import consulo.module.content.ProjectRootManager; @@ -50,450 +48,376 @@ import jakarta.annotation.Nonnull; import jakarta.annotation.Nullable; + import java.util.Collection; import java.util.List; import java.util.function.Function; /** * @author VISTALL - * @since 15.12.14 + * @since 2014-12-15 */ @ExtensionImpl -public class CSharpDocumentationProvider implements LanguageDocumentationProvider -{ - private static final String TYPE_PREFIX = "csharp_type::"; - - @Nullable - @Override - @RequiredReadAction - public String getQuickNavigateInfo(PsiElement element, PsiElement element2) - { - return getNavigateInfo(element, false); - } - - @Nullable - @RequiredReadAction - private static String getNavigateInfo(PsiElement element, boolean isFullDocumentation) - { - if(element instanceof DotNetTypeDeclaration) - { - return generateQuickTypeDeclarationInfo((DotNetTypeDeclaration) element, isFullDocumentation); - } - else if(element instanceof DotNetVariable) - { - return generateQuickVariableInfo((DotNetVariable) element, isFullDocumentation); - } - else if(element instanceof DotNetLikeMethodDeclaration) - { - return generateQuickLikeMethodDeclarationInfo((DotNetLikeMethodDeclaration) element, isFullDocumentation); - } - else if(element instanceof CSharpTypeDefStatement) - { - return generateQuickTypeAliasInfo((CSharpTypeDefStatement) element, isFullDocumentation); - } - return null; - } - - @RequiredReadAction - private static String generateQuickTypeAliasInfo(CSharpTypeDefStatement typeDefStatement, boolean isFullDocumentation) - { - StringBuilder builder = new StringBuilder(); - builder.append("type alias "); - appendName(typeDefStatement, builder, isFullDocumentation); - - DotNetTypeRef typeRef = typeDefStatement.toTypeRef(); - if(typeRef != DotNetTypeRef.ERROR_TYPE) - { - builder.append(" = "); - builder.append(generateLinksForType(typeRef, typeDefStatement, true)); - } - return builder.toString(); - } - - @RequiredReadAction - private static String generateQuickLikeMethodDeclarationInfo(DotNetLikeMethodDeclaration element, boolean isFullDocumentation) - { - StringBuilder builder = new StringBuilder(); - - appendModifiers(element, builder); - - if(element instanceof CSharpMethodDeclaration && ((CSharpMethodDeclaration) element).isDelegate()) - { - builder.append("delegate "); - } - - if(element instanceof DotNetConstructorDeclaration) - { - if(((DotNetConstructorDeclaration) element).isDeConstructor()) - { - builder.append("~"); - } - appendName(element, builder, isFullDocumentation); - } - else - { - builder.append(generateLinksForType(element.getReturnTypeRef(), element, isFullDocumentation)); - builder.append(" "); - if(element instanceof CSharpIndexMethodDeclaration) - { - builder.append("this"); - } - else - { - if(isFullDocumentation) - { - builder.append(""); +public class CSharpDocumentationProvider implements LanguageDocumentationProvider { + private static final String TYPE_PREFIX = "csharp_type::"; + + @Nullable + @Override + @RequiredReadAction + public String getQuickNavigateInfo(PsiElement element, PsiElement element2) { + return getNavigateInfo(element, false); + } + + @Nullable + @RequiredReadAction + private static String getNavigateInfo(PsiElement element, boolean isFullDocumentation) { + if (element instanceof DotNetTypeDeclaration) { + return generateQuickTypeDeclarationInfo((DotNetTypeDeclaration) element, isFullDocumentation); + } + else if (element instanceof DotNetVariable) { + return generateQuickVariableInfo((DotNetVariable) element, isFullDocumentation); + } + else if (element instanceof DotNetLikeMethodDeclaration) { + return generateQuickLikeMethodDeclarationInfo((DotNetLikeMethodDeclaration) element, isFullDocumentation); + } + else if (element instanceof CSharpTypeDefStatement) { + return generateQuickTypeAliasInfo((CSharpTypeDefStatement) element, isFullDocumentation); + } + return null; + } + + @RequiredReadAction + private static String generateQuickTypeAliasInfo(CSharpTypeDefStatement typeDefStatement, boolean isFullDocumentation) { + StringBuilder builder = new StringBuilder(); + builder.append("type alias "); + appendName(typeDefStatement, builder, isFullDocumentation); + + DotNetTypeRef typeRef = typeDefStatement.toTypeRef(); + if (typeRef != DotNetTypeRef.ERROR_TYPE) { + builder.append(" = "); + builder.append(generateLinksForType(typeRef, typeDefStatement, true)); + } + return builder.toString(); + } + + @RequiredReadAction + private static String generateQuickLikeMethodDeclarationInfo(DotNetLikeMethodDeclaration element, boolean isFullDocumentation) { + StringBuilder builder = new StringBuilder(); + + appendModifiers(element, builder); + + if (element instanceof CSharpMethodDeclaration && ((CSharpMethodDeclaration) element).isDelegate()) { + builder.append("delegate "); + } + + if (element instanceof DotNetConstructorDeclaration constructorDeclaration) { + if (constructorDeclaration.isDeConstructor()) { + builder.append("~"); + } + appendName(element, builder, isFullDocumentation); + } + else { + builder.append(generateLinksForType(element.getReturnTypeRef(), element, isFullDocumentation)); + builder.append(" "); + if (element instanceof CSharpIndexMethodDeclaration) { + builder.append("this"); + } + else { + if (isFullDocumentation) { + builder.append(""); + } + + String name = element.getName(); + if (name != null) { + XmlStringUtil.escapeText(name, builder); } - builder.append(XmlStringUtil.escapeString(element.getName())); - - if(isFullDocumentation) - { - builder.append(""); - } - } - } - DotNetGenericParameter[] genericParameters = element.getGenericParameters(); - if(genericParameters.length > 0) - { - builder.append("<"); - for(int i = 0; i < genericParameters.length; i++) - { - DotNetGenericParameter genericParameter = genericParameters[i]; - if(i != 0) - { - builder.append(", "); - } - builder.append(genericParameter.getName()); - } - builder.append(">"); - } - char[] openAndCloseTokens = CSharpParametersInfo.getOpenAndCloseTokens(element); - builder.append(openAndCloseTokens[0]); - builder.append(StringUtil.join(element.getParameters(), new Function() - { - @Override - @RequiredReadAction - public String apply(DotNetParameter dotNetParameter) - { - DotNetTypeRef typeRef = dotNetParameter.toTypeRef(true); - if(typeRef == CSharpStaticTypeRef.__ARGLIST_TYPE) - { - return typeRef.toString(); - } - return generateLinksForType(typeRef, dotNetParameter, false) + " " + dotNetParameter.getName(); - } - }, ", ")); - builder.append(openAndCloseTokens[1]); - return builder.toString(); - } - - @RequiredReadAction - private static String generateQuickVariableInfo(DotNetVariable element, boolean isFullDocumentation) - { - StringBuilder builder = new StringBuilder(); - - appendModifiers(element, builder); - if(element instanceof CSharpEventDeclaration) - { - builder.append("event "); - } - builder.append(generateLinksForType(element.toTypeRef(true), element, isFullDocumentation)); - builder.append(" "); - appendName(element, builder, isFullDocumentation); - DotNetExpression initializer = element.getInitializer(); - if(initializer != null) - { - builder.append(" = "); - builder.append(initializer.getText()); - } - builder.append(";"); - return builder.toString(); - } - - @RequiredReadAction - private static String generateQuickTypeDeclarationInfo(DotNetTypeDeclaration element, boolean isFullDocumentation) - { - StringBuilder builder = new StringBuilder(); - - if(isFullDocumentation) - { - PsiFile containingFile = element.getContainingFile(); - final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(element.getProject()).getFileIndex(); - VirtualFile vFile = containingFile == null ? null : containingFile.getVirtualFile(); - if(vFile != null && (fileIndex.isInLibrarySource(vFile) || fileIndex.isInLibraryClasses(vFile))) - { - final List orderEntries = fileIndex.getOrderEntriesForFile(vFile); - if(orderEntries.size() > 0) - { - final OrderEntry orderEntry = orderEntries.get(0); - builder.append("[").append(StringUtil.escapeXml(orderEntry.getPresentableName())).append("] "); - } - } - else - { - final Module module = containingFile == null ? null : ModuleUtilCore.findModuleForPsiElement(containingFile); - if(module != null) - { - builder.append('[').append(module.getName()).append("] "); - } - } - } - - String presentableParentQName = element.getPresentableParentQName(); - if(!StringUtil.isEmpty(presentableParentQName)) - { - builder.append(presentableParentQName); - } - - if(builder.length() > 0) - { - builder.append("
"); - } - - appendModifiers(element, builder); - - appendTypeDeclarationType(element, builder); - - builder.append(" "); - - appendName(element, builder, isFullDocumentation); - - return builder.toString(); - } - - @RequiredReadAction - private static void appendName(PsiNamedElement element, StringBuilder builder, boolean isFullDocumentation) - { - if(isFullDocumentation) - { - builder.append("").append(element.getName()).append(""); - } - else - { - builder.append(element.getName()); - } - } - - @Nullable - @Override - public List getUrlFor(PsiElement element, PsiElement element2) - { - return null; - } - - @Nullable - @Override - @RequiredReadAction - public String generateDoc(PsiElement element, @Nullable PsiElement element2) - { - if(element instanceof CSharpTypeDefStatement) - { - PsiElement resolvedElement = ((CSharpTypeDefStatement) element).toTypeRef().resolve().getElement(); - if(resolvedElement != null) - { - element = resolvedElement; - } - } - - StringBuilder builder = new StringBuilder(); - String info = getNavigateInfo(element, true); - if(info != null) - { - builder.append(info); - builder.append("

"); - } - - IDocumentation documentation = DotNetDocumentationCache.getInstance().findDocumentation(element); - if(documentation == null) - { - documentation = tryToFindFromOverrideParent(element); - } - - if(documentation != null) - { - builder.append(CodeHierarchyHelper.getFormText(documentation)); - } - - return builder.length() > 0 ? builder.toString() : null; - } - - @RequiredReadAction - private IDocumentation tryToFindFromOverrideParent(PsiElement element) - { - if(!(element instanceof DotNetVirtualImplementOwner)) - { - return null; - } - - Collection members = OverrideUtil.collectOverridingMembers((DotNetVirtualImplementOwner) element); - - for(DotNetVirtualImplementOwner owner : members) - { - IDocumentation documentation = DotNetDocumentationCache.getInstance().findDocumentation(owner); - if(documentation != null) - { - return documentation; - } - } - return null; - } - - @RequiredReadAction - private static void appendTypeDeclarationType(DotNetTypeDeclaration psiElement, StringBuilder builder) - { - if(psiElement.isInterface()) - { - builder.append("interface"); - } - else if(psiElement.isEnum()) - { - builder.append("enum"); - } - else if(psiElement.isStruct()) - { - builder.append("struct"); - } - else - { - builder.append("class"); - } - } - - @RequiredReadAction - private static void appendModifiers(DotNetModifierListOwner owner, StringBuilder builder) - { - DotNetModifierList modifierList = owner.getModifierList(); - if(modifierList == null) - { - return; - } - - for(DotNetModifier modifier : modifierList.getModifiers()) - { - builder.append(modifier.getPresentableText()).append(" "); - } - } - - @Nullable - @Override - @RequiredReadAction - public PsiElement getDocumentationElementForLink(PsiManager psiManager, String s, PsiElement element) - { - if(s.startsWith(TYPE_PREFIX)) - { - String qName = s.substring(TYPE_PREFIX.length(), s.length()); - - return DotNetPsiSearcher.getInstance(element.getProject()).findType(qName, element.getResolveScope()); - } - return null; - } - - @RequiredReadAction - private static String generateLinksForType(DotNetTypeRef typeRef, PsiElement element, boolean qualified) - { - StringBuilder builder = new StringBuilder(); - if(typeRef == DotNetTypeRef.AUTO_TYPE) - { - builder.append("var"); - } - else if(typeRef instanceof DotNetArrayTypeRef) - { - builder.append(generateLinksForType(((DotNetArrayTypeRef) typeRef).getInnerTypeRef(), element, qualified)); - builder.append("[]"); - } - else if(typeRef instanceof CSharpRefTypeRef) - { - builder.append(((CSharpRefTypeRef) typeRef).getType().name()); - builder.append(" "); - builder.append(generateLinksForType(((CSharpRefTypeRef) typeRef).getInnerTypeRef(), element, qualified)); - } - else if(typeRef instanceof DotNetPointerTypeRef) - { - builder.append(generateLinksForType(((DotNetPointerTypeRef) typeRef).getInnerTypeRef(), element, qualified)); - builder.append("*"); - } - else - { - DotNetTypeResolveResult dotNetTypeResolveResult = typeRef.resolve(); - PsiElement resolved = dotNetTypeResolveResult.getElement(); - if(resolved instanceof DotNetQualifiedElement) - { - if(resolved instanceof DotNetGenericParameterListOwner) - { - wrapToLink(resolved, builder, qualified); - - DotNetGenericParameter[] genericParameters = ((DotNetGenericParameterListOwner) resolved).getGenericParameters(); - if(genericParameters.length > 0) - { - DotNetGenericExtractor genericExtractor = dotNetTypeResolveResult.getGenericExtractor(); - builder.append(XmlStringUtil.escapeString("<")); - for(int i = 0; i < genericParameters.length; i++) - { - if(i != 0) - { - builder.append(", "); - } - DotNetGenericParameter parameter = genericParameters[i]; - - DotNetTypeRef extractedTypeRef = genericExtractor.extract(parameter); - if(extractedTypeRef != null) - { - builder.append(generateLinksForType(extractedTypeRef, element, qualified)); - } - else - { - builder.append(parameter.getName()); - } - } - builder.append(XmlStringUtil.escapeString(">")); - } - } - else - { - builder.append(((DotNetQualifiedElement) resolved).getName()); - } - } - else - { - builder.append(CSharpTypeRefPresentationUtil.buildShortText(typeRef)); - } - } - - return builder.toString(); - } - - @RequiredReadAction - private static void wrapToLink(@Nonnull PsiElement resolved, StringBuilder builder, boolean qualified) - { - String parentQName = qualified ? resolved instanceof DotNetQualifiedElement ? ((DotNetQualifiedElement) resolved).getPresentableParentQName() : null : null; - - if(!StringUtil.isEmpty(parentQName)) - { - builder.append(parentQName).append('.'); - } - - builder.append("").append(((PsiNamedElement) resolved).getName()).append(""); - } - - @Nonnull - @Override - public Language getLanguage() - { - return CSharpLanguage.INSTANCE; - } + if (isFullDocumentation) { + builder.append("
"); + } + } + } + DotNetGenericParameter[] genericParameters = element.getGenericParameters(); + if (genericParameters.length > 0) { + builder.append("<"); + for (int i = 0; i < genericParameters.length; i++) { + DotNetGenericParameter genericParameter = genericParameters[i]; + if (i != 0) { + builder.append(", "); + } + builder.append(genericParameter.getName()); + } + builder.append(">"); + } + char[] openAndCloseTokens = CSharpParametersInfo.getOpenAndCloseTokens(element); + builder.append(openAndCloseTokens[0]); + builder.append(StringUtil.join(element.getParameters(), new Function<>() { + @Override + @RequiredReadAction + public String apply(DotNetParameter dotNetParameter) { + DotNetTypeRef typeRef = dotNetParameter.toTypeRef(true); + if (typeRef == CSharpStaticTypeRef.__ARGLIST_TYPE) { + return typeRef.toString(); + } + return generateLinksForType(typeRef, dotNetParameter, false) + " " + dotNetParameter.getName(); + } + }, ", ")); + builder.append(openAndCloseTokens[1]); + return builder.toString(); + } + + @RequiredReadAction + private static String generateQuickVariableInfo(DotNetVariable element, boolean isFullDocumentation) { + StringBuilder builder = new StringBuilder(); + + appendModifiers(element, builder); + if (element instanceof CSharpEventDeclaration) { + builder.append("event "); + } + builder.append(generateLinksForType(element.toTypeRef(true), element, isFullDocumentation)); + builder.append(" "); + appendName(element, builder, isFullDocumentation); + DotNetExpression initializer = element.getInitializer(); + if (initializer != null) { + builder.append(" = "); + builder.append(initializer.getText()); + } + builder.append(";"); + return builder.toString(); + } + + @RequiredReadAction + private static String generateQuickTypeDeclarationInfo(DotNetTypeDeclaration element, boolean isFullDocumentation) { + StringBuilder builder = new StringBuilder(); + + if (isFullDocumentation) { + PsiFile containingFile = element.getContainingFile(); + ProjectFileIndex fileIndex = ProjectRootManager.getInstance(element.getProject()).getFileIndex(); + VirtualFile vFile = containingFile == null ? null : containingFile.getVirtualFile(); + if (vFile != null && (fileIndex.isInLibrarySource(vFile) || fileIndex.isInLibraryClasses(vFile))) { + List orderEntries = fileIndex.getOrderEntriesForFile(vFile); + if (orderEntries.size() > 0) { + OrderEntry orderEntry = orderEntries.get(0); + builder.append("["); + XmlStringUtil.escapeText(orderEntry.getPresentableName(), builder); + builder.append("] "); + } + } + else { + Module module = containingFile == null ? null : containingFile.getModule(); + if (module != null) { + builder.append('[').append(module.getName()).append("] "); + } + } + } + + String presentableParentQName = element.getPresentableParentQName(); + if (!StringUtil.isEmpty(presentableParentQName)) { + builder.append(presentableParentQName); + } + + if (builder.length() > 0) { + builder.append("
"); + } + + appendModifiers(element, builder); + + appendTypeDeclarationType(element, builder); + + builder.append(" "); + + appendName(element, builder, isFullDocumentation); + + return builder.toString(); + } + + @RequiredReadAction + private static void appendName(PsiNamedElement element, StringBuilder builder, boolean isFullDocumentation) { + if (isFullDocumentation) { + builder.append("").append(element.getName()).append(""); + } + else { + builder.append(element.getName()); + } + } + + @Nullable + @Override + public List getUrlFor(PsiElement element, PsiElement element2) { + return null; + } + + @Nullable + @Override + @RequiredReadAction + public String generateDoc(PsiElement element, @Nullable PsiElement element2) { + if (element instanceof CSharpTypeDefStatement) { + PsiElement resolvedElement = ((CSharpTypeDefStatement) element).toTypeRef().resolve().getElement(); + if (resolvedElement != null) { + element = resolvedElement; + } + } + + StringBuilder builder = new StringBuilder(); + String info = getNavigateInfo(element, true); + if (info != null) { + builder.append(info); + builder.append("

"); + } + + IDocumentation documentation = DotNetDocumentationCache.getInstance().findDocumentation(element); + if (documentation == null) { + documentation = tryToFindFromOverrideParent(element); + } + + if (documentation != null) { + builder.append(CodeHierarchyHelper.getFormText(documentation)); + } + + return builder.length() > 0 ? builder.toString() : null; + } + + @RequiredReadAction + private IDocumentation tryToFindFromOverrideParent(PsiElement element) { + if (!(element instanceof DotNetVirtualImplementOwner)) { + return null; + } + + Collection members = OverrideUtil.collectOverridingMembers((DotNetVirtualImplementOwner) element); + + for (DotNetVirtualImplementOwner owner : members) { + IDocumentation documentation = DotNetDocumentationCache.getInstance().findDocumentation(owner); + if (documentation != null) { + return documentation; + } + } + return null; + } + + @RequiredReadAction + private static void appendTypeDeclarationType(DotNetTypeDeclaration psiElement, StringBuilder builder) { + if (psiElement.isInterface()) { + builder.append("interface"); + } + else if (psiElement.isEnum()) { + builder.append("enum"); + } + else if (psiElement.isStruct()) { + builder.append("struct"); + } + else { + builder.append("class"); + } + } + + @RequiredReadAction + private static void appendModifiers(DotNetModifierListOwner owner, StringBuilder builder) { + DotNetModifierList modifierList = owner.getModifierList(); + if (modifierList == null) { + return; + } + + for (DotNetModifier modifier : modifierList.getModifiers()) { + builder.append(modifier.getPresentableText()).append(" "); + } + } + + @Nullable + @Override + @RequiredReadAction + public PsiElement getDocumentationElementForLink(PsiManager psiManager, String s, PsiElement element) { + if (s.startsWith(TYPE_PREFIX)) { + String qName = s.substring(TYPE_PREFIX.length(), s.length()); + + return DotNetPsiSearcher.getInstance(element.getProject()).findType(qName, element.getResolveScope()); + } + return null; + } + + @RequiredReadAction + private static String generateLinksForType(DotNetTypeRef typeRef, PsiElement element, boolean qualified) { + StringBuilder builder = new StringBuilder(); + if (typeRef == DotNetTypeRef.AUTO_TYPE) { + builder.append("var"); + } + else if (typeRef instanceof DotNetArrayTypeRef) { + builder.append(generateLinksForType(((DotNetArrayTypeRef) typeRef).getInnerTypeRef(), element, qualified)); + builder.append("[]"); + } + else if (typeRef instanceof CSharpRefTypeRef) { + builder.append(((CSharpRefTypeRef) typeRef).getType().name()); + builder.append(" "); + builder.append(generateLinksForType(((CSharpRefTypeRef) typeRef).getInnerTypeRef(), element, qualified)); + } + else if (typeRef instanceof DotNetPointerTypeRef) { + builder.append(generateLinksForType(((DotNetPointerTypeRef) typeRef).getInnerTypeRef(), element, qualified)); + builder.append("*"); + } + else { + DotNetTypeResolveResult dotNetTypeResolveResult = typeRef.resolve(); + if (dotNetTypeResolveResult.getElement() instanceof DotNetQualifiedElement qualifiedElem) { + if (qualifiedElem instanceof DotNetGenericParameterListOwner parameterListOwner) { + wrapToLink(parameterListOwner, builder, qualified); + + DotNetGenericParameter[] genericParameters = parameterListOwner.getGenericParameters(); + if (genericParameters.length > 0) { + DotNetGenericExtractor genericExtractor = dotNetTypeResolveResult.getGenericExtractor(); + XmlStringUtil.escapeText("<", builder); + for (int i = 0; i < genericParameters.length; i++) { + if (i != 0) { + builder.append(", "); + } + DotNetGenericParameter parameter = genericParameters[i]; + + DotNetTypeRef extractedTypeRef = genericExtractor.extract(parameter); + if (extractedTypeRef != null) { + builder.append(generateLinksForType(extractedTypeRef, element, qualified)); + } + else { + builder.append(parameter.getName()); + } + } + XmlStringUtil.escapeText(">", builder); + } + } + else { + builder.append(qualifiedElem.getName()); + } + } + else { + builder.append(CSharpTypeRefPresentationUtil.buildShortText(typeRef)); + } + } + + return builder.toString(); + } + + @RequiredReadAction + private static void wrapToLink(@Nonnull PsiElement resolved, StringBuilder builder, boolean qualified) { + String parentQName = + qualified ? resolved instanceof DotNetQualifiedElement ? ((DotNetQualifiedElement) resolved).getPresentableParentQName() : null : null; + + if (!StringUtil.isEmpty(parentQName)) { + builder.append(parentQName).append('.'); + } + + builder.append("").append(((PsiNamedElement) resolved).getName()).append(""); + } + + @Nonnull + @Override + public Language getLanguage() { + return CSharpLanguage.INSTANCE; + } } diff --git a/csharp-impl/src/main/java/consulo/csharp/impl/ide/highlight/check/impl/CC0001.java b/csharp-impl/src/main/java/consulo/csharp/impl/ide/highlight/check/impl/CC0001.java index 280f5fe54..10fa63ddc 100644 --- a/csharp-impl/src/main/java/consulo/csharp/impl/ide/highlight/check/impl/CC0001.java +++ b/csharp-impl/src/main/java/consulo/csharp/impl/ide/highlight/check/impl/CC0001.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package consulo.csharp.impl.ide.highlight.check.impl; import consulo.annotation.access.RequiredReadAction; @@ -37,7 +36,6 @@ import consulo.csharp.module.extension.CSharpLanguageVersion; import consulo.dotnet.psi.*; import consulo.dotnet.psi.resolve.DotNetTypeRef; -import consulo.dotnet.psi.resolve.DotNetTypeResolveResult; import consulo.language.editor.intention.QuickFixAction; import consulo.language.editor.intention.QuickFixActionRegistrar; import consulo.language.editor.intention.UnresolvedReferenceQuickFixProvider; @@ -49,10 +47,10 @@ import consulo.ui.ex.awt.util.ColorUtil; import consulo.util.lang.StringUtil; import consulo.util.lang.xml.XmlStringUtil; -import org.jetbrains.annotations.Contract; - import jakarta.annotation.Nonnull; import jakarta.annotation.Nullable; +import org.jetbrains.annotations.Contract; + import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -60,402 +58,338 @@ /** * @author VISTALL - * @since 19.11.14 + * @since 2014-11-19 */ -public class CC0001 extends CompilerCheck -{ - @RequiredReadAction - @Nonnull - @Override - public List check(@Nonnull CSharpLanguageVersion languageVersion, @Nonnull CSharpHighlightContext highlightContext, @Nonnull CSharpReferenceExpression expression) - { - PsiElement referenceElement = expression.getReferenceElement(); - if(referenceElement == null || expression.isSoft() || CSharpDocUtil.isInsideDoc(expression)) - { - return Collections.emptyList(); - } - - // if parent is call skip - PsiElement parent = expression.getParent(); - if(parent instanceof CSharpMethodCallExpressionImpl || parent instanceof CSharpConstructorSuperCallImpl) - { - return Collections.emptyList(); - } - - return checkReference(expression, Arrays.asList(referenceElement)); - } - - @Nonnull - @RequiredReadAction - public static List checkReference(@Nonnull final PsiElement callElement, @Nonnull List ranges) - { - if(ranges.isEmpty()) - { - return Collections.emptyList(); - } - ResolveResult[] resolveResults = ResolveResult.EMPTY_ARRAY; - - boolean insideDoc = CSharpDocUtil.isInsideDoc(callElement); - - if(callElement instanceof PsiPolyVariantReference) - { - resolveResults = ((PsiPolyVariantReference) callElement).multiResolve(true); - } - else if(callElement instanceof CSharpCallArgumentListOwner) - { - resolveResults = ((CSharpCallArgumentListOwner) callElement).multiResolve(true); - } - - ResolveResult goodResult = CSharpResolveUtil.findFirstValidResult(resolveResults); - - List list = new ArrayList<>(2); - if(goodResult == null) - { - if(callElement instanceof CSharpReferenceExpression && ((CSharpReferenceExpression) callElement).isPlaceholderReference()) - { - if(callElement.getParent() instanceof CSharpOutRefWrapExpressionImpl) - { - return Collections.emptyList(); - } +public class CC0001 extends CompilerCheck { + @RequiredReadAction + @Nonnull + @Override + public List check( + @Nonnull CSharpLanguageVersion languageVersion, + @Nonnull CSharpHighlightContext highlightContext, + @Nonnull CSharpReferenceExpression expression + ) { + PsiElement referenceElement = expression.getReferenceElement(); + if (referenceElement == null || expression.isSoft() || CSharpDocUtil.isInsideDoc(expression)) { + return Collections.emptyList(); + } + + // if parent is call skip + PsiElement parent = expression.getParent(); + if (parent instanceof CSharpMethodCallExpressionImpl || parent instanceof CSharpConstructorSuperCallImpl) { + return Collections.emptyList(); + } + + return checkReference(expression, Arrays.asList(referenceElement)); + } + + @Nonnull + @RequiredReadAction + public static List checkReference( + @Nonnull final PsiElement callElement, + @Nonnull List ranges + ) { + if (ranges.isEmpty()) { + return Collections.emptyList(); + } + ResolveResult[] resolveResults = ResolveResult.EMPTY_ARRAY; + + boolean insideDoc = CSharpDocUtil.isInsideDoc(callElement); + + if (callElement instanceof PsiPolyVariantReference polyVariantRef) { + resolveResults = polyVariantRef.multiResolve(true); + } + else if (callElement instanceof CSharpCallArgumentListOwner argListOwner) { + resolveResults = argListOwner.multiResolve(true); + } + + ResolveResult goodResult = CSharpResolveUtil.findFirstValidResult(resolveResults); + + List list = new ArrayList<>(2); + if (goodResult == null) { + if (callElement instanceof CSharpReferenceExpression refExpr + && refExpr.isPlaceholderReference() + && callElement.getParent() instanceof CSharpOutRefWrapExpressionImpl) { + return Collections.emptyList(); } - if(resolveResults.length == 0) - { - for(PsiElement range : ranges) - { - CompilerCheckBuilder result = new CompilerCheckBuilder() - { + if (resolveResults.length == 0) { + for (PsiElement range : ranges) { + CompilerCheckBuilder result = new CompilerCheckBuilder() { @Nullable - @Override - public HighlightInfo create(boolean insideDoc) - { - HighlightInfo highlightInfo = super.create(insideDoc); - if(highlightInfo != null) - { - PsiReference reference = null; - if(callElement instanceof PsiReference) - { - reference = (PsiReference) callElement; - } - else if(callElement instanceof CSharpMethodCallExpressionImpl) - { - DotNetExpression callExpression = ((CSharpMethodCallExpressionImpl) callElement).getCallExpression(); - if(callExpression instanceof PsiReference) - { - reference = (PsiReference) callExpression; - } + @Override + @RequiredReadAction + public HighlightInfo create(boolean insideDoc) { + HighlightInfo highlightInfo = super.create(insideDoc); + if (highlightInfo != null) { + PsiReference reference = null; + if (callElement instanceof PsiReference ref) { + reference = ref; + } + else if (callElement instanceof CSharpMethodCallExpressionImpl call + && call.getCallExpression() instanceof PsiReference ref) { + reference = ref; } - if(reference != null) - { - UnresolvedReferenceQuickFixProvider.registerReferenceFixes(reference, QuickFixActionRegistrar.create(highlightInfo)); - } - } - return highlightInfo; - } - }; - result.withHighlightInfoType(insideDoc ? HighlightInfoType.WEAK_WARNING : HighlightInfoType.WRONG_REF); - - String unresolvedText = getUnresolvedText(callElement, range); - if(isCalleInsideCalle(callElement)) - { - result.withText(LocalizeValue.localizeTODO("Expression cant be invoked")); - // remap to error, due we want make all exp red - result.withHighlightInfoType(insideDoc ? HighlightInfoType.WEAK_WARNING : HighlightInfoType.ERROR); - } - else - { - result.withText(LocalizeValue.localizeTODO("'" + StringUtil.unescapeXml(unresolvedText) + "' is not resolved")); - } - - result.withTextRange(range.getTextRange()); - list.add(result); - } - } - else - { - final HighlightInfo highlightInfo = createHighlightInfo(callElement, resolveResults[0], insideDoc); - if(highlightInfo == null) - { - return list; - } - - list.add((it) -> highlightInfo); - } - } - return list; - } - - @Contract("null -> false") - @RequiredReadAction - public static boolean isCalleInsideCalle(@Nullable PsiElement callElement) - { - if(callElement instanceof CSharpMethodCallExpressionImpl) - { - return !(((CSharpMethodCallExpressionImpl) callElement).getCallExpression() instanceof CSharpReferenceExpression); - } - else - { - return false; - } - } - - @RequiredReadAction - private static String getUnresolvedText(@Nonnull PsiElement element, @Nonnull PsiElement range) - { - CSharpCallArgumentListOwner callOwner = findCallOwner(element); - if(callOwner != null) - { - String name; - if(element instanceof CSharpIndexAccessExpressionImpl) - { - name = "this"; - } - else - { - name = range.getText(); - } - - StringBuilder builder = new StringBuilder(); - builder.append(name); - char[] openAndCloseTokens = CSharpParametersInfo.getOpenAndCloseTokens(element); - builder.append(openAndCloseTokens[0]); - CSharpCallArgument[] arguments = callOwner.getCallArguments(); - for(int i = 0; i < arguments.length; i++) - { - if(i != 0) - { - builder.append(", "); - } - - CSharpCallArgument callArgument = arguments[i]; - - DotNetExpression argumentExpression = callArgument.getArgumentExpression(); - appendType(builder, argumentExpression == null ? DotNetTypeRef.ERROR_TYPE : argumentExpression.toTypeRef(false), element); - } - builder.append(openAndCloseTokens[1]); - return builder.toString(); - } - else - { - return range.getText(); - } - } - - @Nullable - @RequiredReadAction - public static HighlightInfo createHighlightInfo(@Nonnull PsiElement element, @Nonnull ResolveResult resolveResult, boolean insideDoc) - { - if(!(resolveResult instanceof MethodResolveResult)) - { - return null; - } - - char[] openAndCloseTokens = CSharpParametersInfo.getOpenAndCloseTokens(element); - - PsiElement resolveElement = resolveResult.getElement(); - assert resolveElement != null; - - MethodResolvePriorityInfo calcResult = ((MethodResolveResult) resolveResult).getCalcResult(); - List arguments = calcResult.getArguments(); - for(NCallArgument argument : arguments) - { - // missed arguments returning error type too - but we dont need to hide call error - if(argument instanceof NErrorCallArgument) - { - continue; - } - - if(CSharpTypeUtil.isErrorTypeRef(argument.getTypeRef())) - { - return null; - } - } - - CSharpCallArgumentListOwner callOwner = findCallOwner(element); - if(callOwner != null) - { - StringBuilder tooltipBuilder = new StringBuilder(); - tooltipBuilder.append(""); - // sometimes name can be null - if(element instanceof CSharpOperatorReferenceImpl) - { - String canonicalText = ((CSharpOperatorReferenceImpl) element).getCanonicalText(); - tooltipBuilder.append(XmlStringUtil.escapeString(canonicalText)); - } - else if(element instanceof CSharpIndexAccessExpressionImpl) - { - tooltipBuilder.append(""); //FIXME [VISTALL] some name? - } - else - { - String name = ((PsiNamedElement) resolveElement).getName(); - tooltipBuilder.append(name); - } - - tooltipBuilder.append(" ").append(openAndCloseTokens[0]); - - if(resolveElement instanceof DotNetVariable) - { - DotNetTypeRef typeRef = ((DotNetVariable) resolveElement).toTypeRef(false); - DotNetTypeResolveResult typeResolveResult = typeRef.resolve(); - if(!(typeResolveResult instanceof CSharpLambdaResolveResult)) - { - return null; - } - DotNetTypeRef[] parameterTypes = ((CSharpLambdaResolveResult) typeResolveResult).getParameterTypeRefs(); - for(int i = 0; i < parameterTypes.length; i++) - { - if(i != 0) - { - tooltipBuilder.append(", "); - } - appendType(tooltipBuilder, parameterTypes[i], element); - } - } - else if(resolveElement instanceof DotNetLikeMethodDeclaration) - { - DotNetParameter[] parameters = ((DotNetLikeMethodDeclaration) resolveElement).getParameters(); - for(int i = 0; i < parameters.length; i++) - { - if(i != 0) - { - tooltipBuilder.append(", "); - } - tooltipBuilder.append(parameters[i].getName()).append(" : "); - appendType(tooltipBuilder, parameters[i].toTypeRef(false), element); - } - } - tooltipBuilder.append(openAndCloseTokens[1]).append(" cannot be applied
"); - - tooltipBuilder.append("to ").append(openAndCloseTokens[0]); - - for(int i = 0; i < arguments.size(); i++) - { - if(i != 0) - { - tooltipBuilder.append(", "); - } - - NCallArgument nCallArgument = arguments.get(i); - - if(!nCallArgument.isValid()) - { - tooltipBuilder.append(""); - } - - String parameterName = nCallArgument.getParameterName(); - if(parameterName != null) - { - tooltipBuilder.append(parameterName).append(" : "); - } - - appendType(tooltipBuilder, nCallArgument.getTypeRef(), element); - if(!nCallArgument.isValid()) - { - tooltipBuilder.append(""); - } - } - - tooltipBuilder.append(openAndCloseTokens[1]).append(""); - - PsiElement parameterList = callOwner.getParameterList(); - if(parameterList == null) - { - parameterList = callOwner; - } - - HighlightInfo.Builder builder = HighlightInfo.newHighlightInfo(insideDoc ? HighlightInfoType.WEAK_WARNING :HighlightInfoType.ERROR); - builder = builder.description(""); - builder = builder.escapedToolTip(tooltipBuilder.toString()); - builder = builder.range(parameterList); - - HighlightInfo highlightInfo = builder.create(); - if(highlightInfo != null) - { - registerQuickFixes(callOwner, resolveElement, arguments, highlightInfo); - } - return highlightInfo; - } - return null; - } - - private static void registerQuickFixes(@Nonnull CSharpCallArgumentListOwner element, PsiElement resolveElement, List arguments, HighlightInfo highlightInfo) - { - DotNetExpression callExpression = element instanceof CSharpMethodCallExpressionImpl ? ((CSharpMethodCallExpressionImpl) element).getCallExpression() : null; - if(callExpression instanceof CSharpReferenceExpression) - { - if(resolveElement instanceof CSharpMethodDeclaration) - { - QuickFixAction.registerQuickFixAction(highlightInfo, new CreateUnresolvedMethodFix((CSharpReferenceExpression) callExpression)); - } - - if(resolveElement instanceof CSharpConstructorDeclaration) - { - QuickFixAction.registerQuickFixAction(highlightInfo, new CreateUnresolvedConstructorFix((CSharpReferenceExpression) callExpression)); - } - } - - for(NCallArgument argument : arguments) - { - if(!argument.isValid()) - { - CSharpCallArgument callArgument = argument.getCallArgument(); - if(callArgument == null) - { - continue; - } - DotNetExpression argumentExpression = callArgument.getArgumentExpression(); - if(argumentExpression == null) - { - continue; - } - DotNetTypeRef parameterTypeRef = argument.getParameterTypeRef(); - if(parameterTypeRef == null) - { - continue; - } - String parameterName = argument.getParameterName(); - if(parameterName == null) - { - continue; - } - QuickFixAction.registerQuickFixAction(highlightInfo, new CastNArgumentToTypeRefFix(argumentExpression, parameterTypeRef, parameterName)); - } - } - } - - @RequiredReadAction - private static void appendType(@Nonnull StringBuilder builder, @Nonnull DotNetTypeRef typeRef, @Nonnull PsiElement scope) - { - if(typeRef == DotNetTypeRef.ERROR_TYPE) - { - builder.append("?"); - } - else - { - builder.append(XmlStringUtil.escapeString(CSharpTypeRefPresentationUtil.buildText(typeRef))); - } - } - - private static CSharpCallArgumentListOwner findCallOwner(PsiElement element) - { - PsiElement parent = element.getParent(); - if(element instanceof CSharpOperatorReferenceImpl || - element instanceof CSharpMethodCallExpressionImpl || - element instanceof CSharpConstructorSuperCallImpl || - element instanceof CSharpIndexAccessExpressionImpl) - { - return (CSharpCallArgumentListOwner) element; - } - else if(parent instanceof CSharpAttribute) - { - return (CSharpCallArgumentListOwner) parent; - } - else if(parent instanceof DotNetUserType && parent.getParent() instanceof CSharpNewExpression) - { - return (CSharpCallArgumentListOwner) parent.getParent(); - } - return null; - } + if (reference != null) { + UnresolvedReferenceQuickFixProvider.registerReferenceFixes( + reference, + QuickFixActionRegistrar.create(highlightInfo) + ); + } + } + return highlightInfo; + } + }; + result.withHighlightInfoType(insideDoc ? HighlightInfoType.WEAK_WARNING : HighlightInfoType.WRONG_REF); + + String unresolvedText = getUnresolvedText(callElement, range); + if (isCalleInsideCalle(callElement)) { + result.withText(LocalizeValue.localizeTODO("Expression cant be invoked")); + // remap to error, due we want make all exp red + result.withHighlightInfoType(insideDoc ? HighlightInfoType.WEAK_WARNING : HighlightInfoType.ERROR); + } + else { + result.withText(LocalizeValue.localizeTODO("'" + StringUtil.unescapeXml(unresolvedText) + "' is not resolved")); + } + + result.withTextRange(range.getTextRange()); + list.add(result); + } + } + else { + HighlightInfo highlightInfo = createHighlightInfo(callElement, resolveResults[0], insideDoc); + if (highlightInfo == null) { + return list; + } + + list.add((it) -> highlightInfo); + } + } + return list; + } + + @Contract("null -> false") + @RequiredReadAction + public static boolean isCalleInsideCalle(@Nullable PsiElement callElement) { + return callElement instanceof CSharpMethodCallExpressionImpl call + && !(call.getCallExpression() instanceof CSharpReferenceExpression); + } + + @RequiredReadAction + private static String getUnresolvedText(@Nonnull PsiElement element, @Nonnull PsiElement range) { + CSharpCallArgumentListOwner callOwner = findCallOwner(element); + if (callOwner != null) { + String name; + if (element instanceof CSharpIndexAccessExpressionImpl) { + name = "this"; + } + else { + name = range.getText(); + } + + StringBuilder builder = new StringBuilder(); + builder.append(name); + char[] openAndCloseTokens = CSharpParametersInfo.getOpenAndCloseTokens(element); + builder.append(openAndCloseTokens[0]); + CSharpCallArgument[] arguments = callOwner.getCallArguments(); + for (int i = 0; i < arguments.length; i++) { + if (i != 0) { + builder.append(", "); + } + + CSharpCallArgument callArgument = arguments[i]; + + DotNetExpression argumentExpression = callArgument.getArgumentExpression(); + appendType(builder, argumentExpression == null ? DotNetTypeRef.ERROR_TYPE : argumentExpression.toTypeRef(false), element); + } + builder.append(openAndCloseTokens[1]); + return builder.toString(); + } + else { + return range.getText(); + } + } + + @Nullable + @RequiredReadAction + public static HighlightInfo createHighlightInfo(@Nonnull PsiElement element, @Nonnull ResolveResult resolveResult, boolean insideDoc) { + if (!(resolveResult instanceof MethodResolveResult methodResolveResult)) { + return null; + } + + char[] openAndCloseTokens = CSharpParametersInfo.getOpenAndCloseTokens(element); + + PsiElement resolveElement = resolveResult.getElement(); + assert resolveElement != null; + + MethodResolvePriorityInfo calcResult = methodResolveResult.getCalcResult(); + List arguments = calcResult.getArguments(); + for (NCallArgument argument : arguments) { + // missed arguments returning error type too - but we don't need to hide call error + if (argument instanceof NErrorCallArgument) { + continue; + } + + if (CSharpTypeUtil.isErrorTypeRef(argument.getTypeRef())) { + return null; + } + } + + CSharpCallArgumentListOwner callOwner = findCallOwner(element); + if (callOwner != null) { + StringBuilder tooltipBuilder = new StringBuilder(); + tooltipBuilder.append(""); + // sometimes name can be null + if (element instanceof CSharpOperatorReferenceImpl operatorRef) { + XmlStringUtil.escapeText(operatorRef.getCanonicalText(), tooltipBuilder); + } + else if (element instanceof CSharpIndexAccessExpressionImpl) { + tooltipBuilder.append(""); //FIXME [VISTALL] some name? + } + else { + String name = ((PsiNamedElement) resolveElement).getName(); + tooltipBuilder.append(name); + } + + tooltipBuilder.append(" ").append(openAndCloseTokens[0]); + + if (resolveElement instanceof DotNetVariable variable) { + if (!(variable.toTypeRef(false).resolve() instanceof CSharpLambdaResolveResult lambdaResolveResult)) { + return null; + } + DotNetTypeRef[] parameterTypes = lambdaResolveResult.getParameterTypeRefs(); + for (int i = 0; i < parameterTypes.length; i++) { + if (i != 0) { + tooltipBuilder.append(", "); + } + appendType(tooltipBuilder, parameterTypes[i], element); + } + } + else if (resolveElement instanceof DotNetLikeMethodDeclaration likeMethodDeclaration) { + DotNetParameter[] parameters = likeMethodDeclaration.getParameters(); + for (int i = 0; i < parameters.length; i++) { + if (i != 0) { + tooltipBuilder.append(", "); + } + tooltipBuilder.append(parameters[i].getName()).append(" : "); + appendType(tooltipBuilder, parameters[i].toTypeRef(false), element); + } + } + tooltipBuilder.append(openAndCloseTokens[1]).append(" cannot be applied
"); + + tooltipBuilder.append("to ").append(openAndCloseTokens[0]); + + for (int i = 0; i < arguments.size(); i++) { + if (i != 0) { + tooltipBuilder.append(", "); + } + + NCallArgument nCallArgument = arguments.get(i); + + if (!nCallArgument.isValid()) { + tooltipBuilder.append(""); + } + + String parameterName = nCallArgument.getParameterName(); + if (parameterName != null) { + tooltipBuilder.append(parameterName).append(" : "); + } + + appendType(tooltipBuilder, nCallArgument.getTypeRef(), element); + if (!nCallArgument.isValid()) { + tooltipBuilder.append(""); + } + } + + tooltipBuilder.append(openAndCloseTokens[1]).append(""); + + PsiElement parameterList = callOwner.getParameterList(); + if (parameterList == null) { + parameterList = callOwner; + } + + HighlightInfo.Builder builder = + HighlightInfo.newHighlightInfo(insideDoc ? HighlightInfoType.WEAK_WARNING : HighlightInfoType.ERROR); + builder = builder.description(LocalizeValue.empty()); + builder = builder.escapedToolTip(tooltipBuilder.toString()); + builder = builder.range(parameterList); + + HighlightInfo highlightInfo = builder.create(); + if (highlightInfo != null) { + registerQuickFixes(callOwner, resolveElement, arguments, highlightInfo); + } + return highlightInfo; + } + return null; + } + + private static void registerQuickFixes( + @Nonnull CSharpCallArgumentListOwner element, + PsiElement resolveElement, + List arguments, + HighlightInfo highlightInfo + ) { + if (element instanceof CSharpMethodCallExpressionImpl call + && call.getCallExpression() instanceof CSharpReferenceExpression refExpr) { + if (resolveElement instanceof CSharpMethodDeclaration) { + QuickFixAction.registerQuickFixAction(highlightInfo, new CreateUnresolvedMethodFix(refExpr)); + } + else if (resolveElement instanceof CSharpConstructorDeclaration) { + QuickFixAction.registerQuickFixAction(highlightInfo, new CreateUnresolvedConstructorFix(refExpr)); + } + } + + for (NCallArgument argument : arguments) { + if (!argument.isValid()) { + CSharpCallArgument callArgument = argument.getCallArgument(); + if (callArgument == null) { + continue; + } + DotNetExpression argumentExpression = callArgument.getArgumentExpression(); + if (argumentExpression == null) { + continue; + } + DotNetTypeRef parameterTypeRef = argument.getParameterTypeRef(); + if (parameterTypeRef == null) { + continue; + } + String parameterName = argument.getParameterName(); + if (parameterName == null) { + continue; + } + QuickFixAction.registerQuickFixAction( + highlightInfo, + new CastNArgumentToTypeRefFix(argumentExpression, parameterTypeRef, parameterName) + ); + } + } + } + + @RequiredReadAction + private static void appendType(@Nonnull StringBuilder builder, @Nonnull DotNetTypeRef typeRef, @Nonnull PsiElement scope) { + if (typeRef == DotNetTypeRef.ERROR_TYPE) { + builder.append("?"); + } + else { + XmlStringUtil.escapeText(CSharpTypeRefPresentationUtil.buildText(typeRef), builder); + } + } + + private static CSharpCallArgumentListOwner findCallOwner(PsiElement element) { + PsiElement parent = element.getParent(); + if (element instanceof CSharpOperatorReferenceImpl || + element instanceof CSharpMethodCallExpressionImpl || + element instanceof CSharpConstructorSuperCallImpl || + element instanceof CSharpIndexAccessExpressionImpl) { + return (CSharpCallArgumentListOwner) element; + } + else if (parent instanceof CSharpAttribute attribute) { + return attribute; + } + else if (parent instanceof DotNetUserType && parent.getParent() instanceof CSharpNewExpression newExpr) { + return newExpr; + } + return null; + } } diff --git a/csharp-impl/src/main/java/consulo/csharp/impl/ide/parameterInfo/ParameterPresentationBuilder.java b/csharp-impl/src/main/java/consulo/csharp/impl/ide/parameterInfo/ParameterPresentationBuilder.java index 412baefb2..036de3cb4 100644 --- a/csharp-impl/src/main/java/consulo/csharp/impl/ide/parameterInfo/ParameterPresentationBuilder.java +++ b/csharp-impl/src/main/java/consulo/csharp/impl/ide/parameterInfo/ParameterPresentationBuilder.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package consulo.csharp.impl.ide.parameterInfo; import consulo.application.AccessToken; @@ -21,8 +20,8 @@ import consulo.document.util.UnfairTextRange; import consulo.util.lang.ObjectUtil; import consulo.util.lang.xml.XmlStringUtil; - import jakarta.annotation.Nonnull; + import java.util.HashMap; import java.util.Map; @@ -30,56 +29,46 @@ * @author VISTALL * @since 2020-05-31 */ -public class ParameterPresentationBuilder

-{ - private static final TextRange ourInvalidRange = new UnfairTextRange(-1, -1); +public class ParameterPresentationBuilder

{ + private static final TextRange ourInvalidRange = new UnfairTextRange(-1, -1); - private StringBuilder myBuilder = new StringBuilder(); + private StringBuilder myBuilder = new StringBuilder(); - private Map myRanges = new HashMap<>(); + private Map myRanges = new HashMap<>(); - public ParameterPresentationBuilder() - { - } + public ParameterPresentationBuilder() { + } - public AccessToken beginParameter(int index) - { - int start = myBuilder.length(); - return new AccessToken() - { - @Override - public void finish() - { - myRanges.put(index, new TextRange(start, myBuilder.length())); - } - }; - } + public AccessToken beginParameter(int index) { + int start = myBuilder.length(); + return new AccessToken() { + @Override + public void finish() { + myRanges.put(index, new TextRange(start, myBuilder.length())); + } + }; + } - public void add(String text) - { - myBuilder.append(text); - } + public void add(String text) { + myBuilder.append(text); + } - public void addEscaped(String text) - { - myBuilder.append(XmlStringUtil.escapeString(text)); - } + public void addEscaped(String text) { + XmlStringUtil.escapeText(text, myBuilder); + } - public void addSpace() - { - myBuilder.append(" "); - } + public void addSpace() { + myBuilder.append(" "); + } - @Nonnull - public TextRange getParameterRange(int index) - { - TextRange textRange = myRanges.get(index); - return ObjectUtil.notNull(textRange, ourInvalidRange); - } + @Nonnull + public TextRange getParameterRange(int index) { + TextRange textRange = myRanges.get(index); + return ObjectUtil.notNull(textRange, ourInvalidRange); + } - @Override - public String toString() - { - return myBuilder.toString(); - } + @Override + public String toString() { + return myBuilder.toString(); + } } diff --git a/csharp-psi-impl/src/main/java/consulo/csharp/lang/impl/CSharpExpressionTypeProvider.java b/csharp-psi-impl/src/main/java/consulo/csharp/lang/impl/CSharpExpressionTypeProvider.java index 31e9c8472..7a80b520a 100644 --- a/csharp-psi-impl/src/main/java/consulo/csharp/lang/impl/CSharpExpressionTypeProvider.java +++ b/csharp-psi-impl/src/main/java/consulo/csharp/lang/impl/CSharpExpressionTypeProvider.java @@ -13,21 +13,20 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package consulo.csharp.lang.impl; import consulo.annotation.access.RequiredReadAction; import consulo.annotation.component.ExtensionImpl; import consulo.csharp.lang.CSharpLanguage; -import consulo.csharp.lang.psi.CSharpReferenceExpression; import consulo.csharp.lang.impl.psi.CSharpTypeRefPresentationUtil; import consulo.csharp.lang.impl.psi.source.CSharpMethodCallExpressionImpl; +import consulo.csharp.lang.psi.CSharpReferenceExpression; import consulo.dotnet.psi.DotNetExpression; import consulo.language.Language; import consulo.language.editor.ExpressionTypeProvider; import consulo.language.psi.PsiElement; import consulo.language.psi.util.PsiTreeUtil; -import consulo.util.lang.StringUtil; +import consulo.util.lang.xml.XmlStringUtil; import jakarta.annotation.Nonnull; import java.util.List; @@ -37,44 +36,37 @@ * @since 05/05/2021 */ @ExtensionImpl -public class CSharpExpressionTypeProvider extends ExpressionTypeProvider -{ - @Nonnull - @Override - @RequiredReadAction - public String getInformationHint(@Nonnull DotNetExpression expression) - { - return StringUtil.escapeXmlEntities(CSharpTypeRefPresentationUtil.buildText(expression.toTypeRef(true))); - } +public class CSharpExpressionTypeProvider extends ExpressionTypeProvider { + @Nonnull + @Override + @RequiredReadAction + public String getInformationHint(@Nonnull DotNetExpression expression) { + return XmlStringUtil.escapeText(CSharpTypeRefPresentationUtil.buildText(expression.toTypeRef(true))); + } - @Nonnull - @Override - public String getErrorHint() - { - return "Expression not found"; - } + @Nonnull + @Override + public String getErrorHint() { + return "Expression not found"; + } - @Nonnull - @Override - public List getExpressionsAt(@Nonnull PsiElement psiElement) - { - DotNetExpression expression = PsiTreeUtil.getParentOfType(psiElement, DotNetExpression.class); - if(expression instanceof CSharpReferenceExpression && expression.getParent() instanceof CSharpMethodCallExpressionImpl call) - { - expression = call; - } + @Nonnull + @Override + public List getExpressionsAt(@Nonnull PsiElement psiElement) { + DotNetExpression expression = PsiTreeUtil.getParentOfType(psiElement, DotNetExpression.class); + if (expression instanceof CSharpReferenceExpression && expression.getParent() instanceof CSharpMethodCallExpressionImpl call) { + expression = call; + } - if(expression != null) - { - return List.of(expression); - } - return List.of(); - } + if (expression != null) { + return List.of(expression); + } + return List.of(); + } - @Nonnull - @Override - public Language getLanguage() - { - return CSharpLanguage.INSTANCE; - } + @Nonnull + @Override + public Language getLanguage() { + return CSharpLanguage.INSTANCE; + } }