コード例 #1
0
        /// <summary>
        /// Appends indentation to each line so formatted text appears properly
        /// indented inside the host document (script block in HTML page).
        /// </summary>
        private void IndentLines(IEditorBuffer textBuffer, ITextRange range, AstRoot ast, int originalIndentSizeInSpaces)
        {
            var snapshot  = textBuffer.CurrentSnapshot;
            var firstLine = snapshot.GetLineFromPosition(range.Start);
            var lastLine  = snapshot.GetLineFromPosition(range.End);
            var document  = textBuffer.GetEditorDocument <IREditorDocument>();

            for (var i = firstLine.LineNumber; i <= lastLine.LineNumber; i++)
            {
                // Snapshot is updated after each insertion so do not cache
                var line   = textBuffer.CurrentSnapshot.GetLineFromLineNumber(i);
                var indent = SmartIndenter.GetSmartIndent(line, _settings, ast, originalIndentSizeInSpaces, formatting: true);
                if (indent > 0 && line.Length > 0 && line.Start >= range.Start)
                {
                    // Check current indentation and correct for the difference
                    int currentIndentSize = IndentBuilder.TextIndentInSpaces(line.GetText(), _settings.TabSize);
                    indent = Math.Max(0, indent - currentIndentSize);
                    if (indent > 0)
                    {
                        var indentString = IndentBuilder.GetIndentString(indent, _settings.IndentType, _settings.TabSize);
                        textBuffer.Insert(line.Start, indentString);
                        if (document == null)
                        {
                            // Typically this is a test scenario only. In the real editor
                            // instance document is available and automatically updates AST
                            // when whitespace inserted, not no manual update is necessary.
                            ast.ReflectTextChange(line.Start, 0, indentString.Length, textBuffer.CurrentSnapshot);
                        }
                    }
                }
            }
        }
コード例 #2
0
ファイル: RangeFormatter.cs プロジェクト: nomada2/RTVS
        /// <summary>
        /// Appends indentation to each line so formatted text appears properly
        /// indented inside the host document (script block in HTML page).
        /// </summary>
        private static void IndentLines(ITextView textView, ITextBuffer textBuffer,
                                        ITextRange range, AstRoot ast,
                                        RFormatOptions options, int originalIndentSizeInSpaces)
        {
            ITextSnapshot     snapshot  = textBuffer.CurrentSnapshot;
            ITextSnapshotLine firstLine = snapshot.GetLineFromPosition(range.Start);
            ITextSnapshotLine lastLine  = snapshot.GetLineFromPosition(range.End);
            IREditorDocument  document  = REditorDocument.TryFromTextBuffer(textBuffer);

            for (int i = firstLine.LineNumber; i <= lastLine.LineNumber; i++)
            {
                // Snapshot is updated after each insertion so do not cache
                ITextSnapshotLine line = textBuffer.CurrentSnapshot.GetLineFromLineNumber(i);
                int indent             = SmartIndenter.GetSmartIndent(line, ast, originalIndentSizeInSpaces);
                if (indent > 0 && line.Length > 0 && line.Start >= range.Start)
                {
                    // Check current indentation and correct for the difference
                    int currentIndentSize = line.Length - line.GetText().TrimStart().Length;
                    indent = Math.Max(0, indent - currentIndentSize);
                    if (indent > 0)
                    {
                        string indentString = IndentBuilder.GetIndentString(indent, options.IndentType, options.TabSize);
                        textBuffer.Insert(line.Start, indentString);
                        if (document == null)
                        {
                            // Typically this is a test scenario only. In the real editor
                            // instance document is available and automatically updates AST
                            // when whitespace inserted, not no manual update is necessary.
                            ast.ReflectTextChange(line.Start, 0, indentString.Length);
                        }
                    }
                }
            }
        }