diff --git a/src/EditorFeatures/CSharp/Formatting/CSharpEditorFormattingService.cs b/src/EditorFeatures/CSharp/Formatting/CSharpEditorFormattingService.cs index 4f07d25d9e92c..afe6e1208173c 100644 --- a/src/EditorFeatures/CSharp/Formatting/CSharpEditorFormattingService.cs +++ b/src/EditorFeatures/CSharp/Formatting/CSharpEditorFormattingService.cs @@ -12,12 +12,12 @@ using Microsoft.CodeAnalysis.CSharp.Utilities; using Microsoft.CodeAnalysis.Editor.CSharp.Formatting.Indentation; using Microsoft.CodeAnalysis.Editor.Implementation.Formatting.Indentation; -using Microsoft.CodeAnalysis.Editor.Options; using Microsoft.CodeAnalysis.Editor.Shared.Options; using Microsoft.CodeAnalysis.Editor.Shared.Utilities; using Microsoft.CodeAnalysis.Formatting; using Microsoft.CodeAnalysis.Formatting.Rules; using Microsoft.CodeAnalysis.Host.Mef; +using Microsoft.CodeAnalysis.LanguageServices; using Microsoft.CodeAnalysis.Options; using Microsoft.CodeAnalysis.Shared.Extensions; using Microsoft.CodeAnalysis.Text; @@ -159,6 +159,12 @@ public async Task> GetFormattingChangesAsync(Document document return null; } + var service = document.GetLanguageService(); + if (service != null && service.IsInNonUserCode(token.SyntaxTree, caretPosition, cancellationToken)) + { + return null; + } + // Check to see if the token is ')' and also the parent is a using statement. If not, bail if (TokenShouldNotFormatOnTypeChar(token)) { diff --git a/src/EditorFeatures/CSharpTest/Formatting/FormattingEngineTests.cs b/src/EditorFeatures/CSharpTest/Formatting/FormattingEngineTests.cs index d0b6c2af5a63a..db38b393969e3 100644 --- a/src/EditorFeatures/CSharpTest/Formatting/FormattingEngineTests.cs +++ b/src/EditorFeatures/CSharpTest/Formatting/FormattingEngineTests.cs @@ -481,6 +481,364 @@ static void Main(string[] args) AssertFormatAfterTypeChar(code, expected); } + [WorkItem(449)] + [WorkItem(1077103)] + [Fact, Trait(Traits.Feature, Traits.Features.Formatting)] + public void NoFormattingInsideSingleLineRegularComment_1() + { + var code = @"class Program +{ + // {$$ + static void Main(int a, int b) + { + + } +}"; + + var expected = @"class Program +{ + // { + static void Main(int a, int b) + { + + } +}"; + AssertFormatAfterTypeChar(code, expected); + } + + [WorkItem(449)] + [WorkItem(1077103)] + [Fact, Trait(Traits.Feature, Traits.Features.Formatting)] + public void NoFormattingInsideSingleLineRegularComment_2() + { + var code = @"class Program +{ + // {$$ + static void Main(int a, int b) + { + + } +}"; + + var expected = @"class Program +{ + // { + static void Main(int a, int b) + { + + } +}"; + AssertFormatAfterTypeChar(code, expected); + } + + + + [WorkItem(449)] + [WorkItem(1077103)] + [Fact, Trait(Traits.Feature, Traits.Features.Formatting)] + public void NoFormattingInsideMultiLineRegularComment_1() + { + var code = @"class Program +{ + static void Main(int a/* {$$ */, int b) + { + + } +}"; + + var expected = @"class Program +{ + static void Main(int a/* { */, int b) + { + + } +}"; + AssertFormatAfterTypeChar(code, expected); + } + + [WorkItem(449)] + [WorkItem(1077103)] + [Fact, Trait(Traits.Feature, Traits.Features.Formatting)] + public void NoFormattingInsideMultiLineRegularComment_2() + { + var code = @"class Program +{ + static void Main(int a/* {$$ + */, int b) + { + + } +}"; + + var expected = @"class Program +{ + static void Main(int a/* { + */, int b) + { + + } +}"; + AssertFormatAfterTypeChar(code, expected); + } + + [WorkItem(449)] + [WorkItem(1077103)] + [Fact, Trait(Traits.Feature, Traits.Features.Formatting)] + public void NoFormattingInsideMultiLineRegularComment_3() + { + var code = @"class Program +{ + static void Main(int a/* {$$ + */, int b) + { + + } +}"; + + var expected = @"class Program +{ + static void Main(int a/* { + */, int b) + { + + } +}"; + AssertFormatAfterTypeChar(code, expected); + } + + [WorkItem(449)] + [WorkItem(1077103)] + [Fact, Trait(Traits.Feature, Traits.Features.Formatting)] + public void NoFormattingInsideSingleLineDocComment_1() + { + var code = @"class Program +{ + /// {$$ + static void Main(int a, int b) + { + + } +}"; + + var expected = @"class Program +{ + /// { + static void Main(int a, int b) + { + + } +}"; + AssertFormatAfterTypeChar(code, expected); + } + + [WorkItem(449)] + [WorkItem(1077103)] + [Fact, Trait(Traits.Feature, Traits.Features.Formatting)] + public void NoFormattingInsideSingleLineDocComment_2() + { + var code = @"class Program +{ + /// {$$ + static void Main(int a, int b) + { + + } +}"; + + var expected = @"class Program +{ + /// { + static void Main(int a, int b) + { + + } +}"; + AssertFormatAfterTypeChar(code, expected); + } + + [WorkItem(449)] + [WorkItem(1077103)] + [Fact, Trait(Traits.Feature, Traits.Features.Formatting)] + public void NoFormattingInsideMultiLineDocComment_1() + { + var code = @"class Program +{ + /** {$$ **/ + static void Main(int a, int b) + { + + } +}"; + + var expected = @"class Program +{ + /** { **/ + static void Main(int a, int b) + { + + } +}"; + AssertFormatAfterTypeChar(code, expected); + } + + [WorkItem(449)] + [WorkItem(1077103)] + [Fact, Trait(Traits.Feature, Traits.Features.Formatting)] + public void NoFormattingInsideMultiLineDocComment_2() + { + var code = @"class Program +{ + /** {$$ + **/ + static void Main(int a, int b) + { + + } +}"; + + var expected = @"class Program +{ + /** { + **/ + static void Main(int a, int b) + { + + } +}"; + AssertFormatAfterTypeChar(code, expected); + } + + [WorkItem(449)] + [WorkItem(1077103)] + [Fact, Trait(Traits.Feature, Traits.Features.Formatting)] + public void NoFormattingInsideMultiLineDocComment_3() + { + var code = @"class Program +{ + /** {$$ + **/ + static void Main(int a, int b) + { + + } +}"; + + var expected = @"class Program +{ + /** { + **/ + static void Main(int a, int b) + { + + } +}"; + AssertFormatAfterTypeChar(code, expected); + } + + [WorkItem(449)] + [WorkItem(1077103)] + [Fact, Trait(Traits.Feature, Traits.Features.Formatting)] + public void NoFormattingInsideInactiveCode() + { + var code = @"class Program +{ + #if false + {$$ + #endif + + static void Main(string[] args) + { + + } +}"; + + var expected = @"class Program +{ + #if false + { + #endif + + static void Main(string[] args) + { + + } +}"; + AssertFormatAfterTypeChar(code, expected); + } + + [WorkItem(449)] + [WorkItem(1077103)] + [Fact, Trait(Traits.Feature, Traits.Features.Formatting)] + public void NoFormattingInsideStringLiteral() + { + var code = @"class Program +{ + static void Main(string[] args) + { + var asdas = ""{$$"" ; + } +}"; + + var expected = @"class Program +{ + static void Main(string[] args) + { + var asdas = ""{"" ; + } +}"; + AssertFormatAfterTypeChar(code, expected); + } + + [WorkItem(449)] + [WorkItem(1077103)] + [Fact, Trait(Traits.Feature, Traits.Features.Formatting)] + public void NoFormattingInsideCharLiteral() + { + var code = @"class Program +{ + static void Main(string[] args) + { + var asdas = '{$$' ; + } +}"; + + var expected = @"class Program +{ + static void Main(string[] args) + { + var asdas = '{' ; + } +}"; + AssertFormatAfterTypeChar(code, expected); + } + + [WorkItem(449)] + [WorkItem(1077103)] + [Fact, Trait(Traits.Feature, Traits.Features.Formatting)] + public void NoFormattingInsideCommentsOfPreprocessorDirectves() + { + var code = @"class Program +{ + #region + #endregion // a/*{$$*/ + static void Main(string[] args) + { + + } +}"; + + var expected = @"class Program +{ + #region + #endregion // a/*{*/ + static void Main(string[] args) + { + + } +}"; + AssertFormatAfterTypeChar(code, expected); + } + private static void AssertFormatAfterTypeChar(string code, string expected) { using (var workspace = CSharpWorkspaceFactory.CreateWorkspaceFromFile(code))