コード例 #1
0
ファイル: AutoFormat.cs プロジェクト: Fooway/RTVS
        public static void HandleAutoformat(ITextView textView, char typedChar)
        {
            if (!REditorSettings.AutoFormat || IgnoreOnce)
            {
                IgnoreOnce = false;
                return;
            }

            IEditorTree   tree;
            SnapshotPoint?rPoint = GetCaretPointInBuffer(textView, out tree);

            if (!rPoint.HasValue)
            {
                return;
            }

            // We don't want to auto-format inside strings
            if (tree.AstRoot.IsPositionInsideString(rPoint.Value.Position))
            {
                return;
            }

            ITextBuffer subjectBuffer = rPoint.Value.Snapshot.TextBuffer;

            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(subjectBuffer, rPoint.Value.Position))
                {
                    bool formatScope = ShouldFormatScope(textView, subjectBuffer, -1);
                    if (formatScope)
                    {
                        FormatOperations.FormatCurrentNode <IStatement>(textView, subjectBuffer);
                    }
                    else
                    {
                        FormatOperations.FormatLine(textView, subjectBuffer, tree.AstRoot, -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.
                ITextSnapshotLine line = subjectBuffer.CurrentSnapshot.GetLineFromPosition(rPoint.Value.Position);
                int    positionInLine  = rPoint.Value.Position - line.Start;
                string lineText        = line.GetText();
                if (positionInLine >= lineText.TrimEnd().Length)
                {
                    FormatOperations.FormatLine(textView, subjectBuffer, tree.AstRoot, 0);
                }
            }
            else if (typedChar == '}')
            {
                FormatOperations.FormatNode <IStatement>(textView, subjectBuffer, Math.Max(rPoint.Value - 1, 0));
            }
        }