Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable nullability, round 3 of x #525

Merged
merged 4 commits into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/Markdig/Extensions/Abbreviations/AbbreviationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.

#nullable enable

using System.Collections.Generic;
using Markdig.Helpers;
using Markdig.Syntax;
Expand All @@ -22,20 +24,20 @@ public static bool HasAbbreviations(this MarkdownDocument document)

public static void AddAbbreviation(this MarkdownDocument document, string label, Abbreviation abbr)
{
if (document == null) ThrowHelper.ArgumentNullException(nameof(document));
if (label == null) ThrowHelper.ArgumentNullException_label();
if (abbr == null) ThrowHelper.ArgumentNullException(nameof(abbr));
if (document is null) ThrowHelper.ArgumentNullException(nameof(document));
if (label is null) ThrowHelper.ArgumentNullException_label();
if (abbr is null) ThrowHelper.ArgumentNullException(nameof(abbr));

var map = document.GetAbbreviations();
if (map == null)
if (map is null)
{
map = new Dictionary<string, Abbreviation>();
document.SetData(DocumentKey, map);
}
map[label] = abbr;
}

public static Dictionary<string, Abbreviation> GetAbbreviations(this MarkdownDocument document)
public static Dictionary<string, Abbreviation>? GetAbbreviations(this MarkdownDocument document)
{
return document.GetData(DocumentKey) as Dictionary<string, Abbreviation>;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Markdig/Extensions/Abbreviations/AbbreviationParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public override BlockState TryOpen(BlockProcessor processor)
return BlockState.BreakDiscard;
}

private void DocumentOnProcessInlinesBegin(InlineProcessor inlineProcessor, Inline inline)
private void DocumentOnProcessInlinesBegin(InlineProcessor inlineProcessor, Inline? inline)
{
inlineProcessor.Document.ProcessInlinesBegin -= DocumentOnProcessInlinesBegin;

Expand Down
20 changes: 11 additions & 9 deletions src/Markdig/Extensions/AutoIdentifiers/AutoIdentifierExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.

#nullable enable

using System.Collections.Generic;
using System.IO;
using Markdig.Helpers;
Expand Down Expand Up @@ -95,11 +97,11 @@ private void HeadingBlockParser_Closed(BlockProcessor processor, Block block)
headingBlock.ProcessInlinesEnd += HeadingBlock_ProcessInlinesEnd;
}

private void DocumentOnProcessInlinesBegin(InlineProcessor processor, Inline inline)
private void DocumentOnProcessInlinesBegin(InlineProcessor processor, Inline? inline)
{
var doc = processor.Document;
doc.ProcessInlinesBegin -= DocumentOnProcessInlinesBegin;
var dictionary = (Dictionary<string, HeadingLinkReferenceDefinition>)doc.GetData(this);
var dictionary = (Dictionary<string, HeadingLinkReferenceDefinition>)doc.GetData(this)!;
foreach (var keyPair in dictionary)
{
// Here we make sure that auto-identifiers will not override an existing link definition
Expand All @@ -118,7 +120,7 @@ private void DocumentOnProcessInlinesBegin(InlineProcessor processor, Inline inl
/// Callback when there is a reference to found to a heading.
/// Note that reference are only working if they are declared after.
/// </summary>
private Inline CreateLinkInlineForHeading(InlineProcessor inlineState, LinkReferenceDefinition linkRef, Inline child)
private Inline CreateLinkInlineForHeading(InlineProcessor inlineState, LinkReferenceDefinition linkRef, Inline? child)
{
var headingRef = (HeadingLinkReferenceDefinition) linkRef;
return new LinkInline()
Expand All @@ -135,23 +137,23 @@ private Inline CreateLinkInlineForHeading(InlineProcessor inlineState, LinkRefer
/// </summary>
/// <param name="processor">The processor.</param>
/// <param name="inline">The inline.</param>
private void HeadingBlock_ProcessInlinesEnd(InlineProcessor processor, Inline inline)
private void HeadingBlock_ProcessInlinesEnd(InlineProcessor processor, Inline? inline)
{
var identifiers = processor.Document.GetData(AutoIdentifierKey) as HashSet<string>;
if (identifiers == null)
if (identifiers is null)
{
identifiers = new HashSet<string>();
processor.Document.SetData(AutoIdentifierKey, identifiers);
}

var headingBlock = (HeadingBlock) processor.Block;
if (headingBlock.Inline == null)
var headingBlock = (HeadingBlock) processor.Block!;
if (headingBlock.Inline is null)
{
return;
}

// If id is already set, don't try to modify it
var attributes = processor.Block.GetAttributes();
var attributes = processor.Block!.GetAttributes();
if (attributes.Id != null)
{
return;
Expand All @@ -161,7 +163,7 @@ private void HeadingBlock_ProcessInlinesEnd(InlineProcessor processor, Inline in
var stripRenderer = rendererCache.Get();

stripRenderer.Render(headingBlock.Inline);
var headingText = stripRenderer.Writer.ToString();
var headingText = stripRenderer.Writer.ToString()!;
rendererCache.Release(stripRenderer);

// Urilize the link
Expand Down
6 changes: 4 additions & 2 deletions src/Markdig/Extensions/CustomContainers/CustomContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.

#nullable enable

using Markdig.Helpers;
using Markdig.Parsers;
using Markdig.Syntax;
Expand Down Expand Up @@ -33,7 +35,7 @@ public CustomContainer(BlockParser parser) : base(parser)
public StringSlice TriviaAfterFencedChar { get; set; }

/// <inheritdoc />
public string Info { get; set; }
public string? Info { get; set; }

/// <inheritdoc />
public StringSlice UnescapedInfo { get; set; }
Expand All @@ -42,7 +44,7 @@ public CustomContainer(BlockParser parser) : base(parser)
public StringSlice TriviaAfterInfo { get; set; }

/// <inheritdoc />
public string Arguments { get; set; }
public string? Arguments { get; set; }

/// <inheritdoc />
public StringSlice UnescapedArguments { get; set; }
Expand Down
18 changes: 10 additions & 8 deletions src/Markdig/Extensions/DefinitionLists/DefinitionListParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.

#nullable enable

using System;
using Markdig.Parsers;
using Markdig.Syntax;
Expand All @@ -25,7 +27,7 @@ public DefinitionListParser()
public override BlockState TryOpen(BlockProcessor processor)
{
var paragraphBlock = processor.LastBlock as ParagraphBlock;
if (processor.IsCodeIndent || paragraphBlock == null || paragraphBlock.LastLine - processor.LineIndex > 1)
if (processor.IsCodeIndent || paragraphBlock is null || paragraphBlock.LastLine - processor.LineIndex > 1)
{
return BlockState.None;
}
Expand All @@ -50,7 +52,7 @@ public override BlockState TryOpen(BlockProcessor processor)
processor.GoToColumn(column + 4);
}

var previousParent = paragraphBlock.Parent;
var previousParent = paragraphBlock.Parent!;
var currentDefinitionList = GetCurrentDefinitionList(paragraphBlock, previousParent);

processor.Discard(paragraphBlock);
Expand Down Expand Up @@ -102,7 +104,7 @@ public override BlockState TryOpen(BlockProcessor processor)
return BlockState.Continue;
}

private static DefinitionList GetCurrentDefinitionList(ParagraphBlock paragraphBlock, ContainerBlock previousParent)
private static DefinitionList? GetCurrentDefinitionList(ParagraphBlock paragraphBlock, ContainerBlock previousParent)
{
var index = previousParent.IndexOf(paragraphBlock) - 1;
if (index < 0) return null;
Expand All @@ -124,11 +126,11 @@ public override BlockState TryContinue(BlockProcessor processor, Block block)
return BlockState.Continue;
}

var list = (DefinitionList)definitionItem.Parent;
var list = (DefinitionList)definitionItem.Parent!;
var lastBlankLine = definitionItem.LastChild as BlankLineBlock;

// Check if we have another definition list
if (Array.IndexOf(OpeningCharacters, processor.CurrentChar) >= 0)
if (Array.IndexOf(OpeningCharacters!, processor.CurrentChar) >= 0)
{
var startPosition = processor.Start;
var column = processor.ColumnBeforeIndent;
Expand All @@ -145,7 +147,7 @@ public override BlockState TryContinue(BlockProcessor processor, Block block)
definitionItem.RemoveAt(definitionItem.Count - 1);
}

list.Span.End = list.LastChild.Span.End;
list.Span.End = list.LastChild!.Span.End;
return BlockState.None;
}

Expand Down Expand Up @@ -179,7 +181,7 @@ public override BlockState TryContinue(BlockProcessor processor, Block block)
}

var paragraphBlock = definitionItem.LastChild as ParagraphBlock;
if (lastBlankLine == null && paragraphBlock != null)
if (lastBlankLine is null && paragraphBlock != null)
{
return BlockState.Continue;
}
Expand All @@ -190,7 +192,7 @@ public override BlockState TryContinue(BlockProcessor processor, Block block)
definitionItem.RemoveAt(definitionItem.Count - 1);
}

list.Span.End = list.LastChild.Span.End;
list.Span.End = list.LastChild!.Span.End;
return BlockState.Break;
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/Markdig/Extensions/Emoji/EmojiMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.

#nullable enable

using System.Collections.Generic;
using Markdig.Helpers;

Expand Down Expand Up @@ -1778,7 +1780,7 @@ public EmojiMapping(IDictionary<string, string> shortcodeToUnicode, IDictionary<
if (string.IsNullOrEmpty(smiley.Key) || string.IsNullOrEmpty(smiley.Value))
ThrowHelper.ArgumentException("The dictionaries cannot contain null or empty keys/values", nameof(smileyToShortcode));

if (!shortcodeToUnicode.TryGetValue(smiley.Value, out string unicode))
if (!shortcodeToUnicode.TryGetValue(smiley.Value, out string? unicode))
ThrowHelper.ArgumentException(string.Format("Invalid smiley target: {0} is not present in the emoji shortcodes dictionary", smiley.Value));

firstChars.Add(smiley.Key[0]);
Expand Down
14 changes: 8 additions & 6 deletions src/Markdig/Extensions/Footnotes/FootnoteParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.

#nullable enable

using Markdig.Helpers;
using Markdig.Parsers;
using Markdig.Syntax;
Expand Down Expand Up @@ -41,7 +43,7 @@ private BlockState TryOpen(BlockProcessor processor, bool isContinue)

var saved = processor.Column;
int start = processor.Start;
if (!LinkHelper.TryParseLabel(ref processor.Line, false, out string label, out SourceSpan labelSpan) || !label.StartsWith("^") || processor.CurrentChar != ':')
if (!LinkHelper.TryParseLabel(ref processor.Line, false, out string? label, out SourceSpan labelSpan) || !label.StartsWith("^") || processor.CurrentChar != ':')
{
processor.GoToColumn(saved);
return BlockState.None;
Expand Down Expand Up @@ -130,12 +132,12 @@ public override BlockState TryContinue(BlockProcessor processor, Block block)
/// </summary>
/// <param name="state">The processor.</param>
/// <param name="inline">The inline.</param>
private void Document_ProcessInlinesEnd(InlineProcessor state, Inline inline)
private void Document_ProcessInlinesEnd(InlineProcessor state, Inline? inline)
{
// Unregister
state.Document.ProcessInlinesEnd -= Document_ProcessInlinesEnd;

var footnotes = ((FootnoteGroup)state.Document.GetData(DocumentKey));
var footnotes = (FootnoteGroup)state.Document.GetData(DocumentKey)!;
// Remove the footnotes from the document and readd them at the end
state.Document.Remove(footnotes);
state.Document.Add(footnotes);
Expand Down Expand Up @@ -166,7 +168,7 @@ private void Document_ProcessInlinesEnd(InlineProcessor state, Inline inline)

// Insert all footnote backlinks
var paragraphBlock = footnote.LastChild as ParagraphBlock;
if (paragraphBlock == null)
if (paragraphBlock is null)
{
paragraphBlock = new ParagraphBlock();
footnote.Add(paragraphBlock);
Expand All @@ -191,12 +193,12 @@ private void Document_ProcessInlinesEnd(InlineProcessor state, Inline inline)
}
}

private static Inline CreateLinkToFootnote(InlineProcessor state, LinkReferenceDefinition linkRef, Inline child)
private static Inline CreateLinkToFootnote(InlineProcessor state, LinkReferenceDefinition linkRef, Inline? child)
{
var footnote = ((FootnoteLinkReferenceDefinition)linkRef).Footnote;
if (footnote.Order < 0)
{
var footnotes = (FootnoteGroup)state.Document.GetData(DocumentKey);
var footnotes = (FootnoteGroup)state.Document.GetData(DocumentKey)!;
footnotes.CurrentOrder++;
footnote.Order = footnotes.CurrentOrder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)
}
}
}
var objectToAttach = inline == null || inline == processor.Root ? (MarkdownObject) processor.Block : inline;
var objectToAttach = inline is null || inline == processor.Root ? (MarkdownObject)processor.Block : inline;

// If the current block is a Paragraph, but only the HtmlAttributes is used,
// Try to attach the attributes to the following block
Expand Down
19 changes: 10 additions & 9 deletions src/Markdig/Extensions/MediaLinks/MediaLinkExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.

#nullable enable

using System;
using Markdig.Renderers;
using Markdig.Renderers.Html;
Expand All @@ -20,7 +22,7 @@ public MediaLinkExtension() : this(new MediaOptions())
{
}

public MediaLinkExtension(MediaOptions options)
public MediaLinkExtension(MediaOptions? options)
{
Options = options ?? new MediaOptions();
}
Expand Down Expand Up @@ -53,7 +55,7 @@ private bool TryLinkInlineRenderer(HtmlRenderer renderer, LinkInline linkInline)

bool isSchemaRelative = false;
// Only process absolute Uri
if (!Uri.TryCreate(linkInline.Url, UriKind.RelativeOrAbsolute, out Uri uri) || !uri.IsAbsoluteUri)
if (!Uri.TryCreate(linkInline.Url, UriKind.RelativeOrAbsolute, out Uri? uri) || !uri.IsAbsoluteUri)
{
// see https://tools.ietf.org/html/rfc3986#section-4.2
// since relative uri doesn't support many properties, "http" is used as a placeholder here.
Expand Down Expand Up @@ -98,7 +100,7 @@ private bool TryGuessAudioVideoFile(Uri uri, bool isSchemaRelative, HtmlRenderer
// Otherwise try to detect if we have an audio/video from the file extension
var lastDot = path.LastIndexOf('.');
if (lastDot >= 0 &&
Options.ExtensionToMimeType.TryGetValue(path.Substring(lastDot), out string mimeType))
Options.ExtensionToMimeType.TryGetValue(path.Substring(lastDot), out string? mimeType))
{
var htmlAttributes = GetHtmlAttributes(linkInline);
var isAudio = mimeType.StartsWith("audio");
Expand Down Expand Up @@ -126,9 +128,8 @@ private bool TryGuessAudioVideoFile(Uri uri, bool isSchemaRelative, HtmlRenderer

private bool TryRenderIframeFromKnownProviders(Uri uri, bool isSchemaRelative, HtmlRenderer renderer, LinkInline linkInline)
{

IHostProvider foundProvider = null;
string iframeUrl = null;
IHostProvider? foundProvider = null;
string? iframeUrl = null;
foreach (var provider in Options.Hosts)
{
if (!provider.TryHandle(uri, isSchemaRelative, out iframeUrl))
Expand All @@ -137,7 +138,7 @@ private bool TryRenderIframeFromKnownProviders(Uri uri, bool isSchemaRelative, H
break;
}

if (foundProvider == null)
if (foundProvider is null)
{
return false;
}
Expand All @@ -156,8 +157,8 @@ private bool TryRenderIframeFromKnownProviders(Uri uri, bool isSchemaRelative, H
if (!string.IsNullOrEmpty(Options.Class))
htmlAttributes.AddClass(Options.Class);

if (!string.IsNullOrEmpty(foundProvider.Class))
htmlAttributes.AddClass(foundProvider.Class);
if (foundProvider.Class is { Length: > 0 } className)
htmlAttributes.AddClass(className);

htmlAttributes.AddPropertyIfNotExist("frameborder", "0");
if (foundProvider.AllowFullScreen)
Expand Down
Loading