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

Preserve comments #26

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
29 changes: 23 additions & 6 deletions src/TypeScriptDefinitionGenerator/Generator/IntellisenseParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -442,15 +442,32 @@ private static string GetSummary(vsCMInfoLocation location, string xmlComment, s

try
{
string summary = "";
var summary = new System.Text.StringBuilder();
if (!string.IsNullOrWhiteSpace(xmlComment))
{
summary = XElement.Parse(xmlComment)
.Descendants("summary")
.Select(x => x.Value)
.FirstOrDefault();
var s = XElement.Parse(xmlComment).Descendants("summary").FirstOrDefault();
foreach (var e in s.Nodes())
{
switch (e.NodeType)
{
case System.Xml.XmlNodeType.None:
break;
case System.Xml.XmlNodeType.Element:
if ((e as XElement).Name == "see") {
summary.Append("'" + (e as XElement).Attribute("cref").Value + "'");
}

break;
case System.Xml.XmlNodeType.Text:
summary.Append((e as XText).Value);
break;

default:
break;
}
}
}
if (!string.IsNullOrEmpty(summary)) return summary.Trim();
if (summary.Length > 0) return summary.ToString().Trim();
if (!string.IsNullOrWhiteSpace(inlineComment)) return inlineComment.Trim();
return null;
}
Expand Down
32 changes: 22 additions & 10 deletions src/TypeScriptDefinitionGenerator/Generator/IntellisenseWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ namespace TypeScriptDefinitionGenerator
{
internal static class IntellisenseWriter
{
private static readonly Regex _whitespaceTrimmer = new Regex(@"^\s+|\s+$|\s*[\r\n]+\s*", RegexOptions.Compiled);

/// <summary>
/// Generates TypeScript file for given C# class/enum (IntellisenseObject).
/// </summary>
Expand Down Expand Up @@ -43,9 +41,8 @@ public static string WriteTypeScript(IList<IntellisenseObject> objects, string s

foreach (IntellisenseObject io in ns)
{
if (!string.IsNullOrEmpty(io.Summary))
sbBody.Append(prefixModule).AppendLine("/** " + _whitespaceTrimmer.Replace(io.Summary, "") + " */");

WriteTypeScriptComment(io.Summary, sbBody, prefixModule);

if (io.IsEnum)
{
string type = "enum ";
Expand Down Expand Up @@ -157,17 +154,32 @@ private static string CleanEnumInitValue(string value)
return "0";
}

private static void WriteTypeScriptComment(IntellisenseProperty p, StringBuilder sb, string prefix)
private static void WriteTypeScriptComment(string comment, StringBuilder sb, string prefix)
{
if (string.IsNullOrEmpty(p.Summary)) return;
sb.Append(prefix).AppendLine("/** " + _whitespaceTrimmer.Replace(p.Summary, "") + " */");
if (string.IsNullOrEmpty(comment)) return;
string[] commentLines = comment.Split(
new[] { "\r\n", "\r", "\n" },
StringSplitOptions.None
);
bool isFirstLine = true;
foreach (var commentLine in commentLines)
{
if (isFirstLine)
{
sb.Append(prefix).AppendLine("/** ");
isFirstLine = false;
}

sb.Append(prefix).Append(" * ").AppendLine(commentLine.Replace("*/", "+/"));
}
sb.Append(prefix).AppendLine(" */");
}

private static void WriteTSEnumDefinition(StringBuilder sb, string prefix, IEnumerable<IntellisenseProperty> props)
{
foreach (var p in props)
{
WriteTypeScriptComment(p, sb, prefix);
WriteTypeScriptComment(p.Summary, sb, prefix);

if (p.InitExpression != null)
{
Expand All @@ -185,7 +197,7 @@ private static void WriteTSInterfaceDefinition(StringBuilder sb, string prefix,
{
foreach (var p in props)
{
WriteTypeScriptComment(p, sb, prefix);
WriteTypeScriptComment(p.Summary, sb, prefix);
sb.AppendFormat("{0}{1}: ", prefix, Utility.CamelCasePropertyName(p.NameWithOption));

if (p.Type.IsKnownType) sb.Append(p.Type.TypeScriptName);
Expand Down