Rework file persistence

This commit is contained in:
Matt Parker
2025-10-08 20:03:59 +10:00
parent f4356b7653
commit 8f1f510625
12 changed files with 177 additions and 25 deletions

View File

@@ -0,0 +1 @@
uid://dyoci88kqk2r1

View File

@@ -1,5 +1,6 @@
using Ardalis.GuardClauses;
using Godot;
using R3;
using SharpIDE.Application.Features.Analysis;
using SharpIDE.Application.Features.Debugging;
using SharpIDE.Application.Features.Events;
@@ -74,6 +75,16 @@ public partial class CodeEditorPanel : MarginContainer
_tabContainer.SetTabTooltip(newTabIndex, file.Path);
_tabContainer.CurrentTab = newTabIndex;
});
file.IsDirty.Skip(1).SubscribeOnThreadPool().SubscribeAwait(async (isDirty, ct) =>
{
//GD.Print($"File dirty state changed: {file.Path} is now {(isDirty ? "dirty" : "clean")}");
await this.InvokeAsync(() =>
{
var tabIndex = newTab.GetIndex();
var title = file.Name + (isDirty ? " (*)" : "");
_tabContainer.SetTabTitle(tabIndex, title);
});
});
await newTab.SetSharpIdeFile(file);
if (fileLinePosition is not null) await this.InvokeAsync(() => newTab.SetFileLinePosition(fileLinePosition.Value));
}

View File

@@ -1,5 +1,4 @@
using System.Collections.Immutable;
using Ardalis.GuardClauses;
using Godot;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Classification;
@@ -93,10 +92,11 @@ public partial class SharpIdeCodeEdit : CodeEdit
GD.Print($"Selection changed to line {_currentLine}, start {_selectionStartCol}, end {_selectionEndCol}");
}
private void OnTextChanged()
private async void OnTextChanged()
{
// update the MSBuildWorkspace
RoslynAnalysis.UpdateDocument(_currentFile, Text);
await Task.CompletedTask.ConfigureAwait(ConfigureAwaitOptions.ForceYielding);
_currentFile.IsDirty.Value = true;
Singletons.FileManager.UpdateFileTextInMemory(_currentFile, Text);
_ = Task.GodotRun(async () =>
{
var syntaxHighlighting = RoslynAnalysis.GetDocumentSyntaxHighlighting(_currentFile);
@@ -122,8 +122,12 @@ public partial class SharpIdeCodeEdit : CodeEdit
var vScroll = GetVScroll();
_ = Task.GodotRun(async () =>
{
await RoslynAnalysis.ApplyCodeActionAsync(codeAction);
var fileContents = await File.ReadAllTextAsync(_currentFile.Path);
var affectedFiles = await RoslynAnalysis.ApplyCodeActionAsync(codeAction);
foreach (var (affectedFile, updatedText) in affectedFiles)
{
await Singletons.FileManager.UpdateInMemoryIfOpenAndSaveAsync(affectedFile, updatedText);
}
var fileContents = await Singletons.FileManager.GetFileTextAsync(_currentFile);
var syntaxHighlighting = await RoslynAnalysis.GetDocumentSyntaxHighlighting(_currentFile);
var razorSyntaxHighlighting = await RoslynAnalysis.GetRazorDocumentSyntaxHighlighting(_currentFile);
var diagnostics = await RoslynAnalysis.GetDocumentDiagnostics(_currentFile);
@@ -156,7 +160,7 @@ public partial class SharpIdeCodeEdit : CodeEdit
{
await Task.CompletedTask.ConfigureAwait(ConfigureAwaitOptions.ForceYielding); // get off the UI thread
_currentFile = file;
var readFileTask = File.ReadAllTextAsync(_currentFile.Path);
var readFileTask = Singletons.FileManager.GetFileTextAsync(file);
var syntaxHighlighting = RoslynAnalysis.GetDocumentSyntaxHighlighting(_currentFile);
var razorSyntaxHighlighting = RoslynAnalysis.GetRazorDocumentSyntaxHighlighting(_currentFile);
@@ -234,6 +238,20 @@ public partial class SharpIdeCodeEdit : CodeEdit
{
EmitSignalCodeFixesRequested();
}
else if (@event.IsActionPressed(InputStringNames.SaveAllFiles))
{
_ = Task.GodotRun(async () =>
{
await Singletons.FileManager.SaveAllOpenFilesAsync();
});
}
else if (@event.IsActionPressed(InputStringNames.SaveFile))
{
_ = Task.GodotRun(async () =>
{
await Singletons.FileManager.SaveFileAsync(_currentFile);
});
}
}

View File

@@ -130,7 +130,7 @@ public partial class SolutionExplorerPanel : MarginContainer
foreach (var sharpIdeFolder in project.Folders)
{
AddFoldertoTree(projectItem, sharpIdeFolder);
AddFolderToTree(projectItem, sharpIdeFolder);
}
foreach (var file in project.Files)
@@ -139,7 +139,7 @@ public partial class SolutionExplorerPanel : MarginContainer
}
}
private void AddFoldertoTree(TreeItem projectItem, SharpIdeFolder sharpIdeFolder)
private void AddFolderToTree(TreeItem projectItem, SharpIdeFolder sharpIdeFolder)
{
var folderItem = _tree.CreateItem(projectItem);
folderItem.SetText(0, sharpIdeFolder.Name);
@@ -147,7 +147,7 @@ public partial class SolutionExplorerPanel : MarginContainer
foreach (var subFolder in sharpIdeFolder.Folders)
{
AddFoldertoTree(folderItem, subFolder); // recursion
AddFolderToTree(folderItem, subFolder); // recursion
}
foreach (var file in sharpIdeFolder.Files)

View File

@@ -4,6 +4,7 @@ using Microsoft.Extensions.Hosting;
using SharpIDE.Application.Features.Analysis;
using SharpIDE.Application.Features.Build;
using SharpIDE.Application.Features.Events;
using SharpIDE.Application.Features.FilePersistence;
using SharpIDE.Application.Features.FileWatching;
using SharpIDE.Application.Features.Run;
using SharpIDE.Application.Features.SolutionDiscovery;
@@ -45,6 +46,7 @@ public partial class IdeRoot : Control
Singletons.BuildService = new BuildService();
Singletons.FileWatcher?.Dispose();
Singletons.FileWatcher = new IdeFileWatcher();
Singletons.FileManager = new IdeFileManager();
}
public override void _Ready()

View File

@@ -7,4 +7,6 @@ public static class InputStringNames
public static readonly StringName CodeFixes = "CodeFixes";
public static readonly StringName StepOver = "StepOver";
public static readonly StringName FindInFiles = nameof(FindInFiles);
public static readonly StringName SaveFile = nameof(SaveFile);
public static readonly StringName SaveAllFiles = nameof(SaveAllFiles);
}

View File

@@ -1,4 +1,5 @@
using SharpIDE.Application.Features.Build;
using SharpIDE.Application.Features.FilePersistence;
using SharpIDE.Application.Features.FileWatching;
using SharpIDE.Application.Features.Run;
using SharpIDE.Godot.Features.IdeSettings;
@@ -10,5 +11,6 @@ public static class Singletons
public static RunService RunService { get; set; } = null!;
public static BuildService BuildService { get; set; } = null!;
public static IdeFileWatcher FileWatcher { get; set; } = null!;
public static IdeFileManager FileManager { get; set; } = null!;
public static AppState AppState { get; set; } = null!;
}

View File

@@ -51,3 +51,13 @@ FindInFiles={
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":true,"ctrl_pressed":true,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":70,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
]
}
SaveFile={
"deadzone": 0.2,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":true,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":83,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
]
}
SaveAllFiles={
"deadzone": 0.2,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":true,"ctrl_pressed":true,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":83,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
]
}