Initial commit
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
using Microsoft.CodeAnalysis;
|
||||
using RobotNet10.ScriptEngine.Shared;
|
||||
|
||||
namespace RobotNet10.ScriptEditor.Models;
|
||||
|
||||
public class ScriptFile(DocumentId id, ScriptFileDto data, ScriptFolder? parent = null) : IHierarchyItem
|
||||
{
|
||||
public DocumentId Id { get; } = id;
|
||||
public ScriptFolder? Parent => parent;
|
||||
public string Path => System.IO.Path.Combine(parent?.Path ?? "", Name);
|
||||
public int Level { get; } = data.Level;
|
||||
public bool IsModified { get; private set; }
|
||||
public int WarningCount { get; private set; }
|
||||
public int ErrorCount { get; private set; }
|
||||
|
||||
public event Action? Modified
|
||||
{
|
||||
add => _modified += value;
|
||||
remove => _modified -= value;
|
||||
}
|
||||
|
||||
private event Action? _modified;
|
||||
public event Action? NameChanged;
|
||||
public event Action<int, int>? DiagnosticsChanged;
|
||||
|
||||
private string _name = data.Name;
|
||||
public string Name
|
||||
{
|
||||
get => _name;
|
||||
set
|
||||
{
|
||||
if (string.IsNullOrEmpty(value)) throw new ArgumentNullException($"Tên file {_name} không được để trống");
|
||||
|
||||
if (_name == value) return;
|
||||
|
||||
_name = value;
|
||||
NameChanged?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
private string _code = data.Code;
|
||||
public string Code
|
||||
{
|
||||
get => _code;
|
||||
set
|
||||
{
|
||||
_code = value;
|
||||
if (IsModified)
|
||||
{
|
||||
if (SavedCode == _code)
|
||||
{
|
||||
IsModified = false;
|
||||
_modified?.Invoke();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (SavedCode != _code)
|
||||
{
|
||||
IsModified = true;
|
||||
_modified?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerable<Diagnostic> _diagnostics = [];
|
||||
public IEnumerable<Diagnostic> Diagnostics
|
||||
{
|
||||
get => _diagnostics;
|
||||
set
|
||||
{
|
||||
_diagnostics = value;
|
||||
var warning = _diagnostics.Count(d => d.Severity == DiagnosticSeverity.Warning);
|
||||
var error = _diagnostics.Count(d => d.Severity == DiagnosticSeverity.Error);
|
||||
|
||||
if (WarningCount != warning || ErrorCount != error)
|
||||
{
|
||||
WarningCount = warning;
|
||||
ErrorCount = error;
|
||||
// Send absolute values, not delta, for consistency with ScriptFolder.OnDiagnosticsChanged
|
||||
DiagnosticsChanged?.Invoke(warning, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private string SavedCode = data.Code;
|
||||
|
||||
public void Saved()
|
||||
{
|
||||
if (!IsModified || SavedCode == Code)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
SavedCode = Code;
|
||||
IsModified = false;
|
||||
_modified?.Invoke();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user