Skip to content

Commit

Permalink
Merge pull request #418 from garstenauer/demo-insert-snippet
Browse files Browse the repository at this point in the history
Demonstrate how to use text snippets
  • Loading branch information
danipen authored Apr 27, 2024
2 parents 4207cbe + 60fc084 commit 88625c4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/AvaloniaEdit.Demo/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
MinWidth="0"
MinHeight="300"
Width="950"
Width="1000"
Title="AvaloniaEdit Demo"
x:Class="AvaloniaEdit.Demo.MainWindow">
<DockPanel>
Expand All @@ -24,6 +24,7 @@
<Button Name="clearControlBtn" Content="Clear Buttons" VerticalAlignment="Stretch" VerticalContentAlignment="Center"/>
<ComboBox Name="syntaxModeCombo" VerticalAlignment="Stretch" VerticalContentAlignment="Center"/>
<Button Name="changeThemeBtn" Content="Change theme" VerticalAlignment="Stretch" VerticalContentAlignment="Center"/>
<Button Name="insertSnippetBtn" Content="Insert Snippet" VerticalAlignment="Stretch" VerticalContentAlignment="Center"/>
</StackPanel>
<StackPanel Name="StatusBar" Background="Purple" Height="25" DockPanel.Dock="Bottom" Orientation="Horizontal">
<TextBlock Foreground="White" Name="StatusText" Text="Ready" Margin="5 0 0 0" VerticalAlignment="Center" FontSize="12"/>
Expand Down
32 changes: 31 additions & 1 deletion src/AvaloniaEdit.Demo/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
using AvaloniaEdit.TextMate;
using TextMateSharp.Grammars;
using Avalonia.Diagnostics;
using AvaloniaEdit.Snippets;
using Snippet = AvaloniaEdit.Snippets.Snippet;

namespace AvaloniaEdit.Demo
{
using Pair = KeyValuePair<int, Control>;
Expand All @@ -32,6 +35,7 @@ public class MainWindow : Window
private Button _addControlButton;
private Button _clearControlButton;
private Button _changeThemeButton;
private Button _insertSnippetButton;
private ComboBox _syntaxModeCombo;
private TextBlock _statusTextBlock;
private ElementGenerator _generator = new ElementGenerator();
Expand All @@ -40,7 +44,6 @@ public class MainWindow : Window

public MainWindow()
{

InitializeComponent();

_textEditor = this.FindControl<TextEditor>("Editor");
Expand Down Expand Up @@ -75,6 +78,9 @@ public MainWindow()
_changeThemeButton = this.FindControl<Button>("changeThemeBtn");
_changeThemeButton.Click += ChangeThemeButton_Click;

_insertSnippetButton = this.FindControl<Button>("insertSnippetBtn");
_insertSnippetButton.Click += InsertSnippetButton_Click;

_textEditor.TextArea.TextView.ElementGenerators.Add(_generator);

_registryOptions = new RegistryOptions(
Expand Down Expand Up @@ -400,5 +406,29 @@ int IComparer<Pair>.Compare(Pair x, Pair y)
return x.Key.CompareTo(y.Key);
}
}

private void InsertSnippetButton_Click(object sender, RoutedEventArgs e)
{
var className = new SnippetReplaceableTextElement { Text = "Name" };
var snippet = new Snippet
{
Elements =
{
new SnippetTextElement { Text = "public class " },
className,
new SnippetTextElement
{
Text = "\n{\n public "
},
new SnippetBoundElement { TargetElement = className },
new SnippetTextElement { Text = "()\n {\n " },
new SnippetCaretElement(),
new SnippetTextElement { Text = "\n }\n}" }
}
};

snippet.Insert(_textEditor.TextArea);
_textEditor.Focus();
}
}
}

0 comments on commit 88625c4

Please sign in to comment.