csharp/Actipro/WPF-Controls/Samples/SampleBrowser/ProductSamples/SyntaxEditorSamples/QuickStart/DotNetAddonServerTags/ParentParseData.cs

ParentParseData.cs
using System;
using System.Collections.Generic;
using System.Linq;
using ActiproSoftware.Text;
using ActiproSoftware.Text.Implementation;
using ActiproSoftware.Text.Languages.DotNet;
using ActiproSoftware.Text.Languages.DotNet.Ast.Implementation;
using ActiproSoftware.Text.Parsing;
using ActiproSoftware.Text.Parsing.Implementation;
using ActiproSoftware.Text.Parsing.LLParser;

namespace ActiproSoftware.ProductSamples.SyntaxEditorSamples.QuickStart.DotNetAddOnServerTags {
	
	/// 
	/// Provides the result of a parsing operation.
	/// 
	public clast ParentParseData : IDotNetParseData {

		private List					errors;
		private ILLParseData						generatedParseData;
		private ITextSnapshot						snapshot;
		private List	textRangeMappings		= new List();
		
		/////////////////////////////////////////////////////////////////////////////////////////////////////
		// PUBLIC PROCEDURES
		/////////////////////////////////////////////////////////////////////////////////////////////////////
		
		/// 
		/// Gets the object that contains the abstract syntax tree result.
		/// 
		/// The object that contains the abstract syntax tree result.
		public IAstNode Ast {
			get { 
				return (generatedParseData != null ? generatedParseData.Ast : null); 
			}
		}
		
		/// 
		/// Gets the collection of  objects that specify parse errors.
		/// 
		/// The collection of  objects that specify parse errors.
		public IEnumerable Errors { 
			get {
				if (errors == null) {
					errors = new List();

					if ((generatedParseData != null) && (generatedParseData.Errors != null) && (generatedParseData.Snapshot != null)) {
						// Loop through errors and translate to the editor snapshot
						foreach (var parseError in generatedParseData.Errors) {
							if (parseError != null) {
								var generatedSnapshotRange = new TextSnapshotRange(generatedParseData.Snapshot, generatedParseData.Snapshot.PositionRangeToTextRange(parseError.PositionRange));
								var editorSnapshotOffset = this.TranslateGeneratedToEditor(new TextSnapshotOffset(generatedParseData.Snapshot, generatedSnapshotRange.StartOffset));
								if (editorSnapshotOffset.HasValue) {
									// Add the error
									var positionRange = snapshot.TextRangeToPositionRange(TextRange.FromSpan(editorSnapshotOffset.Value, Math.Max(1, generatedSnapshotRange.AbsoluteLength)));
									errors.Add(new ParseError(parseError.Level, parseError.Description, positionRange));
								}
							}
						}
					}
				}

				return errors;
			}
		}

		/// 
		/// Gets or sets the generated parse data.
		/// 
		/// The generated parse data.
		public ILLParseData GeneratedParseData { 
			get {
				return generatedParseData;
			}
			set {
				generatedParseData = value;
			}
		}
	
		/// 
		/// Gets the collection of preprocessor directives.
		/// 
		/// The collection of preprocessor directives.
		public IList PreprocessorDirectives {
			get { 
				return null; 
			}
		}
		
		/// 
		/// Gets or sets the , if known, from which the parse errors were created.
		/// 
		/// The , if known, from which the parse errors were created.
		public ITextSnapshot Snapshot { 
			get {
				return snapshot;
			}
			set {
				snapshot = value;
			}
		}
		
		/// 
		/// Gets the collection of text range mappings from the editor snapshot to the generated snapshots.
		/// 
		/// The collection of text range mappings from the editor snapshot to the generated snapshots.
		public IList TextRangeMappings { 
			get {
				return textRangeMappings;
			}
		}
		
		/// 
		/// Translates from an editor snapshot to a generated snapshot.
		/// 
		/// The snapshot offset.
		/// The translated snapshot, if within a child language section.
		public TextSnapshotOffset? TranslateEditorToGenerated(TextSnapshotOffset snapshotOffset) {
			if ((generatedParseData != null) && (generatedParseData.Snapshot != null)) {
				// Translate back to the editor snapshot
				snapshotOffset = snapshotOffset.TranslateTo(snapshot, TextOffsetTrackingMode.Negative);

				foreach (var mapping in textRangeMappings) {
					if (mapping.Item1.IntersectsWith(snapshotOffset.Offset)) {
						var generatedSnapshotOffset = new TextSnapshotOffset(generatedParseData.Snapshot, mapping.Item2.StartOffset + (snapshotOffset.Offset - mapping.Item1.StartOffset));
						return generatedSnapshotOffset;
					}
				}
			}

			return null;
		}
		
		/// 
		/// Translates from a generated snapshot to an editor snapshot.
		/// 
		/// The snapshot offset.
		/// The translated snapshot, if within a child language section.
		public TextSnapshotOffset? TranslateGeneratedToEditor(TextSnapshotOffset snapshotOffset) {
			if ((generatedParseData != null) && (generatedParseData.Snapshot != null)) {
				// Translate back to the generated parse data's snapshot
				snapshotOffset = snapshotOffset.TranslateTo(generatedParseData.Snapshot, TextOffsetTrackingMode.Negative);

				foreach (var mapping in textRangeMappings) {
					if (mapping.Item2.IntersectsWith(snapshotOffset.Offset)) {
						var editorSnapshotOffset = new TextSnapshotOffset(snapshot, mapping.Item1.StartOffset + (snapshotOffset.Offset - mapping.Item2.StartOffset));
						return editorSnapshotOffset;
					}
				}
			}

			return null;
		}

	}

}