private void AppendTypeElement([NotNull] ITypeElement typeElement, [NotNull] ISubstitution substitution, QualifierDisplays expectedQualifierDisplay, Context context) { if (!(typeElement is ITypeParameter) && (context.Options.ShowQualifiers & expectedQualifierDisplay) != QualifierDisplays.None) { INamespace containingNamespace = typeElement.GetContainingNamespace(); AppendNamespace(containingNamespace); if (!containingNamespace.IsRootNamespace) { AppendText(".", _highlighterIdProvider.Operator); } ITypeElement containingType = typeElement.GetContainingType(); if (containingType != null && !(typeElement is IDelegate && context.Options.FormatDelegatesAsLambdas)) { AppendDeclaredType(TypeFactory.CreateType(containingType, substitution), QualifierDisplays.None, context); AppendText(".", _highlighterIdProvider.Operator); } } var deleg = typeElement as IDelegate; if (deleg != null && context.Options.FormatDelegatesAsLambdas && expectedQualifierDisplay == QualifierDisplays.Parameters) { AppendParameters(deleg.InvokeMethod, substitution, false, context); AppendText(" => ", _highlighterIdProvider.Operator); AppendTypeWithoutModule(substitution.Apply(deleg.InvokeMethod.ReturnType), expectedQualifierDisplay, context); return; } string highlighterId = _highlighterIdProvider.GetForTypeElement(typeElement); AppendText(FormatShortName(typeElement.ShortName), highlighterId); AppendTypeParameters(typeElement, substitution, context); }
private static ICSharpFile GetLinkedTypeFile(string linkedTypeName, string linkedTypeNamespace, ITypeElement templateLinkedType) { var elementFactory = CSharpElementFactory.GetInstance(templateLinkedType.GetFirstDeclaration <IDeclaration>().NotNull()); var templateLinkedTypeSourceFile = templateLinkedType.GetSingleOrDefaultSourceFile().NotNull("templateLinkedTypeSourceFile != null"); var templateFile = templateLinkedTypeSourceFile.GetPrimaryPsiFile().NotNull("templateFile != null"); var fileText = templateFile.GetText() .Replace(templateLinkedType.GetContainingNamespace().QualifiedName, linkedTypeNamespace) .Replace(templateLinkedType.ShortName, linkedTypeName); var linkedTypeFile = elementFactory.CreateFile(fileText); var typeDeclarations = GetTypeDeclarations(linkedTypeFile); var linkedType = (IClassDeclaration)typeDeclarations.Single(x => x.DeclaredName == linkedTypeName); // Remove base types foreach (var x in linkedType.SuperTypes) { linkedType.RemoveSuperInterface(x); } // Clear body linkedType.SetBody(((IClassLikeDeclaration)elementFactory.CreateTypeMemberDeclaration("class C{}")).Body); // Remove unrelated types foreach (var declaration in linkedTypeFile.TypeDeclarations.Where(x => x.DeclaredName != linkedTypeName)) { ModificationUtil.DeleteChild(declaration); } return(linkedTypeFile); }
public INamespace GetNamespace <T>(T element, out bool includeSubnamespaces) { var match = element as IStructuralMatchResult; if (match == null) { includeSubnamespaces = false; return(null); } includeSubnamespaces = false; var argument = match.GetMatchedElement("subnamespace") as ICSharpArgument; if (argument != null) { if (argument.Value.ConstantValue != null && argument.Value.ConstantValue.IsBoolean()) { includeSubnamespaces = Convert.ToBoolean(argument.Value.ConstantValue.Value); } } var declaredType = match.GetMatchedType("type") as IDeclaredType; if (declaredType != null) { ITypeElement typeElement = declaredType.GetTypeElement(); if (typeElement != null) { return(typeElement.GetContainingNamespace()); } } return(null); }
private void AppendTypeElement([NotNull] ITypeElement typeElement, [NotNull] ISubstitution substitution, NamespaceDisplays expectedNamespaceDisplay) { if (!(typeElement is ITypeParameter)) { if ((_options.ShowNamespaces & expectedNamespaceDisplay) != NamespaceDisplays.None) { AppendNamespace(typeElement.GetContainingNamespace()); AppendText(".", VsHighlightingAttributeIds.Operator); } ITypeElement containingType = typeElement.GetContainingType(); if (containingType != null && !(typeElement is IDelegate && _options.FormatDelegatesAsLambdas)) { AppendDeclaredType(TypeFactory.CreateType(containingType, substitution), NamespaceDisplays.None); AppendText(".", VsHighlightingAttributeIds.Operator); } } var deleg = typeElement as IDelegate; if (deleg != null && _options.FormatDelegatesAsLambdas && expectedNamespaceDisplay == NamespaceDisplays.Parameters) { AppendParameters(deleg.InvokeMethod, substitution, false); AppendText(" => ", VsHighlightingAttributeIds.Operator); AppendType(substitution.Apply(deleg.InvokeMethod.ReturnType), expectedNamespaceDisplay); return; } string attributeId = _options.UseReSharperColors ? HighlightingAttributeIds.GetHighlightAttributeForTypeElement(typeElement) : VsHighlightingAttributeIds.GetForTypeElement(typeElement); AppendText(FormatShortName(typeElement.ShortName), attributeId); AppendTypeParameters(typeElement, substitution); }
public static string GetDerivedNamespace(ITypeElement sourceType, ITypeElement templateLinkedType) { var sourceDefaultNamespace = GetDefaultNamespace(sourceType.GetSingleOrDefaultSourceFile().GetProject().NotNull()); var linkedDefaultNamespace = GetDefaultNamespace(templateLinkedType.GetSingleOrDefaultSourceFile().GetProject().NotNull()); var sourceNamespaceTail = sourceType.GetContainingNamespace().QualifiedName.TrimFromStart(sourceDefaultNamespace); return(linkedDefaultNamespace + sourceNamespaceTail); }
public static string OwnerNamespaceDeclaration(this ITypeElement element) { INamespace containingNamespace = element.GetContainingNamespace(); var list = new List <string>(); for (; !containingNamespace.IsRootNamespace; containingNamespace = containingNamespace.GetContainingNamespace()) { list.Add(containingNamespace.ShortName); } list.Reverse(); return(string.Join(".", list.ToArray())); }
public override bool IsSatisfiedBy(ITypeElement typeElement) { var elementNamespace = typeElement.GetContainingNamespace(); bool isMatch; if (includeSubnamespaces) { isMatch = elementNamespace.QualifiedName == matchedNamespace.QualifiedName || elementNamespace.QualifiedName.StartsWith(matchedNamespace.QualifiedName + ".", StringComparison.OrdinalIgnoreCase); } else { isMatch = elementNamespace.QualifiedName == matchedNamespace.QualifiedName; } return isMatch && base.IsSatisfiedBy(typeElement); }
protected override INamespace GetNamespaceElement(IStructuralMatchResult match, out bool includeSubnamespaces) { includeSubnamespaces = true; var declaredType = match.GetMatchedType("type") as IDeclaredType; if (declaredType != null) { ITypeElement typeElement = declaredType.GetTypeElement(); if (typeElement != null) { return(typeElement.GetContainingNamespace()); } } return(null); }
public override bool IsSatisfiedBy(ITypeElement typeElement) { var elementNamespace = typeElement.GetContainingNamespace(); bool isMatch; if (includeSubnamespaces) { isMatch = elementNamespace.QualifiedName == matchedNamespace.QualifiedName || elementNamespace.QualifiedName.StartsWith(matchedNamespace.QualifiedName + ".", StringComparison.OrdinalIgnoreCase); } else { isMatch = elementNamespace.QualifiedName == matchedNamespace.QualifiedName; } return(isMatch && base.IsSatisfiedBy(typeElement)); }
private INamespace GetNamespaceDeclaration(ICSharpExpression expression) { var typeofExpression = expression as ITypeofExpression; if (typeofExpression != null) { var declaredType = typeofExpression.ArgumentType as IDeclaredType; if (declaredType != null) { ITypeElement typeElement = declaredType.GetTypeElement(); if (typeElement != null) { return(typeElement.GetContainingNamespace()); } } } return(null); }
protected override IEnumerable <FilteredRegistrationBase> DoCreateRegistrations(ITreeNode registrationRootElement, IStructuralMatchResult match) { var declaredType = match.GetMatchedType("type") as IDeclaredType; if (declaredType != null) { ITypeElement typeElement = declaredType.GetTypeElement(); if (typeElement != null) { yield return(new InNamespaceRegistration(registrationRootElement, typeElement.GetContainingNamespace(), true)); } } }
private static TypeAndNamespace GetAccessableTypeElementAndNamespace(ICSharpTypeMemberDeclaration declaration, ISolution solution, ICSharpFile file, IClrDeclaredElement element, IdentifierLookupScopes scope) { //IPsiModule module = element.Module; IXmlDocIdOwner idOwner = element as IXmlDocIdOwner; string docId = idOwner == null ? element.ShortName : idOwner.XMLDocId; // Get the defining type. ITypeElement typeElement = element as ITypeElement ?? element.GetContainingType(); if (typeElement == null) return null; // Create the result TypeAndNamespace result = new TypeAndNamespace { XmlDocId = docId, TypeElement = element }; // Get the namespace it belongs to. INamespace namespaceElement = typeElement.GetContainingNamespace(); string namespaceName = namespaceElement.QualifiedName; // Check if we're ignoring this namespace foreach (string namespacePrefix in NamespacePrefixesToIgnore) { if (namespaceName.StartsWith(namespacePrefix)) return null; } // Check if it would be possible to access the type AccessRights elementAccessRights = GetAccessRights(element); if (elementAccessRights == AccessRights.PRIVATE) { return null; } // Check if the type is defined in this solution IList<IDeclaration> declarations = element.GetDeclarations(); if (declarations.Count == 0) { // Assembly is an import so no internal things allowed if (elementAccessRights == AccessRights.INTERNAL) return null; } // Check if the given namespace is already imported in this file. if (UsingUtil.CheckAlreadyImported(file, new DeclaredElementInstance(namespaceElement)) || declaration.GetContainingNamespaceDeclaration().QualifiedName.StartsWith(namespaceName)) { string newDocId = docId[1] == ':' ? docId.Substring(2) : docId; if (newDocId.StartsWith(namespaceName + ".")) newDocId = newDocId.Substring(namespaceName.Length + 1); result.XmlDocId = newDocId; return result; } // If we require it to be in project or using scope then this is not a match if (scope == IdentifierLookupScopes.ProjectAndUsings || scope == IdentifierLookupScopes.ProjectOnly) { return null; } // No - so add in the namespace. result.RequiredNamespaceImport = namespaceElement; return result; }
public bool ReferenceModuleWithType(IPsiModule module, ITypeElement typeToReference) { return ReferenceModuleImpl(module, typeToReference.Module, typeToReference.GetContainingNamespace().QualifiedName); }
public bool ReferenceModuleWithType(IPsiModule module, ITypeElement typeToReference) { return(ReferenceModuleImpl(module, typeToReference.Module, typeToReference.GetContainingNamespace().QualifiedName)); }