示例#1
0
 public FormatOperations(IServiceContainer services, IEditorView editorView, IEditorBuffer editorBuffer, IIncrementalWhitespaceChangeHandler changeHandler = null)
 {
     _services      = services;
     _editorView    = editorView;
     _editorBuffer  = editorBuffer;
     _changeHandler = changeHandler ?? _services.GetService <IIncrementalWhitespaceChangeHandler>();
 }
示例#2
0
 public AutoFormat(IServiceContainer services, IEditorView editorView, IEditorBuffer editorBuffer, IIncrementalWhitespaceChangeHandler changeHandler)
 {
     EditorView     = editorView;
     EditorBuffer   = editorBuffer;
     Services       = services;
     Settings       = services.GetService <IREditorSettings>();
     _changeHandler = changeHandler ?? services.GetService <IIncrementalWhitespaceChangeHandler>();
 }
示例#3
0
 public RangeFormatter(IServiceContainer services, IEditorView editorView, IEditorBuffer editorBuffer, IIncrementalWhitespaceChangeHandler changeHandler = null)
 {
     _services      = services;
     _settings      = services.GetService <IREditorSettings>();
     _editorView    = editorView;
     _editorBuffer  = editorBuffer;
     _changeHandler = changeHandler ?? _services.GetService <IIncrementalWhitespaceChangeHandler>();
 }
示例#4
0
        public void HandleTyping(char typedChar, int position, IEditorBuffer editorBuffer = null, IIncrementalWhitespaceChangeHandler changeHandler = null)
        {
            if (!Settings.AutoFormat || (!Settings.FormatScope && typedChar == '}'))
            {
                return;
            }

            EditorBuffer = editorBuffer ?? EditorBuffer;
            var document = EditorBuffer.GetEditorDocument <IREditorDocument>();
            // AST may or may not be ready. Upto the caller to decide if it is worth waiting.
            var ast = document.EditorTree.AstRoot;

            // Make sure we are not formatting damaging the projected range in R Markdown
            // which looks like ```{r. 'r' should not separate from {.
            if (!CanFormatContainedLanguageLine(position, typedChar))
            {
                return;
            }

            // We don't want to auto-format inside strings
            if (ast.IsPositionInsideString(position))
            {
                return;
            }

            var fo = new FormatOperations(Services, EditorView, EditorBuffer, _changeHandler);

            if (typedChar.IsLineBreak())
            {
                // Special case for hitting caret after } and before 'else'. We do want to format
                // the construct as '} else {' but if user types Enter after } and we auto-format
                // it will look as if the editor just eats the Enter. Instead, we will not be
                // autoformatting in this specific case. User can always format either the document
                // or select the block and reformat it.
                if (!IsBetweenCurlyAndElse(position))
                {
                    var scopeStatement = GetFormatScope(position, ast);
                    // Do not format large scope blocks for performance reasons
                    if (scopeStatement != null && scopeStatement.Length < 200)
                    {
                        fo.FormatNode(scopeStatement);
                    }
                    else if (CanFormatLine(position, -1))
                    {
                        fo.FormatViewLine(-1);
                    }
                }
            }
            else if (typedChar == ';')
            {
                // Verify we are at the end of the string and not in a middle
                // of another string or inside a statement.
                var line           = EditorBuffer.CurrentSnapshot.GetLineFromPosition(position);
                var positionInLine = position - line.Start;
                var lineText       = line.GetText();
                if (positionInLine >= lineText.TrimEnd().Length)
                {
                    fo.FormatViewLine(0);
                }
            }
            else if (typedChar == '}')
            {
                fo.FormatCurrentStatement(limitAtCaret: true, caretOffset: -1);
            }
        }