open file on select

This commit is contained in:
Matt Parker
2025-08-01 00:42:05 +10:00
parent 1d28ac7432
commit 0360789e80
2 changed files with 44 additions and 25 deletions

View File

@@ -1,10 +1,11 @@
@using SharpIDE.Application.Features.SolutionDiscovery @using Ardalis.GuardClauses
@using SharpIDE.Application.Features.SolutionDiscovery
@using SharpIDE.Photino.Services @using SharpIDE.Photino.Services
@inject RefreshOpenFileService RefreshOpenFileService @inject RefreshOpenFileService RefreshOpenFileService
<MudText> <MudText>
@Path.GetFileName(FilePath) @_internalSelectedFile?.Name
@if (_unsavedEdits) @if (_unsavedEdits)
{ {
<span>*</span> <span>*</span>
@@ -15,36 +16,39 @@
height: calc(100vh - 100px); height: calc(100vh - 100px);
} }
</style> </style>
<StandaloneCodeEditor @ref="_codeEditorRef" Id="my-editor-id" ConstructionOptions="@EditorConstructionOptions" OnDidChangeModelContent="IfDirtyMarkDirty" OnDidBlurEditorText="@SaveFileToDisk" /> <div style="@(_internalSelectedFile is null ? "visibility: hidden" : null)">
<StandaloneCodeEditor @ref="_codeEditorRef" Id="my-editor-id" ConstructionOptions="@EditorConstructionOptions" OnDidChangeModelContent="IfDirtyMarkDirty" OnDidBlurEditorText="@SaveFileToDisk" />
</div>
@code { @code {
[Parameter, EditorRequired] [Parameter, EditorRequired]
public string FilePath { get; set; } = null!;
[Parameter, EditorRequired]
public SharpIdeFile? SelectedFile { get; set; } public SharpIdeFile? SelectedFile { get; set; }
private SharpIdeFile? _internalSelectedFile;
private string? _fileContent; private string? _fileContent;
private bool _unsavedEdits = false; private bool _unsavedEdits = false;
StandaloneCodeEditor _codeEditorRef = null!; StandaloneCodeEditor _codeEditorRef = null!;
private StandaloneEditorConstructionOptions EditorConstructionOptions(StandaloneCodeEditor editor) protected override async Task OnParametersSetAsync()
{ {
return new StandaloneEditorConstructionOptions if (_internalSelectedFile is not null)
{ {
AutomaticLayout = true, await SaveFileToDisk();
Language = "csharp", }
Theme = "vs-dark", if (SelectedFile is null) return;
ScrollBeyondLastLine = false, _internalSelectedFile = SelectedFile;
FontSize = 18, var model = await _codeEditorRef.GetModel();
Value = _fileContent ArgumentNullException.ThrowIfNull(model);
}; await model.SetValue(string.Empty);
await ReadFile();
await model.SetValue(_fileContent);
_unsavedEdits = false;
StateHasChanged();
} }
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
await ReadFile();
RefreshOpenFileService.RefreshOpenFile += async () => RefreshOpenFileService.RefreshOpenFile += async () =>
{ {
Console.WriteLine("RefreshOpenFileService.RefreshOpenFile called"); Console.WriteLine("RefreshOpenFileService.RefreshOpenFile called");
@@ -58,25 +62,27 @@
private async Task ReadFile() private async Task ReadFile()
{ {
var fileInfo = new FileInfo(FilePath); Guard.Against.Null(_internalSelectedFile);
var fileInfo = new FileInfo(_internalSelectedFile.Path);
if (!fileInfo.Exists) if (!fileInfo.Exists)
{ {
throw new FileNotFoundException($"File not found: {FilePath}"); throw new FileNotFoundException($"File not found: {_internalSelectedFile.Path}");
} }
var fileContent = await File.ReadAllTextAsync(FilePath); var fileContent = await File.ReadAllTextAsync(_internalSelectedFile.Path);
_fileContent = fileContent; _fileContent = fileContent;
} }
private async Task SaveFileToDisk(object obj) private async Task SaveFileToDisk()
{ {
Guard.Against.Null(_internalSelectedFile);
if (_unsavedEdits is false) if (_unsavedEdits is false)
{ {
return; return;
} }
var editor = obj as StandaloneCodeEditor;
ArgumentNullException.ThrowIfNull(editor, nameof(editor)); ArgumentNullException.ThrowIfNull(_codeEditorRef, nameof(_codeEditorRef));
var editorTextValue = await editor.GetValue(); var editorTextValue = await _codeEditorRef.GetValue();
await File.WriteAllTextAsync(FilePath, editorTextValue); await File.WriteAllTextAsync(_internalSelectedFile.Path, editorTextValue);
_fileContent = editorTextValue; _fileContent = editorTextValue;
_unsavedEdits = false; _unsavedEdits = false;
} }
@@ -93,4 +99,17 @@
_unsavedEdits = false; _unsavedEdits = false;
} }
} }
private static StandaloneEditorConstructionOptions EditorConstructionOptions(StandaloneCodeEditor editor)
{
return new StandaloneEditorConstructionOptions
{
AutomaticLayout = true,
Language = "csharp",
Theme = "vs-dark",
ScrollBeyondLastLine = false,
FontSize = 18,
Value = string.Empty
};
}
} }

View File

@@ -22,7 +22,7 @@
@* @Body *@ @* @Body *@
@if (_solutionFilePath is not null) @if (_solutionFilePath is not null)
{ {
<CodeViewer SelectedFile="@_selectedFile" FilePath="C:\Users\Matthew\Documents\Git\SharpIDE.Photino\src\SharpIDE.Photino\Program.cs" /> <CodeViewer SelectedFile="@_selectedFile" />
} }
</MudContainer> </MudContainer>
</MudMainContent> </MudMainContent>