예제 #1
0
        private ITextView TestAutoFormat(int position, string textToType, string initialContent = "")
        {
            AstRoot   ast;
            ITextView textView = TextViewTest.MakeTextView(initialContent, position, out ast);

            textView.TextBuffer.Changed += (object sender, TextContentChangedEventArgs e) => {
                List <TextChangeEventArgs> textChanges = TextUtility.ConvertToRelative(e);
                ast.ReflectTextChanges(textChanges);

                if (e.Changes[0].NewText.Length == 1)
                {
                    char ch = e.Changes[0].NewText[0];
                    if (AutoFormat.IsAutoformatTriggerCharacter(ch))
                    {
                        int offset = 0;
                        if (e.Changes[0].NewText[0].IsLineBreak())
                        {
                            position = e.Changes[0].OldPosition + 1;
                            textView.Caret.MoveTo(new SnapshotPoint(e.After, position));
                            offset = -1;
                        }
                        FormatOperations.FormatLine(textView, textView.TextBuffer, ast, offset);
                    }
                }
                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, textToType);

            return(textView);
        }
예제 #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);
        }
예제 #3
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);
        }
예제 #4
0
 /// <summary>
 /// Called by the editor when return is pressed while both
 /// braces are on the same line and no typing has occurred
 /// in the session.
 /// </summary>
 /// <remarks>
 /// Called after the newline has been inserted into the buffer.
 /// Formatting for scenarios where the closing brace needs to be
 /// moved down an additional line past the caret should be done here.
 /// </remarks>
 /// <param name="session">Default brace completion session</param>
 public void OnReturn(IBraceCompletionSession session)
 {
     if (session.OpeningBrace == '{' && _settings.AutoFormat)
     {
         EnsureTreeReady(session.SubjectBuffer);
         FormatOperations.FormatCurrentScope(session.TextView, session.SubjectBuffer, _shell, indentCaret: true);
     }
 }
예제 #5
0
 /// <summary>
 /// Called before the session is added to the stack.
 /// </summary>
 /// <remarks>
 /// If additional formatting is required for the opening or
 /// closing brace it should be done here.
 /// </remarks>
 /// <param name="session">Default brace completion session</param>
 public void Start(IBraceCompletionSession session)
 {
     if (session.OpeningBrace == '{' && _settings.AutoFormat)
     {
         EnsureTreeReady(session.SubjectBuffer);
         FormatOperations.FormatCurrentStatement(session.TextView, session.SubjectBuffer, _shell);
     }
 }
예제 #6
0
 /// <summary>
 /// Called by the editor when return is pressed while both
 /// braces are on the same line and no typing has occurred
 /// in the session.
 /// </summary>
 /// <remarks>
 /// Called after the newline has been inserted into the buffer.
 /// Formatting for scenarios where the closing brace needs to be
 /// moved down an additional line past the caret should be done here.
 /// </remarks>
 /// <param name="session">Default brace completion session</param>
 public void OnReturn(IBraceCompletionSession session)
 {
     if (session.OpeningBrace == '{' && REditorSettings.AutoFormat)
     {
         AutoFormat.IgnoreOnce = true;
         EnsureTreeReady(session.SubjectBuffer);
         FormatOperations.FormatCurrentScope(session.TextView, session.SubjectBuffer, indentCaret: true);
     }
 }
예제 #7
0
 /// <summary>
 /// Called before the session is added to the stack.
 /// </summary>
 /// <remarks>
 /// If additional formatting is required for the opening or
 /// closing brace it should be done here.
 /// </remarks>
 /// <param name="session">Default brace completion session</param>
 public void Start(IBraceCompletionSession session)
 {
     if (session.OpeningBrace == '{' && REditorSettings.AutoFormat)
     {
         AutoFormat.IgnoreOnce = false;
         EnsureTreeReady(session.SubjectBuffer);
         FormatOperations.FormatCurrentStatement(session.TextView, session.SubjectBuffer);
     }
 }
예제 #8
0
 /// <summary>
 /// Called by the editor when return is pressed while both
 /// braces are on the same line and no typing has occurred
 /// in the session.
 /// </summary>
 /// <remarks>
 /// Called after the newline has been inserted into the buffer.
 /// Formatting for scenarios where the closing brace needs to be
 /// moved down an additional line past the caret should be done here.
 /// </remarks>
 /// <param name="session">Default brace completion session</param>
 public void OnReturn(IBraceCompletionSession session)
 {
     if (session.OpeningBrace == '{' && _settings.AutoFormat)
     {
         EnsureTreeReady(session.SubjectBuffer);
         var fo = new FormatOperations(_shell.Services, session.TextView.ToEditorView(), session.SubjectBuffer.ToEditorBuffer());
         fo.FormatCurrentScope(indentCaret: true);
     }
 }
예제 #9
0
 /// <summary>
 /// Called before the session is added to the stack.
 /// </summary>
 /// <remarks>
 /// If additional formatting is required for the opening or
 /// closing brace it should be done here.
 /// </remarks>
 /// <param name="session">Default brace completion session</param>
 public void Start(IBraceCompletionSession session)
 {
     if (session.OpeningBrace == '{' && _settings.AutoFormat)
     {
         EnsureTreeReady(session.SubjectBuffer);
         var fo = new FormatOperations(_shell.Services, session.TextView.ToEditorView(), session.SubjectBuffer.ToEditorBuffer());
         fo.FormatCurrentStatement();
     }
 }
예제 #10
0
        private static void FormatReplDocument(ITextView textView, ITextBuffer textBuffer, int position)
        {
            var document = REditorDocument.TryFromTextBuffer(textBuffer);

            if (document != null)
            {
                var tree = document.EditorTree;
                tree.EnsureTreeReady();
                FormatOperations.FormatCurrentStatement(textView, textBuffer);
            }
        }
예제 #11
0
        private void FormatReplDocument(ITextView textView, ITextBuffer textBuffer, int position)
        {
            var document = textBuffer.GetEditorDocument <IREditorDocument>();

            if (document != null)
            {
                var tree = document.EditorTree;
                tree.EnsureTreeReady();
                FormatOperations.FormatCurrentStatement(textView.ToEditorView(), textBuffer.ToEditorBuffer(), Services);
            }
        }