未验证 提交 1485bd79 编写于 作者: J Jason Malinowski 提交者: GitHub

Merge pull request #40339 from jasonmalinowski/null-annotate-editor-formatting-service

Null annotate IEditorFormattingService and the consuming command handler
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#nullable enable
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
......@@ -99,7 +101,7 @@ public bool SupportsFormattingOnTypedCharacter(Document document, char ch)
public async Task<IList<TextChange>> GetFormattingChangesAsync(Document document, TextSpan? textSpan, CancellationToken cancellationToken)
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var span = textSpan ?? new TextSpan(0, root.FullSpan.Length);
var formattingSpan = CommonFormattingHelpers.GetFormattingSpan(root, span);
......@@ -131,12 +133,12 @@ public async Task<IList<TextChange>> GetFormattingChangesOnPasteAsync(Document d
private IEnumerable<AbstractFormattingRule> GetFormattingRules(Document document, int position, SyntaxToken tokenBeforeCaret)
{
var workspace = document.Project.Solution.Workspace;
var formattingRuleFactory = workspace.Services.GetService<IHostDependentFormattingRuleFactoryService>();
var formattingRuleFactory = workspace.Services.GetRequiredService<IHostDependentFormattingRuleFactoryService>();
return formattingRuleFactory.CreateRule(document, position).Concat(GetTypingRules(tokenBeforeCaret)).Concat(Formatter.GetDefaultFormattingRules(document));
}
Task<IList<TextChange>> IEditorFormattingService.GetFormattingChangesOnReturnAsync(Document document, int caretPosition, CancellationToken cancellationToken)
=> SpecializedTasks.Default<IList<TextChange>>();
Task<IList<TextChange>?> IEditorFormattingService.GetFormattingChangesOnReturnAsync(Document document, int caretPosition, CancellationToken cancellationToken)
=> SpecializedTasks.Default<IList<TextChange>?>();
private static async Task<bool> TokenShouldNotFormatOnTypeCharAsync(
SyntaxToken token, CancellationToken cancellationToken)
......@@ -171,7 +173,7 @@ Task<IList<TextChange>> IEditorFormattingService.GetFormattingChangesOnReturnAsy
return false;
}
public async Task<IList<TextChange>> GetFormattingChangesAsync(Document document, char typedChar, int caretPosition, CancellationToken cancellationToken)
public async Task<IList<TextChange>?> GetFormattingChangesAsync(Document document, char typedChar, int caretPosition, CancellationToken cancellationToken)
{
// first, find the token user just typed.
var token = await GetTokenBeforeTheCaretAsync(document, caretPosition, cancellationToken).ConfigureAwait(false);
......@@ -268,7 +270,7 @@ private bool OnlySmartIndentOpenBrace(DocumentOptionSet options)
private static async Task<SyntaxToken> GetTokenBeforeTheCaretAsync(Document document, int caretPosition, CancellationToken cancellationToken)
{
var tree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);
var tree = await document.GetRequiredSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);
var position = Math.Max(0, caretPosition - 1);
var root = await tree.GetRootAsync(cancellationToken).ConfigureAwait(false);
......@@ -278,7 +280,7 @@ private static async Task<SyntaxToken> GetTokenBeforeTheCaretAsync(Document docu
private async Task<IList<TextChange>> FormatTokenAsync(Document document, OptionSet options, SyntaxToken token, IEnumerable<AbstractFormattingRule> formattingRules, CancellationToken cancellationToken)
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var formatter = CreateSmartTokenFormatter(options, formattingRules, root);
var changes = await formatter.FormatTokenAsync(document.Project.Solution.Workspace, token, cancellationToken).ConfigureAwait(false);
return changes;
......@@ -312,7 +314,7 @@ private ISmartTokenFormatter CreateSmartTokenFormatter(OptionSet optionSet, IEnu
return SpecializedCollections.EmptyList<TextChange>();
}
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var formatter = new CSharpSmartTokenFormatter(options, formattingRules, (CompilationUnitSyntax)root);
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#nullable enable
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.Commanding;
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#nullable enable
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#nullable enable
using System;
using System.Threading;
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#nullable enable
using System;
using Microsoft.VisualStudio.Commanding;
using Microsoft.VisualStudio.Text.Editor.Commanding.Commands;
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#nullable enable
using System;
using Microsoft.VisualStudio.Commanding;
using Microsoft.VisualStudio.Text.Editor.Commanding.Commands;
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#nullable enable
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
......@@ -54,7 +56,7 @@ internal partial class FormatCommandHandler :
private void Format(ITextView textView, Document document, TextSpan? selectionOpt, CancellationToken cancellationToken)
{
var formattingService = document.GetLanguageService<IEditorFormattingService>();
var formattingService = document.GetRequiredLanguageService<IEditorFormattingService>();
using (Logger.LogBlock(FunctionId.CommandHandler_FormatCommand, KeyValueLogMessage.Create(LogType.UserAction, m => m["Span"] = selectionOpt?.Length ?? -1), cancellationToken))
using (var transaction = CreateEditTransaction(textView, EditorFeaturesResources.Formatting))
......@@ -74,7 +76,7 @@ private void ApplyChanges(Document document, IList<TextChange> changes, TextSpan
{
if (selectionOpt.HasValue)
{
var ruleFactory = document.Project.Solution.Workspace.Services.GetService<IHostDependentFormattingRuleFactoryService>();
var ruleFactory = document.Project.Solution.Workspace.Services.GetRequiredService<IHostDependentFormattingRuleFactoryService>();
changes = ruleFactory.FilterFormattedChanges(document, selectionOpt.Value, changes).ToList();
if (changes.Count == 0)
......@@ -129,7 +131,7 @@ public void ExecuteReturnOrTypeCommand(EditorCommandArgs args, Action nextHandle
return;
}
IList<TextChange> textChanges;
IList<TextChange>? textChanges;
// save current caret position
if (args is ReturnKeyCommandArgs)
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#nullable enable
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
......@@ -37,11 +39,11 @@ internal interface IEditorFormattingService : ILanguageService
/// character. The position provided is the position of the caret in the document after
/// the character been inserted into the document.
/// </summary>
Task<IList<TextChange>> GetFormattingChangesAsync(Document document, char typedChar, int position, CancellationToken cancellationToken);
Task<IList<TextChange>?> GetFormattingChangesAsync(Document document, char typedChar, int position, CancellationToken cancellationToken);
/// <summary>
/// Returns the text changes necessary to format the document after the user enters a Return
/// The position provided is the position of the caret in the document after Return.</summary>
Task<IList<TextChange>> GetFormattingChangesOnReturnAsync(Document document, int position, CancellationToken cancellationToken);
Task<IList<TextChange>?> GetFormattingChangesOnReturnAsync(Document document, int position, CancellationToken cancellationToken);
}
}
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#nullable enable
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
......@@ -36,11 +38,11 @@ internal interface IFSharpEditorFormattingService
/// character. The position provided is the position of the caret in the document after
/// the character been inserted into the document.
/// </summary>
Task<IList<TextChange>> GetFormattingChangesAsync(Document document, char typedChar, int position, CancellationToken cancellationToken);
Task<IList<TextChange>?> GetFormattingChangesAsync(Document document, char typedChar, int position, CancellationToken cancellationToken);
/// <summary>
/// Returns the text changes necessary to format the document after the user enters a Return
/// The position provided is the position of the caret in the document after Return.</summary>
Task<IList<TextChange>> GetFormattingChangesOnReturnAsync(Document document, int position, CancellationToken cancellationToken);
Task<IList<TextChange>?> GetFormattingChangesOnReturnAsync(Document document, int position, CancellationToken cancellationToken);
}
}
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#nullable enable
using System;
using System.Collections.Generic;
using System.Composition;
......@@ -38,7 +40,7 @@ public Task<IList<TextChange>> GetFormattingChangesAsync(Document document, Text
return _service.GetFormattingChangesAsync(document, textSpan, cancellationToken);
}
public Task<IList<TextChange>> GetFormattingChangesAsync(Document document, char typedChar, int position, CancellationToken cancellationToken)
public Task<IList<TextChange>?> GetFormattingChangesAsync(Document document, char typedChar, int position, CancellationToken cancellationToken)
{
return _service.GetFormattingChangesAsync(document, typedChar, position, cancellationToken);
}
......@@ -48,7 +50,7 @@ public Task<IList<TextChange>> GetFormattingChangesOnPasteAsync(Document documen
return _service.GetFormattingChangesOnPasteAsync(document, textSpan, cancellationToken);
}
public Task<IList<TextChange>> GetFormattingChangesOnReturnAsync(Document document, int position, CancellationToken cancellationToken)
public Task<IList<TextChange>?> GetFormattingChangesOnReturnAsync(Document document, int position, CancellationToken cancellationToken)
{
return _service.GetFormattingChangesOnReturnAsync(document, position, cancellationToken);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册