Skip to content

Commit

Permalink
Merge pull request #1388 from erri120/fix/1384-text-editor-crash
Browse files Browse the repository at this point in the history
Better null handling for TextDocument
  • Loading branch information
erri120 authored May 21, 2024
2 parents df7da88 + dedddf7 commit 2a3c34e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public interface ITextEditorPageViewModel : IPageViewModelInterface
/// <summary>
/// Gets or sets the document to display and edit in the text editor.
/// </summary>
public TextDocument? Document { get; [UsedImplicitly] set; }
public TextDocument Document { get; [UsedImplicitly] set; }

/// <summary>
/// Gets the command for saving the document.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ public TextEditorPageView()
.SubscribeWithErrorLogging(document =>
{
TextEditor.Document = document;
if (document is null) return;

var extension = Extension.FromPath(document.FileName);
var language = registryOptions.GetLanguageByExtension(extension.ToString());
if (language is null) return;

LanguageNameText.Text = language.ToString();

var scopeName = registryOptions.GetScopeByLanguageId(language.Id);
Expand Down
11 changes: 7 additions & 4 deletions src/NexusMods.App.UI/Pages/TextEdit/TextEditorPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ public class TextEditorPageViewModel : APageViewModel<ITextEditorPageViewModel>,
{
[Reactive] public TextEditorPageContext? Context { get; set; }

[Reactive] public bool IsReadOnly { get; set; }
[Reactive] public bool IsReadOnly { get; set; } = true;

[Reactive] public bool IsModified { get; set; }

[Reactive] public TextDocument? Document { get; set; }
private static readonly TextDocument EmptyDocument = new(new StringTextSource("")) { FileName = "empty.txt" };
[Reactive] public TextDocument Document { get; set; } = EmptyDocument;

public ReactiveCommand<Unit, Unit> SaveCommand { get; }
private readonly ReactiveCommand<TextEditorPageContext, ValueTuple<TextEditorPageContext, string>> _loadFileCommand;
Expand Down Expand Up @@ -75,15 +76,16 @@ public TextEditorPageViewModel(
var canSaveObservable = this.WhenAnyValue(
vm => vm.Document,
vm => vm.IsModified,
(document, isModified) => document is not null && isModified
vm => vm.IsReadOnly,
(document, isModified, isReadOnly) => document != EmptyDocument && isModified && !isReadOnly
);

SaveCommand = ReactiveCommand.CreateFromTask(async () =>
{
var fileId = Context!.FileId;
var filePath = Context!.FilePath;

var text = Document?.Text ?? string.Empty;
var text = Document.Text;

// hash and store the new contents
var bytes = Encoding.UTF8.GetBytes(text);
Expand Down Expand Up @@ -151,6 +153,7 @@ public TextEditorPageViewModel(
};

Document = document;
IsReadOnly = false;
})
.DisposeWith(disposables);

Expand Down

0 comments on commit 2a3c34e

Please sign in to comment.