Пример #1
0
        /// <summary>
        /// Asserts that after user writes text it will be replaced
        /// </summary>
        /// <param name="baseText">Text, $ tag marks cursor </param>
        /// <param name="userInput">Text to input at cursor ($)</param>
        /// <param name="expectedOutput">Final text with replacements</param>
        public void AssertReplacement(string baseText, string userInput, string expectedOutput)
        {
            (int cursor, string inputText, string span) = TestUtils.PrepareTextWithSpan(baseText, userInput);

            TextManipulator manipulator = new TextManipulator(inputText, cursor);

            var change        = new TextChange(cursor, userInput, cursor, span);
            var manipulations = manipulator.ManipulateText(change);
            var actualOutput  = ApplyManipulations(inputText, manipulations);

            Assert.Equal(expectedOutput, actualOutput);
        }
Пример #2
0
        /// <summary>
        /// Asserts that after user writes text it will be replaced
        /// </summary>
        /// <param name="baseText">Text, $ tag marks cursor </param>
        /// <param name="userInput">Text to input at cursor ($)</param>
        /// <param name="expectedOutput">Final text with replacements</param>
        public void AssertInsertion(string baseText, string userInput, string expectedOutput)
        {
            (int cursor, string text) = TestUtils.PrepareTextWithCursor(baseText);
            var inputText = baseText.Replace("$", userInput);

            TextManipulator manipulator = new TextManipulator(inputText, cursor);

            var change        = new TextChange(cursor, userInput, cursor, "");
            var manipulations = manipulator.ManipulateText(change);
            var actualOutput  = ApplyManipulations(inputText, manipulations);

            Assert.Equal(expectedOutput, actualOutput);
        }
        private async Task ApplyTextManipulations(DidChangeTextDocumentParams request, string text, string buffer, string changedBuffer, int position, int deletedCharacters)
        {
            TextManipulator textManipulator = new TextManipulator(changedBuffer, position);
            var             manipulations   = textManipulator.ManipulateText(new TextChangeAdapter(position, text, buffer.Substring(position, deletedCharacters)));

            if (manipulations.Count == 0)
            {
                return;
            }

            var edits = manipulations.Select(n =>
            {
                var start = Utils.OffsetToPosition(n.Start, changedBuffer);

                switch (n.Type)
                {
                case ManipulationType.Insert:
                    return(new TextEdit()
                    {
                        NewText = n.Text,
                        Range = new Range(start, start)
                    });

                case ManipulationType.Delete:
                    var end = Utils.OffsetToPosition(n.End, changedBuffer);
                    return(new TextEdit()
                    {
                        NewText = "",
                        Range = new Range(start, end)
                    });

                default:
                    throw new NotSupportedException();
                }
            }).ToList();

            if (edits.Count > 0)
            {
                await _router.ApplyWorkspaceEdit(new ApplyWorkspaceEditParams()
                {
                    Edit = new WorkspaceEdit()
                    {
                        DocumentChanges = new Container <WorkspaceEditDocumentChange>(new WorkspaceEditDocumentChange(new TextDocumentEdit()
                        {
                            TextDocument = request.TextDocument,
                            Edits        = new TextEditContainer(edits)
                        }))
                    }
                });
            }
        }
        private void TextBuffer_Changed(object sender, TextContentChangedEventArgs e)
        {
            if (_isChangingText)
            {
                return;
            }

            try
            {
                if (_buffer.Properties.TryGetProperty <XamlBufferMetadata>(typeof(XamlBufferMetadata), out var metadata) &&
                    metadata.CompletionMetadata != null)
                {
                    var sw   = Stopwatch.StartNew();
                    var pos  = _textView.Caret.Position.BufferPosition;
                    var text = _buffer.CurrentSnapshot.GetText();


                    foreach (Microsoft.VisualStudio.Text.ITextChange change in e.Changes.ToList())
                    {
                        var textManipulator = new TextManipulator(text, change.NewPosition);
                        var avaloniaChange  = new TextChangeAdapter(change);
                        var manipulations   = textManipulator.ManipulateText(avaloniaChange);
                        if (manipulations?.Count > 0)
                        {
                            _isChangingText = true;
                            ApplyManipulations(manipulations);
                            Log.Logger.Verbose("XAML manipulation took {Time}", sw.Elapsed);
                        }
                    }
                    sw.Stop();
                }
            }
            finally
            {
                _isChangingText = false;
            }
        }