Пример #1
0
 static FarItem NewItem(string text, EventHandler<MenuEventArgs> click)
 {
     SetItem item = new SetItem() { Text = text };
     if (click == null)
         item.IsSeparator = true;
     else
         item.Click = click;
     return item;
 }
Пример #2
0
        public static void ExpandText(ILine editLine, int replacementIndex, int replacementLength, IList words)
        {
            bool isEmpty = words.Count == 0;
            var text = editLine.Text;
            var last = replacementIndex + replacementLength - 1;
            bool custom = last > 0 && last < text.Length && text[last] == '='; //_140112_150217 last can be out of range

            // select a word
            string word;
            if (words.Count == 1)
            {
                // 1 word
                if (words[0] == null)
                    return;

                word = TECompletionText(words[0]);
            }
            else
            {
                // make menu
                IListMenu menu = Far.Api.CreateListMenu();
                var cursor = Far.Api.UI.WindowCursor;
                menu.X = cursor.X;
                menu.Y = cursor.Y;
                Settings.Default.PopupMenu(menu);
                if (isEmpty)
                {
                    menu.Add(Res.Empty).Disabled = true;
                    menu.NoInfo = true;
                    menu.Show();
                    return;
                }

                menu.Incremental = "*";
                menu.IncrementalOptions = PatternOptions.Substring;

                foreach (var it in words)
                {
                    if (it == null) continue;
                    var item = new SetItem();
                    item.Text = TEListItemText(it);
                    item.Data = it;
                    menu.Items.Add(item);
                }

                if (menu.Items.Count == 0)
                    return;

                if (menu.Items.Count == 1)
                {
                    word = TECompletionText(menu.Items[0].Data);
                }
                else
                {
                    // show menu
                    if (!menu.Show())
                        return;
                    word = TECompletionText(menu.Items[menu.Selected].Data);
                }
            }

            // replace

            // head before replaced part
            string head = text.Substring(0, replacementIndex);

            // custom pattern
            int index, caret;
            if (custom && (index = word.IndexOf('#')) >= 0)
            {
                word = word.Substring(0, index) + word.Substring(index + 1);
                caret = head.Length + index;
            }
            // standard
            else
            {
                caret = head.Length + word.Length;
            }

            // set new text = old head + expanded + old tail
            editLine.Text = head + word + text.Substring(replacementIndex + replacementLength);

            // set caret
            editLine.Caret = caret;
        }