Beispiel #1
0
        /// Converts the XML object into C# and adds it to the string builder
        public override void ToCSharp(StringBuilder stringBuilder, int indentationLevel, TemplateSettings settings)
        {
            //Signature
            stringBuilder.AppendIndentations(indentationLevel);
            TemplateBuilder.BeginColorBlock(stringBuilder, settings, TemplateSettings.ACCESSIBILITY_KEYWORD_COLOR);
            stringBuilder.Append(accessibility);
            TemplateBuilder.EndColorBlock(stringBuilder, settings, TemplateSettings.ACCESSIBILITY_KEYWORD_COLOR);
            stringBuilder.AppendSpace();
            TemplateBuilder.BeginColorBlock(stringBuilder, settings, TemplateSettings.SYSTEM_KEYWORD_COLOR);
            stringBuilder.Append(kind);
            stringBuilder.AppendSpace();
            stringBuilder.AppendIf("static ", isStatic);
            stringBuilder.AppendIf("partial", isPartial);
            stringBuilder.AppendIf("abstract ", isAbstract);
            stringBuilder.AppendIf("sealed ", isSealed);
            TemplateBuilder.EndColorBlock(stringBuilder, settings, TemplateSettings.SYSTEM_KEYWORD_COLOR);

            TemplateBuilder.BeginColorBlock(stringBuilder, settings, TemplateSettings.TYPE_COLOR);
            stringBuilder.Append(id);
            TemplateBuilder.EndColorBlock(stringBuilder, settings, TemplateSettings.TYPE_COLOR);

            BuildBaseType(stringBuilder, settings);
            stringBuilder.Append("\n");

            //Body start
            stringBuilder.AppendIndentations(indentationLevel);
            stringBuilder.Append("{\n");

            //Body
            XmlTemplate.ChildrenToCSharp(stringBuilder, indentationLevel + 1, settings, children);

            //Body end
            stringBuilder.Append("\n");
            stringBuilder.AppendIndentations(indentationLevel);
            stringBuilder.Append("}");
        }
        private void CreateFileFromTemplate()
        {
            templateSettings.enableSyntaxHighlighting = false;

            //Create the file
            string sourceCode = TemplateBuilder.BuildCodeFromTemplate(template, templateSettings);
            string filePath   = templateCreateFilePath;

            File.WriteAllText(filePath, sourceCode);

            //Open the file
            AssetDatabase.Refresh();
            Object createdFile = AssetDatabase.LoadAssetAtPath <Object>(filePath);

            Selection.activeObject = createdFile;

            if (templateSettings.openFileOnCreate)
            {
                AssetDatabase.OpenAsset(createdFile);
            }

            //Close this window
            Close();
        }
Beispiel #3
0
        /// Converts the XML object into C# and adds it to the string builder
        public override void ToCSharp(StringBuilder stringBuilder, int indentationLevel, TemplateSettings settings)
        {
            //Signature
            stringBuilder.AppendIndentations(indentationLevel);
            TemplateBuilder.BeginColorBlock(stringBuilder, settings, TemplateSettings.ACCESSIBILITY_KEYWORD_COLOR);
            stringBuilder.Append(accessibility);
            TemplateBuilder.EndColorBlock(stringBuilder, settings, TemplateSettings.ACCESSIBILITY_KEYWORD_COLOR);
            stringBuilder.AppendSpace();
            TemplateBuilder.BeginColorBlock(stringBuilder, settings, TemplateSettings.SYSTEM_KEYWORD_COLOR);
            stringBuilder.AppendIf("static ", isStatic);
            stringBuilder.AppendIf("sealed ", isSealed);

            if (virtuality != Virtuality.none)
            {
                stringBuilder.Append(virtuality.ToString().Replace("@", null));
                stringBuilder.AppendSpace();
            }
            TemplateBuilder.EndColorBlock(stringBuilder, settings, TemplateSettings.SYSTEM_KEYWORD_COLOR);

            TemplateBuilder.BeginColorBlock(stringBuilder, settings, TemplateSettings.TYPE_COLOR);
            stringBuilder.Append(returnType ?? "void");
            TemplateBuilder.EndColorBlock(stringBuilder, settings, TemplateSettings.TYPE_COLOR);
            stringBuilder.AppendSpace();
            stringBuilder.Append(id);

            //Arguments
            stringBuilder.Append("(");

            for (int i = 0; i < arguments.Count; ++i)
            {
                XmlArgument argument = arguments[i];

                TemplateBuilder.BeginColorBlock(stringBuilder, settings, TemplateSettings.TYPE_COLOR);
                stringBuilder.Append(argument.type ?? "?");
                TemplateBuilder.EndColorBlock(stringBuilder, settings, TemplateSettings.TYPE_COLOR);
                stringBuilder.AppendSpace();
                stringBuilder.Append(argument.id ?? "?");

                if (i < arguments.Count - 1)
                {
                    stringBuilder.Append(", ");
                }
            }

            stringBuilder.Append(")");


            //Body
            if (virtuality == Virtuality.@abstract)
            {
                stringBuilder.Append(";\n");
            }
            else
            {
                stringBuilder.Append("\n");
                stringBuilder.AppendIndentations(indentationLevel);
                stringBuilder.Append("{\n");

                base.ToCSharp(stringBuilder, indentationLevel + 1, settings);
                stringBuilder.Append("\n");

                stringBuilder.AppendIndentations(indentationLevel);
                stringBuilder.Append("}");
            }
        }
Beispiel #4
0
 public override void ToCSharp(StringBuilder stringBuilder, int indentationLevel, TemplateSettings settings)
 {
     TemplateBuilder.BeginColorBlock(stringBuilder, settings, textColor);
     stringBuilder.AppendWithIndentation(body, indentationLevel);
     TemplateBuilder.EndColorBlock(stringBuilder, settings, textColor);
 }
        /// Draws the UI of the template builder window
        private void OnGUI()
        {
            RebuildTemplateListIfNecessary();
            ApplyMinimumsToWindow();

            EditorGUIUtility.labelWidth = 100.0f;
            sharedWindowPosition        = position;

            //Update creation path
            if (!string.IsNullOrEmpty(templateCreateFolderPath))
            {
                lastKnownCreationPath = templateCreateFolderPath;
            }

            //Update the preview
            if (codePreview == null && template != null)
            {
                codePreview = TemplateBuilder.BuildCodeFromTemplate(template, templateSettings);
                codePreview = codePreview.Replace("\t", "    ");                 // Unity doesn't render tabs very well in the editor
            }

            if (codePreviewStyle == null)
            {
                codePreviewStyle           = new GUIStyle("box");
                codePreviewStyle.wordWrap  = false;
                codePreviewStyle.alignment = TextAnchor.UpperLeft;
                codePreviewStyle.padding   = new RectOffset(6, 6, 6, 6);
                codePreviewStyle.richText  = true;

                codePreviewStyle.normal.textColor          =
                    codePreviewStyle.active.textColor      =
                        codePreviewStyle.focused.textColor =
                            Color.white;

                codePreviewStyle.normal.background          =
                    codePreviewStyle.active.background      =
                        codePreviewStyle.focused.background =
                            AssetDatabase.LoadAssetAtPath <Texture2D>("Assets/Fright/Editor/Skins/CodePreviewBackground.png");

                Font firaFont = AssetDatabase.LoadAssetAtPath <Font>("Assets/Fright/Editor/Skins/FiraCode-Regular.ttf");

                if (firaFont)
                {
                    codePreviewStyle.font     = firaFont;
                    codePreviewStyle.fontSize = 12;
                }
            }

            //Drawing
            EditorGUI.BeginChangeCheck();
            {
                EditorGUILayout.BeginHorizontal();
                {
                    //Template Settings
                    EditorGUILayout.BeginVertical(GUILayout.Width(SETTINGS_PANEL_WIDTH), GUILayout.ExpandHeight(true));
                    {
                        GUI.enabled = !EditorApplication.isCompiling;
                        {
                            DrawTemplateSettings();
                        }
                        GUI.enabled = true;
                    }
                    EditorGUILayout.EndVertical();

                    //Template Preview
                    EditorGUILayout.BeginVertical(GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
                    {
                        DrawTemplatePreview();
                    }
                    EditorGUILayout.EndVertical();
                }
                EditorGUILayout.EndHorizontal();

                DrawToolbar();
            }
            if (EditorGUI.EndChangeCheck())
            {
                codePreview = null;

                //Apply any peristent template settings
                if (template != null && templateSettings != null)
                {
                    templateSettings.SavePersistentSettings(template);
                }
            }
        }
        /// Converts the XML object into C# and adds it to the string builder
        public override void ToCSharp(StringBuilder stringBuilder, int indentationLevel, TemplateSettings settings)
        {
            stringBuilder.AppendIndentations(indentationLevel);

            //Accessibility
            TemplateBuilder.BeginColorBlock(stringBuilder, settings, TemplateSettings.ACCESSIBILITY_KEYWORD_COLOR);
            stringBuilder.Append(accessibility);
            TemplateBuilder.EndColorBlock(stringBuilder, settings, TemplateSettings.ACCESSIBILITY_KEYWORD_COLOR);

            //Static and Virtuality
            stringBuilder.AppendSpace();
            TemplateBuilder.BeginColorBlock(stringBuilder, settings, TemplateSettings.SYSTEM_KEYWORD_COLOR);
            stringBuilder.AppendIf("static ", isStatic);
            stringBuilder.AppendIf(virtuality.ToString() + " ", virtuality != Virtuality.none);
            TemplateBuilder.EndColorBlock(stringBuilder, settings, TemplateSettings.SYSTEM_KEYWORD_COLOR);

            //Type
            TemplateBuilder.BeginColorBlock(stringBuilder, settings, TemplateSettings.TYPE_COLOR);
            stringBuilder.Append(type ?? "?");
            TemplateBuilder.EndColorBlock(stringBuilder, settings, TemplateSettings.TYPE_COLOR);

            //Name
            stringBuilder.AppendSpace();
            stringBuilder.Append(id);

            //Children
            if (children.Count > 0)
            {
                stringBuilder.Append("\n");
                stringBuilder.AppendIndentations(indentationLevel);
                stringBuilder.Append("{\n");
                base.ToCSharp(stringBuilder, indentationLevel + 1, settings);
                stringBuilder.Append("\n");
                stringBuilder.AppendIndentations(indentationLevel);
                stringBuilder.Append("}");
            }
            else
            {
                //Default
                if (!string.IsNullOrEmpty(defaultValue))
                {
                    stringBuilder.Append(" = ");

                    if (type.Equals("string", System.StringComparison.InvariantCultureIgnoreCase))
                    {
                        stringBuilder.Append("\"");
                        stringBuilder.Append(defaultValue);
                        stringBuilder.Append("\"");
                    }
                    else
                    {
                        stringBuilder.Append(defaultValue);
                    }
                }
                //Lambda
                else if (!string.IsNullOrEmpty(lambdaValue))
                {
                    stringBuilder.Append(" => ");

                    if (type.Equals("string", System.StringComparison.InvariantCultureIgnoreCase))
                    {
                        stringBuilder.Append("\"");
                        stringBuilder.Append(lambdaValue);
                        stringBuilder.Append("\"");
                    }
                    else
                    {
                        stringBuilder.Append(lambdaValue);
                    }
                }

                stringBuilder.Append(';');
            }
        }