Пример #1
0
 public MvcHtmlString Render(LanguageProvider language,
     MethodSignature signature,
     MethodRenderOption options)
 {
     var builder = new HtmlTextWriter(new StringWriter());
     Render(builder, language, signature, options);
     return MvcHtmlString.Create(builder.InnerWriter.ToString());
 }
Пример #2
0
        public void Render(
            HtmlTextWriter writer,
            LanguageProvider language,
            MethodSignature signature,
            MethodRenderOption options)
        {
            // Add link.
            if ((options & MethodRenderOption.Link) == MethodRenderOption.Link)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Href, signature.Url);
                writer.RenderBeginTag(HtmlTextWriterTag.A);
            }

            // Declaring type (with or without namespace).
            if ((options & MethodRenderOption.TypeName) == MethodRenderOption.TypeName ||
                (options & MethodRenderOption.TypeFullName) == MethodRenderOption.TypeFullName)
            {
                var onlyTypeName = (options & MethodRenderOption.TypeName) == MethodRenderOption.TypeName;
                var typeOptions = onlyTypeName ? TypeRenderOption.Name : TypeRenderOption.Namespace;
                _renderer.Render(writer, signature.DeclaringType, typeOptions);
            }

            var isImplicitOperator = signature.OperatorClassification == OperatorClassification.Implicit;
            var isExplicitOperator = signature.OperatorClassification == OperatorClassification.Explicit;
            var isImplicitOrExplicitOperator = isImplicitOperator || isExplicitOperator;

            if (isImplicitOrExplicitOperator)
            {
                // Implicit operator
                RenderImplicitOrExplicitOperator(writer, signature, options, isImplicitOperator);
            }
            else
            {
                // Everything else.
                if ((options & MethodRenderOption.Name) == MethodRenderOption.Name)
                {
                    if ((options & MethodRenderOption.TypeName) == MethodRenderOption.TypeName ||
                        (options & MethodRenderOption.TypeFullName) == MethodRenderOption.TypeFullName)
                    {

                        writer.WriteEncodedText("\u200B.");
                    }
                    writer.WriteEncodedText(GetMethodName(signature));
                }

                var isPropertyAlias = (options & MethodRenderOption.PropertyAlias) == MethodRenderOption.PropertyAlias;
                if (!isPropertyAlias && (options & MethodRenderOption.Parameters) == MethodRenderOption.Parameters)
                {
                    if (signature.Classification != MethodClassification.Operator)
                    {
                        writer.WriteEncodedText("(");
                        var parameterResult = new List<string>();
                        foreach (var parameter in signature.Parameters)
                        {
                            var parameterBuilder = new StringBuilder();
                            if (parameter.IsOutParameter)
                            {
                                parameterBuilder.Append("out ");
                            }
                            else if (parameter.IsRefParameter)
                            {
                                parameterBuilder.Append("ref ");
                            }
                            var paramType = _renderer.Render(parameter.ParameterType, TypeRenderOption.Name);
                            parameterBuilder.Append(paramType);
                            parameterResult.Add(parameterBuilder.ToString());
                        }
                        if (parameterResult.Count > 0)
                        {
                            var isMethodAlias = (options & MethodRenderOption.MethodAlias) == MethodRenderOption.MethodAlias;
                            if (isMethodAlias || (options & MethodRenderOption.ExtensionMethod) == MethodRenderOption.ExtensionMethod)
                            {
                                if (signature.Classification == MethodClassification.ExtensionMethod)
                                {
                                    // Remove first parameter.
                                    parameterResult.RemoveAt(0);
                                }
                            }
                            writer.Write(string.Join(", \u200B", parameterResult));
                        }
                        writer.WriteEncodedText(")\u200B");
                    }
                }
            }

            if ((options & MethodRenderOption.Link) == MethodRenderOption.Link)
            {
                writer.RenderEndTag();
            }
        }
Пример #3
0
        private void RenderImplicitOrExplicitOperator(
            HtmlTextWriter writer,
            MethodSignature signature,
            MethodRenderOption options,
            bool isImplicit)
        {
            if ((options & MethodRenderOption.TypeName) == MethodRenderOption.TypeName ||
                (options & MethodRenderOption.TypeFullName) == MethodRenderOption.TypeFullName)
            {
                writer.WriteEncodedText(".");
            }

            if ((options & MethodRenderOption.Name) == MethodRenderOption.Name)
            {
                writer.WriteEncodedText(isImplicit ? "Implicit" : "Explicit");
            }

            if ((options & MethodRenderOption.Parameters) == MethodRenderOption.Parameters)
            {
                if ((options & MethodRenderOption.Name) == MethodRenderOption.Name)
                {
                    writer.WriteEncodedText(" ");
                }

                writer.WriteEncodedText("(\u200B");
                _renderer.Render(writer, signature.Parameters[0].ParameterType, TypeRenderOption.Name);
                writer.WriteEncodedText(" to ");
                _renderer.Render(writer, signature.ReturnType, TypeRenderOption.Name);
                writer.WriteEncodedText(")\u200B");
            }
        }
Пример #4
0
 private string GetMethodName(MethodSignature signature)
 {
     if (signature.Classification == MethodClassification.Operator)
     {
         var operatorName = signature.OperatorClassification.GetOperatorName();
         if (operatorName != null)
         {
             return operatorName;
         }
     }
     return signature.Name;
 }
Пример #5
0
 public IHtmlString Render(MethodSignature signature)
 {
     var writer = new HtmlTextWriter(new StringWriter());
     Render(writer, signature);
     return MvcHtmlString.Create(writer.InnerWriter.ToString());
 }
Пример #6
0
        /// <summary>
        /// Renders the syntax for a method.
        /// </summary>
        /// <param name="writer">The text writer.</param>
        /// <param name="signature">The method signature.</param>
        public void Render(HtmlTextWriter writer, MethodSignature signature)
        {
            var method = _resolver.FindMethod(signature.Identity);
            if (method == null)
            {
                throw new InvalidOperationException("Could not find method.");
            }

            if (method.Definition.IsPublic)
            {
                writer.WriteEncodedText("public");
            }
            else if (method.Definition.IsFamily)
            {
                writer.WriteEncodedText("protected");
            }
            else if (method.Definition.IsFamilyOrAssembly)
            {
                writer.WriteEncodedText("protected internal");
            }

            if (method.Definition.IsAbstract)
            {
                writer.WriteEncodedText(" ");
                writer.WriteEncodedText("abstract");
            }

            if (method.Definition.IsStatic)
            {
                writer.WriteEncodedText(" ");
                writer.WriteEncodedText("static");
            }

            if (method.Definition.IsVirtual)
            {
                writer.WriteEncodedText(" ");
                writer.WriteEncodedText("virtual");
            }

            var isImplicitOperator = signature.OperatorClassification == OperatorClassification.Implicit;
            var isExplicitOperator = signature.OperatorClassification == OperatorClassification.Explicit;
            var isImplicitOrExplicitOperator = isImplicitOperator || isExplicitOperator;

            // Return type
            if (!method.Definition.IsConstructor && !isImplicitOrExplicitOperator)
            {
                var returnType = method.Definition.ReturnType.GetTypeSignature(_urlResolver);
                writer.WriteEncodedText(" ");
                _renderer.Render(writer, returnType, TypeRenderOption.Name | TypeRenderOption.Link);
            }

            // Name
            if (signature.Classification == MethodClassification.Operator)
            {
                if (isImplicitOperator)
                {
                    writer.WriteEncodedText(" implicit");
                }
                else if (isExplicitOperator)
                {
                    writer.WriteEncodedText(" explicit");
                }

                writer.WriteEncodedText(" operator ");
                if (isImplicitOrExplicitOperator)
                {
                    // Implicit or explicit operator
                    var returnType = method.Definition.ReturnType.GetTypeSignature(_urlResolver);
                    _renderer.Render(writer, returnType, TypeRenderOption.Name | TypeRenderOption.Link);
                }
                else
                {
                    // Operator name.
                    writer.WriteEncodedText(signature.OperatorClassification.GetOperatorSymbol() ?? "[unknown_symbol]");
                }
            }
            else
            {
                writer.WriteEncodedText(" ");
                writer.WriteEncodedText(signature.Name);
            }

            // Parameters
            var isPropertyAlias = method.Metadata.IsPropertyAlias;
            if (!isPropertyAlias && method.Parameters.Count > 0)
            {
                writer.WriteEncodedText("(");
                writer.WriteLine();

                var index = 0;
                foreach (var parameter in method.Parameters)
                {
                    if(index == 0 && method.Metadata.IsAlias)
                    {
                        index++;
                        continue;
                    }

                    writer.WriteEncodedText("       ");

                    if (index == 0)
                    {
                        if (method.Definition.IsExtensionMethod())
                        {
                            writer.WriteEncodedText("this ");
                        }
                    }

                    if (parameter.Definition.IsOut)
                    {
                        writer.WriteEncodedText("out ");
                    }
                    else if (parameter.Definition.ParameterType is ByReferenceType)
                    {
                        writer.WriteEncodedText("ref ");
                    }

                    // Append the type name.
                    var parameterTypeSignature = parameter.Definition.ParameterType.GetTypeSignature(_urlResolver);
                    _renderer.Render(writer, parameterTypeSignature, TypeRenderOption.Name | TypeRenderOption.Link);
                    writer.WriteEncodedText(" ");

                    var parameterName = parameter.Name;
                    if (index == method.Parameters.Count - 1)
                    {
                        writer.WriteEncodedText(parameterName);
                        writer.WriteLine();
                    }
                    else
                    {
                        writer.WriteEncodedText(parameterName);
                        writer.WriteEncodedText(",");
                        writer.WriteLine();
                    }

                    index++;
                }
                writer.WriteEncodedText(")");
                writer.WriteLine();
            }
            else
            {
                if (!isPropertyAlias)
                {
                    writer.WriteEncodedText("()");
                    writer.WriteLine();
                }
            }
        }
Пример #7
0
 public IHtmlString Render(MethodSignature method, MethodRenderOption option)
 {
     return _methodRenderer.Render(_languageProvider, method, option);
 }