Skip to content
New issue

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

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

Already on GitHub? # to your account

Remove imports should only format the section of the document that was touched #76038

Merged
merged 5 commits into from
Nov 23, 2024
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
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
Loading