rename directory from IDE

This commit is contained in:
Matt Parker
2025-10-21 18:16:09 +10:00
parent 575cdf61c0
commit 4496f44a21
11 changed files with 211 additions and 8 deletions

View File

@@ -796,4 +796,11 @@ public class RoslynAnalysis
_workspace.TryApplyChanges(newSolution);
}
public async Task MoveDocument(SharpIdeFile sharpIdeFile, string oldFilePath)
{
var document = _workspace!.CurrentSolution.GetDocumentIdsWithFilePath(oldFilePath).Single();
var updatedSolution = _workspace.CurrentSolution.WithDocumentFilePath(document, sharpIdeFile.Path);
_workspace.TryApplyChanges(updatedSolution);
}
}

View File

@@ -23,6 +23,15 @@ public class FileChangedService(RoslynAnalysis roslynAnalysis, IdeOpenTabsFileMa
public SharpIdeSolutionModel SolutionModel { get; set; } = null!;
public async Task SharpIdeFileMoved(SharpIdeFile file, string oldFilePath)
{
if (file.IsRoslynWorkspaceFile)
{
await HandleWorkspaceFileMoved(file, oldFilePath);
}
// TODO: handle csproj moved
}
public async Task SharpIdeFileAdded(SharpIdeFile file, string content)
{
if (file.IsRoslynWorkspaceFile)
@@ -125,4 +134,15 @@ public class FileChangedService(RoslynAnalysis roslynAnalysis, IdeOpenTabsFileMa
GlobalEvents.Instance.SolutionAltered.InvokeParallelFireAndForget();
await _roslynAnalysis.UpdateSolutionDiagnostics(newCts.Token);
}
private async Task HandleWorkspaceFileMoved(SharpIdeFile file, string oldFilePath)
{
var newCts = new CancellationTokenSource();
var oldCts = Interlocked.Exchange(ref _updateSolutionDiagnosticsCts, newCts);
await oldCts.CancelAsync();
oldCts.Dispose();
await _roslynAnalysis.MoveDocument(file, oldFilePath);
GlobalEvents.Instance.SolutionAltered.InvokeParallelFireAndForget();
await _roslynAnalysis.UpdateSolutionDiagnostics(newCts.Token);
}
}

View File

@@ -18,6 +18,24 @@ public class IdeFileExternalChangeHandler
GlobalEvents.Instance.FileSystemWatcherInternal.FileCreated.Subscribe(OnFileCreated);
GlobalEvents.Instance.FileSystemWatcherInternal.DirectoryCreated.Subscribe(OnFolderCreated);
GlobalEvents.Instance.FileSystemWatcherInternal.DirectoryDeleted.Subscribe(OnFolderDeleted);
GlobalEvents.Instance.FileSystemWatcherInternal.DirectoryRenamed.Subscribe(OnFolderRenamed);
}
// TODO: Test - this most likely only will ever be called on linux - windows and macos(?) does delete + create on rename of folders
private async Task OnFolderRenamed(string oldFolderPath, string newFolderPath)
{
var sharpIdeFolder = SolutionModel.AllFolders.SingleOrDefault(f => f.Path == newFolderPath);
if (sharpIdeFolder is null)
{
return;
}
var isMoveRatherThanRename = Path.GetDirectoryName(oldFolderPath) != Path.GetDirectoryName(newFolderPath);
if (isMoveRatherThanRename)
{
throw new NotImplementedException("Moving folders is not yet supported. Note that this should only be encountered on Linux.");
}
var newFolderName = Path.GetFileName(newFolderPath);
await _sharpIdeSolutionModificationService.RenameDirectory(sharpIdeFolder, newFolderName);
}
private async Task OnFolderDeleted(string folderPath)

View File

@@ -7,6 +7,14 @@ public class IdeFileOperationsService(SharpIdeSolutionModificationService sharpI
{
private readonly SharpIdeSolutionModificationService _sharpIdeSolutionModificationService = sharpIdeSolutionModificationService;
public async Task RenameDirectory(SharpIdeFolder folder, string newDirectoryName)
{
var parentPath = Path.GetDirectoryName(folder.Path)!;
var newDirectoryPath = Path.Combine(parentPath, newDirectoryName);
Directory.Move(folder.Path, newDirectoryPath);
await _sharpIdeSolutionModificationService.RenameDirectory(folder, newDirectoryName);
}
public async Task CreateDirectory(IFolderOrProject parentNode, string newDirectoryName)
{
var newDirectoryPath = Path.Combine(parentNode.ChildNodeBasePath, newDirectoryName);

View File

@@ -56,14 +56,23 @@ public sealed class IdeFileWatcher : IDisposable
case ChangeType.CHANGED: HandleChanged(e.FullPath); break;
case ChangeType.CREATED: HandleCreated(e.FullPath); break;
case ChangeType.DELETED: HandleDeleted(e.FullPath); break;
case ChangeType.RENAMED: HandleRenamed(e.OldFullPath, e.FullPath); break;
case ChangeType.RENAMED: HandleRenamed(e.OldFullPath!, e.FullPath); break;
default: throw new ArgumentOutOfRangeException();
}
}
private void HandleRenamed(string? oldFullPath, string fullPath)
private void HandleRenamed(string oldFullPath, string fullPath)
{
Console.WriteLine($"FileSystemWatcher: Renamed - {oldFullPath}, {fullPath}");
var isDirectory = Path.HasExtension(fullPath) is false;
if (isDirectory)
{
GlobalEvents.Instance.FileSystemWatcherInternal.DirectoryRenamed.InvokeParallelFireAndForget(oldFullPath, fullPath);
}
else
{
GlobalEvents.Instance.FileSystemWatcherInternal.FileRenamed.InvokeParallelFireAndForget(oldFullPath, fullPath);
}
//Console.WriteLine($"FileSystemWatcher: Renamed - {oldFullPath}, {fullPath}");
}
private void HandleDeleted(string fullPath)

View File

@@ -60,6 +60,40 @@ public class SharpIdeSolutionModificationService(FileChangedService fileChangedS
}
}
public async Task MoveDirectory(SharpIdeFolder folder, string newDirectoryPath)
{
}
public async Task RenameDirectory(SharpIdeFolder folder, string renamedFolderName)
{
var oldFolderPath = folder.Path;
folder.Name = renamedFolderName;
folder.Path = Path.Combine(Path.GetDirectoryName(oldFolderPath)!, renamedFolderName);
var stack = new Stack<SharpIdeFolder>();
stack.Push(folder);
while (stack.Count > 0)
{
var current = stack.Pop();
foreach (var subfolder in current.Folders)
{
subfolder.Path = Path.Combine(current.Path, subfolder.Name);
stack.Push(subfolder);
}
foreach (var file in current.Files)
{
var oldPath = file.Path;
file.Path = Path.Combine(current.Path, file.Name);
await _fileChangedService.SharpIdeFileMoved(file, oldPath);
}
}
}
public async Task<SharpIdeFile> CreateFile(IFolderOrProject parentNode, string newFilePath, string fileName, string contents)
{
var sharpIdeFile = new SharpIdeFile(newFilePath, fileName, parentNode, []);