~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/mono-addins/Samples/TextEditorSWF/SnippetsAddin/SnippetsAddin.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using System;
 
2
using System.Collections.Generic;
 
3
using System.Linq;
 
4
using System.Text;
 
5
using System.Windows.Forms;
 
6
using Mono.Addins;
 
7
using TextEditorSWF.ExtensionModel;
 
8
using TextEditorSWF;
 
9
 
 
10
[assembly: Addin ("SnippetsAddin","1.0", Namespace="TextEditor")]
 
11
[assembly: AddinDependency ("Core", "1.0")]
 
12
 
 
13
namespace SnippetsAddin
 
14
{
 
15
        [Extension]
 
16
        public class SnippetsExtension: EditorExtension
 
17
        {
 
18
                public override void Initialize ()
 
19
                {
 
20
                        Program.MainWindow.Editor.KeyPress += new KeyPressEventHandler (EditorKeyPress);
 
21
                }
 
22
 
 
23
                void EditorKeyPress (object sender, KeyPressEventArgs e)
 
24
                {
 
25
                        if (e.KeyChar != '\t')
 
26
                                return;
 
27
                        RichTextBox editor = Program.MainWindow.Editor;
 
28
                        int p = editor.SelectionStart - 1;
 
29
                        string txt = editor.Text;
 
30
                        while (p >= 0 && char.IsLetterOrDigit (txt[p]))
 
31
                                p--;
 
32
                        p++;
 
33
                        string word = txt.Substring (p, editor.SelectionStart - p);
 
34
 
 
35
                        foreach (ISnippetProvider provider in AddinManager.GetExtensionObjects <ISnippetProvider>()) {
 
36
                                string fullText = provider.GetText (word);
 
37
                                if (fullText != null) {
 
38
                                        int nextp;
 
39
                                        int cursorPos = fullText.IndexOf ("<|>");
 
40
                                        if (cursorPos != -1) {
 
41
                                                fullText = fullText.Remove (cursorPos, 3);
 
42
                                                nextp = p + cursorPos;
 
43
                                        }
 
44
                                        else
 
45
                                                nextp = p + fullText.Length;
 
46
 
 
47
                                        editor.Text = txt.Substring (0, p) + fullText + txt.Substring (editor.SelectionStart);
 
48
                                        editor.SelectionStart = nextp;
 
49
                                        e.Handled = true;
 
50
                                        return;
 
51
                                }
 
52
                        }
 
53
                }
 
54
        }
 
55
}