예제 #1
0
        public void AppendLine(string appendLine, IndentChange change)
        {
            switch (change)
            {
            case IndentChange.Increase:
                IndentLevel++;
                break;

            case IndentChange.Decrease:
                if (IndentLevel > 0)
                {
                    IndentLevel--;
                }
                break;

            default: break;
            }
            InsertIndentation();
            stringBuilder.AppendLine(appendLine);
        }
예제 #2
0
        public int?GetDesiredIndentation(ITextSnapshotLine line)
        {
            // Look at the previous line
            int line_no  = line.LineNumber - 1;
            var snapshot = line.Snapshot;

            while (line_no >= 0)
            {
                var  prevLine   = snapshot.GetLineFromLineNumber(line_no);
                var  lineSpan   = new SnapshotSpan(snapshot, new Span(prevLine.Start, prevLine.Length));
                int  prevIndent = 0;
                bool nonBlank   = false;
                int  indentInc  = 0;

                foreach (var tag in _lexTags.GetTags(lineSpan))
                {
                    TokenId id = tag.Tag.type;

                    if (id == TokenId.Ignore || id == TokenId.Comment) // Ignore comments and whitespace
                    {
                        continue;
                    }

                    IndentChange change = IndentChange.None;
                    _indenters.TryGetValue(id, out change);

                    if (!nonBlank)
                    {
                        var tagSpans = tag.Span.GetSpans(snapshot);
                        prevIndent = tagSpans[0].Start - prevLine.Start;
                        nonBlank   = true;

                        if (change == IndentChange.Dec || change == IndentChange.BackOne)
                        {
                            // Our 0 change position should be one level in from previous line
                            indentInc++;
                        }
                    }

                    if (change == IndentChange.Inc)
                    {
                        indentInc++;
                    }

                    if (change == IndentChange.Dec)
                    {
                        indentInc--;
                    }
                }

                if (nonBlank)
                {
                    // We found a non-blank line
                    int newIndent = prevIndent + (indentInc * _options.GetIndentSize());

                    if (newIndent < 0)
                    {
                        return(0);
                    }

                    return(newIndent);
                }

                // That line was blank, try the one before
                line_no--;
            }

            // No non-blank lines before this one, no indent
            return(0);
        }