Skip to content

Commit

Permalink
Make AutoFormattingOptions global
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat committed Feb 21, 2022
1 parent de42d54 commit 64ff0f7
Show file tree
Hide file tree
Showing 96 changed files with 686 additions and 738 deletions.
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.Editor.Host;
using Microsoft.CodeAnalysis.Editor.Implementation.DocumentationComments;
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 @@ -93,9 +93,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,12 @@ public void X()
}
}";

var optionSet = new Dictionary<OptionKey2, object>
{
{ new OptionKey2(AutoFormattingOptions.Metadata.AutoFormattingOnCloseBrace, LanguageNames.CSharp), false },
{ new OptionKey2(AutoFormattingOptions.Metadata.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.IndentationSize, LanguageNames.CSharp), FormattingOptions.IndentStyle.Block);

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

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

var optionSet = new Dictionary<OptionKey2, object>
{
{ new OptionKey2(AutoFormattingOptions.Metadata.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.IndentationSize, LanguageNames.CSharp), FormattingOptions.IndentStyle.None);

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

var optionSet = new Dictionary<OptionKey2, object>
{
{ new OptionKey2(AutoFormattingOptions.Metadata.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.IndentationSize, LanguageNames.CSharp), FormattingOptions.IndentStyle.Block);

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

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

var optionSet = new Dictionary<OptionKey2, object>
{
{ new OptionKey2(AutoFormattingOptions.Metadata.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.IndentationSize, LanguageNames.CSharp), FormattingOptions.IndentStyle.Block);

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

Expand Down
58 changes: 28 additions & 30 deletions src/EditorFeatures/CSharpTest/Formatting/FormattingEngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ private static Dictionary<OptionKey2, object> SmartIndentButDoNotFormatWhileTypi
{
return new Dictionary<OptionKey2, object>
{
{ new OptionKey2(AutoFormattingOptions.Metadata.SmartIndent, LanguageNames.CSharp), FormattingOptions.IndentStyle.Smart },
{ new OptionKey2(AutoFormattingOptions.Metadata.AutoFormattingOnTyping, LanguageNames.CSharp), false },
{ new OptionKey2(AutoFormattingOptions.Metadata.AutoFormattingOnCloseBrace, LanguageNames.CSharp), false },
{ new OptionKey2(AutoFormattingOptionsStorage.SmartIndent, LanguageNames.CSharp), FormattingOptions.IndentStyle.Smart },
{ new OptionKey2(AutoFormattingOptionsStorage.FormatOnTyping, LanguageNames.CSharp), false },
{ new OptionKey2(AutoFormattingOptionsStorage.FormatOnCloseBrace, LanguageNames.CSharp), false },
};
}

Expand Down Expand Up @@ -1077,11 +1077,11 @@ class C1<U>
class C1<U>
{
}";
var optionSet = new Dictionary<OptionKey2, object>
{
{ new OptionKey2(AutoFormattingOptions.Metadata.SmartIndent, LanguageNames.CSharp), FormattingOptions.IndentStyle.None }
};
AssertFormatAfterTypeChar(code, expected, optionSet);
var globalOptions = new Dictionary<OptionKey2, object>
{
{ new OptionKey2(AutoFormattingOptionsStorage.SmartIndent, LanguageNames.CSharp), FormattingOptions.IndentStyle.None }
};
AssertFormatAfterTypeChar(code, expected, globalOptions);
}

[WpfFact]
Expand All @@ -1108,12 +1108,12 @@ class C
}
";

var optionSet = new Dictionary<OptionKey2, object>
var globalOptions = new Dictionary<OptionKey2, object>
{
{ new OptionKey2(AutoFormattingOptions.Metadata.AutoFormattingOnCloseBrace, LanguageNames.CSharp), false }
{ new OptionKey2(AutoFormattingOptionsStorage.FormatOnCloseBrace, LanguageNames.CSharp), false }
};

AssertFormatAfterTypeChar(code, expected, optionSet);
AssertFormatAfterTypeChar(code, expected, globalOptions);
}

[WpfFact]
Expand All @@ -1140,12 +1140,12 @@ class C
}
";

var optionSet = new Dictionary<OptionKey2, object>
var globalOptions = new Dictionary<OptionKey2, object>
{
{ new OptionKey2(AutoFormattingOptions.Metadata.AutoFormattingOnTyping, LanguageNames.CSharp), false }
{ new OptionKey2(AutoFormattingOptionsStorage.FormatOnTyping, LanguageNames.CSharp), false }
};

AssertFormatAfterTypeChar(code, expected, optionSet);
AssertFormatAfterTypeChar(code, expected, globalOptions);
}

[WorkItem(5873, "https://github.com/dotnet/roslyn/issues/5873")]
Expand All @@ -1172,12 +1172,12 @@ static void Main()
}
}";

var optionSet = new Dictionary<OptionKey2, object>
var globalOptions = new Dictionary<OptionKey2, object>
{
{ new OptionKey2(AutoFormattingOptions.Metadata.AutoFormattingOnTyping, LanguageNames.CSharp), false }
{ new OptionKey2(AutoFormattingOptionsStorage.FormatOnTyping, LanguageNames.CSharp), false }
};

AssertFormatAfterTypeChar(code, expected, optionSet);
AssertFormatAfterTypeChar(code, expected, globalOptions);
}

[WorkItem(5873, "https://github.com/dotnet/roslyn/issues/5873")]
Expand Down Expand Up @@ -1230,12 +1230,12 @@ class C
}
";

var optionSet = new Dictionary<OptionKey2, object>
var globalOptions = new Dictionary<OptionKey2, object>
{
{ new OptionKey2(AutoFormattingOptions.Metadata.AutoFormattingOnSemicolon, LanguageNames.CSharp), false }
{ new OptionKey2(AutoFormattingOptionsStorage.FormatOnSemicolon, LanguageNames.CSharp), false }
};

AssertFormatAfterTypeChar(code, expected, optionSet);
AssertFormatAfterTypeChar(code, expected, globalOptions);
}

[WpfFact]
Expand All @@ -1262,12 +1262,12 @@ class C
}
";

var optionSet = new Dictionary<OptionKey2, object>
var globalOptions = new Dictionary<OptionKey2, object>
{
{ new OptionKey2(AutoFormattingOptions.Metadata.AutoFormattingOnTyping, LanguageNames.CSharp), false }
{ new OptionKey2(AutoFormattingOptionsStorage.FormatOnTyping, LanguageNames.CSharp), false }
};

AssertFormatAfterTypeChar(code, expected, optionSet);
AssertFormatAfterTypeChar(code, expected, globalOptions);
}

[WpfFact, WorkItem(4435, "https://github.com/dotnet/roslyn/issues/4435")]
Expand Down Expand Up @@ -2234,18 +2234,16 @@ class Test
Assert.Single(annotatedTrivia);
}

private static void AssertFormatAfterTypeChar(string code, string expected, Dictionary<OptionKey2, object> changedOptionSet = null)
private static void AssertFormatAfterTypeChar(string code, string expected, Dictionary<OptionKey2, object> globalOptions = null)
{
using var workspace = TestWorkspace.CreateCSharp(code);
if (changedOptionSet != null)
if (globalOptions != null)
{
var options = workspace.Options;
foreach (var entry in changedOptionSet)
var options = workspace.GlobalOptions;
foreach (var entry in globalOptions)
{
options = options.WithChangedOption(entry.Key, entry.Value);
options.SetGlobalOption((OptionKey)entry.Key, entry.Value);
}

workspace.TryApplyChanges(workspace.CurrentSolution.WithOptions(options));
}

var subjectDocument = workspace.Documents.Single();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ private static async Task TokenFormatWorkerAsync(TestWorkspace workspace, ITextB

var rules = formattingRuleProvider.CreateRule(document, position).Concat(Formatter.GetDefaultFormattingRules(document));

var options = await IndentationOptions.FromDocumentAsync(document, CancellationToken.None);
var options = new IndentationOptions(
await SyntaxFormattingOptions.FromDocumentAsync(document, CancellationToken.None).ConfigureAwait(false),
AutoFormattingOptions.Default);

var formatter = new CSharpSmartTokenFormatter(options, rules, root);
var changes = await formatter.FormatTokenAsync(workspace.Services, token, CancellationToken.None);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp.Formatting;
using Microsoft.CodeAnalysis.CSharp.Indentation;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces;
Expand Down Expand Up @@ -1501,21 +1502,17 @@ private static async Task AssertIndentUsingSmartTokenFormatterAsync(
// create tree service
using var workspace = TestWorkspace.CreateCSharp(code);

workspace.TryApplyChanges(workspace.CurrentSolution.WithOptions(workspace.Options
.WithChangedOption(UseTabs, LanguageNames.CSharp, useTabs)));

var hostdoc = workspace.Documents.First();

var buffer = hostdoc.GetTextBuffer();

var snapshot = buffer.CurrentSnapshot;
var line = snapshot.GetLineFromLineNumber(indentationLine);

var document = workspace.CurrentSolution.GetDocument(hostdoc.Id);

var root = (await document.GetSyntaxRootAsync()) as CompilationUnitSyntax;

var options = await IndentationOptions.FromDocumentAsync(document, CancellationToken.None);
var options = new IndentationOptions(
CSharpSyntaxFormattingOptions.Default.With(useTabs: useTabs, tabSize: 4, indentationSize: 4),
AutoFormattingOptions.Default);

Assert.True(
CSharpIndentationService.ShouldUseSmartTokenFormatterInsteadOfIndenter(
Expand Down Expand Up @@ -1545,9 +1542,7 @@ private static async Task AssertIndentNotUsingSmartTokenFormatterButUsingIndente
{
// create tree service
using var workspace = TestWorkspace.CreateCSharp(code);
workspace.TryApplyChanges(workspace.CurrentSolution.WithOptions(workspace.Options
.WithChangedOption(AutoFormattingOptions.Metadata.SmartIndent, LanguageNames.CSharp, indentStyle)
.WithChangedOption(UseTabs, LanguageNames.CSharp, useTabs)));

var hostdoc = workspace.Documents.First();
var buffer = hostdoc.GetTextBuffer();
var snapshot = buffer.CurrentSnapshot;
Expand All @@ -1558,14 +1553,16 @@ private static async Task AssertIndentNotUsingSmartTokenFormatterButUsingIndente

var root = (await document.GetSyntaxRootAsync()) as CompilationUnitSyntax;

var options = await IndentationOptions.FromDocumentAsync(document, CancellationToken.None);
var options = new IndentationOptions(
CSharpSyntaxFormattingOptions.Default.With(useTabs: useTabs, tabSize: 4, indentationSize: 4),
new AutoFormattingOptions(IndentStyle: indentStyle));

Assert.False(
CSharpIndentationService.ShouldUseSmartTokenFormatterInsteadOfIndenter(
Formatter.GetDefaultFormattingRules(document),
root, line.AsTextLine(), options, out _));

TestIndentation(workspace, indentationLine, expectedIndentation);
TestIndentation(workspace, indentationLine, expectedIndentation, indentStyle, useTabs);
}
}
}
Loading

0 comments on commit 64ff0f7

Please sign in to comment.