Example #1
0
        // Internal for testing
        internal static bool TryCreateIndentationContext(int changePosition, int changeLength, string finalText, VisualStudioDocumentTracker documentTracker, out BraceIndentationContext context)
        {
            var focusedTextView = documentTracker.GetFocusedTextView();

            if (focusedTextView != null && ParserHelpers.IsNewLine(finalText))
            {
                var currentSnapshot       = documentTracker.TextBuffer.CurrentSnapshot;
                var preChangeLineSnapshot = currentSnapshot.GetLineFromPosition(changePosition);

                // Handle the case where the \n comes through separately from the \r and the position
                // on the line is beyond what the GetText call above gives back.
                var linePosition = Math.Min(preChangeLineSnapshot.Length, changePosition - preChangeLineSnapshot.Start) - 1;

                if (AfterOpeningBrace(linePosition, preChangeLineSnapshot))
                {
                    var afterChangePosition     = changePosition + changeLength;
                    var afterChangeLineSnapshot = currentSnapshot.GetLineFromPosition(afterChangePosition);
                    var afterChangeLinePosition = afterChangePosition - afterChangeLineSnapshot.Start;

                    if (BeforeClosingBrace(afterChangeLinePosition, afterChangeLineSnapshot))
                    {
                        context = new BraceIndentationContext(focusedTextView, changePosition);
                        return(true);
                    }
                }
            }

            context = null;
            return(false);
        }
Example #2
0
        // Internal for testing
        internal void TextBuffer_OnChanged(object sender, TextContentChangedEventArgs args)
        {
            _dispatcher.AssertForegroundThread();

            if (!args.TextChangeOccurred(out var changeInformation))
            {
                return;
            }

            var newText = changeInformation.newText;

            if (TryCreateIndentationContext(changeInformation.firstChange.NewPosition, newText.Length, newText, _documentTracker, out var context))
            {
                _context = context;
            }
        }
        private void TextBuffer_OnPostChanged(object sender, EventArgs e)
        {
            _dispatcher.AssertForegroundThread();

            var context = _context;

            _context = null;

            if (context != null)
            {
                // Save the current caret position
                var textView       = context.FocusedTextView;
                var caret          = textView.Caret.Position.BufferPosition;
                var textViewBuffer = textView.TextBuffer;
                var indent         = CalculateIndent(textViewBuffer, context.ChangePosition);

                // Current state, pipe is cursor:
                // @{
                // |}

                // Insert the completion text, i.e. "\r\n      "
                InsertIndent(caret.Position, indent, textViewBuffer);

                // @{
                //
                // |}

                // Place the caret inbetween the braces (before our indent).
                RestoreCaretTo(caret.Position, textView);

                // @{
                // |
                // }

                // For Razor metacode cases the editor's smart indent wont kick in automatically.
                TriggerSmartIndent(textView);

                // @{
                //     |
                // }
            }
        }
        // Internal for testing
        internal void TextBuffer_OnChanged(object sender, TextContentChangedEventArgs args)
        {
            _dispatcher.AssertForegroundThread();

            if (!args.TextChangeOccurred(out var changeInformation))
            {
                return;
            }

            var newText = changeInformation.newText;

            if (!_codeDocumentProvider.TryGetFromBuffer(_documentTracker.TextBuffer, out var codeDocument))
            {
                // Parse not available.
                return;
            }

            var syntaxTree = codeDocument.GetSyntaxTree();

            if (TryCreateIndentationContext(changeInformation.firstChange.NewPosition, newText.Length, newText, syntaxTree, _documentTracker, out var context))
            {
                _context = context;
            }
        }