Skip to content

Commit

Permalink
Revert "Revert "Global indentation options (dotnet#59679)" (dotnet#60199
Browse files Browse the repository at this point in the history
)"

This reverts commit ab57ce8.
  • Loading branch information
tmat committed Apr 7, 2022
1 parent a6d0805 commit a4f8367
Show file tree
Hide file tree
Showing 103 changed files with 850 additions and 801 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,22 @@
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Formatting;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Indentation;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Roslyn.Utilities;

#if CODE_STYLE
using OptionSet = Microsoft.CodeAnalysis.Diagnostics.AnalyzerConfigOptions;
#else
using Microsoft.CodeAnalysis.Options;
#endif

namespace Microsoft.CodeAnalysis.CSharp.ConvertNamespace
{
using static ConvertNamespaceAnalysis;
Expand Down Expand Up @@ -60,7 +69,14 @@ protected override async Task FixAllAsync(
var diagnostic = diagnostics.First();

var namespaceDecl = (BaseNamespaceDeclarationSyntax)diagnostic.AdditionalLocations[0].FindNode(cancellationToken);
var converted = await ConvertAsync(document, namespaceDecl, cancellationToken).ConfigureAwait(false);

#if CODE_STYLE
var configOptions = document.Project.AnalyzerOptions.GetAnalyzerOptionSet(namespaceDecl.SyntaxTree, cancellationToken);
var options = CSharpSyntaxFormattingOptions.Create(configOptions);
#else
var options = await SyntaxFormattingOptions.FromDocumentAsync(document, cancellationToken).ConfigureAwait(false);
#endif
var converted = await ConvertAsync(document, namespaceDecl, options, cancellationToken).ConfigureAwait(false);

editor.ReplaceNode(
editor.OriginalRoot,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,29 @@ namespace Microsoft.CodeAnalysis.CSharp.ConvertNamespace
{
internal static class ConvertNamespaceTransform
{
public static async Task<Document> ConvertAsync(Document document, BaseNamespaceDeclarationSyntax baseNamespace, CancellationToken cancellationToken)
public static async Task<Document> ConvertAsync(Document document, BaseNamespaceDeclarationSyntax baseNamespace, SyntaxFormattingOptions options, CancellationToken cancellationToken)
{
switch (baseNamespace)
{
case FileScopedNamespaceDeclarationSyntax fileScopedNamespace:
return await ConvertFileScopedNamespaceAsync(document, fileScopedNamespace, cancellationToken).ConfigureAwait(false);

case NamespaceDeclarationSyntax namespaceDeclaration:
var (doc, _) = await ConvertNamespaceDeclarationAsync(document, namespaceDeclaration, cancellationToken).ConfigureAwait(false);
var (doc, _) = await ConvertNamespaceDeclarationAsync(document, namespaceDeclaration, options, cancellationToken).ConfigureAwait(false);
return doc;

default:
throw ExceptionUtilities.UnexpectedValue(baseNamespace.Kind());
}
}

public static async Task<(Document document, TextSpan semicolonSpan)> ConvertNamespaceDeclarationAsync(Document document, NamespaceDeclarationSyntax namespaceDeclaration, CancellationToken cancellationToken)
public static async Task<(Document document, TextSpan semicolonSpan)> ConvertNamespaceDeclarationAsync(Document document, NamespaceDeclarationSyntax namespaceDeclaration, SyntaxFormattingOptions options, CancellationToken cancellationToken)
{
// First, determine how much indentation we had inside the original block namespace. We'll attempt to remove
// that much indentation from each applicable line after we conver the block namespace to a file scoped
// namespace.
var indentation = await GetIndentationAsync(document, namespaceDeclaration, cancellationToken).ConfigureAwait(false);

var indentation = await GetIndentationAsync(document, namespaceDeclaration, options, cancellationToken).ConfigureAwait(false);

// Next, actually replace the block namespace with the file scoped namespace.
var annotation = new SyntaxAnnotation();
Expand All @@ -77,7 +78,7 @@ public static async Task<Document> ConvertAsync(Document document, BaseNamespace
return (document.WithSyntaxRoot(updatedRoot), fileScopedNamespace.SemicolonToken.Span);
}

private static async Task<string?> GetIndentationAsync(Document document, NamespaceDeclarationSyntax namespaceDeclaration, CancellationToken cancellationToken)
private static async Task<string?> GetIndentationAsync(Document document, NamespaceDeclarationSyntax namespaceDeclaration, SyntaxFormattingOptions options, CancellationToken cancellationToken)
{
var indentationService = document.GetRequiredLanguageService<IIndentationService>();
var sourceText = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
Expand All @@ -87,19 +88,12 @@ public static async Task<Document> ConvertAsync(Document document, BaseNamespace
if (openBraceLine == closeBraceLine)
return null;

#if CODE_STYLE
var options = document.Project.AnalyzerOptions.AnalyzerConfigOptionsProvider.GetOptions(namespaceDeclaration.SyntaxTree!);
#else
var options = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);
#endif
var style = options.GetOption(FormattingOptions2.SmartIndent, document.Project.Language);

var indentation = indentationService.GetIndentation(document, openBraceLine + 1, (FormattingOptions.IndentStyle)style, cancellationToken);
// Auto-formatting options are not relevant since they only control behavior on typing.
var indentationOptions = new IndentationOptions(options, AutoFormattingOptions.Default);

var useTabs = options.GetOption(FormattingOptions2.UseTabs);
var tabSize = options.GetOption(FormattingOptions2.TabSize);
var indentation = indentationService.GetIndentation(document, openBraceLine + 1, indentationOptions, cancellationToken);

return indentation.GetIndentationString(sourceText, useTabs, tabSize);
return indentation.GetIndentationString(sourceText, options.UseTabs, options.TabSize);
}

private static async Task<(Document document, TextSpan semicolonSpan)> DedentNamespaceAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ public void ExecuteCommand(TypeCharCommandArgs args, Action nextCommandHandler,
if (!ConvertNamespaceAnalysis.CanOfferUseFileScoped(s_optionSet, root, namespaceDecl, forAnalyzer: true, LanguageVersion.CSharp10))
return default;

var (converted, semicolonSpan) = ConvertNamespaceTransform.ConvertNamespaceDeclarationAsync(document, namespaceDecl, cancellationToken).WaitAndGetResult(cancellationToken);
var formattingOptions = SyntaxFormattingOptions.FromDocumentAsync(document, cancellationToken).WaitAndGetResult(cancellationToken);
var (converted, semicolonSpan) = ConvertNamespaceTransform.ConvertNamespaceDeclarationAsync(document, namespaceDecl, formattingOptions, cancellationToken).WaitAndGetResult(cancellationToken);
var text = converted.GetTextSynchronously(cancellationToken);
return (text, semicolonSpan);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.CodeAnalysis.DocumentationComments;
using Microsoft.CodeAnalysis.Editor.Host;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Options;
using Microsoft.VisualStudio.Commanding;
using Microsoft.VisualStudio.Language.Intellisense.AsyncCompletion;
using Microsoft.VisualStudio.Text.Operations;
Expand All @@ -20,16 +21,17 @@ namespace Microsoft.CodeAnalysis.Editor.CSharp.DocumentationComments
[Name(PredefinedCommandHandlerNames.DocumentationComments)]
[Order(After = PredefinedCommandHandlerNames.Rename)]
[Order(After = PredefinedCompletionNames.CompletionCommandHandler)]
internal class DocumentationCommentCommandHandler
internal sealed class DocumentationCommentCommandHandler
: AbstractDocumentationCommentCommandHandler
{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public DocumentationCommentCommandHandler(
IUIThreadOperationExecutor uiThreadOperationExecutor,
ITextUndoHistoryRegistry undoHistoryRegistry,
IEditorOperationsFactoryService editorOperationsFactoryService)
: base(uiThreadOperationExecutor, undoHistoryRegistry, editorOperationsFactoryService)
IEditorOperationsFactoryService editorOperationsFactoryService,
IGlobalOptionService globalOptions)
: base(uiThreadOperationExecutor, undoHistoryRegistry, editorOperationsFactoryService, globalOptions)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,10 @@ SyntaxKind.InterpolatedSingleLineRawStringStartToken or
return false;
}

var indentation = token.GetPreferredIndentation(document, cancellationToken);
var indentationOptions = _globalOptions.GetIndentationOptionsAsync(document, cancellationToken).WaitAndGetResult(cancellationToken);
var indentation = token.GetPreferredIndentation(document, indentationOptions, cancellationToken);

var newLine = document.Project.Solution.Options.GetOption(FormattingOptions.NewLine, LanguageNames.CSharp);
var newLine = indentationOptions.FormattingOptions.NewLine;

using var transaction = CaretPreservingEditTransaction.TryCreate(
CSharpEditorResources.Split_string, textView, _undoHistoryRegistry, _editorOperationsFactoryService);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Indentation;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.Commanding;
Expand Down Expand Up @@ -120,12 +121,12 @@ private bool SplitString(ITextView textView, ITextBuffer subjectBuffer, int posi
}

// TODO: read option from textView.Options (https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1412138)
var indentStyle = document.Project.Solution.Options.GetOption(FormattingOptions.SmartIndent, LanguageNames.CSharp);
var options = _globalOptions.GetIndentationOptionsAsync(document, cancellationToken).WaitAndGetResult(cancellationToken);

using var transaction = CaretPreservingEditTransaction.TryCreate(
CSharpEditorResources.Split_string, textView, _undoHistoryRegistry, _editorOperationsFactoryService);

var splitter = StringSplitter.TryCreate(document, position, useTabs, tabSize, indentStyle, cancellationToken);
var splitter = StringSplitter.TryCreate(document, position, options, useTabs, tabSize, cancellationToken);
if (splitter?.TrySplit(out var newDocument, out var newPosition) != true)
{
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1117,14 +1117,13 @@ public void X()
}
}";

var optionSet = new Dictionary<OptionKey2, object>
{
{ new OptionKey2(AutoFormattingOptions.Metadata.AutoFormattingOnCloseBrace, LanguageNames.CSharp), false },
{ new OptionKey2(FormattingOptions2.SmartIndent, LanguageNames.CSharp), FormattingOptions.IndentStyle.Block }
};
using var session = CreateSession(code, optionSet);

using var session = CreateSession(code);
Assert.NotNull(session);

session.Workspace.GlobalOptions.SetGlobalOption(new OptionKey(AutoFormattingOptionsStorage.FormatOnCloseBrace, LanguageNames.CSharp), false);
session.Workspace.GlobalOptions.SetGlobalOption(new OptionKey(FormattingOptions.SmartIndent, LanguageNames.CSharp), FormattingOptions.IndentStyle.Block);

CheckStart(session.Session);
Assert.Equal(expected, session.Session.SubjectBuffer.CurrentSnapshot.GetText());

Expand All @@ -1147,13 +1146,11 @@ public class C1
{ }
}";

var optionSet = new Dictionary<OptionKey2, object>
{
{ new OptionKey2(FormattingOptions2.SmartIndent, LanguageNames.CSharp), FormattingOptions.IndentStyle.None }
};
using var session = CreateSession(code, optionSet);
using var session = CreateSession(code);
Assert.NotNull(session);

session.Workspace.GlobalOptions.SetGlobalOption(new OptionKey(FormattingOptions.SmartIndent, LanguageNames.CSharp), FormattingOptions.IndentStyle.None);

CheckStart(session.Session);
Assert.Equal(expected, session.Session.SubjectBuffer.CurrentSnapshot.GetText());
}
Expand Down Expand Up @@ -1182,13 +1179,11 @@ public class C1
}
}";

var optionSet = new Dictionary<OptionKey2, object>
{
{ new OptionKey2(FormattingOptions2.SmartIndent, LanguageNames.CSharp), FormattingOptions.IndentStyle.Block }
};
using var session = CreateSession(code, optionSet);
using var session = CreateSession(code);
Assert.NotNull(session);

session.Workspace.GlobalOptions.SetGlobalOption(new OptionKey(FormattingOptions.SmartIndent, LanguageNames.CSharp), FormattingOptions.IndentStyle.Block);

CheckStart(session.Session);
Assert.Equal(expected, session.Session.SubjectBuffer.CurrentSnapshot.GetText());

Expand Down Expand Up @@ -1225,13 +1220,11 @@ public class C1
}
}";

var optionSet = new Dictionary<OptionKey2, object>
{
{ new OptionKey2(FormattingOptions2.SmartIndent, LanguageNames.CSharp), FormattingOptions.IndentStyle.Block }
};
using var session = CreateSession(code, optionSet);
using var session = CreateSession(code);
Assert.NotNull(session);

session.Workspace.GlobalOptions.SetGlobalOption(new OptionKey(FormattingOptions.SmartIndent, LanguageNames.CSharp), FormattingOptions.IndentStyle.Block);

CheckStart(session.Session);
Assert.Equal(expected, session.Session.SubjectBuffer.CurrentSnapshot.GetText());

Expand Down
Loading

0 comments on commit a4f8367

Please sign in to comment.