Ejemplo n.º 1
0
        private void UpdateOrShowAutoComplete(bool calledByUser)
        {
            if (IsDisposed)
            {
                return;
            }

            if (_customUnderlines.IsImeStartingComposition)
            {
                return;
            }

            if (_autoCompleteListTask == null || !AppSettings.ProvideAutocompletion)
            {
                return;
            }

            if (!_autoCompleteListTask.IsCompleted)
            {
                if (_autoCompleteListTask.Status == TaskStatus.Created)
                {
                    _autoCompleteListTask.Start();
                }

                if (calledByUser)
                {
                    AutoCompleteToolTip.Show(
                        "AutoComplete is not available yet (it is still parsing the changed files).",
                        TextBox,
                        GetCursorPosition());
                    AutoCompleteToolTipTimer.Start();
                }

                return;
            }

            AutoCompleteToolTipTimer.Stop();
            AutoCompleteToolTip.Hide(TextBox);

            var word = GetWordAtCursor();

            if (word == null || (word.Length <= 1 && !calledByUser && !_autoCompleteWasUserActivated))
            {
                if (AutoComplete.Visible)
                {
                    CloseAutoComplete();
                }

                return;
            }

            var list = _autoCompleteListTask.Result.Where(x => x.Matches(word)).Distinct().ToList();

            if (list.Count == 0)
            {
                if (AutoComplete.Visible)
                {
                    CloseAutoComplete();
                }

                return;
            }

            if (list.Count == 1 && calledByUser)
            {
                AcceptAutoComplete(list[0]);
                return;
            }

            if (calledByUser)
            {
                _autoCompleteWasUserActivated = true;
            }

            var sizes = list.Select(x => TextRenderer.MeasureText(x.Word, TextBox.Font)).ToList();

            var cursorPos = GetCursorPosition();

            var top    = cursorPos.Y;
            var height = (sizes.Count + 1) * AutoComplete.ItemHeight;
            var width  = sizes.Max(x => x.Width);

            if (top + height > TextBox.Height)
            {
                // if reduced height is not too small then shrink only
                if (TextBox.Height - top > TextBox.Height / 2)
                {
                    height = TextBox.Height - top;
                }
                else
                {
                    // if shrinking wasn't acceptable, move higher
                    top = Math.Max(0, TextBox.Height - height);
                    // and reduce height if moving up wasn't enough
                    height = Math.Min(TextBox.Height - top, height);
                }
                width += SystemInformation.VerticalScrollBarWidth;
            }

            AutoComplete.SetBounds(cursorPos.X, top, width, height);

            AutoComplete.DataSource = list.ToList();
            AutoComplete.Show();
            TextBox.Focus();
        }
Ejemplo n.º 2
0
        private void UpdateOrShowAutoComplete(bool calledByUser)
        {
            if (_autoCompleteListTask == null)
            {
                return;
            }

            if (!_autoCompleteListTask.IsCompleted)
            {
                if (_autoCompleteListTask.Status == TaskStatus.Created)
                {
                    _autoCompleteListTask.Start();
                }

                if (calledByUser)
                {
                    AutoCompleteToolTip.Show(
                        "AutoComplete is not available yet (it is still parsing the changed files).",
                        TextBox,
                        GetCursorPosition());
                    AutoCompleteToolTipTimer.Start();
                }

                return;
            }

            AutoCompleteToolTipTimer.Stop();
            AutoCompleteToolTip.Hide(TextBox);

            var word = GetWordAtCursor();

            if (word == null || (word.Length <= 1 && !calledByUser && !_autoCompleteWasUserActivated))
            {
                if (AutoComplete.Visible)
                {
                    CloseAutoComplete();
                }

                return;
            }

            var list = _autoCompleteListTask.Result.Where(x => x.Matches(word)).Distinct().ToList();

            if (list.Count == 0)
            {
                if (AutoComplete.Visible)
                {
                    CloseAutoComplete();
                }

                return;
            }

            if (list.Count == 1 && calledByUser)
            {
                AcceptAutoComplete(list[0]);
                return;
            }

            if (calledByUser)
            {
                _autoCompleteWasUserActivated = true;
            }

            var sizes = list.Select(x => TextRenderer.MeasureText(x.Word, TextBox.Font)).ToList();

            var cursorPos = GetCursorPosition();

            var height = (sizes.Count + 1) * AutoComplete.ItemHeight;
            var width  = sizes.Max(x => x.Width);

            if (cursorPos.Y + height > TextBox.Height)
            {
                height = TextBox.Height - cursorPos.Y;
                width += SystemInformation.VerticalScrollBarWidth;
            }

            AutoComplete.SetBounds(cursorPos.X, cursorPos.Y, width, height);

            AutoComplete.DataSource = list.ToList();
            AutoComplete.Show();
            TextBox.Focus();
        }