Ejemplo n.º 1
0
        private ITextView TestAutoFormat(int position, string initialContent = "")
        {
            var editorView = TextViewTest.MakeTextView(initialContent, position, out AstRoot ast);
            var textView   = editorView.As <ITextView>();
            var af         = new AutoFormat(textView, _services);

            textView.TextBuffer.Changed += (s, e) => {
                var tc = e.ToTextChange();
                ast.ReflectTextChange(tc.Start, tc.OldLength, tc.NewLength, tc.NewTextProvider);

                if (e.Changes[0].NewText.Length == 1)
                {
                    var ch = e.Changes[0].NewText[0];
                    if (af.IsPostProcessAutoformatTriggerCharacter(ch))
                    {
                        position = e.Changes[0].OldPosition + 1;
                        textView.Caret.MoveTo(new SnapshotPoint(e.After, position));
                        FormatOperations.FormatViewLine(editorView, editorView.EditorBuffer, -1, _services);
                    }
                }
                else
                {
                    var line = e.After.GetLineFromPosition(position);
                    textView.Caret.MoveTo(new SnapshotPoint(e.After, Math.Min(e.After.Length, line.Length + 1)));
                }
            };

            Typing.Type(textView.TextBuffer, position, "\n");
            return(textView);
        }
Ejemplo n.º 2
0
        private ITextView TestAutoFormat(int position, string initialContent = "")
        {
            AstRoot   ast;
            ITextView textView = TextViewTest.MakeTextView(initialContent, position, out ast);

            textView.TextBuffer.Changed += (object sender, TextContentChangedEventArgs e) => {
                ast.ReflectTextChanges(e.ConvertToRelative(), new TextProvider(textView.TextBuffer.CurrentSnapshot));

                if (e.Changes[0].NewText.Length == 1)
                {
                    char ch = e.Changes[0].NewText[0];
                    if (AutoFormat.IsPostProcessAutoformatTriggerCharacter(ch))
                    {
                        position = e.Changes[0].OldPosition + 1;
                        textView.Caret.MoveTo(new SnapshotPoint(e.After, position));
                        FormatOperations.FormatViewLine(textView, textView.TextBuffer, -1, _exportProvider.GetExportedValue <IEditorShell>());
                    }
                }
                else
                {
                    ITextSnapshotLine line = e.After.GetLineFromPosition(position);
                    textView.Caret.MoveTo(new SnapshotPoint(e.After, Math.Min(e.After.Length, line.Length + 1)));
                }
            };

            Typing.Type(textView.TextBuffer, position, "\n");

            return(textView);
        }
Ejemplo n.º 3
0
        public override void PostProcessInvoke(CommandResult result, Guid group, int id, object inputArg, ref object outputArg)
        {
            if (group == VSConstants.VSStd2K)
            {
                char typedChar = GetTypedChar(group, id, inputArg);
                if (AutoFormat.IsPostProcessAutoformatTriggerCharacter(typedChar))
                {
                    AutoFormat.HandleAutoformat(TextView, typedChar);
                }

                base.PostProcessInvoke(result, group, id, inputArg, ref outputArg);
            }
        }