提交 fa7e6d11 编写于 作者: M Matt Warren

Merge pull request #3184 from mattwar/Bug1171470

Still enable smart indent when auto-format on close brace is off
......@@ -58,15 +58,17 @@ public CSharpEditorFormattingService()
public bool SupportsFormattingOnTypedCharacter(Document document, char ch)
{
var optionsService = document.Project.Solution.Workspace.Options;
if ((ch == '}' && !optionsService.GetOption(FeatureOnOffOptions.AutoFormattingOnCloseBrace, document.Project.Language)) ||
(ch == ';' && !optionsService.GetOption(FeatureOnOffOptions.AutoFormattingOnSemicolon, document.Project.Language)))
var options = document.Project.Solution.Workspace.Options;
var smartIndentOn = options.GetOption(FormattingOptions.SmartIndent, document.Project.Language) == FormattingOptions.IndentStyle.Smart;
if ((ch == '}' && !options.GetOption(FeatureOnOffOptions.AutoFormattingOnCloseBrace, document.Project.Language) && !smartIndentOn) ||
(ch == ';' && !options.GetOption(FeatureOnOffOptions.AutoFormattingOnSemicolon, document.Project.Language)))
{
return false;
}
// don't auto format after these keys if smart indenting is not on.
if ((ch == '#' || ch == 'n') && optionsService.GetOption(FormattingOptions.SmartIndent, document.Project.Language) != FormattingOptions.IndentStyle.Smart)
if ((ch == '#' || ch == 'n') && !smartIndentOn)
{
return false;
}
......@@ -180,11 +182,21 @@ public async Task<IList<TextChange>> GetFormattingChangesAsync(Document document
return null;
}
// if formatting range fails, do format token one at least
var changes = await FormatRangeAsync(document, token, formattingRules, cancellationToken).ConfigureAwait(false);
if (changes.Count > 0)
var options = document.Project.Solution.Workspace.Options;
// dont attempt to format on close brace if autoformat on close brace feature is off, instead just smart indent
bool smartIndentOnly =
token.IsKind(SyntaxKind.CloseBraceToken) &&
!options.GetOption(FeatureOnOffOptions.AutoFormattingOnCloseBrace, document.Project.Language);
if (!smartIndentOnly)
{
return changes;
// if formatting range fails, do format token one at least
var changes = await FormatRangeAsync(document, token, formattingRules, cancellationToken).ConfigureAwait(false);
if (changes.Count > 0)
{
return changes;
}
}
// if we can't, do normal smart indentation
......
......@@ -5,6 +5,7 @@
using Microsoft.CodeAnalysis.Editor.Commands;
using Microsoft.CodeAnalysis.Editor.Implementation.Formatting;
using Microsoft.CodeAnalysis.Editor.Options;
using Microsoft.CodeAnalysis.Editor.Shared.Options;
using Microsoft.CodeAnalysis.Editor.UnitTests.Utilities;
using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces;
using Microsoft.CodeAnalysis.Formatting;
......@@ -1083,6 +1084,38 @@ class C1<U>
AssertFormatAfterTypeChar(code, expected, optionSet);
}
[Fact]
[Trait(Traits.Feature, Traits.Features.SmartTokenFormatting)]
public void StillAutoIndentCloseBraceWhenFormatOnCloseBraceIsOff()
{
var code = @"namespace N
{
class C
{
// improperly indented code
int x = 10;
}$$
}
";
var expected = @"namespace N
{
class C
{
// improperly indented code
int x = 10;
}
}
";
var optionSet = new Dictionary<OptionKey, object>
{
{ new OptionKey(FeatureOnOffOptions.AutoFormattingOnCloseBrace, LanguageNames.CSharp), false }
};
AssertFormatAfterTypeChar(code, expected, optionSet);
}
private static void AssertFormatAfterTypeChar(string code, string expected, Dictionary<OptionKey, object> changedOptionSet = null)
{
using (var workspace = CSharpWorkspaceFactory.CreateWorkspaceFromFile(code))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册