Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert Global indentation options (#59679) #60199

Merged
merged 1 commit into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@ public void ExecuteCommand(TypeCharCommandArgs args, Action nextCommandHandler,
if (!ConvertNamespaceAnalysis.CanOfferUseFileScoped(s_optionSet, root, namespaceDecl, forAnalyzer: true, LanguageVersion.CSharp10))
return default;

var formattingOptions = SyntaxFormattingOptions.FromDocumentAsync(document, cancellationToken).WaitAndGetResult(cancellationToken);
var (converted, semicolonSpan) = ConvertNamespaceTransform.ConvertNamespaceDeclarationAsync(document, namespaceDecl, formattingOptions, cancellationToken).WaitAndGetResult(cancellationToken);
var (converted, semicolonSpan) = ConvertNamespaceTransform.ConvertNamespaceDeclarationAsync(document, namespaceDecl, 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,7 +8,6 @@
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 @@ -21,17 +20,16 @@ namespace Microsoft.CodeAnalysis.Editor.CSharp.DocumentationComments
[Name(PredefinedCommandHandlerNames.DocumentationComments)]
[Order(After = PredefinedCommandHandlerNames.Rename)]
[Order(After = PredefinedCompletionNames.CompletionCommandHandler)]
internal sealed class DocumentationCommentCommandHandler
internal class DocumentationCommentCommandHandler
: AbstractDocumentationCommentCommandHandler
{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public DocumentationCommentCommandHandler(
IUIThreadOperationExecutor uiThreadOperationExecutor,
ITextUndoHistoryRegistry undoHistoryRegistry,
IEditorOperationsFactoryService editorOperationsFactoryService,
IGlobalOptionService globalOptions)
: base(uiThreadOperationExecutor, undoHistoryRegistry, editorOperationsFactoryService, globalOptions)
IEditorOperationsFactoryService editorOperationsFactoryService)
: base(uiThreadOperationExecutor, undoHistoryRegistry, editorOperationsFactoryService)
{
}

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

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

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

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,7 +13,6 @@
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 @@ -121,12 +120,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 options = _globalOptions.GetIndentationOptionsAsync(document, cancellationToken).WaitAndGetResult(cancellationToken);
var indentStyle = document.Project.Solution.Options.GetOption(FormattingOptions.SmartIndent, LanguageNames.CSharp);

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

var splitter = StringSplitter.TryCreate(document, position, options, useTabs, tabSize, cancellationToken);
var splitter = StringSplitter.TryCreate(document, position, useTabs, tabSize, indentStyle, 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,12 +1117,14 @@ public void X()
}
}";

using var session = CreateSession(code);
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);
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 @@ -1145,11 +1147,13 @@ public class C1
{ }
}";

using var session = CreateSession(code);
var optionSet = new Dictionary<OptionKey2, object>
{
{ new OptionKey2(AutoFormattingOptions.Metadata.SmartIndent, LanguageNames.CSharp), FormattingOptions.IndentStyle.None }
};
using var session = CreateSession(code, optionSet);
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 @@ -1178,11 +1182,13 @@ public class C1
}
}";

using var session = CreateSession(code);
var optionSet = new Dictionary<OptionKey2, object>
{
{ new OptionKey2(AutoFormattingOptions.Metadata.SmartIndent, LanguageNames.CSharp), FormattingOptions.IndentStyle.Block }
};
using var session = CreateSession(code, optionSet);
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 @@ -1219,11 +1225,13 @@ public class C1
}
}";

using var session = CreateSession(code);
var optionSet = new Dictionary<OptionKey2, object>
{
{ new OptionKey2(AutoFormattingOptions.Metadata.SmartIndent, LanguageNames.CSharp), FormattingOptions.IndentStyle.Block }
};
using var session = CreateSession(code, optionSet);
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
58 changes: 30 additions & 28 deletions src/EditorFeatures/CSharpTest/Formatting/FormattingEngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ private static Dictionary<OptionKey2, object> SmartIndentButDoNotFormatWhileTypi
{
return new Dictionary<OptionKey2, object>
{
{ new OptionKey2(AutoFormattingOptionsStorage.SmartIndent, LanguageNames.CSharp), FormattingOptions.IndentStyle.Smart },
{ new OptionKey2(AutoFormattingOptionsStorage.FormatOnTyping, LanguageNames.CSharp), false },
{ new OptionKey2(AutoFormattingOptionsStorage.FormatOnCloseBrace, LanguageNames.CSharp), false },
{ 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 },
};
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

private static void AssertFormatAfterTypeChar(string code, string expected, Dictionary<OptionKey2, object> globalOptions = null)
private static void AssertFormatAfterTypeChar(string code, string expected, Dictionary<OptionKey2, object> changedOptionSet = null)
{
using var workspace = TestWorkspace.CreateCSharp(code);
if (globalOptions != null)
if (changedOptionSet != null)
{
var options = workspace.GlobalOptions;
foreach (var entry in globalOptions)
var options = workspace.Options;
foreach (var entry in changedOptionSet)
{
options.SetGlobalOption((OptionKey)entry.Key, entry.Value);
options = options.WithChangedOption(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,10 +82,7 @@ private static async Task TokenFormatWorkerAsync(TestWorkspace workspace, ITextB

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

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

var options = await IndentationOptions.FromDocumentAsync(document, CancellationToken.None);
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,7 +7,6 @@
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 @@ -1502,17 +1501,21 @@ 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 = new IndentationOptions(
CSharpSyntaxFormattingOptions.Default.With(useTabs: useTabs, tabSize: 4, indentationSize: 4),
AutoFormattingOptions.Default);
var options = await IndentationOptions.FromDocumentAsync(document, CancellationToken.None);

Assert.True(
CSharpIndentationService.ShouldUseSmartTokenFormatterInsteadOfIndenter(
Expand All @@ -1523,7 +1526,7 @@ private static async Task AssertIndentUsingSmartTokenFormatterAsync(
Assert.Equal(expectedIndentation.Value, actualIndentation);
}

private async Task AssertIndentNotUsingSmartTokenFormatterButUsingIndenterAsync(
private static async Task AssertIndentNotUsingSmartTokenFormatterButUsingIndenterAsync(
string code,
int indentationLine,
int? expectedIndentation,
Expand All @@ -1533,7 +1536,7 @@ private async Task AssertIndentNotUsingSmartTokenFormatterButUsingIndenterAsync(
await AssertIndentNotUsingSmartTokenFormatterButUsingIndenterAsync(code.Replace(" ", "\t"), indentationLine, expectedIndentation, useTabs: true, indentStyle).ConfigureAwait(false);
}

private async Task AssertIndentNotUsingSmartTokenFormatterButUsingIndenterAsync(
private static async Task AssertIndentNotUsingSmartTokenFormatterButUsingIndenterAsync(
string code,
int indentationLine,
int? expectedIndentation,
Expand All @@ -1542,7 +1545,9 @@ private async Task AssertIndentNotUsingSmartTokenFormatterButUsingIndenterAsync(
{
// 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 @@ -1553,16 +1558,14 @@ private async Task AssertIndentNotUsingSmartTokenFormatterButUsingIndenterAsync(

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

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

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

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