Example #1
0
        private int GetColumn(TokenData tokenData, Func <TokenData, TokenData, TriviaData> triviaDataGetter)
        {
            // at the beginning of a file.
            var previousToken = tokenData.GetPreviousTokenData();

            var spaces = 0;

            for (; previousToken.Token.RawKind != 0; previousToken = previousToken.GetPreviousTokenData())
            {
                var triviaInfo = triviaDataGetter(previousToken, tokenData);
                if (triviaInfo.SecondTokenIsFirstTokenOnLine)
                {
                    // current token is the first token on line.
                    // add up spaces so far and triviaInfo.Space which means indentation in this case
                    return(spaces + triviaInfo.Spaces);
                }

                // add spaces so far
                spaces += triviaInfo.Spaces;
                // here, we can't just add token's length since there is token that span multiple lines.
                GetTokenLength(previousToken.Token, out var tokenLength, out var multipleLines);

                if (multipleLines)
                {
                    return(spaces + tokenLength);
                }

                spaces   += tokenLength;
                tokenData = previousToken;
            }

            // we reached beginning of the tree, add spaces at the beginning of the tree
            return(spaces + triviaDataGetter(previousToken, tokenData).Spaces);
        }
Example #2
0
        /// <summary>
        /// Get column of the token
        /// * column means text position on a line where all tabs are converted to spaces that first position on a line becomes 0
        /// </summary>
        private int GetColumn(TokenData tokenData, Func <TokenData, TokenData, TriviaData> triviaDataGetter)
        {
            // at the beginning of a file.
            var previousToken = tokenData.GetPreviousTokenData();

            if (previousToken.Token.RawKind == 0)
            {
                // use space of the trivia data since it means indentation in this case
                return(triviaDataGetter(previousToken, tokenData).Spaces);
            }

            var spaces = 0;

            for (; previousToken.Token.RawKind != 0; previousToken = previousToken.GetPreviousTokenData())
            {
                var triviaInfo = triviaDataGetter(previousToken, tokenData);
                if (triviaInfo.SecondTokenIsFirstTokenOnLine)
                {
                    // current token is the first token on line.
                    // add up spaces so far and triviaInfo.Space which means indentation in this case
                    return(spaces + triviaInfo.Spaces);
                }

                // add spaces so far
                spaces += triviaInfo.Spaces;

                // here, we can't just add token's length since there is token that span multiple lines.
                var text = previousToken.Token.ToString();
                if (text.GetNumberOfLineBreaks() > 0)
                {
                    // add up spaces so far and text indentation
                    return(spaces + text.GetTextColumn(this.optionSet.GetOption(FormattingOptions.TabSize, this.treeData.Root.Language), initialColumn: 0));
                }

                // add spaces so far
                if (text.ContainsTab())
                {
                    // do expansive calculation
                    var initialColumn = this.treeData.GetOriginalColumn(this.optionSet.GetOption(FormattingOptions.TabSize, this.treeData.Root.Language), previousToken.Token);
                    spaces += text.ConvertTabToSpace(this.optionSet.GetOption(FormattingOptions.TabSize, this.treeData.Root.Language), initialColumn, text.Length);
                }
                else
                {
                    spaces += text.Length;
                }

                tokenData = previousToken;
            }

            // we reached beginning of the tree, add spaces at the beginning of the tree
            return(spaces + triviaDataGetter(previousToken, tokenData).Spaces);
        }