Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeLookUp Structured Documentation Feature to be used by VS Code #1038

Merged
merged 16 commits into from
Dec 8, 2017
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace OmniSharp.Models.TypeLookup
{
public class DocumentationComment
{
public string RemarksText { get; set; }
public string ExampleText { get; set; }
public string ReturnsText { get; set; }
public string SummaryText { get; set; }
public string ValueText { get; set; }
public string[ ] ParamElements { get; set; }
public string[ ] TypeParamElements { get; set; }
public string[ ] Exception { get; set; }

public DocumentationComment()
{
RemarksText = "";
ExampleText = "";
ReturnsText = "";
SummaryText = "";
ValueText = "";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ public class TypeLookupResponse
{
public string Type { get; set; }
public string Documentation { get; set; }
public DocumentationComment StructuredDocumentation { get; set; }
}
}
}
128 changes: 128 additions & 0 deletions src/OmniSharp.Roslyn.CSharp/Services/DocumentationConverter.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using OmniSharp.Models.TypeLookup;

namespace OmniSharp.Roslyn.CSharp.Services.Documentation
{
Expand Down Expand Up @@ -126,5 +128,131 @@ private static string GetCref(string cref)
}
return cref + " ";
}

public static DocumentationComment GetStructuredDocumentation(string xmlDocumentation, string lineEnding)
{
if (string.IsNullOrEmpty(xmlDocumentation))
return null;
var docComment = new DocumentationComment();
var reader = new StringReader("<docroot>" + xmlDocumentation + "</docroot>");
StringBuilder remarksText = new StringBuilder();
StringBuilder exampleText = new StringBuilder();
StringBuilder returnsText = new StringBuilder();
StringBuilder summaryText = new StringBuilder();
StringBuilder valueText = new StringBuilder();
List<StringBuilder> paramElements = new List<StringBuilder>();
List<StringBuilder> typeParamElements = new List<StringBuilder>();
List<StringBuilder> exception = new List<StringBuilder>();

using (var xml = XmlReader.Create(reader))
{
try
{
xml.Read();
string elementName = null;
StringBuilder currentSectionBuilder = null;
do
{
if (xml.NodeType == XmlNodeType.Element)
{
elementName = xml.Name.ToLowerInvariant();
switch (elementName)
{
case "filterpriority":
xml.Skip();
break;
case "remarks":
remarksText.Append("Remarks: ");
currentSectionBuilder = remarksText;
break;
case "example":
exampleText.Append("Example: ");
currentSectionBuilder = exampleText;
break;
case "exception":
StringBuilder ExceptionInstance = new StringBuilder();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lowerCase.

ExceptionInstance.Append(GetCref(xml["cref"]).TrimEnd());
ExceptionInstance.Append(": ");
currentSectionBuilder = ExceptionInstance;
exception.Add(ExceptionInstance);
break;
case "returns":
returnsText.Append("Returns: ");
currentSectionBuilder = returnsText;
break;
case "summary":
summaryText.Append("Summary: ");
currentSectionBuilder = summaryText;
break;
case "see":
currentSectionBuilder.Append(GetCref(xml["cref"]));
currentSectionBuilder.Append(xml["langword"]);
break;
case "seealso":
currentSectionBuilder.Append("See also: ");
currentSectionBuilder.Append(GetCref(xml["cref"]));
break;
case "paramref":
currentSectionBuilder.Append(xml["name"]);
currentSectionBuilder.Append(" ");
break;
case "param":
StringBuilder paramInstance = new StringBuilder();
paramInstance.Append(TrimMultiLineString(xml["name"], lineEnding));
paramInstance.Append(": ");
currentSectionBuilder = paramInstance;
paramElements.Add(paramInstance);
break;
case "typeparamref":
currentSectionBuilder.Append(xml["name"]);
currentSectionBuilder.Append(" ");
break;
case "typeparam":
StringBuilder typeParamInstance = new StringBuilder();
typeParamInstance.Append(TrimMultiLineString(xml["name"], lineEnding));
typeParamInstance.Append(": ");
currentSectionBuilder = typeParamInstance;
typeParamElements.Add(typeParamInstance);
break;
case "value":
valueText.Append("Value: ");
currentSectionBuilder = valueText;
break;
case "br":
case "para":
currentSectionBuilder.Append(lineEnding);
break;
}
}
else if (xml.NodeType == XmlNodeType.Text && currentSectionBuilder != null)
{
if (elementName == "code")
{
currentSectionBuilder.Append(xml.Value);
}
else
{
currentSectionBuilder.Append(TrimMultiLineString(xml.Value, lineEnding));
}
}
} while (xml.Read());
}
catch (Exception)
{
return null;
}

docComment.RemarksText = remarksText.ToString();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you feel about adding a constructor to docComment that takes all of these, rather than having to remember to set all of the properties? (Maybe makes it easier to not forget a section if we refactor).

docComment.ExampleText = exampleText.ToString();
docComment.ReturnsText = returnsText.ToString();
docComment.SummaryText = summaryText.ToString();
docComment.ValueText = valueText.ToString();
docComment.ParamElements = paramElements.Select(s => s.ToString()).ToArray();
docComment.TypeParamElements = typeParamElements.Select(s => s.ToString()).ToArray();
docComment.Exception = exception.Select(s => s.ToString()).ToArray();
return docComment;
}
}
}
}

1 change: 1 addition & 0 deletions src/OmniSharp.Roslyn.CSharp/Services/Types/TypeLookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public async Task<TypeLookupResponse> Handle(TypeLookupRequest request)
if (request.IncludeDocumentation)
{
response.Documentation = DocumentationConverter.ConvertDocumentation(symbol.GetDocumentationCommentXml(), _formattingOptions.NewLine);
response.StructuredDocumentation = DocumentationConverter.GetStructuredDocumentation(symbol.GetDocumentationCommentXml(), _formattingOptions.NewLine);
}
}
}
Expand Down
Loading