public TemplateCompletionData(CodeTemplate template)
 {
     this.template = template;
 }
 public EditTemplateDialog(CodeTemplate codeTemplate)
 {
     this.codeTemplate = codeTemplate;
     InitializeComponents();
     this.ShowAll();
 }
        /// <remarks>
        /// This method inserts a code template at the current caret position
        /// </remarks>
        public void InsertTemplate(CodeTemplate template)
        {
            int newCaretOffset   = ActiveTextAreaControl.TextArea.Caret.Offset;
            string word = GetWordBeforeCaret().Trim();
            if (word.Length > 0) {
                newCaretOffset = DeleteWordBeforeCaret();
            }
            int finalCaretOffset = newCaretOffset;
            int firstLine        = Document.GetLineNumberForOffset(newCaretOffset);

            // save old properties, these properties cause strange effects, when not
            // be turned off (like insert curly braces or other formatting stuff)
            bool save1         = TextEditorProperties.AutoInsertCurlyBracket;
            IndentStyle save2  = TextEditorProperties.IndentStyle;
            TextEditorProperties.AutoInsertCurlyBracket = false;
            TextEditorProperties.IndentStyle            = IndentStyle.Auto;

            BeginUpdate();
            for (int i =0; i < template.Text.Length; ++i) {
                switch (template.Text[i]) {
                    case '|':
                        finalCaretOffset = newCaretOffset;
                        break;
                    case '\r':
                        break;
                    case '\t':
                        new Tab().Execute(ActiveTextAreaControl.TextArea);
                    break;
                    case '\n':
                        ActiveTextAreaControl.TextArea.Caret.Position = Document.OffsetToPosition(newCaretOffset);
                        new Return().Execute(ActiveTextAreaControl.TextArea);
                        newCaretOffset = ActiveTextAreaControl.TextArea.Caret.Offset;
                        break;
                    default:
                        ActiveTextAreaControl.TextArea.InsertChar(template.Text[i]);
                        newCaretOffset = ActiveTextAreaControl.TextArea.Caret.Offset;
                        break;
                }
            }
            EndUpdate();
            Document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.LinesBetween, firstLine, Document.GetLineNumberForOffset(newCaretOffset)));
            Document.CommitUpdate();
            ActiveTextAreaControl.TextArea.Caret.Position = Document.OffsetToPosition(finalCaretOffset);

            // restore old property settings
            TextEditorProperties.AutoInsertCurlyBracket = save1;
            TextEditorProperties.IndentStyle            = save2;
        }
            void AddEvent(object sender, System.EventArgs e)
            {
                CodeTemplate newTemplate = new CodeTemplate();
                using (EditTemplateDialog etd = new EditTemplateDialog(newTemplate)) {
                    if (etd.Run() == (int) Gtk.ResponseType.Ok) {
                        CurrentTemplateGroup.Templates.Add(newTemplate);
                        templateListView.Selection.UnselectAll();
                        BuildListView();

                        // select the newly added last one
                        TreeIter nextIter;
                        int maxIndex = templateListViewStore.IterNChildren() - 1;
                        if(templateListViewStore.GetIterFromString(out nextIter, (maxIndex).ToString())) {
                            templateListView.Selection.SelectIter(nextIter);
                        }
                    }
                }
            }
        public void InsertTemplate(CodeTemplate template)
        {
            TextIter iter = buf.GetIterAtMark (buf.InsertMark);
            int newCaretOffset = iter.Offset;
            string word = GetWordBeforeCaret ().Trim ();
            int beginLine = iter.Line;
            int endLine = beginLine;
            if (word.Length > 0)
                newCaretOffset = DeleteWordBeforeCaret ();

            string leadingWhiteSpace = GetLeadingWhiteSpace (beginLine);

            int finalCaretOffset = newCaretOffset;

            for (int i =0; i < template.Text.Length; ++i) {
                switch (template.Text[i]) {
                    case '|':
                        finalCaretOffset = newCaretOffset;
                        break;
                    case '\r':
                        break;
                    case '\t':
                        buf.InsertAtCursor ('\t'.ToString ());
                        newCaretOffset++;
                        break;
                    case '\n':
                        buf.InsertAtCursor ('\n'.ToString ());
                        newCaretOffset++;
                        endLine++;
                        break;
                    default:
                        buf.InsertAtCursor (template.Text[i].ToString ());
                        newCaretOffset++;
                        break;
                }
            }

            if (endLine > beginLine) {
                IndentLines (beginLine+1, endLine, leadingWhiteSpace);
            }

            buf.PlaceCursor (buf.GetIterAtOffset (finalCaretOffset));
        }