Пример #1
0
		public Collection<IFormattedLine> FormatLineInVisualBuffer(ITextSnapshotLine visualLine) {
			if (visualLine == null)
				throw new ArgumentNullException(nameof(visualLine));
			if (visualLine.Snapshot != TopTextSnapshot)
				throw new ArgumentException();

			var seqColl = TextAndAdornmentSequencer.CreateTextAndAdornmentCollection(visualLine, visualLine.Snapshot);
			var bufferLine = GetBufferLine(seqColl);
			var linePartsCollection = CreateLinePartsCollection(seqColl, bufferLine.ExtentIncludingLineBreak);
			var textSource = new LinePartsTextSource(linePartsCollection);
			var lines = new Collection<IFormattedLine>();

			TextLineBreak previousLineBreak = null;
			double autoIndent = BaseIndentation;
			int column = 0;
			int linePartsIndex = 0;
			var lineParts = linePartsCollection.LineParts;
			for (int lineSegment = 0; ; lineSegment++) {
				var paragraphProperties = textParagraphPropertiesFactoryService?.Create(this,
					classificationFormatMap.DefaultTextProperties,
					TextAndAdornmentSequencer.BufferGraph.CreateMappingSpan(bufferLine.Extent, SpanTrackingMode.EdgeNegative),
					TextAndAdornmentSequencer.BufferGraph.CreateMappingPoint(textSource.ConvertColumnToBufferPosition(column), PointTrackingMode.Negative), lineSegment)
					?? defaultTextParagraphProperties;

				double paragraphWidth = WordWrapWidth == 0 ? 0 : Math.Max(1, WordWrapWidth - autoIndent - wrapGlyphWidth);

				textSource.SetMaxLineLength(MAX_LINE_LENGTH);
				var textLine = textFormatter.FormatLine(textSource, column, paragraphWidth, paragraphProperties, previousLineBreak);

				int startColumn = column;
				int length = textLine.GetLength(textSource.EndOfLine);
				column += length;

				int linePartsEnd = linePartsIndex;
				Debug.Assert(lineParts.Count == 0 || linePartsEnd < lineParts.Count);
				while (linePartsEnd < lineParts.Count) {
					var part = lineParts[linePartsEnd];
					linePartsEnd++;
					if (column <= part.Column + part.ColumnLength)
						break;
				}
				linePartsEnd--;

				var startPos = textSource.ConvertColumnToBufferPosition(startColumn);
				var endPos = textSource.ConvertColumnToBufferPosition(column);
				if (column >= textSource.Length) {
					endPos = bufferLine.ExtentIncludingLineBreak.End;
					linePartsEnd = lineParts.Count - 1;
				}

				var lineSpan = new SnapshotSpan(startPos, endPos);
				var wpfLine = new WpfTextViewLine(TextAndAdornmentSequencer.BufferGraph, linePartsCollection, linePartsIndex, linePartsEnd - linePartsIndex + 1, startColumn, column, bufferLine, lineSpan, visualLine.Snapshot, textLine, autoIndent, ColumnWidth);
				lines.Add(wpfLine);

				if (column >= textSource.Length) {
					Debug.Assert(column == textSource.Length);
					break;
				}
				if (startColumn == column)
					throw new InvalidOperationException();

				linePartsIndex = linePartsEnd;
				while (linePartsIndex < lineParts.Count) {
					var part = lineParts[linePartsIndex];
					if (column < part.Column + part.ColumnLength)
						break;
					linePartsIndex++;
				}

				if (lineSegment == 0) {
					autoIndent = 0;
					var firstCharColumn = textSource.GetColumnOfFirstNonWhitespace();
					if (firstCharColumn < column)
						autoIndent += textLine.GetDistanceFromCharacterHit(new CharacterHit(firstCharColumn, 0));
					autoIndent += TabSize / 2 * ColumnWidth;
					if (autoIndent > MaxAutoIndent)
						autoIndent = MaxAutoIndent;
					// Base indentation should always be included
					autoIndent += BaseIndentation;
				}

				previousLineBreak = textLine.GetTextLineBreak();
			}

			return lines;
		}
Пример #2
0
        public Collection <IFormattedLine> FormatLineInVisualBuffer(ITextSnapshotLine visualLine)
        {
            if (visualLine == null)
            {
                throw new ArgumentNullException(nameof(visualLine));
            }
            if (visualLine.Snapshot != TopTextSnapshot)
            {
                throw new ArgumentException();
            }

            var seqColl             = TextAndAdornmentSequencer.CreateTextAndAdornmentCollection(visualLine, visualLine.Snapshot);
            var bufferLine          = GetBufferLine(seqColl);
            var linePartsCollection = CreateLinePartsCollection(seqColl, bufferLine.ExtentIncludingLineBreak);
            var textSource          = new LinePartsTextSource(linePartsCollection);
            var lines = new Collection <IFormattedLine>();

            TextLineBreak previousLineBreak = null;
            double        autoIndent        = BaseIndentation;
            int           column            = 0;

            for (int lineSegment = 0; ; lineSegment++)
            {
                var paragraphProperties = textParagraphPropertiesFactoryService?.Create(this,
                                                                                        classificationFormatMap.DefaultTextProperties,
                                                                                        TextAndAdornmentSequencer.BufferGraph.CreateMappingSpan(bufferLine.Extent, SpanTrackingMode.EdgeNegative),
                                                                                        TextAndAdornmentSequencer.BufferGraph.CreateMappingPoint(textSource.ConvertColumnToBufferPosition(column), PointTrackingMode.Negative), lineSegment)
                                          ?? defaultTextParagraphProperties;

                double paragraphWidth = WordWrapWidth == 0 ? 0 : Math.Max(1, WordWrapWidth - autoIndent - wrapGlyphWidth);

                textSource.SetMaxLineLength(MAX_LINE_LENGTH);
                var textLine = textFormatter.FormatLine(textSource, column, paragraphWidth, paragraphProperties, previousLineBreak);

                int startColumn = column;
                int length      = textLine.GetLength(textSource.EndOfLine);
                column += length;

                var startPos = textSource.ConvertColumnToBufferPosition(startColumn);
                var endPos   = textSource.ConvertColumnToBufferPosition(column);
                if (column >= textSource.Length)
                {
                    endPos = bufferLine.ExtentIncludingLineBreak.End;
                }

                var lineSpan = new SnapshotSpan(startPos, endPos);
                var wpfLine  = new WpfTextViewLine(TextAndAdornmentSequencer.BufferGraph, linePartsCollection, startColumn, column, bufferLine, lineSpan, visualLine.Snapshot, textLine, autoIndent, ColumnWidth);
                lines.Add(wpfLine);

                if (column >= textSource.Length)
                {
                    Debug.Assert(column == textSource.Length);
                    break;
                }
                if (startColumn == column)
                {
                    throw new InvalidOperationException();
                }

                if (lineSegment == 0)
                {
                    autoIndent = 0;
                    var firstCharColumn = textSource.GetColumnOfFirstNonWhitespace();
                    if (firstCharColumn < column)
                    {
                        autoIndent += textLine.GetDistanceFromCharacterHit(new CharacterHit(firstCharColumn, 0));
                    }
                    autoIndent += TabSize / 2 * ColumnWidth;
                    if (autoIndent > MaxAutoIndent)
                    {
                        autoIndent = MaxAutoIndent;
                    }
                    // Base indentation should always be included
                    autoIndent += BaseIndentation;
                }

                previousLineBreak = textLine.GetTextLineBreak();
            }

            return(lines);
        }