void ButtonOkClicked(object sender, EventArgs e)
 {
     template.Shortcut    = this.entryShortcut1.Text.Trim();
     template.Group       = this.comboboxentryGroups.Entry.Text;
     template.MimeType    = this.comboboxentryMime.Entry.Text;
     template.Description = this.entryDescription.Text;
     template.Code        = this.textEditor.Document.Text;
     variables.ForEach(v => template.AddVariable(v));
     template.CodeTemplateType = CodeTemplateType.Unknown;
     if (checkbuttonExpansion.Active)
     {
         template.CodeTemplateType |= CodeTemplateType.Expansion;
     }
     if (checkbuttonSurroundWith.Active)
     {
         template.CodeTemplateType |= CodeTemplateType.SurroundsWith;
     }
 }
Exemple #2
0
        static CodeTemplate ConvertToTemplate(TmSnippet setting)
        {
            var result = new CodeTemplate();

            result.Shortcut = setting.TabTrigger;
            var  sb                  = new StringBuilder();
            var  nameBuilder         = new StringBuilder();
            bool readDollar          = false;
            bool inBracketExpression = false;
            bool inExpressionContent = false;
            bool inVariable          = false;
            int  number              = 0;

            foreach (var ch in setting.Content)
            {
                if (inVariable)
                {
                    if (char.IsLetter(ch))
                    {
                        nameBuilder.Append(ch);
                    }
                    else
                    {
                        sb.Append(ConvertVariable(nameBuilder.ToString()));
                        nameBuilder.Length = 0;
                        inVariable         = false;
                    }
                }

                if (ch == '$')
                {
                    readDollar = true;
                    continue;
                }
                if (readDollar)
                {
                    if (ch == '{')
                    {
                        number = 0;
                        inBracketExpression = true;
                        readDollar          = false;
                        continue;
                    }
                    else if (char.IsLetter(ch))
                    {
                        inVariable = true;
                    }
                    else
                    {
                        sb.Append("$$");
                        readDollar = false;
                    }
                }
                if (inBracketExpression)
                {
                    if (ch == ':')
                    {
                        inBracketExpression = false;
                        inExpressionContent = true;
                        continue;
                    }
                    number = number * 10 + (ch - '0');
                    continue;
                }


                if (inExpressionContent)
                {
                    if (ch == '}')
                    {
                        if (number == 0)
                        {
                            sb.Append("$end$");
                            sb.Append(nameBuilder);
                        }
                        else
                        {
                            sb.Append("$" + nameBuilder + "$");
                            result.AddVariable(new CodeTemplateVariable(nameBuilder.ToString())
                            {
                                Default = nameBuilder.ToString(), IsEditable = true
                            });
                        }
                        nameBuilder.Length  = 0;
                        number              = 0;
                        inExpressionContent = false;
                        continue;
                    }
                    nameBuilder.Append(ch);
                    continue;
                }
                sb.Append(ch);
            }
            if (inVariable)
            {
                sb.Append(ConvertVariable(nameBuilder.ToString()));
                nameBuilder.Length = 0;
                inVariable         = false;
            }

            result.Code = sb.ToString();
            result.CodeTemplateContext = CodeTemplateContext.Standard;
            result.CodeTemplateType    = CodeTemplateType.Expansion;
            result.Description         = setting.Name;
            return(result);
        }