Skip to content

Commit

Permalink
Remove imports should only format the section of the document that wa…
Browse files Browse the repository at this point in the history
…s touched (#76038)
  • Loading branch information
CyrusNajmabadi authored Nov 23, 2024
2 parents b25b211 + 3ffa7c7 commit f16c920
Show file tree
Hide file tree
Showing 7 changed files with 3,159 additions and 2,790 deletions.
1 change: 1 addition & 0 deletions SpellingExclusions.dic
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
awaitable
Refactorings
Infos
cref
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.RemoveUnnecessaryImport
CSharpRemoveUnnecessaryImportsCodeFixProvider>;

[Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryImports)]
public class RemoveUnnecessaryImportsTests
public sealed class RemoveUnnecessaryImportsTests
{
private static readonly string s_tab = "\t";

[Fact]
public async Task TestNoReferences()
{
Expand Down Expand Up @@ -2246,4 +2248,36 @@ static void Main(string[] args)
"""
}.RunAsync();
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/65114")]
public async Task DoNotTouchInnerNamespaceWithoutUsings()
{
await VerifyCS.VerifyCodeFixAsync(
$$"""
[|{|IDE0005:using System;
using System.Collections.Generic;
using System.Linq;|}|]
namespace N
{
{{s_tab}}class Program
{
static void Main(string[] args)
{
}
}
}
""",
$$"""
namespace N
{
{{s_tab}}class Program
{
static void Main(string[] args)
{
}
}
}
""");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Shared.Extensions;

Expand All @@ -30,19 +28,19 @@ public sealed override Task RegisterCodeFixesAsync(CodeFixContext context)
context.RegisterCodeFix(
CodeAction.Create(
title,
c => RemoveUnnecessaryImportsAsync(context.Document, c),
cancellationToken => RemoveUnnecessaryImportsAsync(context.Document, cancellationToken),
title),
context.Diagnostics);
return Task.CompletedTask;
}

protected abstract string GetTitle();

private static async Task<Document> RemoveUnnecessaryImportsAsync(
private static Task<Document> RemoveUnnecessaryImportsAsync(
Document document,
CancellationToken cancellationToken)
{
var service = document.GetRequiredLanguageService<IRemoveUnnecessaryImportsService>();
return await service.RemoveUnnecessaryImportsAsync(document, cancellationToken).ConfigureAwait(false);
return service.RemoveUnnecessaryImportsAsync(document, cancellationToken);
}
}
Loading

0 comments on commit f16c920

Please sign in to comment.