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

Global indentation options #59679

Merged
merged 7 commits into from
Mar 15, 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,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,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.SmartIndent, 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.SmartIndent, 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.SmartIndent, 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.SmartIndent, 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 @@ -34,9 +34,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 @@ -1070,11 +1070,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 @@ -1101,12 +1101,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 @@ -1133,12 +1133,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 @@ -1165,12 +1165,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 @@ -1223,12 +1223,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 @@ -1255,12 +1255,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 @@ -2289,18 +2289,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 All @@ -1526,7 +1523,7 @@ private static async Task AssertIndentUsingSmartTokenFormatterAsync(
Assert.Equal(expectedIndentation.Value, actualIndentation);
}

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

private static async Task AssertIndentNotUsingSmartTokenFormatterButUsingIndenterAsync(
private async Task AssertIndentNotUsingSmartTokenFormatterButUsingIndenterAsync(
string code,
int indentationLine,
int? expectedIndentation,
Expand All @@ -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