public static Deferred <T> AutoComplete <T>(this Scintilla editor, int length, bool cancel, IDictionary <string, T> data, bool allItem = true)
        {
            _autoCompleteTimer?.Stop();

            var defer = new Deferred <T>();

            if (data.Count == 0)
            {
                editor.AutoCCancel();
                defer.Reject();
                return(defer);
            }

            _autoCompleteTimer = Redux.Timer.Timeout(100, () =>
            {
                editor.SafeInvoke(delegate
                {
                    var items = data.Keys.Take(MaxAutoCompleteItems).OrderBy(x => x.StartsWith("@") ? "\0" : x).ToArray();
                    var list  = (data.Count <= 20 && allItem && !cancel ? "<All>" + AutoCompleteSeprator : string.Empty) + string.Join(AutoCompleteSeprator.ToString(), items);

                    editor.AutoCShow(length, list);

                    _autoCompleteDeferred = new Deferred <string>();
                    _autoCompleteDeferred.Then(x =>
                    {
                        if (x == "<All>" && allItem && !cancel)
                        {
                            editor.AutoCCancel();
                            editor.AddText(string.Join(",", items.Take(20)));
                            return;
                        }

                        if (data.TryGetValue(x, out T d))
                        {
                            defer.Resolve(d);
                        }
                        if (cancel)
                        {
                            editor.AutoCCancel();
                        }
                    })
                    .Fail(x =>
                    {
                        defer.Reject(x);
                    });
                });
Exemplo n.º 2
0
        private void ShowAutocomplete(Scintilla scintilla, bool all)
        {
            // Find the word start
            string word = scintilla.GetWordFromPosition(scintilla.CurrentPosition)?.Trim();

            if (string.IsNullOrWhiteSpace(word) && !all)
            {
                scintilla.AutoCCancel();
                return;
            }

            var list = Items.Distinct()
                       .Where(s => !string.IsNullOrWhiteSpace(s) && s.Contains(word, StringComparison.CurrentCultureIgnoreCase))
                       .OrderBy(a => a);

            if (!list.Any())
            {
                scintilla.AutoCCancel();
                return;
            }

            // Display the autocompletion list
            scintilla.AutoCShow(word.Length, string.Join(Separator, list.Select(FormatForAutocomplete)));
        }
Exemplo n.º 3
0
        //mxd. Autocompletion handling (https://github.com/jacobslusser/ScintillaNET/wiki/Basic-Autocompletion)
        public virtual bool ShowAutoCompletionList()
        {
            int currentpos   = scriptedit.CurrentPosition;
            int wordstartpos = scriptedit.WordStartPosition(currentpos, true);

            if (wordstartpos >= currentpos)
            {
                // Hide the list
                scriptedit.AutoCCancel();
                return(false);
            }

            // Get entered text
            string start = scriptedit.GetTextRange(wordstartpos, currentpos - wordstartpos);

            if (string.IsNullOrEmpty(start))
            {
                // Hide the list
                scriptedit.AutoCCancel();
                return(false);
            }

            // Don't show Auto-completion list when editing comment, include or string
            switch (scriptcontrol.GetScriptStyle(scriptedit.GetStyleAt(currentpos)))
            {
            case ScriptStyleType.Comment:
            case ScriptStyleType.String:
                // Hide the list
                scriptedit.AutoCCancel();
                return(false);

            case ScriptStyleType.Include:
                // Hide the list unless current word is a keyword
                if (!start.StartsWith("#"))
                {
                    scriptedit.AutoCCancel();
                    return(false);
                }
                break;
            }

            // Filter the list
            List <string> filtered = new List <string>();

            foreach (string s in autocompletelist)
            {
                if (s.IndexOf(start, StringComparison.OrdinalIgnoreCase) != -1)
                {
                    filtered.Add(s);
                }
            }

            // Any matches?
            if (filtered.Count > 0)
            {
                // Show the list
                scriptedit.AutoCShow(currentpos - wordstartpos, string.Join(" ", filtered.ToArray()));
                return(true);
            }

            // Hide the list
            scriptedit.AutoCCancel();
            return(false);
        }