diff --git a/T4TS.Example/Models/Barfoo.cs b/T4TS.Example/Models/Barfoo.cs index 11c97df..ec3d90e 100644 --- a/T4TS.Example/Models/Barfoo.cs +++ b/T4TS.Example/Models/Barfoo.cs @@ -3,10 +3,22 @@ namespace T4TS.Example.Models { + /// + /// Barfoo has some comments! + /// var bar = new Barfoo(); + /// [TypeScriptInterface(Module = "")] public class Barfoo { + /// + /// Well, this is a number + /// And has multiple lines of comment + /// "Nicely" formated + /// public int Number { get; set; } + /// + /// Okay, this has a single line of comment + /// public Inherited Complex { get; set; } public string Name { get; set; } public DateTime DateTime { get; set; } diff --git a/T4TS.Example/T4TS.d.ts b/T4TS.Example/T4TS.d.ts index 6818401..0069283 100644 --- a/T4TS.Example/T4TS.d.ts +++ b/T4TS.Example/T4TS.d.ts @@ -18,8 +18,20 @@ // -- Begin global interfaces /** Generated from T4TS.Example.Models.Barfoo **/ + /** + * Barfoo has some comments! + * var bar = new Barfoo(); + * */ interface Barfoo { + /** + * Well, this is a number + * And has multiple lines of comment + * "Nicely" formated + * */ Number: number; + /** + * Okay, this has a single line of comment + * */ Complex: T4TS.OverridenName; Name: string; DateTime: string; @@ -111,6 +123,12 @@ declare module T4TS { MyProperty: number; SomeDateTime: string; } + /** Generated from T4TS.Tests.Fixtures.Dictionary.DictionaryModel **/ + export interface DictionaryModel { + IntKey: { [name: number]: T4TS.BasicModel}; + StringKey: { [name: string]: T4TS.BasicModel}; + [index: number]: T4TS.BasicModel; + } /** Generated from T4TS.Tests.Fixtures.Enumerable.EnumerableModel **/ export interface EnumerableModel { NormalProperty: number; @@ -120,6 +138,7 @@ declare module T4TS { InterfaceList: T4TS.BasicModel[]; DeepArray: number[][]; DeepList: number[][]; + Generic: string[]; } /** Generated from T4TS.Tests.Fixtures.ExternalProp.ExternalPropModel **/ export interface ExternalPropModel { diff --git a/T4TS/CodeTraverser.cs b/T4TS/CodeTraverser.cs index 773923a..bc6a349 100644 --- a/T4TS/CodeTraverser.cs +++ b/T4TS/CodeTraverser.cs @@ -114,8 +114,10 @@ private TypeScriptInterface BuildInterface(CodeClass codeClass, TypeScriptInterf { var tsInterface = new TypeScriptInterface { - FullName = codeClass.FullName, - Name = GetInterfaceName(attributeValues) + FullName = codeClass.FullName, + Name = GetInterfaceName(attributeValues), + Comment = codeClass.Comment, + DocComment = codeClass.DocComment }; TypescriptType indexedType; @@ -209,8 +211,10 @@ private bool TryGetMember(CodeProperty property, TypeContext typeContext, out Ty Optional = values.Optional, Ignore = values.Ignore, Type = (string.IsNullOrWhiteSpace(values.Type)) - ? typeContext.GetTypeScriptType(getter.Type) - : new InterfaceType(values.Type) + ? typeContext.GetTypeScriptType(getter.Type) + : new InterfaceType(values.Type), + Comment = property.Comment, + DocComment = property.DocComment }; if (member.Ignore) diff --git a/T4TS/Outputs/InterfaceOutputAppender.cs b/T4TS/Outputs/InterfaceOutputAppender.cs index 1bc0e7c..46e9cf8 100644 --- a/T4TS/Outputs/InterfaceOutputAppender.cs +++ b/T4TS/Outputs/InterfaceOutputAppender.cs @@ -1,65 +1,69 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace T4TS -{ - public class InterfaceOutputAppender : OutputAppender - { - private bool InGlobalModule { get; set; } - - public InterfaceOutputAppender(StringBuilder output, int baseIndentation, Settings settings, bool inGlobalModule) - : base(output, baseIndentation, settings) - { - this.InGlobalModule = inGlobalModule; - } - - public override void AppendOutput(TypeScriptInterface tsInterface) - { - BeginInterface(tsInterface); - - AppendMembers(tsInterface); - - if (tsInterface.IndexedType != null) - AppendIndexer(tsInterface); - - EndInterface(); - } - - private void AppendMembers(TypeScriptInterface tsInterface) - { - var appender = new MemberOutputAppender(Output, BaseIndentation + 4, Settings); - foreach (var member in tsInterface.Members) - appender.AppendOutput(member); - } - - private void BeginInterface(TypeScriptInterface tsInterface) - { - AppendIndentedLine("/** Generated from " + tsInterface.FullName + " **/"); - - if (InGlobalModule) - AppendIndented("interface " + tsInterface.Name); - else - AppendIndented("export interface " + tsInterface.Name); - - if (tsInterface.Parent != null) - Output.Append(" extends " + (tsInterface.Parent.Module.IsGlobal ? "" : tsInterface.Parent.Module.QualifiedName + ".") + tsInterface.Parent.Name); - - Output.AppendLine(" {"); - } - - private void EndInterface() - { - AppendIndentedLine("}"); - } - - private void AppendIndexer(TypeScriptInterface tsInterface) - { - AppendIndendation(); - Output.AppendFormat(" [index: number]: {0};", tsInterface.IndexedType); - Output.AppendLine(); - } - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace T4TS +{ + public class InterfaceOutputAppender : OutputAppender + { + private bool InGlobalModule { get; set; } + + public InterfaceOutputAppender(StringBuilder output, int baseIndentation, Settings settings, bool inGlobalModule) + : base(output, baseIndentation, settings) + { + this.InGlobalModule = inGlobalModule; + } + + public override void AppendOutput(TypeScriptInterface tsInterface) + { + BeginInterface(tsInterface); + + AppendMembers(tsInterface); + + if (tsInterface.IndexedType != null) + AppendIndexer(tsInterface); + + EndInterface(); + } + + private void AppendMembers(TypeScriptInterface tsInterface) + { + var appender = new MemberOutputAppender(Output, BaseIndentation + 4, Settings); + foreach (var member in tsInterface.Members) + appender.AppendOutput(member); + } + + private void BeginInterface(TypeScriptInterface tsInterface) + { + AppendIndentedLine("/** Generated from " + tsInterface.FullName + " **/"); + + //replace NewLine characters, so multiline comments will align nicely, and don't stick to the begining of the lines... + if(!string.IsNullOrWhiteSpace(tsInterface.Comment)) { AppendIndentedLine("/** " + tsInterface.Comment.Replace(Environment.NewLine, Environment.NewLine + new string(' ', BaseIndentation) + "* ") + " */"); } + if(!string.IsNullOrWhiteSpace(tsInterface.DocComment)) { AppendIndentedLine("/** " + tsInterface.DocComment.Replace(Environment.NewLine, Environment.NewLine + new string(' ', BaseIndentation) + "* ") + " */"); } + + if (InGlobalModule) + AppendIndented("interface " + tsInterface.Name); + else + AppendIndented("export interface " + tsInterface.Name); + + if (tsInterface.Parent != null) + Output.Append(" extends " + (tsInterface.Parent.Module.IsGlobal ? "" : tsInterface.Parent.Module.QualifiedName + ".") + tsInterface.Parent.Name); + + Output.AppendLine(" {"); + } + + private void EndInterface() + { + AppendIndentedLine("}"); + } + + private void AppendIndexer(TypeScriptInterface tsInterface) + { + AppendIndendation(); + Output.AppendFormat(" [index: number]: {0};", tsInterface.IndexedType); + Output.AppendLine(); + } + } +} diff --git a/T4TS/Outputs/MemberOutputAppender.cs b/T4TS/Outputs/MemberOutputAppender.cs index a3bcc46..4c7b1cb 100644 --- a/T4TS/Outputs/MemberOutputAppender.cs +++ b/T4TS/Outputs/MemberOutputAppender.cs @@ -15,6 +15,10 @@ public MemberOutputAppender(StringBuilder output, int baseIndentation, Settings public override void AppendOutput(TypeScriptInterfaceMember member) { + //replace NewLine characters, so multiline comments will align nicely, and don't stick to the begining of the lines... + if(!string.IsNullOrWhiteSpace(member.Comment)) { AppendIndentedLine("/** " + member.Comment.Replace(Environment.NewLine, Environment.NewLine + new string(' ', BaseIndentation) + "* ") + " */"); } + if(!string.IsNullOrWhiteSpace(member.DocComment)) { AppendIndentedLine("/** " + member.DocComment.Replace(Environment.NewLine, Environment.NewLine + new string(' ', BaseIndentation) + "* ") + " */"); } + AppendIndendation(); bool isOptional = member.Optional; @@ -26,8 +30,7 @@ public override void AppendOutput(TypeScriptInterfaceMember member) type = "bool"; else type = "boolean"; - } - + } Output.AppendFormat("{0}{1}: {2}", member.Name, (isOptional ? "?" : ""), diff --git a/T4TS/Outputs/TypeScriptInterface.cs b/T4TS/Outputs/TypeScriptInterface.cs index 1269fe5..e801039 100644 --- a/T4TS/Outputs/TypeScriptInterface.cs +++ b/T4TS/Outputs/TypeScriptInterface.cs @@ -10,7 +10,22 @@ namespace T4TS public class TypeScriptInterface { public string Name { get; set; } - public string FullName { get; set; } + public string FullName { get; set; } + + public string Comment { get; set; } + + private string _docComment; + public string DocComment { + get { return _docComment; } + set { + _docComment = value; + if(value == null) { return; } + //strip the ' ' xml tags + if(value.StartsWith("") && value.EndsWith("")) { + _docComment = value.Substring(5, value.Length - 11).Trim(); + } else { _docComment = value; } + } + } public List Members { get; set; } public TypescriptType IndexedType { get; set; } diff --git a/T4TS/Outputs/TypeScriptInterfaceMember.cs b/T4TS/Outputs/TypeScriptInterfaceMember.cs index 81368d1..3c834c8 100644 --- a/T4TS/Outputs/TypeScriptInterfaceMember.cs +++ b/T4TS/Outputs/TypeScriptInterfaceMember.cs @@ -7,7 +7,22 @@ namespace T4TS { public class TypeScriptInterfaceMember { - public string Name { get; set; } + public string Name { get; set; } + + public string Comment { get; set; } + + private string _docComment; + public string DocComment { + get { return _docComment; } + set { + _docComment = value; + if(value == null) { return; } + //strip the ' ' xml tags + if(value.StartsWith("") && value.EndsWith("")) { + _docComment = value.Substring(5, value.Length - 11).Trim(); + } else { _docComment = value; } + } + } public TypescriptType Type { get; set; } public bool Optional { get; set; } //public string FullName { get; set; }