generate namespace for new file

This commit is contained in:
Matt Parker
2025-10-20 19:55:28 +10:00
parent 6fa0049f62
commit e114e2c524
4 changed files with 23 additions and 4 deletions

View File

@@ -1,4 +1,5 @@
using SharpIDE.Application.Features.SolutionDiscovery;
using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence;
namespace SharpIDE.Application.Features.FileWatching;
@@ -25,10 +26,11 @@ public class IdeFileOperationsService(SharpIdeSolutionModificationService sharpI
// await _sharpIdeSolutionModificationService.RemoveFile(file);
// }
public async Task CreateCsFile(SharpIdeFolder parentFolder, string newFileName, string @namespace)
public async Task CreateCsFile(SharpIdeFolder parentFolder, string newFileName)
{
var newFilePath = Path.Combine(parentFolder.Path, newFileName);
var className = Path.GetFileNameWithoutExtension(newFileName);
var @namespace = NewFileTemplates.ComputeNamespace(parentFolder);
var fileText = NewFileTemplates.CsharpClass(className, @namespace);
await File.WriteAllTextAsync(newFilePath, fileText);
await _sharpIdeSolutionModificationService.CreateFile(parentFolder, newFileName, fileText);

View File

@@ -1,4 +1,7 @@
namespace SharpIDE.Application.Features.FileWatching;
using SharpIDE.Application.Features.SolutionDiscovery;
using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence;
namespace SharpIDE.Application.Features.FileWatching;
public static class NewFileTemplates
{
@@ -14,4 +17,17 @@ public static class NewFileTemplates
""";
return text;
}
public static string ComputeNamespace(SharpIdeFolder folder)
{
var names = new List<string>();
IFolderOrProject? current = folder;
while (current is not null)
{
names.Add(current.Name);
current = current.Parent as IFolderOrProject;
}
names.Reverse();
return string.Join('.', names);
}
}

View File

@@ -15,10 +15,11 @@ public interface IExpandableSharpIdeNode
public bool Expanded { get; set; }
}
public interface IFolderOrProject : IExpandableSharpIdeNode
public interface IFolderOrProject : IExpandableSharpIdeNode, IChildSharpIdeNode
{
public ObservableHashSet<SharpIdeFolder> Folders { get; init; }
public ObservableHashSet<SharpIdeFile> Files { get; init; }
public string Name { get; set; }
}
public interface IChildSharpIdeNode
{

View File

@@ -72,7 +72,7 @@ public partial class NewCsharpFileDialog : ConfirmationDialog
_ = Task.GodotRun(async () =>
{
await _ideFileOperationsService.CreateCsFile(ParentFolder, fileName, "asdf");
await _ideFileOperationsService.CreateCsFile(ParentFolder, fileName);
});
QueueFree();
}