Skip to content

Commit ab57ce8

Browse files
authored
Revert "Global indentation options (dotnet#59679)" (dotnet#60199)
This reverts commit 5fd5e5b.
1 parent 01e272d commit ab57ce8

99 files changed

Lines changed: 722 additions & 803 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/EditorFeatures/CSharp/ConvertNamespace/ConvertNamespaceCommandHandler.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,7 @@ public void ExecuteCommand(TypeCharCommandArgs args, Action nextCommandHandler,
151151
if (!ConvertNamespaceAnalysis.CanOfferUseFileScoped(s_optionSet, root, namespaceDecl, forAnalyzer: true, LanguageVersion.CSharp10))
152152
return default;
153153

154-
var formattingOptions = SyntaxFormattingOptions.FromDocumentAsync(document, cancellationToken).WaitAndGetResult(cancellationToken);
155-
var (converted, semicolonSpan) = ConvertNamespaceTransform.ConvertNamespaceDeclarationAsync(document, namespaceDecl, formattingOptions, cancellationToken).WaitAndGetResult(cancellationToken);
154+
var (converted, semicolonSpan) = ConvertNamespaceTransform.ConvertNamespaceDeclarationAsync(document, namespaceDecl, cancellationToken).WaitAndGetResult(cancellationToken);
156155
var text = converted.GetTextSynchronously(cancellationToken);
157156
return (text, semicolonSpan);
158157
}

src/EditorFeatures/CSharp/DocumentationComments/DocumentationCommentCommandHandler.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using Microsoft.CodeAnalysis.DocumentationComments;
99
using Microsoft.CodeAnalysis.Editor.Host;
1010
using Microsoft.CodeAnalysis.Host.Mef;
11-
using Microsoft.CodeAnalysis.Options;
1211
using Microsoft.VisualStudio.Commanding;
1312
using Microsoft.VisualStudio.Language.Intellisense.AsyncCompletion;
1413
using Microsoft.VisualStudio.Text.Operations;
@@ -21,17 +20,16 @@ namespace Microsoft.CodeAnalysis.Editor.CSharp.DocumentationComments
2120
[Name(PredefinedCommandHandlerNames.DocumentationComments)]
2221
[Order(After = PredefinedCommandHandlerNames.Rename)]
2322
[Order(After = PredefinedCompletionNames.CompletionCommandHandler)]
24-
internal sealed class DocumentationCommentCommandHandler
23+
internal class DocumentationCommentCommandHandler
2524
: AbstractDocumentationCommentCommandHandler
2625
{
2726
[ImportingConstructor]
2827
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
2928
public DocumentationCommentCommandHandler(
3029
IUIThreadOperationExecutor uiThreadOperationExecutor,
3130
ITextUndoHistoryRegistry undoHistoryRegistry,
32-
IEditorOperationsFactoryService editorOperationsFactoryService,
33-
IGlobalOptionService globalOptions)
34-
: base(uiThreadOperationExecutor, undoHistoryRegistry, editorOperationsFactoryService, globalOptions)
31+
IEditorOperationsFactoryService editorOperationsFactoryService)
32+
: base(uiThreadOperationExecutor, undoHistoryRegistry, editorOperationsFactoryService)
3533
{
3634
}
3735

src/EditorFeatures/CSharp/RawStringLiteral/RawStringLiteralCommandHandler_Return.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,9 @@ SyntaxKind.InterpolatedSingleLineRawStringStartToken or
9797
return false;
9898
}
9999

100-
var indentationOptions = _globalOptions.GetIndentationOptionsAsync(document, cancellationToken).WaitAndGetResult(cancellationToken);
101-
var indentation = token.GetPreferredIndentation(document, indentationOptions, cancellationToken);
100+
var indentation = token.GetPreferredIndentation(document, cancellationToken);
102101

103-
var newLine = indentationOptions.FormattingOptions.NewLine;
102+
var newLine = document.Project.Solution.Options.GetOption(FormattingOptions.NewLine, LanguageNames.CSharp);
104103

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

src/EditorFeatures/CSharp/SplitStringLiteral/SplitStringLiteralCommandHandler.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
1414
using Microsoft.CodeAnalysis.Formatting;
1515
using Microsoft.CodeAnalysis.Host.Mef;
16-
using Microsoft.CodeAnalysis.Indentation;
1716
using Microsoft.CodeAnalysis.Options;
1817
using Microsoft.CodeAnalysis.Text;
1918
using Microsoft.VisualStudio.Commanding;
@@ -121,12 +120,12 @@ private bool SplitString(ITextView textView, ITextBuffer subjectBuffer, int posi
121120
}
122121

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

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

129-
var splitter = StringSplitter.TryCreate(document, position, options, useTabs, tabSize, cancellationToken);
128+
var splitter = StringSplitter.TryCreate(document, position, useTabs, tabSize, indentStyle, cancellationToken);
130129
if (splitter?.TrySplit(out var newDocument, out var newPosition) != true)
131130
{
132131
return false;

src/EditorFeatures/CSharpTest/AutomaticCompletion/AutomaticBraceCompletionTests.cs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,12 +1117,14 @@ public void X()
11171117
}
11181118
}";
11191119

1120-
using var session = CreateSession(code);
1120+
var optionSet = new Dictionary<OptionKey2, object>
1121+
{
1122+
{ new OptionKey2(AutoFormattingOptions.Metadata.AutoFormattingOnCloseBrace, LanguageNames.CSharp), false },
1123+
{ new OptionKey2(AutoFormattingOptions.Metadata.SmartIndent, LanguageNames.CSharp), FormattingOptions.IndentStyle.Block }
1124+
};
1125+
using var session = CreateSession(code, optionSet);
11211126
Assert.NotNull(session);
11221127

1123-
session.Workspace.GlobalOptions.SetGlobalOption(new OptionKey(AutoFormattingOptionsStorage.FormatOnCloseBrace, LanguageNames.CSharp), false);
1124-
session.Workspace.GlobalOptions.SetGlobalOption(new OptionKey(FormattingOptions.SmartIndent, LanguageNames.CSharp), FormattingOptions.IndentStyle.Block);
1125-
11261128
CheckStart(session.Session);
11271129
Assert.Equal(expected, session.Session.SubjectBuffer.CurrentSnapshot.GetText());
11281130

@@ -1145,11 +1147,13 @@ public class C1
11451147
{ }
11461148
}";
11471149

1148-
using var session = CreateSession(code);
1150+
var optionSet = new Dictionary<OptionKey2, object>
1151+
{
1152+
{ new OptionKey2(AutoFormattingOptions.Metadata.SmartIndent, LanguageNames.CSharp), FormattingOptions.IndentStyle.None }
1153+
};
1154+
using var session = CreateSession(code, optionSet);
11491155
Assert.NotNull(session);
11501156

1151-
session.Workspace.GlobalOptions.SetGlobalOption(new OptionKey(FormattingOptions.SmartIndent, LanguageNames.CSharp), FormattingOptions.IndentStyle.None);
1152-
11531157
CheckStart(session.Session);
11541158
Assert.Equal(expected, session.Session.SubjectBuffer.CurrentSnapshot.GetText());
11551159
}
@@ -1178,11 +1182,13 @@ public class C1
11781182
}
11791183
}";
11801184

1181-
using var session = CreateSession(code);
1185+
var optionSet = new Dictionary<OptionKey2, object>
1186+
{
1187+
{ new OptionKey2(AutoFormattingOptions.Metadata.SmartIndent, LanguageNames.CSharp), FormattingOptions.IndentStyle.Block }
1188+
};
1189+
using var session = CreateSession(code, optionSet);
11821190
Assert.NotNull(session);
11831191

1184-
session.Workspace.GlobalOptions.SetGlobalOption(new OptionKey(FormattingOptions.SmartIndent, LanguageNames.CSharp), FormattingOptions.IndentStyle.Block);
1185-
11861192
CheckStart(session.Session);
11871193
Assert.Equal(expected, session.Session.SubjectBuffer.CurrentSnapshot.GetText());
11881194

@@ -1219,11 +1225,13 @@ public class C1
12191225
}
12201226
}";
12211227

1222-
using var session = CreateSession(code);
1228+
var optionSet = new Dictionary<OptionKey2, object>
1229+
{
1230+
{ new OptionKey2(AutoFormattingOptions.Metadata.SmartIndent, LanguageNames.CSharp), FormattingOptions.IndentStyle.Block }
1231+
};
1232+
using var session = CreateSession(code, optionSet);
12231233
Assert.NotNull(session);
12241234

1225-
session.Workspace.GlobalOptions.SetGlobalOption(new OptionKey(FormattingOptions.SmartIndent, LanguageNames.CSharp), FormattingOptions.IndentStyle.Block);
1226-
12271235
CheckStart(session.Session);
12281236
Assert.Equal(expected, session.Session.SubjectBuffer.CurrentSnapshot.GetText());
12291237

src/EditorFeatures/CSharpTest/Formatting/FormattingEngineTests.cs

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ private static Dictionary<OptionKey2, object> SmartIndentButDoNotFormatWhileTypi
3434
{
3535
return new Dictionary<OptionKey2, object>
3636
{
37-
{ new OptionKey2(AutoFormattingOptionsStorage.SmartIndent, LanguageNames.CSharp), FormattingOptions.IndentStyle.Smart },
38-
{ new OptionKey2(AutoFormattingOptionsStorage.FormatOnTyping, LanguageNames.CSharp), false },
39-
{ new OptionKey2(AutoFormattingOptionsStorage.FormatOnCloseBrace, LanguageNames.CSharp), false },
37+
{ new OptionKey2(AutoFormattingOptions.Metadata.SmartIndent, LanguageNames.CSharp), FormattingOptions.IndentStyle.Smart },
38+
{ new OptionKey2(AutoFormattingOptions.Metadata.AutoFormattingOnTyping, LanguageNames.CSharp), false },
39+
{ new OptionKey2(AutoFormattingOptions.Metadata.AutoFormattingOnCloseBrace, LanguageNames.CSharp), false },
4040
};
4141
}
4242

@@ -1070,11 +1070,11 @@ class C1<U>
10701070
class C1<U>
10711071
{
10721072
}";
1073-
var globalOptions = new Dictionary<OptionKey2, object>
1074-
{
1075-
{ new OptionKey2(AutoFormattingOptionsStorage.SmartIndent, LanguageNames.CSharp), FormattingOptions.IndentStyle.None }
1076-
};
1077-
AssertFormatAfterTypeChar(code, expected, globalOptions);
1073+
var optionSet = new Dictionary<OptionKey2, object>
1074+
{
1075+
{ new OptionKey2(AutoFormattingOptions.Metadata.SmartIndent, LanguageNames.CSharp), FormattingOptions.IndentStyle.None }
1076+
};
1077+
AssertFormatAfterTypeChar(code, expected, optionSet);
10781078
}
10791079

10801080
[WpfFact]
@@ -1101,12 +1101,12 @@ class C
11011101
}
11021102
";
11031103

1104-
var globalOptions = new Dictionary<OptionKey2, object>
1104+
var optionSet = new Dictionary<OptionKey2, object>
11051105
{
1106-
{ new OptionKey2(AutoFormattingOptionsStorage.FormatOnCloseBrace, LanguageNames.CSharp), false }
1106+
{ new OptionKey2(AutoFormattingOptions.Metadata.AutoFormattingOnCloseBrace, LanguageNames.CSharp), false }
11071107
};
11081108

1109-
AssertFormatAfterTypeChar(code, expected, globalOptions);
1109+
AssertFormatAfterTypeChar(code, expected, optionSet);
11101110
}
11111111

11121112
[WpfFact]
@@ -1133,12 +1133,12 @@ class C
11331133
}
11341134
";
11351135

1136-
var globalOptions = new Dictionary<OptionKey2, object>
1136+
var optionSet = new Dictionary<OptionKey2, object>
11371137
{
1138-
{ new OptionKey2(AutoFormattingOptionsStorage.FormatOnTyping, LanguageNames.CSharp), false }
1138+
{ new OptionKey2(AutoFormattingOptions.Metadata.AutoFormattingOnTyping, LanguageNames.CSharp), false }
11391139
};
11401140

1141-
AssertFormatAfterTypeChar(code, expected, globalOptions);
1141+
AssertFormatAfterTypeChar(code, expected, optionSet);
11421142
}
11431143

11441144
[WorkItem(5873, "https://github.com/dotnet/roslyn/issues/5873")]
@@ -1165,12 +1165,12 @@ static void Main()
11651165
}
11661166
}";
11671167

1168-
var globalOptions = new Dictionary<OptionKey2, object>
1168+
var optionSet = new Dictionary<OptionKey2, object>
11691169
{
1170-
{ new OptionKey2(AutoFormattingOptionsStorage.FormatOnTyping, LanguageNames.CSharp), false }
1170+
{ new OptionKey2(AutoFormattingOptions.Metadata.AutoFormattingOnTyping, LanguageNames.CSharp), false }
11711171
};
11721172

1173-
AssertFormatAfterTypeChar(code, expected, globalOptions);
1173+
AssertFormatAfterTypeChar(code, expected, optionSet);
11741174
}
11751175

11761176
[WorkItem(5873, "https://github.com/dotnet/roslyn/issues/5873")]
@@ -1223,12 +1223,12 @@ class C
12231223
}
12241224
";
12251225

1226-
var globalOptions = new Dictionary<OptionKey2, object>
1226+
var optionSet = new Dictionary<OptionKey2, object>
12271227
{
1228-
{ new OptionKey2(AutoFormattingOptionsStorage.FormatOnSemicolon, LanguageNames.CSharp), false }
1228+
{ new OptionKey2(AutoFormattingOptions.Metadata.AutoFormattingOnSemicolon, LanguageNames.CSharp), false }
12291229
};
12301230

1231-
AssertFormatAfterTypeChar(code, expected, globalOptions);
1231+
AssertFormatAfterTypeChar(code, expected, optionSet);
12321232
}
12331233

12341234
[WpfFact]
@@ -1255,12 +1255,12 @@ class C
12551255
}
12561256
";
12571257

1258-
var globalOptions = new Dictionary<OptionKey2, object>
1258+
var optionSet = new Dictionary<OptionKey2, object>
12591259
{
1260-
{ new OptionKey2(AutoFormattingOptionsStorage.FormatOnTyping, LanguageNames.CSharp), false }
1260+
{ new OptionKey2(AutoFormattingOptions.Metadata.AutoFormattingOnTyping, LanguageNames.CSharp), false }
12611261
};
12621262

1263-
AssertFormatAfterTypeChar(code, expected, globalOptions);
1263+
AssertFormatAfterTypeChar(code, expected, optionSet);
12641264
}
12651265

12661266
[WpfFact, WorkItem(4435, "https://github.com/dotnet/roslyn/issues/4435")]
@@ -2289,16 +2289,18 @@ class Test
22892289
Assert.Single(annotatedTrivia);
22902290
}
22912291

2292-
private static void AssertFormatAfterTypeChar(string code, string expected, Dictionary<OptionKey2, object> globalOptions = null)
2292+
private static void AssertFormatAfterTypeChar(string code, string expected, Dictionary<OptionKey2, object> changedOptionSet = null)
22932293
{
22942294
using var workspace = TestWorkspace.CreateCSharp(code);
2295-
if (globalOptions != null)
2295+
if (changedOptionSet != null)
22962296
{
2297-
var options = workspace.GlobalOptions;
2298-
foreach (var entry in globalOptions)
2297+
var options = workspace.Options;
2298+
foreach (var entry in changedOptionSet)
22992299
{
2300-
options.SetGlobalOption((OptionKey)entry.Key, entry.Value);
2300+
options = options.WithChangedOption(entry.Key, entry.Value);
23012301
}
2302+
2303+
workspace.TryApplyChanges(workspace.CurrentSolution.WithOptions(options));
23022304
}
23032305

23042306
var subjectDocument = workspace.Documents.Single();

src/EditorFeatures/CSharpTest/Formatting/Indentation/CSharpFormatterTestsBase.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,7 @@ private static async Task TokenFormatWorkerAsync(TestWorkspace workspace, ITextB
8282

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

85-
var options = new IndentationOptions(
86-
await SyntaxFormattingOptions.FromDocumentAsync(document, CancellationToken.None).ConfigureAwait(false),
87-
AutoFormattingOptions.Default);
88-
85+
var options = await IndentationOptions.FromDocumentAsync(document, CancellationToken.None);
8986
var formatter = new CSharpSmartTokenFormatter(options, rules, root);
9087
var changes = await formatter.FormatTokenAsync(workspace.Services, token, CancellationToken.None);
9188

src/EditorFeatures/CSharpTest/Formatting/Indentation/SmartIndenterEnterOnTokenTests.cs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.Linq;
88
using System.Threading;
99
using System.Threading.Tasks;
10-
using Microsoft.CodeAnalysis.CSharp.Formatting;
1110
using Microsoft.CodeAnalysis.CSharp.Indentation;
1211
using Microsoft.CodeAnalysis.CSharp.Syntax;
1312
using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces;
@@ -1502,17 +1501,21 @@ private static async Task AssertIndentUsingSmartTokenFormatterAsync(
15021501
// create tree service
15031502
using var workspace = TestWorkspace.CreateCSharp(code);
15041503

1504+
workspace.TryApplyChanges(workspace.CurrentSolution.WithOptions(workspace.Options
1505+
.WithChangedOption(UseTabs, LanguageNames.CSharp, useTabs)));
1506+
15051507
var hostdoc = workspace.Documents.First();
1508+
15061509
var buffer = hostdoc.GetTextBuffer();
1510+
15071511
var snapshot = buffer.CurrentSnapshot;
15081512
var line = snapshot.GetLineFromLineNumber(indentationLine);
1513+
15091514
var document = workspace.CurrentSolution.GetDocument(hostdoc.Id);
15101515

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

1513-
var options = new IndentationOptions(
1514-
CSharpSyntaxFormattingOptions.Default.With(useTabs: useTabs, tabSize: 4, indentationSize: 4),
1515-
AutoFormattingOptions.Default);
1518+
var options = await IndentationOptions.FromDocumentAsync(document, CancellationToken.None);
15161519

15171520
Assert.True(
15181521
CSharpIndentationService.ShouldUseSmartTokenFormatterInsteadOfIndenter(
@@ -1523,7 +1526,7 @@ private static async Task AssertIndentUsingSmartTokenFormatterAsync(
15231526
Assert.Equal(expectedIndentation.Value, actualIndentation);
15241527
}
15251528

1526-
private async Task AssertIndentNotUsingSmartTokenFormatterButUsingIndenterAsync(
1529+
private static async Task AssertIndentNotUsingSmartTokenFormatterButUsingIndenterAsync(
15271530
string code,
15281531
int indentationLine,
15291532
int? expectedIndentation,
@@ -1533,7 +1536,7 @@ private async Task AssertIndentNotUsingSmartTokenFormatterButUsingIndenterAsync(
15331536
await AssertIndentNotUsingSmartTokenFormatterButUsingIndenterAsync(code.Replace(" ", "\t"), indentationLine, expectedIndentation, useTabs: true, indentStyle).ConfigureAwait(false);
15341537
}
15351538

1536-
private async Task AssertIndentNotUsingSmartTokenFormatterButUsingIndenterAsync(
1539+
private static async Task AssertIndentNotUsingSmartTokenFormatterButUsingIndenterAsync(
15371540
string code,
15381541
int indentationLine,
15391542
int? expectedIndentation,
@@ -1542,7 +1545,9 @@ private async Task AssertIndentNotUsingSmartTokenFormatterButUsingIndenterAsync(
15421545
{
15431546
// create tree service
15441547
using var workspace = TestWorkspace.CreateCSharp(code);
1545-
1548+
workspace.TryApplyChanges(workspace.CurrentSolution.WithOptions(workspace.Options
1549+
.WithChangedOption(AutoFormattingOptions.Metadata.SmartIndent, LanguageNames.CSharp, indentStyle)
1550+
.WithChangedOption(UseTabs, LanguageNames.CSharp, useTabs)));
15461551
var hostdoc = workspace.Documents.First();
15471552
var buffer = hostdoc.GetTextBuffer();
15481553
var snapshot = buffer.CurrentSnapshot;
@@ -1553,16 +1558,14 @@ private async Task AssertIndentNotUsingSmartTokenFormatterButUsingIndenterAsync(
15531558

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

1556-
var options = new IndentationOptions(
1557-
CSharpSyntaxFormattingOptions.Default.With(useTabs: useTabs, tabSize: 4, indentationSize: 4),
1558-
new AutoFormattingOptions(IndentStyle: indentStyle));
1561+
var options = await IndentationOptions.FromDocumentAsync(document, CancellationToken.None);
15591562

15601563
Assert.False(
15611564
CSharpIndentationService.ShouldUseSmartTokenFormatterInsteadOfIndenter(
15621565
Formatter.GetDefaultFormattingRules(document),
15631566
root, line.AsTextLine(), options, out _));
15641567

1565-
TestIndentation(workspace, indentationLine, expectedIndentation, indentStyle, useTabs);
1568+
TestIndentation(workspace, indentationLine, expectedIndentation);
15661569
}
15671570
}
15681571
}

0 commit comments

Comments
 (0)