forked from PavelTorgashov/FastColoredTextBox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutocompleteSample3.cs
More file actions
101 lines (87 loc) · 3.13 KB
/
Copy pathAutocompleteSample3.cs
File metadata and controls
101 lines (87 loc) · 3.13 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System.Reflection;
using System.Windows.Forms;
using FastColoredTextBoxNS;
using System.Drawing;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace Tester
{
public partial class AutocompleteSample3 : Form
{
AutocompleteMenu popupMenu;
public AutocompleteSample3()
{
InitializeComponent();
//create autocomplete popup menu
popupMenu = new AutocompleteMenu(fctb);
popupMenu.ForeColor = Color.White;
popupMenu.BackColor = Color.Gray;
popupMenu.SelectedColor = Color.Purple;
popupMenu.SearchPattern = @"[\w\.]";
popupMenu.AllowTabKey = true;
//assign DynamicCollection as items source
popupMenu.Items.SetAutocompleteItems(new DynamicCollection(popupMenu, fctb));
}
}
/// <summary>
/// Builds list of methods and properties for current class name was typed in the textbox
/// </summary>
internal class DynamicCollection : IEnumerable<AutocompleteItem>
{
private AutocompleteMenu menu;
private FastColoredTextBox tb;
public DynamicCollection(AutocompleteMenu menu, FastColoredTextBox tb)
{
this.menu = menu;
this.tb = tb;
}
public IEnumerator<AutocompleteItem> GetEnumerator()
{
//get current fragment of the text
var text = menu.Fragment.Text;
//extract class name (part before dot)
var parts = text.Split('.');
if (parts.Length < 2)
yield break;
var className = parts[parts.Length - 2];
//find type for given className
var type = FindTypeByName(className);
if (type == null)
yield break;
//return static methods of the class
foreach (var methodName in type.GetMethods().AsEnumerable().Select(mi => mi.Name).Distinct())
yield return new MethodAutocompleteItem(methodName + "()")
{
ToolTipTitle = methodName,
ToolTipText = "Description of method " + methodName + " goes here.",
};
//return static properties of the class
foreach (var pi in type.GetProperties())
yield return new MethodAutocompleteItem(pi.Name)
{
ToolTipTitle = pi.Name,
ToolTipText = "Description of property " + pi.Name + " goes here.",
};
}
Type FindTypeByName(string name)
{
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
Type type = null;
foreach (var a in assemblies)
{
foreach (var t in a.GetTypes())
if (t.Name == name)
{
return t;
}
}
return null;
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}