|
| 1 | +using System; |
| 2 | +using System.Drawing.Printing; |
| 3 | + |
| 4 | +namespace FastColoredTextBoxNS |
| 5 | +{ |
| 6 | + /// <summary> |
| 7 | + /// Item of autocomplete menu |
| 8 | + /// </summary> |
| 9 | + public class AutocompleteItem |
| 10 | + { |
| 11 | + public string Text; |
| 12 | + public int ImageIndex = -1; |
| 13 | + public object Tag; |
| 14 | + string toolTipTitle; |
| 15 | + string toolTipText; |
| 16 | + string menuText; |
| 17 | + public AutocompleteMenu Parent { get; internal set; } |
| 18 | + |
| 19 | + |
| 20 | + public AutocompleteItem() |
| 21 | + { |
| 22 | + } |
| 23 | + |
| 24 | + public AutocompleteItem(string text) |
| 25 | + { |
| 26 | + Text = text; |
| 27 | + } |
| 28 | + |
| 29 | + public AutocompleteItem(string text, int imageIndex) |
| 30 | + : this(text) |
| 31 | + { |
| 32 | + this.ImageIndex = imageIndex; |
| 33 | + } |
| 34 | + |
| 35 | + public AutocompleteItem(string text, int imageIndex, string menuText) |
| 36 | + : this(text, imageIndex) |
| 37 | + { |
| 38 | + this.menuText = menuText; |
| 39 | + } |
| 40 | + |
| 41 | + public AutocompleteItem(string text, int imageIndex, string menuText, string toolTipTitle, string toolTipText) |
| 42 | + : this(text, imageIndex, menuText) |
| 43 | + { |
| 44 | + this.toolTipTitle = toolTipTitle; |
| 45 | + this.toolTipText = toolTipText; |
| 46 | + } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Returns text for inserting into Textbox |
| 50 | + /// </summary> |
| 51 | + public virtual string GetTextForReplace() |
| 52 | + { |
| 53 | + return Text; |
| 54 | + } |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Compares fragment text with this item |
| 58 | + /// </summary> |
| 59 | + public virtual CompareResult Compare(string fragmentText) |
| 60 | + { |
| 61 | + if (Text.StartsWith(fragmentText, StringComparison.InvariantCultureIgnoreCase) && |
| 62 | + Text != fragmentText) |
| 63 | + return CompareResult.VisibleAndSelected; |
| 64 | + |
| 65 | + return CompareResult.Hidden; |
| 66 | + } |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// Returns text for display into popup menu |
| 70 | + /// </summary> |
| 71 | + public override string ToString() |
| 72 | + { |
| 73 | + return menuText ?? Text; |
| 74 | + } |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// This method is called after item inserted into text |
| 78 | + /// </summary> |
| 79 | + public virtual void OnSelected(AutocompleteMenu popupMenu, SelectedEventArgs e) |
| 80 | + { |
| 81 | + ; |
| 82 | + } |
| 83 | + |
| 84 | + /// <summary> |
| 85 | + /// Title for tooltip. |
| 86 | + /// </summary> |
| 87 | + /// <remarks>Return null for disable tooltip for this item</remarks> |
| 88 | + public virtual string ToolTipTitle |
| 89 | + { |
| 90 | + get { return toolTipTitle; } |
| 91 | + set { toolTipTitle = value; } |
| 92 | + } |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// Tooltip text. |
| 96 | + /// </summary> |
| 97 | + /// <remarks>For display tooltip text, ToolTipTitle must be not null</remarks> |
| 98 | + public virtual string ToolTipText |
| 99 | + { |
| 100 | + get{ return toolTipText; } |
| 101 | + set { toolTipText = value; } |
| 102 | + } |
| 103 | + |
| 104 | + /// <summary> |
| 105 | + /// Menu text. This text is displayed in the drop-down menu. |
| 106 | + /// </summary> |
| 107 | + public virtual string MenuText |
| 108 | + { |
| 109 | + get { return menuText; } |
| 110 | + set { menuText = value; } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + public enum CompareResult |
| 115 | + { |
| 116 | + /// <summary> |
| 117 | + /// Item do not appears |
| 118 | + /// </summary> |
| 119 | + Hidden, |
| 120 | + /// <summary> |
| 121 | + /// Item appears |
| 122 | + /// </summary> |
| 123 | + Visible, |
| 124 | + /// <summary> |
| 125 | + /// Item appears and will selected |
| 126 | + /// </summary> |
| 127 | + VisibleAndSelected |
| 128 | + } |
| 129 | + |
| 130 | + /// <summary> |
| 131 | + /// Autocomplete item for code snippets |
| 132 | + /// </summary> |
| 133 | + /// <remarks>Snippet can contain special char ^ for caret position.</remarks> |
| 134 | + public class SnippetAutocompleteItem : AutocompleteItem |
| 135 | + { |
| 136 | + public SnippetAutocompleteItem(string snippet) |
| 137 | + { |
| 138 | + Text = snippet.Replace("\r", ""); |
| 139 | + ToolTipTitle = "Code snippet:"; |
| 140 | + ToolTipText = Text; |
| 141 | + } |
| 142 | + |
| 143 | + public override string ToString() |
| 144 | + { |
| 145 | + return MenuText ?? Text.Replace("\n", " ").Replace("^", ""); |
| 146 | + } |
| 147 | + |
| 148 | + public override string GetTextForReplace() |
| 149 | + { |
| 150 | + return Text; |
| 151 | + } |
| 152 | + |
| 153 | + public override void OnSelected(AutocompleteMenu popupMenu, SelectedEventArgs e) |
| 154 | + { |
| 155 | + e.Tb.BeginUpdate(); |
| 156 | + e.Tb.Selection.BeginUpdate(); |
| 157 | + //remember places |
| 158 | + var p1 = popupMenu.Fragment.Start; |
| 159 | + var p2 = e.Tb.Selection.Start; |
| 160 | + //do auto indent |
| 161 | + if (e.Tb.AutoIndent) |
| 162 | + { |
| 163 | + for (int iLine = p1.iLine + 1; iLine <= p2.iLine; iLine++) |
| 164 | + { |
| 165 | + e.Tb.Selection.Start = new Place(0, iLine); |
| 166 | + e.Tb.DoAutoIndent(iLine); |
| 167 | + } |
| 168 | + } |
| 169 | + e.Tb.Selection.Start = p1; |
| 170 | + //move caret position right and find char ^ |
| 171 | + while (e.Tb.Selection.CharBeforeStart != '^') |
| 172 | + if (!e.Tb.Selection.GoRightThroughFolded()) |
| 173 | + break; |
| 174 | + //remove char ^ |
| 175 | + e.Tb.Selection.GoLeft(true); |
| 176 | + e.Tb.InsertText(""); |
| 177 | + // |
| 178 | + e.Tb.Selection.EndUpdate(); |
| 179 | + e.Tb.EndUpdate(); |
| 180 | + } |
| 181 | + |
| 182 | + /// <summary> |
| 183 | + /// Compares fragment text with this item |
| 184 | + /// </summary> |
| 185 | + public override CompareResult Compare(string fragmentText) |
| 186 | + { |
| 187 | + if (Text.StartsWith(fragmentText, StringComparison.InvariantCultureIgnoreCase) && |
| 188 | + Text != fragmentText) |
| 189 | + return CompareResult.Visible; |
| 190 | + |
| 191 | + return CompareResult.Hidden; |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + /// <summary> |
| 196 | + /// This autocomplete item appears after dot |
| 197 | + /// </summary> |
| 198 | + public class MethodAutocompleteItem : AutocompleteItem |
| 199 | + { |
| 200 | + string firstPart; |
| 201 | + string lowercaseText; |
| 202 | + |
| 203 | + public MethodAutocompleteItem(string text) |
| 204 | + : base(text) |
| 205 | + { |
| 206 | + lowercaseText = Text.ToLower(); |
| 207 | + } |
| 208 | + |
| 209 | + public override CompareResult Compare(string fragmentText) |
| 210 | + { |
| 211 | + int i = fragmentText.LastIndexOf('.'); |
| 212 | + if (i < 0) |
| 213 | + return CompareResult.Hidden; |
| 214 | + string lastPart = fragmentText.Substring(i + 1); |
| 215 | + firstPart = fragmentText.Substring(0, i); |
| 216 | + |
| 217 | + if(lastPart=="") return CompareResult.Visible; |
| 218 | + if(Text.StartsWith(lastPart, StringComparison.InvariantCultureIgnoreCase)) |
| 219 | + return CompareResult.VisibleAndSelected; |
| 220 | + if(lowercaseText.Contains(lastPart.ToLower())) |
| 221 | + return CompareResult.Visible; |
| 222 | + |
| 223 | + return CompareResult.Hidden; |
| 224 | + } |
| 225 | + |
| 226 | + public override string GetTextForReplace() |
| 227 | + { |
| 228 | + return firstPart + "." + Text; |
| 229 | + } |
| 230 | + } |
| 231 | +} |
0 commit comments