未验证 提交 4158203a 编写于 作者: S Shen Chen 提交者: GitHub

Merge pull request #47812 from Cosifne/dev/shech/checkPathChars

Don't rename the file if the replacement text is invalid
......@@ -3,7 +3,6 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.ComponentModel;
using System.Linq;
......@@ -31,6 +30,7 @@ internal class DashboardViewModel : INotifyPropertyChanged, IDisposable
private bool _defaultRenameInCommentsFlag;
private bool _defaultRenameFileFlag;
private bool _defaultPreviewChangesFlag;
private bool _isReplacementTextValid;
public DashboardViewModel(InlineRenameSession session)
{
......@@ -50,9 +50,9 @@ public DashboardViewModel(InlineRenameSession session)
_session.ReplacementsComputed += OnReplacementsComputed;
_session.ReplacementTextChanged += OnReplacementTextChanged;
// Set the flag to true by default if we're showing the option. Use
// the property so we correctly update the session as well
DefaultRenameFileFlag = session.OptionSet.GetOption(RenameOptions.RenameFile) || AllowFileRename;
// Set the flag to true by default if we're showing the option.
_isReplacementTextValid = true;
ComputeDefaultRenameFileFlag();
}
public event PropertyChangedEventHandler PropertyChanged;
......@@ -65,12 +65,31 @@ private void OnReferenceLocationsChanged(object sender, ImmutableArray<InlineRen
UpdateSearchText(totalSpansCount, totalFilesCount);
}
private void OnIsReplacementTextValidChanged(bool isReplacementTextValid)
{
if (isReplacementTextValid == _isReplacementTextValid)
{
return;
}
_isReplacementTextValid = isReplacementTextValid;
ComputeDefaultRenameFileFlag();
NotifyPropertyChanged(nameof(AllowFileRename));
}
private void ComputeDefaultRenameFileFlag()
{
// If replacementText is invalid, we won't rename the file.
DefaultRenameFileFlag = _isReplacementTextValid
&& (_session.OptionSet.GetOption(RenameOptions.RenameFile) || AllowFileRename);
}
private void OnReplacementsComputed(object sender, IInlineRenameReplacementInfo result)
{
var session = (InlineRenameSession)sender;
_resolvableConflictCount = 0;
_unresolvableConflictCount = 0;
OnIsReplacementTextValidChanged(result.ReplacementTextValid);
if (result.ReplacementTextValid)
{
_errorText = null;
......@@ -154,7 +173,7 @@ private void UpdateSeverity()
public DashboardSeverity Severity => _severity;
public bool AllowFileRename => _session.FileRenameInfo == InlineRenameFileRenameInfo.Allowed;
public bool AllowFileRename => _session.FileRenameInfo == InlineRenameFileRenameInfo.Allowed && _isReplacementTextValid;
public bool ShowFileRename => _session.FileRenameInfo != InlineRenameFileRenameInfo.NotAllowed;
public string FileRenameString => _session.FileRenameInfo switch
{
......
......@@ -110,6 +110,38 @@ class C
End Using
End Function
<WpfTheory>
<CombinatorialData, Trait(Traits.Feature, Traits.Features.Rename)>
<WorkItem(46208, "https://github.com/dotnet/roslyn/issues/46208")>
Public Async Function RenameWithInvalidIdentifier(host As RenameTestHost) As Task
Using workspace = CreateWorkspaceWithWaiter(
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document><![CDATA[
class [|Test1$$|]
{
}
]]></Document>
</Project>
</Workspace>, host)
Dim options = workspace.CurrentSolution.Options
workspace.TryApplyChanges(
workspace.CurrentSolution.WithOptions(options.WithChangedOption(RenameOptions.RenameFile, True)))
Dim session = StartSession(workspace)
Dim selectedSpan = workspace.DocumentWithCursor.CursorPosition.Value
Dim textBuffer = workspace.Documents.Single().GetTextBuffer()
' User could use copy & paste to enter invalid character
textBuffer.Insert(selectedSpan, "<>")
session.Commit()
Await VerifyTagsAreCorrect(workspace, "Test1<>")
VerifyFileName(workspace, "Test1")
End Using
End Function
<WpfTheory>
<CombinatorialData, Trait(Traits.Feature, Traits.Features.Rename)>
<WorkItem(22495, "https://github.com/dotnet/roslyn/issues/22495")>
......
......@@ -245,7 +245,7 @@ public async Task<MutableConflictResolution> ResolveConflictsAsync()
.Select(l => conflictResolution.OldSolution.GetDocument(l.SourceTree))
.Distinct();
if (definitionDocuments.Count() == 1)
if (definitionDocuments.Count() == 1 && _replacementTextValid)
{
// At the moment, only single document renaming is allowed
conflictResolution.RenameDocumentToMatchNewSymbol(definitionDocuments.Single());
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册