PageRenderTime 29ms CodeModel.GetById 19ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 1ms

/AvalonEdit/ICSharpCode.AvalonEdit/Rendering/DocumentColorizingTransformer.cs

http://github.com/icsharpcode/ILSpy
C# | 98 lines | 54 code | 8 blank | 36 comment | 11 complexity | 24704c30ab4ab29df0378c45ad8a8443 MD5 | raw file
 1// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
 2// 
 3// Permission is hereby granted, free of charge, to any person obtaining a copy of this
 4// software and associated documentation files (the "Software"), to deal in the Software
 5// without restriction, including without limitation the rights to use, copy, modify, merge,
 6// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
 7// to whom the Software is furnished to do so, subject to the following conditions:
 8// 
 9// The above copyright notice and this permission notice shall be included in all copies or
10// substantial portions of the Software.
11// 
12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17// DEALINGS IN THE SOFTWARE.
18
19using System;
20using System.Linq;
21using ICSharpCode.AvalonEdit.Document;
22
23namespace ICSharpCode.AvalonEdit.Rendering
24{
25	/// <summary>
26	/// Base class for <see cref="IVisualLineTransformer"/> that helps
27	/// colorizing the document. Derived classes can work with document lines
28	/// and text offsets and this class takes care of the visual lines and visual columns.
29	/// </summary>
30	public abstract class DocumentColorizingTransformer : ColorizingTransformer
31	{
32		DocumentLine currentDocumentLine;
33		int firstLineStart;
34		int currentDocumentLineStartOffset, currentDocumentLineEndOffset;
35		
36		/// <summary>
37		/// Gets the current ITextRunConstructionContext.
38		/// </summary>
39		protected ITextRunConstructionContext CurrentContext { get; private set; }
40		
41		/// <inheritdoc/>
42		protected override void Colorize(ITextRunConstructionContext context)
43		{
44			if (context == null)
45				throw new ArgumentNullException("context");
46			this.CurrentContext = context;
47			
48			currentDocumentLine = context.VisualLine.FirstDocumentLine;
49			firstLineStart = currentDocumentLineStartOffset = currentDocumentLine.Offset;
50			currentDocumentLineEndOffset = currentDocumentLineStartOffset + currentDocumentLine.Length;
51			int currentDocumentLineTotalEndOffset = currentDocumentLineStartOffset + currentDocumentLine.TotalLength;
52			
53			if (context.VisualLine.FirstDocumentLine == context.VisualLine.LastDocumentLine) {
54				ColorizeLine(currentDocumentLine);
55			} else {
56				ColorizeLine(currentDocumentLine);
57				// ColorizeLine modifies the visual line elements, loop through a copy of the line elements
58				foreach (VisualLineElement e in context.VisualLine.Elements.ToArray()) {
59					int elementOffset = firstLineStart + e.RelativeTextOffset;
60					if (elementOffset >= currentDocumentLineTotalEndOffset) {
61						currentDocumentLine = context.Document.GetLineByOffset(elementOffset);
62						currentDocumentLineStartOffset = currentDocumentLine.Offset;
63						currentDocumentLineEndOffset = currentDocumentLineStartOffset + currentDocumentLine.Length;
64						currentDocumentLineTotalEndOffset = currentDocumentLineStartOffset + currentDocumentLine.TotalLength;
65						ColorizeLine(currentDocumentLine);
66					}
67				}
68			}
69			currentDocumentLine = null;
70			this.CurrentContext = null;
71		}
72		
73		/// <summary>
74		/// Override this method to colorize an individual document line.
75		/// </summary>
76		protected abstract void ColorizeLine(DocumentLine line);
77		
78		/// <summary>
79		/// Changes a part of the current document line.
80		/// </summary>
81		/// <param name="startOffset">Start offset of the region to change</param>
82		/// <param name="endOffset">End offset of the region to change</param>
83		/// <param name="action">Action that changes an individual <see cref="VisualLineElement"/>.</param>
84		protected void ChangeLinePart(int startOffset, int endOffset, Action<VisualLineElement> action)
85		{
86			if (startOffset < currentDocumentLineStartOffset || startOffset > currentDocumentLineEndOffset)
87				throw new ArgumentOutOfRangeException("startOffset", startOffset, "Value must be between " + currentDocumentLineStartOffset + " and " + currentDocumentLineEndOffset);
88			if (endOffset < startOffset || endOffset > currentDocumentLineEndOffset)
89				throw new ArgumentOutOfRangeException("endOffset", endOffset, "Value must be between " + startOffset + " and " + currentDocumentLineEndOffset);
90			VisualLine vl = this.CurrentContext.VisualLine;
91			int visualStart = vl.GetVisualColumn(startOffset - firstLineStart);
92			int visualEnd = vl.GetVisualColumn(endOffset - firstLineStart);
93			if (visualStart < visualEnd) {
94				ChangeVisualElements(visualStart, visualEnd, action);
95			}
96		}
97	}
98}