handle copy pasting a file
This commit is contained in:
@@ -34,9 +34,11 @@ public class IdeFileOperationsService(SharpIdeSolutionModificationService sharpI
|
||||
await _sharpIdeSolutionModificationService.RemoveFile(file);
|
||||
}
|
||||
|
||||
// TODO: Pass class/interface/enum type to create different templates
|
||||
public async Task<SharpIdeFile> CreateCsFile(IFolderOrProject parentNode, string newFileName)
|
||||
{
|
||||
var newFilePath = Path.Combine(GetFileParentNodePath(parentNode), newFileName);
|
||||
if (File.Exists(newFilePath)) throw new InvalidOperationException($"File {newFilePath} already exists.");
|
||||
var className = Path.GetFileNameWithoutExtension(newFileName);
|
||||
var @namespace = NewFileTemplates.ComputeNamespace(parentNode);
|
||||
var fileText = NewFileTemplates.CsharpClass(className, @namespace);
|
||||
@@ -45,6 +47,16 @@ public class IdeFileOperationsService(SharpIdeSolutionModificationService sharpI
|
||||
return sharpIdeFile;
|
||||
}
|
||||
|
||||
public async Task<SharpIdeFile> CopyFile(IFolderOrProject destinationParentNode, string sourceFilePath, string newFileName)
|
||||
{
|
||||
var newFilePath = Path.Combine(GetFileParentNodePath(destinationParentNode), newFileName);
|
||||
if (File.Exists(newFilePath)) throw new InvalidOperationException($"File {newFilePath} already exists.");
|
||||
var fileContents = await File.ReadAllTextAsync(sourceFilePath);
|
||||
File.Copy(sourceFilePath, newFilePath);
|
||||
var sharpIdeFile = await _sharpIdeSolutionModificationService.CreateFile(destinationParentNode, newFilePath, newFileName, fileContents);
|
||||
return sharpIdeFile;
|
||||
}
|
||||
|
||||
private static string GetFileParentNodePath(IFolderOrProject parentNode) => parentNode switch
|
||||
{
|
||||
SharpIdeFolder folder => folder.Path,
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
using Godot;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence;
|
||||
|
||||
namespace SharpIDE.Godot.Features.Problems;
|
||||
|
||||
|
||||
@@ -27,6 +27,8 @@ public partial class SolutionExplorerPanel : MarginContainer
|
||||
public SharpIdeSolutionModel SolutionModel { get; set; } = null!;
|
||||
private Tree _tree = null!;
|
||||
private TreeItem _rootItem = null!;
|
||||
private enum ClipboardOperation { Cut, Copy }
|
||||
private (SharpIdeFile, ClipboardOperation)? _itemOnClipboard;
|
||||
public override void _Ready()
|
||||
{
|
||||
_tree = GetNode<Tree>("Tree");
|
||||
@@ -34,6 +36,57 @@ public partial class SolutionExplorerPanel : MarginContainer
|
||||
GodotGlobalEvents.Instance.FileExternallySelected.Subscribe(OnFileExternallySelected);
|
||||
}
|
||||
|
||||
public override void _UnhandledKeyInput(InputEvent @event)
|
||||
{
|
||||
// Copy
|
||||
if (@event is InputEventKey { Pressed: true, Keycode: Key.C, CtrlPressed: true })
|
||||
{
|
||||
var selected = _tree.GetSelected();
|
||||
if (selected is null) return;
|
||||
var genericMetadata = selected.GetMetadata(0).As<RefCounted?>();
|
||||
if (genericMetadata is RefCountedContainer<SharpIdeFile> fileContainer)
|
||||
{
|
||||
_itemOnClipboard = (fileContainer.Item, ClipboardOperation.Copy);
|
||||
}
|
||||
}
|
||||
// Cut
|
||||
else if (@event is InputEventKey { Pressed: true, Keycode: Key.X, CtrlPressed: true })
|
||||
{
|
||||
var selected = _tree.GetSelected();
|
||||
if (selected is null) return;
|
||||
var genericMetadata = selected.GetMetadata(0).As<RefCounted?>();
|
||||
if (genericMetadata is RefCountedContainer<SharpIdeFile> fileContainer)
|
||||
{
|
||||
_itemOnClipboard = (fileContainer.Item, ClipboardOperation.Cut);
|
||||
}
|
||||
}
|
||||
// Paste
|
||||
else if (@event is InputEventKey { Pressed: true, Keycode: Key.V, CtrlPressed: true })
|
||||
{
|
||||
var selected = _tree.GetSelected();
|
||||
if (selected is null || _itemOnClipboard is null) return;
|
||||
var genericMetadata = selected.GetMetadata(0).As<RefCounted?>();
|
||||
IFolderOrProject? folderOrProject = genericMetadata switch
|
||||
{
|
||||
RefCountedContainer<SharpIdeFolder> f => f.Item,
|
||||
RefCountedContainer<SharpIdeProjectModel> p => p.Item,
|
||||
_ => null
|
||||
};
|
||||
if (folderOrProject is null) return;
|
||||
|
||||
var (fileToPaste, operation) = _itemOnClipboard.Value;
|
||||
_itemOnClipboard = null;
|
||||
_ = Task.GodotRun(async () =>
|
||||
{
|
||||
if (operation is ClipboardOperation.Copy)
|
||||
{
|
||||
await _ideFileOperationsService.CopyFile(folderOrProject, fileToPaste.Path, fileToPaste.Name);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void TreeOnItemMouseSelected(Vector2 mousePosition, long mouseButtonIndex)
|
||||
{
|
||||
var selected = _tree.GetSelected();
|
||||
|
||||
Reference in New Issue
Block a user