1 #region Copyright notice and license 2 // Protocol Buffers - Google's data interchange format 3 // Copyright 2018 Google Inc. All rights reserved. 4 // 5 // Use of this source code is governed by a BSD-style 6 // license that can be found in the LICENSE file or at 7 // https://developers.google.com/open-source/licenses/bsd 8 #endregion 9 10 using System.Collections.Generic; 11 using System.Collections.ObjectModel; 12 using System.Linq; 13 using static Google.Protobuf.Reflection.SourceCodeInfo.Types; 14 15 namespace Google.Protobuf.Reflection 16 { 17 /// <summary> 18 /// Provides additional information about the declaration of a descriptor, 19 /// such as source location and comments. 20 /// </summary> 21 public sealed class DescriptorDeclaration 22 { 23 /// <summary> 24 /// The descriptor this declaration relates to. 25 /// </summary> 26 public IDescriptor Descriptor { get; } 27 28 /// <summary> 29 /// The start line of the declaration within the source file. This value is 1-based. 30 /// </summary> 31 public int StartLine { get; } 32 /// <summary> 33 /// The start column of the declaration within the source file. This value is 1-based. 34 /// </summary> 35 public int StartColumn { get; } 36 37 /// <summary> 38 /// // The end line of the declaration within the source file. This value is 1-based. 39 /// </summary> 40 public int EndLine { get; } 41 /// <summary> 42 /// The end column of the declaration within the source file. This value is 1-based, and 43 /// exclusive. (The final character of the declaration is on the column before this value.) 44 /// </summary> 45 public int EndColumn { get; } 46 47 /// <summary> 48 /// Comments appearing before the declaration. Never null, but may be empty. Multi-line comments 49 /// are represented as a newline-separated string. Leading whitespace and the comment marker ("//") 50 /// are removed from each line. 51 /// </summary> 52 public string LeadingComments { get; } 53 54 /// <summary> 55 /// Comments appearing after the declaration. Never null, but may be empty. Multi-line comments 56 /// are represented as a newline-separated string. Leading whitespace and the comment marker ("//") 57 /// are removed from each line. 58 /// </summary> 59 public string TrailingComments { get; } 60 61 /// <summary> 62 /// Comments appearing before the declaration, but separated from it by blank 63 /// lines. Each string represents a newline-separated paragraph of comments. 64 /// Leading whitespace and the comment marker ("//") are removed from each line. 65 /// The list is never null, but may be empty. Likewise each element is never null, but may be empty. 66 /// </summary> 67 public IReadOnlyList<string> LeadingDetachedComments { get; } 68 DescriptorDeclaration(IDescriptor descriptor, Location location)69 private DescriptorDeclaration(IDescriptor descriptor, Location location) 70 { 71 // TODO: Validation 72 Descriptor = descriptor; 73 bool hasEndLine = location.Span.Count == 4; 74 // Lines and columns are 0-based in the proto. 75 StartLine = location.Span[0] + 1; 76 StartColumn = location.Span[1] + 1; 77 EndLine = hasEndLine ? location.Span[2] + 1 : StartLine; 78 EndColumn = location.Span[hasEndLine ? 3 : 2] + 1; 79 LeadingComments = location.LeadingComments; 80 TrailingComments = location.TrailingComments; 81 LeadingDetachedComments = new ReadOnlyCollection<string>(location.LeadingDetachedComments.ToList()); 82 } 83 FromProto(IDescriptor descriptor, Location location)84 internal static DescriptorDeclaration FromProto(IDescriptor descriptor, Location location) => 85 new DescriptorDeclaration(descriptor, location); 86 } 87 } 88