Exemple #1
0
        private void CollectNewLines(IndentRange indentRange)
        {
            IList <TreeOffset> newLineOffsets = new List <TreeOffset>();
            var token     = indentRange.Nodes[0].GetFirstTokenIn();
            var prevToken = token.GetPrevToken();

            while ((prevToken != null) && (prevToken.IsWhitespaceToken()))
            {
                if (prevToken.GetTokenType() == NewLineType)
                {
                    newLineOffsets.Add(prevToken.GetTreeEndOffset());
                    break;
                }
                prevToken = prevToken.GetPrevToken();
            }
            while ((token != null) && (indentRange.ContainsNewLine(token.GetTreeEndOffset())))
            {
                if (token.GetTokenType() == NewLineType)
                {
                    newLineOffsets.Add(token.GetTreeEndOffset());
                    var range = GetCurrentRange(token.GetTreeEndOffset(), indentRange);
                    range.HasNewLine = true;
                }
                token = token.GetNextToken();
            }
        }
Exemple #2
0
        private void CalcIndents(IndentRange indentRange, string parentIndent = "")
        {
            if (indentRange.HasNewLine)
            {
                if (indentRange.Rule is AlignmentIndentingRule)
                {
                    int length = GetLineLength(indentRange.Nodes[0]);
                    indentRange.Indent = parentIndent;
                    for (int i = 0; i < length; ++i)
                    {
                        indentRange.Indent += " ";
                    }
                }
                else
                {
                    indentRange.Indent = parentIndent + StandardIndent;
                }
            }
            else
            {
                indentRange.Indent = parentIndent;
            }

            foreach (var range in indentRange.ChildRanges)
            {
                CalcIndents(range, indentRange.Indent);
            }
        }
Exemple #3
0
        private IndentRange GetCurrentRangeForInsert(TreeOffset offset, IEnumerable <IndentRange> indentRanges)
        {
            IndentRange range = null;

            foreach (var indentRange in indentRanges)
            {
                if (indentRange.Contains(offset))
                {
                    range = GetCurrentRangeForInsert(offset, indentRange);
                }
            }
            return(range);
        }
Exemple #4
0
        private IndentRange GetCurrentRangeForInsert(TreeOffset offset, IndentRange indentRange)
        {
            if (indentRange.ChildRanges.Count() == 0)
            {
                return(indentRange);
            }
            var range = GetCurrentRangeForInsert(offset, indentRange.ChildRanges);

            if (range == null)
            {
                return(indentRange);
            }
            return(range);
        }
Exemple #5
0
        private IList <IndentRange> BuildRanges(ITreeNode node)
        {
            IList <IndentRange> ranges = new List <IndentRange>();

            var child = node.FirstChild;

            while (child != null)
            {
                bool match = false;
                foreach (var rule in Formatter.IndentingRules)
                {
                    var nextChild = Match(child, rule);
                    if (nextChild != child)
                    {
                        IList <ITreeNode> nodes = new List <ITreeNode>();
                        var tempNode            = child;
                        while (tempNode != nextChild)
                        {
                            nodes.Add(tempNode);
                            tempNode = tempNode.NextSibling;
                        }
                        var range = new IndentRange(nodes.ToArray(), rule);
                        foreach (var treeNode in range.Nodes)
                        {
                            var childRanges = BuildRanges(treeNode);
                            range.AddChildRanges(childRanges);
                        }
                        ranges.Add(range);
                        child = nextChild;
                        match = true;
                        break;
                    }
                }
                if (!match)
                {
                    var childRange = BuildRanges(child);
                    ranges.AddRange(childRange);
                    child = child.NextSibling;
                }
            }
            return(ranges);
        }