Exemple #1
0
        public static void GenerateTemplate(string pattern, TextArea textArea, CodeTemplateManager templateManager, bool withPatternLength = true, Func <string, string> PostAction = null)
        {
            try
            {
                StringBuilder sb  = new StringBuilder();
                IDocument     doc = textArea.Document;

                if (textArea.SelectionManager.HasSomethingSelected) // удаление выделенного
                {
                    var isel = textArea.SelectionManager.SelectionCollection[0];
                    textArea.Caret.Line   = isel.StartPosition.Line;
                    textArea.Caret.Column = isel.StartPosition.Column;
                    textArea.SelectionManager.RemoveSelectedText();
                }

                int    line = textArea.Caret.Line;
                int    col  = textArea.Caret.Column;
                string name = templateManager.GetTemplateHeader(pattern);
                if (name == null)
                {
                    return;
                }
                string templ = templateManager.GetTemplate(name);
                int    ind   = withPatternLength ? pattern.Length : 0;
                int    cline;
                int    ccol;
                find_cursor_pos(templ, out cline, out ccol);
                sb.Append(templ);
                int cur_ind = templ.IndexOf('|');
                if (cur_ind != -1)
                {
                    sb = sb.Remove(cur_ind, 1);
                }
                sb    = sb.Replace("<filename>", Path.GetFileNameWithoutExtension(textArea.MotherTextEditorControl.FileName));
                templ = sb.ToString();
                int i = 0;
                i = templ.IndexOf('\n', i);
                while (i != -1)
                {
                    if (i + 1 < sb.Length)
                    {
                        sb.Insert(i + 1, " ", col - ind);
                    }
                    i = sb.ToString().IndexOf('\n', i + 1);
                }
                TextLocation tl_beg = new TextLocation(col - ind, line);
                int          offset = doc.PositionToOffset(tl_beg);
                doc.Replace(offset, ind, "");
                doc.CommitUpdate();
                textArea.Caret.Column = col - ind;
                var sbs = sb.ToString();
                if (PostAction != null)
                {
                    sbs = PostAction(sbs);
                }
                textArea.InsertString(sbs);
                textArea.Caret.Line   = line + cline;
                textArea.Caret.Column = col - ind + ccol;
            }
            catch (Exception e)
            {
            }
        }