未验证 提交 75f08f86 编写于 作者: M msftbot[bot] 提交者: GitHub

Merge pull request #48167 from dibarbet/disable_file_rename_razor

Disable document rename for razor mapped files
......@@ -8,6 +8,7 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Internal.Log;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.PooledObjects;
......@@ -75,6 +76,12 @@ public static partial class Renamer
throw new ArgumentNullException(nameof(document));
}
if (document.Services.GetService<ISpanMappingService>() != null)
{
// Don't advertise that we can file rename generated documents that map to a different file.
return new RenameDocumentActionSet(ImmutableArray<RenameDocumentAction>.Empty, document.Id, document.Name, document.Folders.ToImmutableArray(), document.Project.Solution.Options);
}
using var _ = ArrayBuilder<RenameDocumentAction>.GetInstance(out var actions);
if (newDocumentName != null && !newDocumentName.Equals(document.Name))
......
......@@ -5,6 +5,7 @@
#nullable disable
using System.Threading.Tasks;
using Roslyn.Test.Utilities;
using Xunit;
namespace Microsoft.CodeAnalysis.UnitTests.Renamer
......@@ -368,5 +369,17 @@ class C2
documentName: @"C.cs",
newDocumentName: @"C2",
newDocumentPath: @"Test\C2.cs");
[Fact]
[WorkItem(46580, "https://github.com/dotnet/roslyn/issues/46580")]
public Task CSharp_RenameDocument_MappedDocumentHasNoResults()
{
var documentName = "Component1.razor";
var documentText =
@"<h3>Component1</h3>
@code {}";
return TestRenameMappedFile(documentText, documentName, newDocumentName: "MyComponent.razor");
}
}
}
......@@ -203,5 +203,36 @@ protected async Task TestEmptyActionSet(string startText, string newDocumentName
Assert.Empty(documentRenameResult.ApplicableActions);
}
}
protected async Task TestRenameMappedFile(string startText, string documentName, string newDocumentName)
{
using var workspace = new AdhocWorkspace();
var solution = workspace.CurrentSolution;
var projectId = ProjectId.CreateNewId();
var projectInfo = ProjectInfo.Create(projectId, VersionStamp.Create(), "ProjectName", "AssemblyName", LanguageName, filePath: "");
solution = solution.AddProject(projectInfo);
var startSourceText = SourceText.From(startText);
var documentId = DocumentId.CreateNewId(projectId);
var documentInfo = DocumentInfo.Create(
documentId,
documentName,
GetDocumentFolders(s_defaultDocumentPath),
SourceCodeKind.Regular,
TextLoader.From(TextAndVersion.Create(startSourceText, VersionStamp.Create(), documentName)),
s_defaultDocumentPath,
isGenerated: true,
designTimeOnly: false,
new TestDocumentServiceProvider());
solution = solution.AddDocument(documentInfo);
var document = solution.GetDocument(documentId);
var documentRenameResult = await Rename.Renamer.RenameDocumentAsync(document, newDocumentName, GetDocumentFolders(s_defaultDocumentPath));
Assert.Empty(documentRenameResult.ApplicableActions);
}
}
}
......@@ -4,7 +4,12 @@
#nullable disable
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Text;
namespace Microsoft.CodeAnalysis.Test.Utilities
{
......@@ -17,16 +22,24 @@ public TestDocumentServiceProvider(bool canApplyChange = true, bool supportDiagn
CanApplyChange = canApplyChange,
SupportDiagnostics = supportDiagnostics
};
SpanMappingService = new TestSpanMappingService();
}
public IDocumentOperationService DocumentOperationService { get; }
public ISpanMappingService SpanMappingService { get; }
public TService GetService<TService>() where TService : class, IDocumentService
{
if (DocumentOperationService is TService service)
{
return service;
}
else if (SpanMappingService is TService spanMappingService)
{
return spanMappingService;
}
return null;
}
......@@ -40,5 +53,13 @@ public TestDocumentOperationService()
public bool CanApplyChange { get; set; }
public bool SupportDiagnostics { get; set; }
}
private class TestSpanMappingService : ISpanMappingService
{
public Task<ImmutableArray<MappedSpanResult>> MapSpansAsync(Document document, IEnumerable<TextSpan> spans, CancellationToken cancellationToken)
{
return Task.FromResult(ImmutableArray<MappedSpanResult>.Empty);
}
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册