Microsoft.Languages.Core.Formatting.IndentBuilder.GetWhiteSpaceCharLength C# (CSharp) Method

GetWhiteSpaceCharLength() public static method

Returns a conversion of tabs or space to space count. You can't simply get the tab size from options, because spaces before tabs blend in with the tabs, while spaces after the tabs add up.
public static GetWhiteSpaceCharLength ( char character, int spacesSoFar, int tabSize ) : int
character char
spacesSoFar int
tabSize int
return int
        public static int GetWhiteSpaceCharLength(char character, int spacesSoFar, int tabSize) {
            Debug.Assert(spacesSoFar >= 0, "number of spaces must be bigger than zero");

            if (character == '\t') {
                return tabSize - spacesSoFar % tabSize;
            }

            if (character.IsLineBreak()) {
                Debug.Fail("We don't expect any new lines here");
                return 1;
            }

            if (Char.IsWhiteSpace(character)) {
                return 1;
            }

            return 1;
        }

Usage Example

Example #1
0
        /// <summary>
        /// Calculates length of text in spaces, converting tabs to spaces using specified tab size.
        /// </summary>
        public static int TextLengthInSpaces(string text, int tabSize)
        {
            var length = 0;
            var spaces = 0;

            for (var i = 0; i < text.Length; i++)
            {
                var ch = text[i];

                if (ch.IsLineBreak())
                {
                    break;
                }

                length += IndentBuilder.GetWhiteSpaceCharLength(ch, spaces, tabSize);

                if (ch == ' ')
                {
                    spaces++;
                }
            }

            return(length);
        }