public override void VisitUsingDirective(UsingDirectiveSyntax node) { if (node.ChildNodes().Any(x => x is NameEqualsSyntax)) { var nameNode = node.ChildNodes().FirstOrDefault(x => x is NameEqualsSyntax); var alternate = nameNode.ChildNodes().FirstOrDefault().ToString(); var ns = node.ChildNodes().LastOrDefault().ToString(); UsingNamespaces.Add(new NamespaceInfo { Namespace = ns, Alternate = alternate, }); } else { var ns = node.ChildNodes().FirstOrDefault().ToString(); UsingNamespaces.Add(new NamespaceInfo { Namespace = ns, Alternate = string.Empty, }); } base.VisitUsingDirective(node); }
/// <summary> /// Add using namespace. /// </summary> /// <param name="namespace">namespace</param> protected void AddUsingNamespace(string @namespace) { if (UsingNamespaces != null && !UsingNamespaces.Contains(@namespace)) { UsingNamespaces.Add(@namespace); } }
protected string ScopeTo(AST.Namespace ns) { var usingParent = ns; string ret = ""; while (usingParent != WorkingNamespace && !UsingNamespaces.Contains(usingParent) && !usingParent.IsGlobal) { ret = usingParent.Name + ScopeOperator + ret; usingParent = usingParent.Parent; } return(ret); }
public void WriteUsingStatements() { if (UsingNamespaces.Any()) { AppendLineIfNeeded(); foreach (var usingNamespace in UsingNamespaces.OrderBy(usingNamespace => usingNamespace)) { WriteIndentation(); Write($"using {usingNamespace};"); WriteNewLine(); } appendLine = true; } }
public virtual bool Use(AST.Namespace ns, bool abi) { if (!abi && !UsingNamespaces.Contains(ns)) { UsingNamespaces.Add(ns); } else if (abi && !UsingABINamespaces.Contains(ns)) { UsingABINamespaces.Add(ns); } else { return(false); } return(true); }
internal static CompilationUnitSyntax CreateCompilationUnitSyntax(string nameSpace, IReadOnlyCollection <HassState> entities, IReadOnlyCollection <HassServiceDomain> services) { var orderedEntities = entities.OrderBy(x => x.EntityId).ToArray(); var orderedServiceDomains = services.OrderBy(x => x.Domain).ToArray(); var code = CompilationUnit() .AddUsings(UsingDirective(ParseName("System"))) .AddUsings(UsingDirective(ParseName("System.Collections.Generic"))) .AddUsings(UsingNamespaces.OrderBy(s => s).Select(u => UsingDirective(ParseName(u))).ToArray()); var namespaceDeclaration = NamespaceDeclaration(ParseName(nameSpace)).NormalizeWhitespace(); namespaceDeclaration = namespaceDeclaration.AddMembers(EntitiesGenerator.Generate(orderedEntities).ToArray()); namespaceDeclaration = namespaceDeclaration.AddMembers(ServicesGenerator.Generate(orderedServiceDomains).ToArray()); namespaceDeclaration = namespaceDeclaration.AddMembers(ExtensionMethodsGenerator.Generate(orderedServiceDomains, entities).ToArray()); code = code.AddMembers(namespaceDeclaration); code = code.NormalizeWhitespace(Tab.ToString(), eol: "\n"); return(code); }
public SortedDictionary <string, string> GenerateCode() { var codeByType = new Dictionary <Type, string>(); var sb = new StringBuilder(); var cw = new Serenity.Reflection.CodeWriter(sb, 4); foreach (var assembly in Assemblies) { foreach (var type in assembly.GetTypes()) { var formScriptAttribute = (FormScriptAttribute)type.GetCustomAttribute(typeof(FormScriptAttribute)); if (formScriptAttribute == null) { continue; } var ns = DoGetNamespace(type); sb.Clear(); cw.Indented("public partial class "); sb.Append(DoGetTypeName(type)); sb.AppendLine(" : PrefixedContext"); cw.InBrace(delegate { cw.Indented("[InlineConstant] public const string FormKey = \""); sb.Append(formScriptAttribute.Key); sb.AppendLine("\";"); sb.AppendLine(); cw.Indented("public "); sb.Append(DoGetTypeName(type)); sb.AppendLine("(string idPrefix) : base(idPrefix) {}"); sb.AppendLine(); foreach (var item in Serenity.PropertyGrid.PropertyItemHelper.GetPropertyItemsFor(type)) { var editorType = item.EditorType ?? "String"; string widgetTypeName = null; foreach (var rootNamespace in RootNamespaces) { string wn = rootNamespace + "." + editorType; if (WidgetTypes.Contains(wn)) { widgetTypeName = wn; break; } wn += "Editor"; if (WidgetTypes.Contains(wn)) { widgetTypeName = wn; break; } } if (widgetTypeName == null) { var wn = editorType; if (!WidgetTypes.Contains(editorType)) { wn = editorType + "Editor"; } if (WidgetTypes.Contains(wn)) { widgetTypeName = wn; } } if (widgetTypeName == null) { continue; } var fullName = widgetTypeName; if (widgetTypeName.StartsWith(ns + ".")) { widgetTypeName = widgetTypeName.Substring(ns.Length + 1); } else { foreach (var rn in RootNamespaces) { if (widgetTypeName.StartsWith(rn + ".")) { widgetTypeName = widgetTypeName.Substring(rn.Length + 1); } } } cw.Indented("public "); sb.Append(widgetTypeName); sb.Append(" "); sb.Append(item.Name); sb.Append(" { "); sb.Append("[InlineCode(\"{this}.w('"); sb.Append(item.Name); sb.Append("', "); sb.Append(fullName); sb.AppendLine(")\")] get; private set; }"); } }); codeByType[type] = sb.ToString(); sb.Clear(); } } var ordered = codeByType.Keys.OrderBy(x => DoGetNamespace(x)).ThenBy(x => x.Name); var byNameSpace = ordered.ToLookup(x => DoGetNamespace(x)); var result = new SortedDictionary <string, string>(); foreach (var ns in byNameSpace.ToArray().OrderBy(x => x.Key)) { foreach (var type in ns) { sb.Clear(); sb.AppendLine(); cw.Indented("namespace "); sb.AppendLine(ns.Key); cw.InBrace(delegate { foreach (var usingNamespace in UsingNamespaces.ToArray().OrderBy(x => x)) { cw.Indented("using "); sb.Append(usingNamespace); sb.AppendLine(";"); } sb.AppendLine(); int i = 0; { if (i++ > 0) { sb.AppendLine(); } cw.IndentedMultiLine(codeByType[type].TrimEnd()); } }); var filename = ns.Key + "." + DoGetTypeName(type) + ".cs"; foreach (var rn in RootNamespaces) { if (filename.StartsWith(rn + ".")) { filename = filename.Substring(rn.Length + 1); } } result.Add(filename, sb.ToString()); } } return(result); }
public SortedDictionary <string, string> GenerateCode() { this.sb = new StringBuilder(4096); this.cw = new CodeWriter(sb, 4); this.generateQueue = new Queue <Type>(); this.visited = new HashSet <Type>(); foreach (var fromType in this.Assembly.GetTypes()) { if (fromType.IsAbstract) { continue; } if (fromType.IsSubclassOf(typeof(ServiceRequest)) || fromType.IsSubclassOf(typeof(ServiceResponse)) || fromType.IsSubclassOf(typeof(Row)) || fromType.GetCustomAttribute <ScriptIncludeAttribute>() != null) { EnqueueType(fromType); } } Dictionary <Type, string> generatedCode = new Dictionary <Type, string>(); while (generateQueue.Count > 0) { var type = generateQueue.Dequeue(); if (type.Assembly != this.Assembly) { continue; } GenerateCodeFor(type); generatedCode[type] = sb.ToString(); sb.Clear(); } sb.Clear(); sb.AppendLine(); var ordered = generatedCode.Keys.OrderBy(x => GetNamespace(x)).ThenBy(x => x.Name); var byNameSpace = ordered.ToLookup(x => GetNamespace(x)); var byOwnerType = ordered.ToLookup(x => (x.IsNested ? x.DeclaringType : null)); var outputted = new HashSet <Type>(); var result = new SortedDictionary <string, string>(); foreach (var ns in byNameSpace.ToArray().OrderBy(x => x.Key)) { Action <Type> outputType = delegate(Type type) { var filename = ns.Key + "." + MakeFriendlyName(type) + ".cs"; foreach (var rn in RootNamespaces) { if (filename.StartsWith(rn + ".")) { filename = filename.Substring(rn.Length + 1); } } result.Add(filename, sb.ToString()); }; foreach (var owner in byOwnerType) { bool skip = false; sb.Clear(); sb.AppendLine(); cw.Indented("namespace "); sb.AppendLine(ns.Key); cw.InBrace(delegate { foreach (var usingNamespace in UsingNamespaces.OrderBy(x => x)) { cw.Indented("using "); sb.Append(usingNamespace); sb.AppendLine(";"); } sb.AppendLine(); if (owner.Key == null) { skip = true; return; } if (GetNamespace(owner.Key) != ns.Key) { skip = true; return; } if (outputted.Contains(owner.Key)) { skip = true; return; } outputted.Add(owner.Key); if (!generatedCode.ContainsKey(owner.Key)) { return; } string code = generatedCode[owner.Key].TrimEnd(); code = code.Substring(0, code.Length - 1).TrimEnd(); cw.IndentedMultiLine(code); cw.Block(delegate { sb.AppendLine(); foreach (var subType in owner) { cw.IndentedMultiLine(generatedCode[subType]); outputted.Add(subType); } }); cw.IndentedLine("}"); sb.AppendLine(); }); if (skip) { continue; } outputType(owner.Key); } foreach (var type in ns) { if (outputted.Contains(type)) { continue; } sb.Clear(); sb.AppendLine(); cw.Indented("namespace "); sb.AppendLine(ns.Key); cw.InBrace(() => { foreach (var usingNamespace in UsingNamespaces.OrderBy(x => x)) { cw.Indented("using "); sb.Append(usingNamespace); sb.AppendLine(";"); } sb.AppendLine(); cw.IndentedMultiLine(generatedCode[type]); }); outputType(type); outputted.Add(type); } } return(result); }
private List <BaseTypeInfo> GetBaseTypeInfos(IEnumerable <SimpleBaseTypeSyntax> baseTypes, string defaultNamespace) { var candidatesNamespaces = new List <string>(); candidatesNamespaces.Add(defaultNamespace); while (defaultNamespace.Contains(".")) { defaultNamespace = defaultNamespace.Substring(0, defaultNamespace.LastIndexOf(".")); candidatesNamespaces.Add(defaultNamespace); } var firstUsingIndex = -1; if (UsingNamespaces.Any()) { var results = UsingNamespaces.Select(x => x.Namespace).ToList(); candidatesNamespaces.AddRange(results); firstUsingIndex = candidatesNamespaces.IndexOf(results.FirstOrDefault()); } var items = new List <BaseTypeInfo>(); foreach (var baseType in baseTypes) { var hasGlobal = false; var typeName = baseType.ToString(); if (UsingNamespaces.Any()) { foreach (var check in UsingNamespaces) { if (!string.IsNullOrEmpty(check.Alternate) && typeName.Contains($"{check.Alternate}")) { typeName = typeName.Replace(check.Alternate, check.Namespace); } // 名前空間エイリアス修飾子(::演算子)の場合、:: が残る場合は . に置換 if (typeName.Contains("global::")) { hasGlobal = true; typeName = typeName.Replace("global::", string.Empty); } if (typeName.Contains("::")) { typeName = typeName.Replace("::", "."); } } } var candidatesDefineFullNames = new List <string>(); if (firstUsingIndex == -1) { if (typeName.Contains(".")) { candidatesDefineFullNames.Add(typeName); } for (var i = 0; i < candidatesNamespaces.Count; i++) { candidatesDefineFullNames.Add($"{candidatesNamespaces[i]}.{typeName}"); } if (!typeName.Contains(".")) { candidatesDefineFullNames.Add(typeName); } } else { if (typeName.Contains(".")) { candidatesDefineFullNames.Add(typeName); } if (!hasGlobal) { for (var i = 0; i < firstUsingIndex; i++) { candidatesDefineFullNames.Add($"{candidatesNamespaces[i]}.{typeName}"); } } if (!typeName.Contains(".")) { candidatesDefineFullNames.Insert(firstUsingIndex, typeName); } for (var i = firstUsingIndex; i < candidatesNamespaces.Count; i++) { candidatesDefineFullNames.Add($"{candidatesNamespaces[i]}.{typeName}"); } } var genericParts = new List <string>(); // IEnumerable<int>, IEnumerable<IEnumerable<int>>, Dictionary<int, int> // Dictionary<int, Dictionary<int, int>> var node = baseType.ChildNodes().FirstOrDefault(); if (node is GenericNameSyntax) { var listNode = node.ChildNodes().FirstOrDefault(); // TypeArgumentListSyntax var genericTypes = listNode.ChildNodes(); // PredefinedTypeSyntax, GenericNameSyntax, ... foreach (var genericType in genericTypes) { genericParts.Add(genericType.ToString()); } } var startLength = baseType.Span.Start; var endLength = baseType.Span.End; items.Add(new BaseTypeInfo { CandidatesNamespaces = candidatesNamespaces, CandidatesDefineFullNames = candidatesDefineFullNames, BaseType = typeName, DisplayBaseType = RemoveNamespace(typeName), GenericParts = genericParts, SourceFile = SourceFile, StartLength = startLength, EndLength = endLength, }); } return(items); }
public void WriteUsingStatement(string @namespace) { UsingNamespaces.Add(@namespace); }
public string GenerateCode() { var codeByType = new Dictionary <Type, string>(); var sb = new StringBuilder(); var cw = new Serenity.Reflection.CodeWriter(sb, 4); foreach (var type in this.Assembly.GetTypes()) { if (type.GetCustomAttribute(typeof(Serenity.FormScriptAttribute)) == null) { continue; } var ns = DoGetNamespace(type); sb.Clear(); cw.Indented("public partial class "); sb.Append(DoGetTypeName(type)); sb.AppendLine(" : PrefixedContext"); cw.InBrace(delegate { cw.Indented("public "); sb.Append(DoGetTypeName(type)); sb.AppendLine("(string idPrefix) : base(idPrefix) {}"); sb.AppendLine(); foreach (var item in Serenity.Web.PropertyEditor.PropertyEditorHelper.GetPropertyItemsFor(type)) { var editorType = item.EditorType; string widgetTypeName = null; foreach (var rootNamespace in RootNamespaces) { string wn = rootNamespace + "." + editorType; if (WidgetTypes.Contains(wn)) { widgetTypeName = wn; UsingNamespaces.Add(rootNamespace); break; } wn += "Editor"; if (WidgetTypes.Contains(wn)) { widgetTypeName = wn; UsingNamespaces.Add(rootNamespace); break; } } if (widgetTypeName == null) { continue; } if (widgetTypeName.StartsWith(ns + ".")) { widgetTypeName = widgetTypeName.Substring(ns.Length + 1); } else { foreach (var rn in RootNamespaces) { if (widgetTypeName.StartsWith(rn + ".")) { widgetTypeName = widgetTypeName.Substring(rn.Length + 1); } } } cw.Indented("public "); sb.Append(widgetTypeName); sb.Append(" "); sb.Append(item.Name); sb.Append(" { get { return ById<"); sb.Append(widgetTypeName); sb.Append(">(\""); sb.Append(item.Name); sb.AppendLine("\"); } }"); } }); codeByType[type] = sb.ToString(); sb.Clear(); } sb.Clear(); sb.AppendLine(); foreach (var ns in UsingNamespaces) { cw.Indented("using "); sb.Append(ns); sb.AppendLine(";"); } var ordered = codeByType.Keys.OrderBy(x => DoGetNamespace(x)).ThenBy(x => x.Name); var byNameSpace = ordered.ToLookup(x => DoGetNamespace(x)); foreach (var ns in byNameSpace.ToArray().OrderBy(x => x.Key)) { sb.AppendLine(); cw.Indented("namespace "); sb.AppendLine(ns.Key); cw.InBrace(delegate { int i = 0; foreach (var type in ns) { if (i++ > 0) { sb.AppendLine(); } cw.IndentedMultiLine(codeByType[type].TrimEnd()); } }); } return(sb.ToString()); }