示例#1
0
        /// <summary>
        /// Implementation of default indentation.
        /// </summary>
        /// <param name="line">The current line after Enter.</param>
        /// <param name="tabSize">The TAB size.</param>
        /// <returns>Desired indentation size.</returns>
        private int?GetDefaultIndentationImp(ITextSnapshotLine line, int tabSize)
        {
            int lineNumber = line.LineNumber;

            if (lineNumber < 1)
            {
                return(0);
            }

            string            baselineText = null;
            ITextSnapshotLine baseline     = null;

            IndentUtilities.SkipPrecedingBlankLines(line, out baselineText, out baseline);
            return(IndentUtilities.GetCurrentLineIndentation(baselineText, tabSize));
        }
示例#2
0
        /// <summary>
        /// Implementation of smart indentation.
        /// </summary>
        /// <param name="line">The current line after Enter.</param>
        /// <param name="tabSize">The TAB size.</param>
        /// <returns>Desired indentation size.</returns>
        private int?GetSmartIndentationImp(ITextSnapshotLine line, int tabSize)
        {
            int lineNumber = line.LineNumber;

            if (lineNumber < 0)
            {
                return(null);
            }

            string            baselineText;
            ITextSnapshotLine baseline;
            char   groupStartChar;
            string groupStartLineText;

            IndentUtilities.SkipPrecedingBlankLines(line, out baselineText, out baseline);
            int indentation = IndentUtilities.GetCurrentLineIndentation(baselineText, tabSize);

            // If no group start can be found, follow the default indentation
            if (!FindFirstGroupStart(baseline, out groupStartChar, out groupStartLineText))
            {
                return(indentation);
            }

            indentation = IndentUtilities.GetCurrentLineIndentation(groupStartLineText, tabSize);

            // If there is no group end in the current line, or there is one but there are other non-whitespace chars preceding it
            // then add a TAB compared with the indentation of the line of group start.
            SnapshotPoint lastGroupEnd;

            if (!FindFirstGroupEnd(line, groupStartChar, out lastGroupEnd))
            {
                return(indentation += tabSize);
            }

            // Approach here as the group end was found and there are only white spaces between line start and the group end.
            // We need to delete all the white spaces and then indent it the size as same as group start line.
            var textBuffer           = line.Snapshot.TextBuffer;
            int precedingWhiteSpaces = lastGroupEnd - line.Start;

            if (precedingWhiteSpaces > 0 &&
                !textBuffer.EditInProgress &&
                textBuffer.CurrentSnapshot.Length >= precedingWhiteSpaces)
            {
                textBuffer.Delete(new Span(line.Start, precedingWhiteSpaces));
            }

            return(indentation);
        }