示例#1
0
        public static SymbolDisplayPart MassageErrorTypeNames(
            this SymbolDisplayPart part,
            string?replacement = null
            )
        {
            if (part.Kind == SymbolDisplayPartKind.ErrorTypeName)
            {
                var text = part.ToString();
                if (text == string.Empty)
                {
                    return(replacement == null
                      ? new SymbolDisplayPart(SymbolDisplayPartKind.Keyword, null, "object")
                      : new SymbolDisplayPart(SymbolDisplayPartKind.Text, null, replacement));
                }

                if (SyntaxFacts.GetKeywordKind(text) != SyntaxKind.None)
                {
                    return(new SymbolDisplayPart(
                               SymbolDisplayPartKind.ErrorTypeName,
                               null,
                               string.Format("@{0}", text)
                               ));
                }
            }

            return(part);
        }
        protected override void WriteParameterName(ISymbol symbol, SymbolDisplayPart part)
        {
            if (DocumentationDisplayMode == DocumentationDisplayMode.ToolTip)
            {
                XElement element = DocumentationProvider?
                                   .GetXmlDocumentation(symbol)?
                                   .Element(WellKnownXmlTags.Param, "name", part.ToString());

                if (element != null)
                {
                    WriteStartElement("span");
                    WriteStartAttribute("title");
                    WriteDocumentationCommentToolTip(element);
                    WriteEndAttribute();
                }

                base.WriteParameterName(symbol, part);

                if (element != null)
                {
                    WriteEndElement();
                }
            }
            else
            {
                base.WriteParameterName(symbol, part);
            }
        }
示例#3
0
        private static JObject Categorize(SymbolDisplayPart part)
        {
            var kind = part.Kind.ToString();

            kind = capital.Replace(kind, "$1-$2").ToLowerInvariant();

            return(new JObject(
                       new JProperty("kind", new JValue(kind)),
                       new JProperty("val", new JValue(part.ToString()))
                       ));
        }
示例#4
0
 public static void WriteSymbolDisplayPart(this IFastJsonWriter writer, SymbolDisplayPart part, bool selected)
 {
     writer.WriteStartObject();
     writer.WriteProperty("text", part.ToString());
     writer.WriteProperty("kind", FastConvert.EnumToLowerInvariantString(part.Kind));
     if (selected)
     {
         writer.WriteProperty("selected", true);
     }
     writer.WriteEndObject();
 }
示例#5
0
        private DisplayPartContent GetPartContent(SymbolDisplayPart item)
        {
            if (item.Kind == SymbolDisplayPartKind.NamespaceName || item.Kind == SymbolDisplayPartKind.Punctuation || item.Kind == SymbolDisplayPartKind.PropertyName)
            {
                return(new DisplayPartContent(item.ToString(), Brushes.Black));
            }
            else if (item.Kind == SymbolDisplayPartKind.ClassName || item.Kind == SymbolDisplayPartKind.InterfaceName || item.Kind == SymbolDisplayPartKind.EnumName || item.Kind == SymbolDisplayPartKind.DelegateName || item.Kind == SymbolDisplayPartKind.TypeParameterName)
            {
                return(new DisplayPartContent(item.ToString(), new SolidColorBrush(Color.FromRgb(43, 145, 175))));
            }
            else if (item.Kind == SymbolDisplayPartKind.MethodName || item.Kind == SymbolDisplayPartKind.ExtensionMethodName)
            {
                return(new DisplayPartContent(item.ToString(), new SolidColorBrush(Color.FromRgb(116, 83, 31))));
            }
            else if (item.Kind == SymbolDisplayPartKind.Keyword)
            {
                return(new DisplayPartContent(item.ToString(), Brushes.Blue));
            }
            else if (item.Kind == SymbolDisplayPartKind.ParameterName)
            {
                return(new DisplayPartContent(item.ToString(), Brushes.DarkSlateGray));
            }
            else if (item.Kind == SymbolDisplayPartKind.Space)
            {
                return(new DisplayPartContent(item.ToString(), Brushes.Transparent));
            }

            return(new DisplayPartContent());
        }
示例#6
0
        SymbolDisplayPart Mutator(SymbolDisplayPart part)
        {
            switch (part.Kind)
            {
            case SymbolDisplayPartKind.Punctuation when part.ToString() == ".":
                return(new SymbolDisplayPart(part.Kind, part.Symbol, "_"));

            case SymbolDisplayPartKind.InterfaceName when part.ToString().StartsWith(Prefix.ProtectedToPublicInterface):
                return(new SymbolDisplayPart(part.Kind, part.Symbol,
                                             part.ToString().Replace(Prefix.ProtectedToPublicInterface, "")));
            }

            return(part);
        }
示例#7
0
        public TokenApiView(SymbolDisplayPart part)
        {
            this.DisplayString = part.ToString();
            this.IsNavigable   = false;
            if (part.Symbol == null)
            {
                this.Id = "";
            }
            else
            {
                var typeParamIndex = part.Symbol.ToDisplayString().LastIndexOf("<");
                if (typeParamIndex > 0)
                {
                    this.Id = part.Symbol.ToDisplayString().Remove(typeParamIndex);
                }
                else
                {
                    this.Id = part.Symbol.ToDisplayString();
                }
            }

            switch (part.Kind)
            {
            case SymbolDisplayPartKind.ClassName:
            case SymbolDisplayPartKind.ErrorTypeName:
            case SymbolDisplayPartKind.InterfaceName:
            case SymbolDisplayPartKind.StructName:
                this.Type        = TypeReferenceApiView.TokenType.ClassType;
                this.IsNavigable = true;
                break;

            case SymbolDisplayPartKind.EnumName:
                this.Type        = TypeReferenceApiView.TokenType.EnumType;
                this.IsNavigable = true;
                break;

            case SymbolDisplayPartKind.Punctuation:
            case SymbolDisplayPartKind.Space:
                this.Type = TypeReferenceApiView.TokenType.Punctuation;
                break;

            case SymbolDisplayPartKind.Keyword:
                this.Type = TypeReferenceApiView.TokenType.BuiltInType;
                break;

            default:
                this.Type = TypeReferenceApiView.TokenType.TypeArgument;
                break;
            }
        }
示例#8
0
        public static string ToVisibleDisplayString(this SymbolDisplayPart part, bool includeLeftToRightMarker)
        {
            var text = part.ToString();

            if (includeLeftToRightMarker)
            {
                if (part.Kind == SymbolDisplayPartKind.Punctuation ||
                    part.Kind == SymbolDisplayPartKind.Space ||
                    part.Kind == SymbolDisplayPartKind.LineBreak)
                {
                    text = LeftToRightMarkerPrefix + text;
                }
            }

            return(text);
        }
        public static string ToVisibleDisplayString(this SymbolDisplayPart part, bool includeLeftToRightMarker)
        {
            var text = part.ToString();

            if (includeLeftToRightMarker)
            {
                var classificationTypeName = part.Kind.ToClassificationTypeName();
                if (classificationTypeName == ClassificationTypeNames.Punctuation ||
                    classificationTypeName == ClassificationTypeNames.WhiteSpace)
                {
                    text = LeftToRightMarkerPrefix + text;
                }
            }

            return(text);
        }
示例#10
0
        public static ImmutableArray <SymbolDisplayPart> RemoveAttributeSuffix(ImmutableArray <SymbolDisplayPart> parts)
        {
            SymbolDisplayPart part = parts.FirstOrDefault(f => f.Kind == SymbolDisplayPartKind.ClassName);

            Debug.Assert(part.Kind == SymbolDisplayPartKind.ClassName, part.Kind.ToString());

            if (part.Kind == SymbolDisplayPartKind.ClassName)
            {
                const string attributeSuffix = "Attribute";

                string text = part.ToString();

                if (text.EndsWith(attributeSuffix, StringComparison.Ordinal))
                {
                    parts = parts.Replace(part, part.WithText(text.Remove(text.Length - attributeSuffix.Length)));
                }
            }

            return(parts);
        }
示例#11
0
        private static Run ToRun(this SymbolDisplayPart text)
        {
            var run = new Run(text.ToString());

            switch (text.Kind)
            {
            case SymbolDisplayPartKind.Keyword:
                run.Foreground = Brushes.Blue;
                break;

            case SymbolDisplayPartKind.ClassName:
            case SymbolDisplayPartKind.DelegateName:
            case SymbolDisplayPartKind.EnumName:
            case SymbolDisplayPartKind.EventName:
            case SymbolDisplayPartKind.InterfaceName:
            case SymbolDisplayPartKind.StructName:
                run.Foreground = Brushes.Teal;
                break;
            }

            return(run);
        }
示例#12
0
        private void Initialize()
        {
            var content = new StringBuilder();
            var prettyPrintedContent = new StringBuilder();

            var parts = new List<SymbolDisplayPart>();
            var prettyPrintedParts = new List<SymbolDisplayPart>();

            var parameters = new List<IParameter>();

            var signaturePrefixParts = _signatureHelpItem.PrefixDisplayParts;
            var signaturePrefixContent = _signatureHelpItem.PrefixDisplayParts.GetFullText();

            AddRange(signaturePrefixParts, parts, prettyPrintedParts);
            Append(signaturePrefixContent, content, prettyPrintedContent);

            var separatorParts = _signatureHelpItem.SeparatorDisplayParts;
            var separatorContent = separatorParts.GetFullText();

            var newLinePart = new SymbolDisplayPart(SymbolDisplayPartKind.LineBreak, null, "\r\n");
            var newLineContent = newLinePart.ToString();
            var spacerPart = new SymbolDisplayPart(SymbolDisplayPartKind.Space, null, new string(' ', signaturePrefixContent.Length));
            var spacerContent = spacerPart.ToString();

            var paramColumnCount = 0;

            for (int i = 0; i < _signatureHelpItem.Parameters.Length; i++)
            {
                var sigHelpParameter = _signatureHelpItem.Parameters[i];

                var parameterPrefixParts = sigHelpParameter.PrefixDisplayParts;
                var parameterPrefixContext = sigHelpParameter.PrefixDisplayParts.GetFullText();

                var parameterParts = AddOptionalBrackets(sigHelpParameter.IsOptional, sigHelpParameter.DisplayParts);
                var parameterContent = parameterParts.GetFullText();

                var parameterSuffixParts = sigHelpParameter.SuffixDisplayParts;
                var parameterSuffixContext = sigHelpParameter.SuffixDisplayParts.GetFullText();

                paramColumnCount += separatorContent.Length + parameterPrefixContext.Length + parameterContent.Length + parameterSuffixContext.Length;

                if (i > 0)
                {
                    AddRange(separatorParts, parts, prettyPrintedParts);
                    Append(separatorContent, content, prettyPrintedContent);

                    if (paramColumnCount > MaxParamColumnCount)
                    {
                        prettyPrintedParts.Add(newLinePart);
                        prettyPrintedParts.Add(spacerPart);
                        prettyPrintedContent.Append(newLineContent);
                        prettyPrintedContent.Append(spacerContent);

                        paramColumnCount = 0;
                    }
                }

                AddRange(parameterPrefixParts, parts, prettyPrintedParts);
                Append(parameterPrefixContext, content, prettyPrintedContent);

                parameters.Add(new Parameter(this, sigHelpParameter, parameterContent, content.Length, prettyPrintedContent.Length));

                AddRange(parameterParts, parts, prettyPrintedParts);
                Append(parameterContent, content, prettyPrintedContent);

                AddRange(parameterSuffixParts, parts, prettyPrintedParts);
                Append(parameterSuffixContext, content, prettyPrintedContent);
            }

            AddRange(_signatureHelpItem.SuffixDisplayParts, parts, prettyPrintedParts);
            Append(_signatureHelpItem.SuffixDisplayParts.GetFullText(), content, prettyPrintedContent);

            if (_parameterIndex >= 0)
            {
                var sigHelpParameter = _signatureHelpItem.Parameters[_parameterIndex];

                AddRange(sigHelpParameter.SelectedDisplayParts, parts, prettyPrintedParts);
                Append(sigHelpParameter.SelectedDisplayParts.GetFullText(), content, prettyPrintedContent);
            }

            AddRange(_signatureHelpItem.DescriptionParts, parts, prettyPrintedParts);
            Append(_signatureHelpItem.DescriptionParts.GetFullText(), content, prettyPrintedContent);

            var documentation = _signatureHelpItem.DocumentationFactory(CancellationToken.None).ToList();
            if (documentation.Count > 0)
            {
                AddRange(new[] { newLinePart }, parts, prettyPrintedParts);
                Append(newLineContent, content, prettyPrintedContent);

                AddRange(documentation, parts, prettyPrintedParts);
                Append(documentation.GetFullText(), content, prettyPrintedContent);
            }

            _content = content.ToString();
            _prettyPrintedContent = prettyPrintedContent.ToString();
            _displayParts = parts.ToImmutableArrayOrEmpty();
            _prettyPrintedDisplayParts = prettyPrintedParts.ToImmutableArrayOrEmpty();
            _parameters = parameters.ToReadOnlyCollection();
        }
示例#13
0
        private static int FindParameterListStart(
            ISymbol symbol,
            IList <SymbolDisplayPart> parts)
        {
            int parenthesesDepth   = 0;
            int bracesDepth        = 0;
            int bracketsDepth      = 0;
            int angleBracketsDepth = 0;

            for (int i = 0; i < parts.Count; i++)
            {
                SymbolDisplayPart part = parts[i];

                if (part.Kind == SymbolDisplayPartKind.Punctuation)
                {
                    switch (part.ToString())
                    {
                    case "(":
                    {
                        parenthesesDepth++;

                        if (symbol.IsKind(SymbolKind.Method, SymbolKind.NamedType) &&
                            parenthesesDepth == 1 &&
                            bracesDepth == 0 &&
                            bracketsDepth == 0 &&
                            angleBracketsDepth == 0)
                        {
                            return(i);
                        }

                        break;
                    }

                    case ")":
                    {
                        Debug.Assert(parenthesesDepth >= 0);
                        parenthesesDepth--;
                        break;
                    }

                    case "[":
                    {
                        bracketsDepth++;

                        if (symbol.Kind == SymbolKind.Property &&
                            parenthesesDepth == 0 &&
                            bracesDepth == 0 &&
                            bracketsDepth == 1 &&
                            angleBracketsDepth == 0)
                        {
                            return(i);
                        }

                        break;
                    }

                    case "]":
                    {
                        Debug.Assert(bracketsDepth >= 0);
                        bracketsDepth--;
                        break;
                    }

                    case "{":
                    {
                        bracesDepth++;
                        break;
                    }

                    case "}":
                    {
                        Debug.Assert(bracesDepth >= 0);
                        bracesDepth--;
                        break;
                    }

                    case "<":
                    {
                        angleBracketsDepth++;
                        break;
                    }

                    case ">":
                    {
                        Debug.Assert(angleBracketsDepth >= 0);
                        angleBracketsDepth--;
                        break;
                    }
                    }
                }
            }

            return(-1);
        }
示例#14
0
        private static void FormatParameters(
            ISymbol symbol,
            ImmutableArray <SymbolDisplayPart> .Builder parts,
            string indentChars)
        {
            int parenthesesDepth   = 0;
            int bracesDepth        = 0;
            int bracketsDepth      = 0;
            int angleBracketsDepth = 0;

            int i = 0;

            int index = FindParameterListStart(symbol, parts);

            Debug.Assert(index != -1);

            if (index == -1)
            {
                return;
            }

            parts.Insert(index + 1, SymbolDisplayPartFactory.Indentation(indentChars));
            parts.Insert(index + 1, SymbolDisplayPartFactory.LineBreak());

            i++;

            while (i < parts.Count)
            {
                SymbolDisplayPart part = parts[i];

                if (part.Kind == SymbolDisplayPartKind.Punctuation)
                {
                    switch (part.ToString())
                    {
                    case ",":
                    {
                        if (((angleBracketsDepth == 0 && parenthesesDepth == 1 && bracesDepth == 0 && bracketsDepth == 0) ||
                             (angleBracketsDepth == 0 && parenthesesDepth == 0 && bracesDepth == 0 && bracketsDepth == 1)) &&
                            i < parts.Count - 1)
                        {
                            SymbolDisplayPart nextPart = parts[i + 1];

                            if (nextPart.Kind == SymbolDisplayPartKind.Space)
                            {
                                parts[i + 1] = SymbolDisplayPartFactory.LineBreak();
                                parts.Insert(i + 2, SymbolDisplayPartFactory.Indentation(indentChars));
                            }
                        }

                        break;
                    }

                    case "(":
                    {
                        parenthesesDepth++;
                        break;
                    }

                    case ")":
                    {
                        Debug.Assert(parenthesesDepth >= 0);
                        parenthesesDepth--;

                        if (parenthesesDepth == 0 &&
                            symbol.IsKind(SymbolKind.Method, SymbolKind.NamedType))
                        {
                            return;
                        }

                        break;
                    }

                    case "[":
                    {
                        bracketsDepth++;
                        break;
                    }

                    case "]":
                    {
                        Debug.Assert(bracketsDepth >= 0);
                        bracketsDepth--;

                        if (bracketsDepth == 0 &&
                            symbol.Kind == SymbolKind.Property)
                        {
                            return;
                        }

                        break;
                    }

                    case "{":
                    {
                        bracesDepth++;
                        break;
                    }

                    case "}":
                    {
                        Debug.Assert(bracesDepth >= 0);
                        bracesDepth--;
                        break;
                    }

                    case "<":
                    {
                        angleBracketsDepth++;
                        break;
                    }

                    case ">":
                    {
                        Debug.Assert(angleBracketsDepth >= 0);
                        angleBracketsDepth--;
                        break;
                    }
                    }
                }

                i++;
            }
        }
示例#15
0
        private static void AddParameterAttributes(
            ImmutableArray <SymbolDisplayPart> .Builder parts,
            ISymbol symbol,
            ImmutableArray <IParameterSymbol> parameters,
            SymbolDisplayFormat format,
            SymbolDisplayAdditionalOptions additionalOptions,
            Func <ISymbol, AttributeData, bool> shouldDisplayAttribute)
        {
            int i = FindParameterListStart(symbol, parts);

            if (i == -1)
            {
                return;
            }

            i++;

            int parameterIndex = 0;

            AddParameterAttributes();

            int parenthesesDepth   = 1;
            int bracesDepth        = 0;
            int bracketsDepth      = 0;
            int angleBracketsDepth = 0;

            while (i < parts.Count)
            {
                SymbolDisplayPart part = parts[i];

                if (part.Kind == SymbolDisplayPartKind.Punctuation)
                {
                    switch (part.ToString())
                    {
                    case ",":
                    {
                        if (((angleBracketsDepth == 0 && parenthesesDepth == 1 && bracesDepth == 0 && bracketsDepth == 0) ||
                             (angleBracketsDepth == 0 && parenthesesDepth == 0 && bracesDepth == 0 && bracketsDepth == 1)) &&
                            i < parts.Count - 1)
                        {
                            SymbolDisplayPart nextPart = parts[i + 1];

                            if (nextPart.Kind == SymbolDisplayPartKind.Space)
                            {
                                i += 2;
                                parameterIndex++;

                                AddParameterAttributes();
                                continue;
                            }
                        }

                        break;
                    }

                    case "(":
                    {
                        parenthesesDepth++;
                        break;
                    }

                    case ")":
                    {
                        Debug.Assert(parenthesesDepth >= 0);
                        parenthesesDepth--;

                        if (parenthesesDepth == 0 &&
                            symbol.IsKind(SymbolKind.Method, SymbolKind.NamedType))
                        {
                            return;
                        }

                        break;
                    }

                    case "[":
                    {
                        bracketsDepth++;
                        break;
                    }

                    case "]":
                    {
                        Debug.Assert(bracketsDepth >= 0);
                        bracketsDepth--;

                        if (bracketsDepth == 0 &&
                            symbol.Kind == SymbolKind.Property)
                        {
                            return;
                        }

                        break;
                    }

                    case "{":
                    {
                        bracesDepth++;
                        break;
                    }

                    case "}":
                    {
                        Debug.Assert(bracesDepth >= 0);
                        bracesDepth--;
                        break;
                    }

                    case "<":
                    {
                        angleBracketsDepth++;
                        break;
                    }

                    case ">":
                    {
                        Debug.Assert(angleBracketsDepth >= 0);
                        angleBracketsDepth--;
                        break;
                    }
                    }
                }

                i++;
            }

            void AddParameterAttributes()
            {
                IParameterSymbol parameter = parameters[parameterIndex];

                ImmutableArray <SymbolDisplayPart> attributeParts = GetAttributesParts(
                    parameter,
                    format,
                    additionalOptions,
                    shouldDisplayAttribute: shouldDisplayAttribute);

                if (attributeParts.Any())
                {
                    parts.Insert(i, SymbolDisplayPartFactory.Space());
                    parts.InsertRange(i, attributeParts);
                    i += attributeParts.Length + 1;
                }
            }
        }
示例#16
0
        private static void AddDisplayParts(
            this ImmutableArray <SymbolDisplayPart> .Builder parts,
            ISymbol symbol,
            SymbolDisplayFormat format,
            SymbolDisplayAdditionalOptions additionalOptions,
            bool removeAttributeSuffix = false)
        {
            SymbolDisplayFormat format2;

            if (additionalOptions.HasOption(SymbolDisplayAdditionalOptions.OmitContainingNamespace))
            {
                format2 = TypeSymbolDisplayFormats.Name_ContainingTypes_SpecialTypes;
            }
            else if (format.GlobalNamespaceStyle == SymbolDisplayGlobalNamespaceStyle.Included)
            {
                format2 = TypeSymbolDisplayFormats.Name_ContainingTypes_Namespaces_GlobalNamespace_SpecialTypes;
            }
            else
            {
                format2 = TypeSymbolDisplayFormats.Name_ContainingTypes_Namespaces_SpecialTypes;
            }

            parts.AddRange(symbol.ToDisplayParts(format2));

            if (!(symbol is INamedTypeSymbol typeSymbol))
            {
                return;
            }

            if (removeAttributeSuffix)
            {
                SymbolDisplayPart last = parts.Last();

                if (last.Kind == SymbolDisplayPartKind.ClassName)
                {
                    const string attributeSuffix = "Attribute";

                    string text = last.ToString();
                    if (text.EndsWith(attributeSuffix, StringComparison.Ordinal))
                    {
                        parts[parts.Count - 1] = last.WithText(text.Remove(text.Length - attributeSuffix.Length));
                    }
                }
            }

            ImmutableArray <ITypeSymbol> typeArguments = typeSymbol.TypeArguments;

            ImmutableArray <ITypeSymbol> .Enumerator en = typeArguments.GetEnumerator();

            if (en.MoveNext())
            {
                parts.AddPunctuation("<");

                while (true)
                {
                    if (en.Current.Kind == SymbolKind.NamedType)
                    {
                        parts.AddDisplayParts((INamedTypeSymbol)en.Current, format, additionalOptions);
                    }
                    else
                    {
                        Debug.Assert(en.Current.Kind == SymbolKind.TypeParameter, en.Current.Kind.ToString());

                        parts.Add(new SymbolDisplayPart(SymbolDisplayPartKind.TypeParameterName, en.Current, en.Current.Name));
                    }

                    if (en.MoveNext())
                    {
                        parts.AddPunctuation(",");
                        parts.AddSpace();
                    }
                    else
                    {
                        break;
                    }
                }

                parts.AddPunctuation(">");
            }
        }
 public static bool IsKeyword(this SymbolDisplayPart part, string text)
 {
     return(part.Kind == SymbolDisplayPartKind.Keyword &&
            part.ToString() == text);
 }
示例#18
0
 public static bool IsInternal(this SymbolDisplayPart sdp)
 => sdp.Kind == SymbolDisplayPartKind.Keyword && sdp.ToString() == "internal";
示例#19
0
        private CodeFileToken MapToken(ISymbol definedSymbol, SymbolDisplayPart symbolDisplayPart)
        {
            CodeFileTokenKind kind;

            switch (symbolDisplayPart.Kind)
            {
            case SymbolDisplayPartKind.TypeParameterName:
            case SymbolDisplayPartKind.AliasName:
            case SymbolDisplayPartKind.AssemblyName:
            case SymbolDisplayPartKind.ClassName:
            case SymbolDisplayPartKind.DelegateName:
            case SymbolDisplayPartKind.EnumName:
            case SymbolDisplayPartKind.ErrorTypeName:
            case SymbolDisplayPartKind.InterfaceName:
            case SymbolDisplayPartKind.StructName:
                kind = CodeFileTokenKind.TypeName;
                break;

            case SymbolDisplayPartKind.Keyword:
                kind = CodeFileTokenKind.Keyword;
                break;

            case SymbolDisplayPartKind.LineBreak:
                kind = CodeFileTokenKind.Newline;
                break;

            case SymbolDisplayPartKind.StringLiteral:
                kind = CodeFileTokenKind.StringLiteral;
                break;

            case SymbolDisplayPartKind.Punctuation:
                kind = CodeFileTokenKind.Punctuation;
                break;

            case SymbolDisplayPartKind.Space:
                kind = CodeFileTokenKind.Whitespace;
                break;

            case SymbolDisplayPartKind.PropertyName:
            case SymbolDisplayPartKind.EventName:
            case SymbolDisplayPartKind.FieldName:
            case SymbolDisplayPartKind.MethodName:
            case SymbolDisplayPartKind.Operator:
            case SymbolDisplayPartKind.EnumMemberName:
            case SymbolDisplayPartKind.ExtensionMethodName:
            case SymbolDisplayPartKind.ConstantName:
                kind = CodeFileTokenKind.MemberName;
                break;

            default:
                kind = CodeFileTokenKind.Text;
                break;
            }

            string navigateToId = null;
            var    symbol       = symbolDisplayPart.Symbol;

            if (symbol is INamedTypeSymbol &&
                (definedSymbol == null || !SymbolEqualityComparer.Default.Equals(definedSymbol, symbol)) &&
                SymbolEqualityComparer.Default.Equals(_assembly, symbol.ContainingAssembly))
            {
                navigateToId = symbol.GetId();
            }

            return(new CodeFileToken()
            {
                DefinitionId = (definedSymbol != null && SymbolEqualityComparer.Default.Equals(definedSymbol, symbol)) ? definedSymbol.GetId() : null,
                NavigateToId = navigateToId,
                Value = symbolDisplayPart.ToString(),
                Kind = kind
            });
        }
 private static bool CompareParts(SymbolDisplayPart p1, SymbolDisplayPart p2)
 {
     return(p1.ToString() == p2.ToString());
 }
        private static ImmutableArray <SymbolDisplayPart> .Builder ReplaceDefaultExpressionWithDefaultLiteral(
            ISymbol symbol,
            ImmutableArray <SymbolDisplayPart> .Builder parts)
        {
            ImmutableArray <SymbolDisplayPart> .Builder builder = null;

            int prevIndex = 0;
            int i         = 0;

            while (i < parts.Count)
            {
                if (parts[i].IsKeyword("default"))
                {
                    ReplaceDefaultExpressionWithDefaultLiteral3();
                }

                i++;
            }

            if (builder == null)
            {
                return(parts);
            }

            for (int j = prevIndex; j < parts.Count; j++)
            {
                builder.Add(parts[j]);
            }

            return(builder);

            void ReplaceDefaultExpressionWithDefaultLiteral3()
            {
                int openParenIndex = i + 1;

                if (openParenIndex >= parts.Count ||
                    !parts[openParenIndex].IsPunctuation("("))
                {
                    return;
                }

                int closeParenIndex = FindClosingParentheses(openParenIndex + 1);

                if (closeParenIndex == -1)
                {
                    return;
                }

                if (builder == null)
                {
                    builder = ImmutableArray.CreateBuilder <SymbolDisplayPart>(parts.Count);
                }

                for (int l = prevIndex; l < openParenIndex; l++)
                {
                    builder.Add(parts[l]);
                }

                i = closeParenIndex;

                prevIndex = i + 1;
            }

            int FindClosingParentheses(int startIndex)
            {
                int depth = 1;

                int j = startIndex;

                while (j < parts.Count)
                {
                    SymbolDisplayPart part = parts[j];

                    if (part.IsPunctuation())
                    {
                        string text = part.ToString();

                        if (text == "(")
                        {
                            depth++;
                        }
                        else if (text == ")")
                        {
                            Debug.Assert(depth > 0, "Parentheses depth should be greater than 0\r\n" + symbol.ToDisplayString(Roslynator.SymbolDisplayFormats.Test));

                            depth--;

                            if (depth == 0)
                            {
                                return(j);
                            }
                        }
                    }

                    j++;
                }

                return(-1);
            }
        }
 public static bool IsPunctuation(this SymbolDisplayPart part, string text)
 {
     return(part.Kind == SymbolDisplayPartKind.Punctuation &&
            part.ToString() == text);
 }
示例#23
0
        private void Initialize()
        {
            var content = new StringBuilder();
            var prettyPrintedContent = new StringBuilder();

            var parts = new List <SymbolDisplayPart>();
            var prettyPrintedParts = new List <SymbolDisplayPart>();

            var parameters = new List <IParameter>();

            var signaturePrefixParts   = _signatureHelpItem.PrefixDisplayParts;
            var signaturePrefixContent = _signatureHelpItem.PrefixDisplayParts.GetFullText();

            AddRange(signaturePrefixParts, parts, prettyPrintedParts);
            Append(signaturePrefixContent, content, prettyPrintedContent);

            var separatorParts   = _signatureHelpItem.SeparatorDisplayParts;
            var separatorContent = separatorParts.GetFullText();

            var newLinePart    = new SymbolDisplayPart(SymbolDisplayPartKind.LineBreak, null, "\r\n");
            var newLineContent = newLinePart.ToString();
            var spacerPart     = new SymbolDisplayPart(SymbolDisplayPartKind.Space, null, new string(' ', signaturePrefixContent.Length));
            var spacerContent  = spacerPart.ToString();

            var paramColumnCount = 0;

            for (int i = 0; i < _signatureHelpItem.Parameters.Length; i++)
            {
                var sigHelpParameter = _signatureHelpItem.Parameters[i];

                var parameterPrefixParts   = sigHelpParameter.PrefixDisplayParts;
                var parameterPrefixContext = sigHelpParameter.PrefixDisplayParts.GetFullText();

                var parameterParts   = AddOptionalBrackets(sigHelpParameter.IsOptional, sigHelpParameter.DisplayParts);
                var parameterContent = parameterParts.GetFullText();

                var parameterSuffixParts   = sigHelpParameter.SuffixDisplayParts;
                var parameterSuffixContext = sigHelpParameter.SuffixDisplayParts.GetFullText();

                paramColumnCount += separatorContent.Length + parameterPrefixContext.Length + parameterContent.Length + parameterSuffixContext.Length;

                if (i > 0)
                {
                    AddRange(separatorParts, parts, prettyPrintedParts);
                    Append(separatorContent, content, prettyPrintedContent);

                    if (paramColumnCount > MaxParamColumnCount)
                    {
                        prettyPrintedParts.Add(newLinePart);
                        prettyPrintedParts.Add(spacerPart);
                        prettyPrintedContent.Append(newLineContent);
                        prettyPrintedContent.Append(spacerContent);

                        paramColumnCount = 0;
                    }
                }

                AddRange(parameterPrefixParts, parts, prettyPrintedParts);
                Append(parameterPrefixContext, content, prettyPrintedContent);

                parameters.Add(new Parameter(this, sigHelpParameter, parameterContent, content.Length, prettyPrintedContent.Length));

                AddRange(parameterParts, parts, prettyPrintedParts);
                Append(parameterContent, content, prettyPrintedContent);

                AddRange(parameterSuffixParts, parts, prettyPrintedParts);
                Append(parameterSuffixContext, content, prettyPrintedContent);
            }

            AddRange(_signatureHelpItem.SuffixDisplayParts, parts, prettyPrintedParts);
            Append(_signatureHelpItem.SuffixDisplayParts.GetFullText(), content, prettyPrintedContent);

            if (_parameterIndex >= 0)
            {
                var sigHelpParameter = _signatureHelpItem.Parameters[_parameterIndex];

                AddRange(sigHelpParameter.SelectedDisplayParts, parts, prettyPrintedParts);
                Append(sigHelpParameter.SelectedDisplayParts.GetFullText(), content, prettyPrintedContent);
            }

            AddRange(_signatureHelpItem.DescriptionParts, parts, prettyPrintedParts);
            Append(_signatureHelpItem.DescriptionParts.GetFullText(), content, prettyPrintedContent);

            var documentation = _signatureHelpItem.DocumentationFactory(CancellationToken.None).ToList();

            if (documentation.Count > 0)
            {
                AddRange(new[] { newLinePart }, parts, prettyPrintedParts);
                Append(newLineContent, content, prettyPrintedContent);

                AddRange(documentation, parts, prettyPrintedParts);
                Append(documentation.GetFullText(), content, prettyPrintedContent);
            }

            _content = content.ToString();
            _prettyPrintedContent      = prettyPrintedContent.ToString();
            _displayParts              = parts.ToImmutableArrayOrEmpty();
            _prettyPrintedDisplayParts = prettyPrintedParts.ToImmutableArrayOrEmpty();
            _parameters = parameters.ToReadOnlyCollection();
        }
示例#24
0
 SymbolDisplayPart ToP2P(SymbolDisplayPart p, ISymbol symbol)
 {
     return(p.Kind == SymbolDisplayPartKind.ClassName && p.ToString() == symbol.ContainingType.Name
         ? new SymbolDisplayPart(p.Kind, p.Symbol, Prefix.ProtectedToPublicInterface + className)
         : p);
 }
示例#25
0
        private ImmutableArray <SymbolDisplayPart> AppendParameterAttributes(
            ImmutableArray <SymbolDisplayPart> parts,
            ISymbol symbol,
            ImmutableArray <IParameterSymbol> parameters)
        {
            int i = SymbolDeclarationBuilder.FindParameterListStart(symbol, parts);

            if (i == -1)
            {
                return(parts);
            }

            int parameterIndex = 0;

            IParameterSymbol parameter = parameters[parameterIndex];

            ImmutableArray <SymbolDisplayPart> attributeParts = SymbolDeclarationBuilder.GetAttributesParts(
                parameter.GetAttributes(),
                predicate: IsVisibleAttribute,
                splitAttributes: Options.SplitAttributes,
                includeAttributeArguments: Options.IncludeAttributeArguments,
                addNewLine: false);

            if (attributeParts.Any())
            {
                parts = parts.Insert(i + 1, SymbolDisplayPartFactory.Space());
                parts = parts.InsertRange(i + 1, attributeParts);
            }

            int parenthesesDepth   = 0;
            int bracesDepth        = 0;
            int bracketsDepth      = 0;
            int angleBracketsDepth = 0;

            ImmutableArray <SymbolDisplayPart> .Builder builder = null;

            int prevIndex = 0;

            AddParameterAttributes();

            if (builder != null)
            {
                while (prevIndex < parts.Length)
                {
                    builder.Add(parts[prevIndex]);
                    prevIndex++;
                }

                return(builder.ToImmutableArray());
            }

            return(parts);

            void AddParameterAttributes()
            {
                while (i < parts.Length)
                {
                    SymbolDisplayPart part = parts[i];

                    if (part.Kind == SymbolDisplayPartKind.Punctuation)
                    {
                        switch (part.ToString())
                        {
                        case ",":
                        {
                            if (((angleBracketsDepth == 0 && parenthesesDepth == 1 && bracesDepth == 0 && bracketsDepth == 0) ||
                                 (angleBracketsDepth == 0 && parenthesesDepth == 0 && bracesDepth == 0 && bracketsDepth == 1)) &&
                                i < parts.Length - 1)
                            {
                                SymbolDisplayPart nextPart = parts[i + 1];

                                if (nextPart.Kind == SymbolDisplayPartKind.Space)
                                {
                                    parameterIndex++;

                                    attributeParts = SymbolDeclarationBuilder.GetAttributesParts(
                                        parameters[parameterIndex].GetAttributes(),
                                        predicate: IsVisibleAttribute,
                                        splitAttributes: Options.SplitAttributes,
                                        includeAttributeArguments: Options.IncludeAttributeArguments,
                                        addNewLine: false);

                                    if (attributeParts.Any())
                                    {
                                        if (builder == null)
                                        {
                                            builder = ImmutableArray.CreateBuilder <SymbolDisplayPart>();

                                            builder.AddRange(parts, i + 1);
                                        }
                                        else
                                        {
                                            for (int j = prevIndex; j <= i; j++)
                                            {
                                                builder.Add(parts[j]);
                                            }
                                        }

                                        builder.Add(SymbolDisplayPartFactory.Space());
                                        builder.AddRange(attributeParts);

                                        prevIndex = i + 1;
                                    }
                                }
                            }

                            break;
                        }

                        case "(":
                        {
                            parenthesesDepth++;
                            break;
                        }

                        case ")":
                        {
                            Debug.Assert(parenthesesDepth >= 0);
                            parenthesesDepth--;

                            if (parenthesesDepth == 0 &&
                                symbol.IsKind(SymbolKind.Method, SymbolKind.NamedType))
                            {
                                return;
                            }

                            break;
                        }

                        case "[":
                        {
                            bracketsDepth++;
                            break;
                        }

                        case "]":
                        {
                            Debug.Assert(bracketsDepth >= 0);
                            bracketsDepth--;

                            if (bracketsDepth == 0 &&
                                symbol.Kind == SymbolKind.Property)
                            {
                                return;
                            }

                            break;
                        }

                        case "{":
                        {
                            bracesDepth++;
                            break;
                        }

                        case "}":
                        {
                            Debug.Assert(bracesDepth >= 0);
                            bracesDepth--;
                            break;
                        }

                        case "<":
                        {
                            angleBracketsDepth++;
                            break;
                        }

                        case ">":
                        {
                            Debug.Assert(angleBracketsDepth >= 0);
                            angleBracketsDepth--;
                            break;
                        }
                        }
                    }

                    i++;
                }
            }
        }
示例#26
0
        public static ImmutableArray <SymbolDisplayPart> ToDisplayParts(this ISymbol symbol, SymbolDisplayFormat format, SymbolDisplayAdditionalMemberOptions additionalOptions)
        {
            if (additionalOptions == SymbolDisplayAdditionalMemberOptions.None)
            {
                return(symbol.ToDisplayParts(format));
            }

            ImmutableArray <SymbolDisplayPart> parts = symbol.ToDisplayParts(format);
            int length = parts.Length;

            for (int i = 0; i < length; i++)
            {
                SymbolDisplayPart part = parts[i];

                switch (part.Kind)
                {
                case SymbolDisplayPartKind.Keyword:
                {
                    switch (part.ToString())
                    {
                    case "this":
                    {
                        if ((additionalOptions & SymbolDisplayAdditionalMemberOptions.UseItemPropertyName) != 0 &&
                            (symbol as IPropertySymbol)?.IsIndexer == true)
                        {
                            parts = parts.Replace(part, SymbolDisplayPartFactory.PropertyName("Item", part.Symbol));
                        }

                        break;
                    }

                    case "operator":
                    {
                        if ((additionalOptions & SymbolDisplayAdditionalMemberOptions.UseOperatorName) != 0 &&
                            symbol is IMethodSymbol methodSymbol &&
                            methodSymbol.MethodKind == MethodKind.UserDefinedOperator)
                        {
                            string name = methodSymbol.Name;

                            Debug.Assert(name.StartsWith("op_", StringComparison.Ordinal), name);

                            if (name.StartsWith("op_", StringComparison.Ordinal) &&
                                i < length - 2 &&
                                parts[i + 1].IsSpace() &&
                                parts[i + 2].Kind == SymbolDisplayPartKind.MethodName)
                            {
                                parts   = parts.Replace(parts[i + 2], SymbolDisplayPartFactory.MethodName(name.Substring(3), parts[i + 2].Symbol));
                                parts   = parts.RemoveRange(i, 2);
                                length -= 2;
                            }
                        }

                        break;
                    }

                    case "implicit":
                    case "explicit":
                    {
                        if ((additionalOptions & SymbolDisplayAdditionalMemberOptions.UseOperatorName) != 0 &&
                            symbol is IMethodSymbol methodSymbol &&
                            methodSymbol.MethodKind == MethodKind.Conversion)
                        {
                            string name = methodSymbol.Name;

                            Debug.Assert(name.StartsWith("op_", StringComparison.Ordinal), name);

                            if (name.StartsWith("op_", StringComparison.Ordinal) &&
                                i < length - 2 &&
                                parts[i + 1].IsSpace() &&
                                parts[i + 2].IsKeyword("operator"))
                            {
                                List <SymbolDisplayPart> list = parts.ToList();

                                list[i + 2] = SymbolDisplayPartFactory.MethodName(name.Substring(3), list[i + 4].Symbol);
                                list.RemoveRange(i, 2);
                                length -= 2;

                                if (i == length - 3 &&
                                    list[i + 1].IsSpace() &&
                                    list[i + 2].IsName())
                                {
                                    list.RemoveRange(i + 1, 2);
                                    length -= 2;
                                }
                                else if (i < length - 5 &&
                                         list[i + 1].IsSpace() &&
                                         list[i + 2].IsName() &&
                                         list[i + 3].IsPunctuation() &&
                                         list[i + 4].IsName() &&
                                         list[i + 5].IsPunctuation())
                                {
                                    list.Insert(i + 5, list[i + 2]);
                                    list.Insert(i + 5, SymbolDisplayPartFactory.Text(" to "));
                                    list.RemoveRange(i + 1, 2);
                                    length -= 5;
                                }

                                parts = list.ToImmutableArray();
                            }
                        }

                        break;
                    }
                    }

                    break;
                }
                }
            }

            return(parts);
        }
 public static TaggedText ToTaggedText(this SymbolDisplayPart part) =>
 new TaggedText(SymbolDisplayPartKindTags.GetTag(part.Kind), part.ToString());
 private static bool CompareParts(SymbolDisplayPart p1, SymbolDisplayPart p2)
 {
     return p1.ToString() == p2.ToString();
 }
示例#29
0
 static SymbolDisplayPart ReplaceDotWithUnderline(SymbolDisplayPart p)
 => p.Kind == SymbolDisplayPartKind.Punctuation && p.ToString() == "."
     ? new SymbolDisplayPart(p.Kind, p.Symbol, "_")
     : p;