mirror of
https://onedev.site.tesses.net/crosslang/crosslangdevstudio
synced 2026-02-08 09:15:45 +00:00
132 lines
3.6 KiB
C#
132 lines
3.6 KiB
C#
using System;
|
|
using System.Collections.ObjectModel;
|
|
using System.IO;
|
|
using System.Threading.Tasks;
|
|
using Avalonia;
|
|
using Avalonia.Controls;
|
|
using Avalonia.Platform.Storage;
|
|
using AvaloniaEdit;
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
using CommunityToolkit.Mvvm.Messaging;
|
|
using CrossLangDevStudio.Messages;
|
|
using CrossLangDevStudio.Views;
|
|
using AvaloniaEdit.CodeCompletion;
|
|
using AvaloniaEdit.Document;
|
|
using AvaloniaEdit.Editing;
|
|
using AvaloniaEdit.Folding;
|
|
using AvaloniaEdit.Rendering;
|
|
using TextMateSharp.Grammars;
|
|
using AvaloniaEdit.TextMate;
|
|
using Avalonia.Input;
|
|
using System.Collections.Generic;
|
|
using Avalonia.Media;
|
|
namespace CrossLangDevStudio.ViewModels;
|
|
|
|
public partial class FileEditorViewModel : ViewModelBase, ISavable
|
|
{
|
|
TabItemViewModel tab;
|
|
public string FilePath { get; }
|
|
bool _modified = false;
|
|
public bool Modified
|
|
{
|
|
get => _modified;
|
|
set
|
|
{
|
|
_modified = value;
|
|
|
|
if (value)
|
|
{
|
|
tab.Header = $"{Path.GetFileName(FilePath)}*";
|
|
}
|
|
else
|
|
{
|
|
tab.Header = Path.GetFileName(FilePath);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
TextEditor textEdit = new TextEditor();
|
|
|
|
public TextEditor TextEditor => textEdit;
|
|
|
|
|
|
|
|
public FileEditorViewModel(string path, TabItemViewModel tab)
|
|
{
|
|
this.tab = tab;
|
|
FilePath = path;
|
|
textEdit.ShowLineNumbers = true;
|
|
textEdit.FontSize = 24;
|
|
string ext = Path.GetExtension(FilePath);
|
|
if (ext == ".tcross")
|
|
{
|
|
ext = ".go";
|
|
}
|
|
{
|
|
|
|
var _registryOptions = new RegistryOptions(ThemeName.Dark);
|
|
|
|
var _textMateInstallation = textEdit.InstallTextMate(_registryOptions);
|
|
|
|
//definition.
|
|
|
|
|
|
|
|
//Here we are getting the language by the extension and right after that we are initializing grammar with this language.
|
|
//And that's all 😀, you are ready to use AvaloniaEdit with syntax highlighting!
|
|
try
|
|
{
|
|
//AssetLoader.Open(new Uri("avares://CrossLangDevStudio/Assets/crosslang.png")
|
|
if (Path.GetFileNameWithoutExtension(path).ToLower() == "dockerfile")
|
|
{
|
|
|
|
_textMateInstallation.SetGrammarFile(_registryOptions.GetScopeByLanguageId("dockerfile"));
|
|
//_textMateInstallation.SetGrammar(_registryOptions.GetScopeByLanguageId(.Id));
|
|
}
|
|
if (Path.GetFileNameWithoutExtension(path).ToLower() == "makefile")
|
|
{
|
|
|
|
_textMateInstallation.SetGrammarFile(_registryOptions.GetScopeByLanguageId("makefile"));
|
|
//_textMateInstallation.SetGrammar(_registryOptions.GetScopeByLanguageId(.Id));
|
|
}
|
|
else if (Path.GetFileNameWithoutExtension(path) == "docker-compose" && (ext == ".yml" || ext == ".yaml"))
|
|
_textMateInstallation.SetGrammarFile(_registryOptions.GetScopeByLanguageId("dockercompose"));
|
|
else
|
|
_textMateInstallation.SetGrammar(_registryOptions.GetScopeByLanguageId(_registryOptions.GetLanguageByExtension(ext).Id));
|
|
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
}
|
|
}
|
|
|
|
|
|
textEdit.Text = File.ReadAllText(path);
|
|
textEdit.TextChanged += (sender, e) =>
|
|
{
|
|
Modified = true;
|
|
};
|
|
}
|
|
|
|
|
|
public void Save()
|
|
{
|
|
|
|
File.WriteAllText(FilePath, textEdit.Text);
|
|
Modified = false;
|
|
|
|
}
|
|
|
|
public void SaveRecovery(string path)
|
|
{
|
|
|
|
File.WriteAllText(path, textEdit.Text);
|
|
|
|
|
|
}
|
|
}
|