Browse Source

#2500: nullable enable XmlDocumentationElement

pull/2509/head
Siegfried Pammer 4 years ago
parent
commit
47dcf0507f
  1. 44
      ICSharpCode.Decompiler/Documentation/XmlDocumentationElement.cs

44
ICSharpCode.Decompiler/Documentation/XmlDocumentationElement.cs

@ -16,6 +16,8 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE. // DEALINGS IN THE SOFTWARE.
#nullable enable
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -40,10 +42,10 @@ namespace ICSharpCode.Decompiler.Documentation
return new XmlDocumentationElement(XElement.Parse(documentationComment), declaringEntity, null); return new XmlDocumentationElement(XElement.Parse(documentationComment), declaringEntity, null);
} }
readonly XElement element; readonly XElement? element;
readonly IEntity declaringEntity; readonly IEntity? declaringEntity;
readonly Func<string, IEntity> crefResolver; readonly Func<string, IEntity?>? crefResolver;
volatile string textContent; volatile string? textContent;
/// <summary> /// <summary>
/// Inheritance level; used to prevent cyclic doc inheritance. /// Inheritance level; used to prevent cyclic doc inheritance.
@ -53,7 +55,7 @@ namespace ICSharpCode.Decompiler.Documentation
/// <summary> /// <summary>
/// Creates a new documentation element. /// Creates a new documentation element.
/// </summary> /// </summary>
public XmlDocumentationElement(XElement element, IEntity declaringEntity, Func<string, IEntity> crefResolver) public XmlDocumentationElement(XElement element, IEntity? declaringEntity, Func<string, IEntity?>? crefResolver)
{ {
if (element == null) if (element == null)
throw new ArgumentNullException(nameof(element)); throw new ArgumentNullException(nameof(element));
@ -65,7 +67,7 @@ namespace ICSharpCode.Decompiler.Documentation
/// <summary> /// <summary>
/// Creates a new documentation element. /// Creates a new documentation element.
/// </summary> /// </summary>
public XmlDocumentationElement(string text, IEntity declaringEntity) public XmlDocumentationElement(string text, IEntity? declaringEntity)
{ {
if (text == null) if (text == null)
throw new ArgumentNullException(nameof(text)); throw new ArgumentNullException(nameof(text));
@ -77,26 +79,26 @@ namespace ICSharpCode.Decompiler.Documentation
/// Gets the entity on which this documentation was originally declared. /// Gets the entity on which this documentation was originally declared.
/// May return null. /// May return null.
/// </summary> /// </summary>
public IEntity DeclaringEntity { public IEntity? DeclaringEntity {
get { return declaringEntity; } get { return declaringEntity; }
} }
IEntity referencedEntity; IEntity? referencedEntity;
volatile bool referencedEntityInitialized; volatile bool referencedEntityInitialized;
/// <summary> /// <summary>
/// Gets the entity referenced by the 'cref' attribute. /// Gets the entity referenced by the 'cref' attribute.
/// May return null. /// May return null.
/// </summary> /// </summary>
public IEntity ReferencedEntity { public IEntity? ReferencedEntity {
get { get {
if (!referencedEntityInitialized) if (!referencedEntityInitialized)
{ {
string cref = GetAttribute("cref"); string? cref = GetAttribute("cref");
try try
{ {
if (!string.IsNullOrEmpty(cref) && crefResolver != null) if (!string.IsNullOrEmpty(cref) && crefResolver != null)
referencedEntity = crefResolver(cref); referencedEntity = crefResolver(cref!);
} }
catch catch
{ {
@ -120,7 +122,7 @@ namespace ICSharpCode.Decompiler.Documentation
/// <summary> /// <summary>
/// Gets the attribute value. /// Gets the attribute value.
/// </summary> /// </summary>
public string GetAttribute(string name) public string? GetAttribute(string? name)
{ {
return element?.Attribute(name)?.Value; return element?.Attribute(name)?.Value;
} }
@ -148,7 +150,7 @@ namespace ICSharpCode.Decompiler.Documentation
} }
} }
IList<XmlDocumentationElement> children; IList<XmlDocumentationElement>? children;
/// <summary> /// <summary>
/// Gets the child elements. /// Gets the child elements.
@ -159,7 +161,7 @@ namespace ICSharpCode.Decompiler.Documentation
return EmptyList<XmlDocumentationElement>.Instance; return EmptyList<XmlDocumentationElement>.Instance;
return LazyInitializer.EnsureInitialized( return LazyInitializer.EnsureInitialized(
ref this.children, ref this.children,
() => CreateElements(element.Nodes(), declaringEntity, crefResolver, nestingLevel)); () => CreateElements(element.Nodes(), declaringEntity, crefResolver, nestingLevel))!;
} }
} }
@ -168,7 +170,7 @@ namespace ICSharpCode.Decompiler.Documentation
"remarks", "returns", "threadsafety", "value" "remarks", "returns", "threadsafety", "value"
}; };
static List<XmlDocumentationElement> CreateElements(IEnumerable<XObject> childObjects, IEntity declaringEntity, Func<string, IEntity> crefResolver, int nestingLevel) static List<XmlDocumentationElement> CreateElements(IEnumerable<XObject?> childObjects, IEntity? declaringEntity, Func<string, IEntity?>? crefResolver, int nestingLevel)
{ {
List<XmlDocumentationElement> list = new List<XmlDocumentationElement>(); List<XmlDocumentationElement> list = new List<XmlDocumentationElement>();
foreach (var child in childObjects) foreach (var child in childObjects)
@ -189,9 +191,9 @@ namespace ICSharpCode.Decompiler.Documentation
if (nestingLevel < 5 && childElement.Name == "inheritdoc") if (nestingLevel < 5 && childElement.Name == "inheritdoc")
{ {
string cref = childElement.Attribute("cref").Value; string cref = childElement.Attribute("cref").Value;
IEntity inheritedFrom = null; IEntity? inheritedFrom = null;
string inheritedDocumentation = null; string? inheritedDocumentation = null;
if (cref != null) if (cref != null && crefResolver != null)
{ {
inheritedFrom = crefResolver(cref); inheritedFrom = crefResolver(cref);
if (inheritedFrom != null) if (inheritedFrom != null)
@ -225,7 +227,7 @@ namespace ICSharpCode.Decompiler.Documentation
var inheritedChildren = doc.Nodes().Where( var inheritedChildren = doc.Nodes().Where(
inheritedObject => { inheritedObject => {
XElement inheritedElement = inheritedObject as XElement; XElement? inheritedElement = inheritedObject as XElement;
return !(inheritedElement != null && doNotInherit.Contains(inheritedElement.Name.LocalName)); return !(inheritedElement != null && doNotInherit.Contains(inheritedElement.Name.LocalName));
}); });
@ -244,14 +246,14 @@ namespace ICSharpCode.Decompiler.Documentation
if (string.IsNullOrWhiteSpace(list[0].textContent)) if (string.IsNullOrWhiteSpace(list[0].textContent))
list.RemoveAt(0); list.RemoveAt(0);
else else
list[0].textContent = list[0].textContent.TrimStart(); list[0].textContent = list[0].textContent!.TrimStart();
} }
if (list.Count > 0 && list[list.Count - 1].IsTextNode) if (list.Count > 0 && list[list.Count - 1].IsTextNode)
{ {
if (string.IsNullOrWhiteSpace(list[list.Count - 1].textContent)) if (string.IsNullOrWhiteSpace(list[list.Count - 1].textContent))
list.RemoveAt(list.Count - 1); list.RemoveAt(list.Count - 1);
else else
list[list.Count - 1].textContent = list[list.Count - 1].textContent.TrimEnd(); list[list.Count - 1].textContent = list[list.Count - 1].textContent!.TrimEnd();
} }
return list; return list;
} }

Loading…
Cancel
Save