public void EnumerateList()
        {
            var list = new[] { 0, 1, 2 };
            var e    = CircularEnumerator.Create(list);

            e.Should().NotBeNull();
            e.CursorIndex.Should().NotHaveValue();
            e.Values.Should().BeSameAs(list);
            e.Started.Should().BeFalse();

            Action currentItem = () => { var x = e.GetCurrentItem(); };

            currentItem.Should().Throw <InvalidOperationException>();

            e.MovePrevious();
            e.GetCurrentItem().Should().Be(2);

            e.MoveNext();
            e.GetCurrentItem().Should().Be(0);

            e.MoveNext();
            e.GetCurrentItem().Should().Be(1);

            e.MoveNext();
            e.GetCurrentItem().Should().Be(2);

            e.MoveNext();
            e.GetCurrentItem().Should().Be(0);
        }
        private void ReplaceCurrentTokenWithCompletion(bool reverseOrder, bool lastOperationWasCompletion)
        {
            if (TokenCompleter == null)
            {
                return;
            }

            // If we can't pull a completion from the cache, then generate new ones.
            if (!lastOperationWasCompletion || (_lastCompletions == null))
            {
                _lastCompletions      = TokenCompletionSet.Create(Buffer.Contents, Buffer.CursorIndex, TokenCompleter);
                _completionEnumerator = CircularEnumerator.Create(_lastCompletions.Completions);
            }

            // Bail if no completions are available.
            if (_lastCompletions.Empty)
            {
                return;
            }

            // Find the existing token length.
            var existingTokenLength = _completionEnumerator.Started
                ? _completionEnumerator.GetCurrentItem().Length
                : _lastCompletions.OriginalToken.InnerLength;

            // Find the existing token start.
            var existingTokenStart =
                _lastCompletions.OriginalToken.InnerStartingOffset;

            // Select the new completion.
            if (reverseOrder)
            {
                _completionEnumerator.MovePrevious();
            }
            else
            {
                _completionEnumerator.MoveNext();
            }

            // Select the completion.
            var completion = _completionEnumerator.GetCurrentItem();

            // Replace the current token in the buffer with the completion.
            MoveConsoleAndBufferCursors(SeekOrigin.Begin, existingTokenStart);
            Buffer.Remove(existingTokenLength);
            Buffer.Insert(completion);

            // Rewrite the input text.
            SyncBufferToConsole(
                existingTokenStart,
                Buffer.Length - existingTokenStart,
                (existingTokenLength > completion.Length) ? existingTokenLength - completion.Length : 0);

            MoveConsoleAndBufferCursors(SeekOrigin.Current, completion.Length);
        }
Exemple #3
0
        public void EnumerateEmptyList()
        {
            var list = new string[] { };
            var e    = CircularEnumerator.Create(list);

            e.Should().NotBeNull();
            e.CursorIndex.Should().NotHaveValue();
            e.Values.Should().BeSameAs(list);
            e.Started.Should().BeFalse();

            Action currentItem = () => { var x = e.CurrentItem; };

            currentItem.ShouldThrow <IndexOutOfRangeException>();

            e.MoveNext();
            currentItem.ShouldThrow <IndexOutOfRangeException>();

            e.MovePrevious();
            currentItem.ShouldThrow <IndexOutOfRangeException>();
        }