diff --git a/src/Workspaces/CSharp/Portable/Indentation/CSharpIndentationService.Indenter.cs b/src/Workspaces/CSharp/Portable/Indentation/CSharpIndentationService.Indenter.cs index bcd53246f399e20c4b3c92d90979e6e3256a067d..3a939e4f22e67e60c39452da9277112273deca10 100644 --- a/src/Workspaces/CSharp/Portable/Indentation/CSharpIndentationService.Indenter.cs +++ b/src/Workspaces/CSharp/Portable/Indentation/CSharpIndentationService.Indenter.cs @@ -31,9 +31,9 @@ protected override ISmartTokenFormatter CreateSmartTokenFormatter(Indenter inden return new CSharpSmartTokenFormatter(indenter.OptionSet, rules, indenter.Root); } - protected override IndentationResult GetDesiredIndentationWorker(Indenter indenter, SyntaxToken? tokenOpt, SyntaxTrivia? triviaOpt) + protected override IndentationResult? GetDesiredIndentationWorker(Indenter indenter, SyntaxToken? tokenOpt, SyntaxTrivia? triviaOpt) => TryGetDesiredIndentation(indenter, triviaOpt) ?? - TryGetDesiredIndentation(indenter, tokenOpt) ?? default; + TryGetDesiredIndentation(indenter, tokenOpt); private IndentationResult? TryGetDesiredIndentation(Indenter indenter, SyntaxTrivia? triviaOpt) { diff --git a/src/Workspaces/Core/Portable/Indentation/AbstractIndentationService.Indenter.cs b/src/Workspaces/Core/Portable/Indentation/AbstractIndentationService.Indenter.cs index c349f3225fe9b15bb02cce96a3340525cbefea0e..275167de12a41aceb1754863816a6fa7e07fd22b 100644 --- a/src/Workspaces/Core/Portable/Indentation/AbstractIndentationService.Indenter.cs +++ b/src/Workspaces/Core/Portable/Indentation/AbstractIndentationService.Indenter.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable + using System; using System.Collections.Generic; using System.Diagnostics; @@ -50,7 +52,7 @@ protected struct Indenter Document = document; _service = service; - _syntaxFacts = document.Document.GetLanguageService(); + _syntaxFacts = document.Document.GetRequiredLanguageService(); OptionSet = optionSet; OptionService = document.Document.Project.Solution.Workspace.Services.GetRequiredService(); Root = (TSyntaxRoot)document.Root; @@ -66,11 +68,11 @@ protected struct Indenter tokenStream: null); } - public IndentationResult GetDesiredIndentation(FormattingOptions.IndentStyle indentStyle) + public IndentationResult? GetDesiredIndentation(FormattingOptions.IndentStyle indentStyle) { // If the caller wants no indent, then we'll return an effective '0' indent. if (indentStyle == FormattingOptions.IndentStyle.None) - return default; + return null; // If the user has explicitly set 'block' indentation, or they're in an inactive preprocessor region, // then just do simple block indentation. @@ -84,7 +86,7 @@ public IndentationResult GetDesiredIndentation(FormattingOptions.IndentStyle ind return GetDesiredSmartIndentation(); } - private readonly IndentationResult GetDesiredSmartIndentation() + private readonly IndentationResult? GetDesiredSmartIndentation() { // For smart indent, we generally will be computing from either the previous token in the code, or in a // few special cases, the previous trivia. @@ -97,7 +99,7 @@ public IndentationResult GetDesiredIndentation(FormattingOptions.IndentStyle ind var trivia = TryGetImmediatelyPrecedingVisibleTrivia(); if (token == null && trivia == null) - return default; + return null; return _service.GetDesiredIndentationWorker(this, token, trivia); } @@ -143,7 +145,7 @@ public IndentationResult GetDesiredIndentation(FormattingOptions.IndentStyle ind return token; } - private IndentationResult GetDesiredBlockIndentation() + private IndentationResult? GetDesiredBlockIndentation() { // Block indentation is simple, we keep walking back lines until we find a line with any sort of // text on it. We then set our indentation to whatever the indentation of that line was. @@ -158,8 +160,8 @@ private IndentationResult GetDesiredBlockIndentation() return new IndentationResult(basePosition: line.Start + offset.Value, offset: 0); } - // Couldn't find a previous non-blank line. Don't indent at all. - return default; + // Couldn't find a previous non-blank line. + return null; } public bool TryGetSmartTokenIndentation(out IndentationResult indentationResult) diff --git a/src/Workspaces/Core/Portable/Indentation/AbstractIndentationService.cs b/src/Workspaces/Core/Portable/Indentation/AbstractIndentationService.cs index 1596ad1e09504d95df833e0934d0cd15164907eb..d0ca40d8e9b9faf0194e30b9aff0e2030c33cb56 100644 --- a/src/Workspaces/Core/Portable/Indentation/AbstractIndentationService.cs +++ b/src/Workspaces/Core/Portable/Indentation/AbstractIndentationService.cs @@ -2,12 +2,13 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable + using System.Collections.Generic; using System.Linq; using System.Threading; using Microsoft.CodeAnalysis.Formatting; using Microsoft.CodeAnalysis.Formatting.Rules; -using Microsoft.CodeAnalysis.Text; using Roslyn.Utilities; namespace Microsoft.CodeAnalysis.Indentation @@ -45,7 +46,8 @@ private IEnumerable GetFormattingRules(Document document return indentationResult; } - return indenter.GetDesiredIndentation(indentStyle); + // If the indenter can't produce a valid result, just default to 0 as our indentation. + return indenter.GetDesiredIndentation(indentStyle) ?? default; } private Indenter GetIndenter(Document document, int lineNumber, FormattingOptions.IndentStyle indentStyle, CancellationToken cancellationToken) @@ -70,7 +72,7 @@ private Indenter GetIndenter(Document document, int lineNumber, FormattingOption protected abstract bool ShouldUseTokenIndenter(Indenter indenter, out SyntaxToken token); protected abstract ISmartTokenFormatter CreateSmartTokenFormatter(Indenter indenter); - protected abstract IndentationResult GetDesiredIndentationWorker( + protected abstract IndentationResult? GetDesiredIndentationWorker( Indenter indenter, SyntaxToken? token, SyntaxTrivia? trivia); } } diff --git a/src/Workspaces/VisualBasic/Portable/Indentation/VisualBasicIndentationService.Indenter.vb b/src/Workspaces/VisualBasic/Portable/Indentation/VisualBasicIndentationService.Indenter.vb index 56bac90b17e1d3aed8729f617ae84bcd239e7221..b774dc108bb4e23ed5d45401b0b73a21d34d4f9e 100644 --- a/src/Workspaces/VisualBasic/Portable/Indentation/VisualBasicIndentationService.Indenter.vb +++ b/src/Workspaces/VisualBasic/Portable/Indentation/VisualBasicIndentationService.Indenter.vb @@ -27,7 +27,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Indentation Protected Overrides Function GetDesiredIndentationWorker( indenter As Indenter, tokenOpt As SyntaxToken?, - triviaOpt As SyntaxTrivia?) As IndentationResult + triviaOpt As SyntaxTrivia?) As IndentationResult? If triviaOpt.HasValue Then Dim trivia = triviaOpt.Value