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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Mono.Addins;
using TextEditorSWF.ExtensionModel;
using TextEditorSWF;

[assembly: Addin ("SnippetsAddin","1.0", Namespace="TextEditor")]
[assembly: AddinDependency ("Core", "1.0")]

namespace SnippetsAddin
{
	[Extension]
	public class SnippetsExtension: EditorExtension
	{
		public override void Initialize ()
		{
			Program.MainWindow.Editor.KeyPress += new KeyPressEventHandler (EditorKeyPress);
		}

		void EditorKeyPress (object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar != '\t')
				return;
			RichTextBox editor = Program.MainWindow.Editor;
			int p = editor.SelectionStart - 1;
			string txt = editor.Text;
			while (p >= 0 && char.IsLetterOrDigit (txt[p]))
				p--;
			p++;
			string word = txt.Substring (p, editor.SelectionStart - p);

			foreach (ISnippetProvider provider in AddinManager.GetExtensionObjects <ISnippetProvider>()) {
				string fullText = provider.GetText (word);
				if (fullText != null) {
					int nextp;
					int cursorPos = fullText.IndexOf ("<|>");
					if (cursorPos != -1) {
						fullText = fullText.Remove (cursorPos, 3);
						nextp = p + cursorPos;
					}
					else
						nextp = p + fullText.Length;

					editor.Text = txt.Substring (0, p) + fullText + txt.Substring (editor.SelectionStart);
					editor.SelectionStart = nextp;
					e.Handled = true;
					return;
				}
			}
		}
	}
}