AvaloniaUI / AvaloniaEdit

Avalonia-based text editor (port of AvalonEdit)
MIT License
731 stars 148 forks source link

Example instructions not working with theme colors #437

Open renderhjs opened 1 month ago

renderhjs commented 1 month ago

Followed getting started instructions from here multiple times from scratch but I can't seem to apply theme styling (colors) to the AvaloniaEdit.

image New Avalonia project with additional steps from https://github.com/AvaloniaUI/AvaloniaEdit to add AvaloniaEdit + apply a theme.

The AvaloniaEdit.Demo project on GitHub seems to have different properties in the TextMate classes for example https://github.com/AvaloniaUI/AvaloniaEdit/blob/318277d9f175b8e3ec882d30c023433e96db8545/src/AvaloniaEdit.TextMate/TextMate.cs#L66 and behaves differently to what's in NuGet. When I am trying to replicate the demo implementation I can't make it work with the NuGet libraries which offer different methods in TextMate and other classes.

Sample

MainView.axaml

<UserControl xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:vm="clr-namespace:AvaloniaEditTheme2.ViewModels"
             mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
             x:Class="AvaloniaEditTheme2.Views.MainView"
             x:DataType="vm:MainViewModel"
                         xmlns:AvaloniaEdit="clr-namespace:AvaloniaEdit;assembly=AvaloniaEdit"
                         >
  <Design.DataContext>
    <!-- This only sets the DataContext for the previewer in an IDE,
         to set the actual DataContext for runtime, set the DataContext property in code (look at App.axaml.cs) -->
    <vm:MainViewModel />
  </Design.DataContext>
    <Grid>
        <TextBlock Text="{Binding Greeting}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
        <AvaloniaEdit:TextEditor Name="Editor" Text="Hello AvaloniaEdit!"
                           ShowLineNumbers="True"
                           FontFamily="Cascadia Code,Consolas,Menlo,Monospace"/>
    </Grid>
</UserControl>

MainView.axaml.cs ...

using Avalonia.Controls;
using AvaloniaEdit;
using AvaloniaEdit.TextMate;
using TextMateSharp.Grammars;

namespace AvaloniaEditTheme2.Views;

public partial class MainView : UserControl
{
    public MainView()
    {
        InitializeComponent();

        //First of all you need to have a reference for your TextEditor for it to be used inside AvaloniaEdit.TextMate project.
        var _textEditor = this.FindControl<TextEditor>("Editor");

        //Here we initialize RegistryOptions with the theme we want to use.
        var  _registryOptions = new RegistryOptions(ThemeName.LightPlus);

        //Initial setup of TextMate.
        var _textMateInstallation = _textEditor.InstallTextMate(_registryOptions);

        //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!
        _textMateInstallation.SetGrammar(_registryOptions.GetScopeByLanguageId(_registryOptions.GetLanguageByExtension(".cs").Id));

        // Python Sample
        _textEditor.Text = "import subprocess\r\n\r\ndef open_visual_studio(path):\r\n    try:\r\n        # Use subprocess to run the command\r\n        subprocess.run(f'start devenv \"{path}\"', shell=True, check=True)";
    }
}

Expected Result

Theme color of ThemeName.LightPlus or any other should render in their colors

Actual result

There are no syntax theme colors applied

mgarstenauer commented 1 month ago

What NuGet packages are you using? When I run your code with the following NuGet packages, it seems to be working fine.

    <PackageReference Include="Avalonia" Version="11.0.11" />
    <PackageReference Include="Avalonia.Desktop" Version="11.0.11" />
    <PackageReference Include="Avalonia.Fonts.Inter" Version="11.0.11" />
    <PackageReference Include="Avalonia.Themes.Fluent" Version="11.0.11" />
    <PackageReference Include="Avalonia.AvaloniaEdit" Version="11.0.6" />
    <PackageReference Include="AvaloniaEdit.TextMate" Version="11.0.6" />

Note that I had to change:

_registryOptions.GetLanguageByExtension(".cs")

to

_registryOptions.GetLanguageByExtension(".py")

Are you perhaps including an incompatible version of TextMateSharp? For example, this one works with AvaloniaEdit 11.0.6:

<PackageReference Include="TextMateSharp.Grammars" Version="1.0.56" />

But this one is incompatible:

<PackageReference Include="TextMateSharp.Grammars" Version="1.0.57" />