-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from ItsTheSky/features/code-parsing
Code parsing & basic refactoring for options and variables
- Loading branch information
Showing
22 changed files
with
1,194 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
SkEditor/Controls/Sidebar/CodeParser/ParserSidebarPanel.axaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
<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:ui="using:FluentAvalonia.UI.Controls" | ||
xmlns:parser="clr-namespace:SkEditor.Utilities.Parser" | ||
x:Class="SkEditor.Controls.Sidebar.ParserSidebarPanel"> | ||
|
||
<UserControl.Styles> | ||
<Style Selector="Button.barButton"> | ||
<Setter Property="Background" Value="Transparent"/> | ||
<Setter Property="BorderThickness" Value="0"/> | ||
<Setter Property="Padding" Value="0"/> | ||
</Style> | ||
<Style Selector="Separator"> | ||
<Setter Property="Margin" Value="0,1"/> | ||
</Style> | ||
<Style Selector="TreeViewItem"> | ||
<Setter Property="FontWeight" Value="Regular"/> | ||
</Style> | ||
</UserControl.Styles> | ||
|
||
<Border MinWidth="350" Name="ExtendedSideBar" Background="{DynamicResource SkEditorBorderBackground}" CornerRadius="7" Margin="10,0,0,0"> | ||
|
||
|
||
<Grid RowDefinitions="auto,auto,auto,*,auto"> | ||
<TextBlock Grid.Row="0" Text="Code Parsing" FontWeight="DemiBold" Margin="20,10,20,10"/> | ||
<Separator Grid.Row="1" Margin="0,0,0,10"/> | ||
<ui:InfoBar Margin="5 0" Grid.Row="2" IsOpen="True" IsClosable="False" Severity="Warning" Message="The code parser is still in beta! Report any bugs found in the Discord server."/> | ||
<StackPanel Margin="10" Name="ParserDisabled" Grid.Row="3" VerticalAlignment="Center" HorizontalAlignment="Stretch" IsVisible="False" Spacing="10"> | ||
<TextBlock TextAlignment="Center" TextWrapping="Wrap">The code parser is still in beta and bugs may occur. It is disabled by default but you can enable it using the button below.</TextBlock> | ||
<Button Name="EnableParser" HorizontalAlignment="Center" VerticalAlignment="Center" Classes="accent">Enable</Button> | ||
</StackPanel> | ||
<ScrollViewer Name="ScrollViewer" Grid.Row="3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" VerticalContentAlignment="Top" HorizontalContentAlignment="Center"> | ||
<ItemsRepeater Name="ItemsRepeater"> | ||
<ItemsRepeater.ItemTemplate> | ||
<DataTemplate DataType="parser:CodeSection"> | ||
<Expander CornerRadius="0" Margin="0 5"> | ||
<Expander.Header> | ||
<StackPanel Orientation="Horizontal"> | ||
<ui:IconSourceElement Width="20" Height="20" IconSource="{Binding Icon}"></ui:IconSourceElement> | ||
<TextBlock Text="{Binding Name}" Margin="10 0 0 0" FontWeight="DemiBold"/> | ||
</StackPanel> | ||
</Expander.Header> | ||
<StackPanel Spacing="10"> | ||
<Grid ColumnDefinitions="*,*"> | ||
<TextBlock VerticalAlignment="Center" Text="{Binding LinesDisplay}" /> | ||
<Button Grid.Column="1" HorizontalContentAlignment="Center" HorizontalAlignment="Stretch" | ||
Content="Navigate" | ||
Name="NavigateToButton" Command="{Binding NavigateToCommand}" /> | ||
</Grid> | ||
<Separator Margin="0 3"/> | ||
|
||
<!-- Function Argument --> | ||
<StackPanel VerticalAlignment="Top" HorizontalAlignment="Stretch" | ||
Spacing="10" IsVisible="{Binding HasFunctionArguments}"> | ||
<TextBlock FontSize="18" TextAlignment="Left" HorizontalAlignment="Center" FontWeight="DemiBold" Text="{Binding FunctionArgumentTitle}"/> | ||
<StackPanel Orientation="Vertical" HorizontalAlignment="Stretch" Margin="5 8"> | ||
<ItemsControl ItemsSource="{Binding FunctionArguments}"> | ||
<ItemsControl.ItemTemplate> | ||
<DataTemplate> | ||
<Grid ColumnDefinitions="*,*,Auto" Margin="0 2"> | ||
<TextBlock TextAlignment="Left" FontWeight="DemiBold" Text="{Binding Name}"/> | ||
<TextBlock Grid.Column="1" TextAlignment="Left" Text="{Binding Type}"/> | ||
<Button Height="28" Width="28" Grid.Column="2" Command="{Binding Rename}" Padding="0" ToolTip.Tip="Rename"> | ||
<ui:SymbolIcon Symbol="Rename" FontSize="20" Margin="0"></ui:SymbolIcon> | ||
</Button> | ||
</Grid> | ||
</DataTemplate> | ||
</ItemsControl.ItemTemplate> | ||
</ItemsControl> | ||
</StackPanel> | ||
</StackPanel> | ||
|
||
<!-- Variables --> | ||
<StackPanel VerticalAlignment="Top" HorizontalAlignment="Stretch" | ||
Spacing="10" IsVisible="{Binding HasAnyVariables}"> | ||
<TextBlock FontSize="18" TextAlignment="Left" HorizontalAlignment="Center" FontWeight="DemiBold" Text="{Binding VariableTitle}"/> | ||
<StackPanel Orientation="Vertical" HorizontalAlignment="Stretch" Margin="5 8"> | ||
<ItemsControl ItemsSource="{Binding UniqueVariables}"> | ||
<ItemsControl.ItemTemplate> | ||
<DataTemplate> | ||
<Grid ColumnDefinitions="*,Auto" Margin="0 2"> | ||
<TextBlock TextAlignment="Left" FontStyle="{Binding Style}" Text="{Binding Name}"/> | ||
<Button Height="28" Width="28" Grid.Column="1" Command="{Binding Rename}" Padding="0" ToolTip.Tip="Rename"> | ||
<ui:SymbolIcon Symbol="Rename" FontSize="20" Margin="0"></ui:SymbolIcon> | ||
</Button> | ||
</Grid> | ||
</DataTemplate> | ||
</ItemsControl.ItemTemplate> | ||
</ItemsControl> | ||
</StackPanel> | ||
</StackPanel> | ||
|
||
<!-- Options Reference --> | ||
<StackPanel VerticalAlignment="Top" HorizontalAlignment="Stretch" | ||
Spacing="10" IsVisible="{Binding HasAnyOptionReferences}"> | ||
<TextBlock FontSize="18" TextAlignment="Left" HorizontalAlignment="Center" FontWeight="DemiBold" Text="{Binding OptionReferenceTitle}"/> | ||
<StackPanel Orientation="Vertical" HorizontalAlignment="Stretch" Margin="5 8"> | ||
<ItemsControl ItemsSource="{Binding UniqueOptionReferences}"> | ||
<ItemsControl.ItemTemplate> | ||
<DataTemplate> | ||
<Grid ColumnDefinitions="*,Auto,Auto" Margin="0 2"> | ||
<TextBlock TextAlignment="Left" VerticalAlignment="Center" HorizontalAlignment="Left" Text="{Binding Name}"/> | ||
<Button Height="28" Width="28" Grid.Column="1" Command="{Binding NavigateToDefinition}" Padding="0" ToolTip.Tip="Navigate to definition"> | ||
<ui:SymbolIcon Symbol="Go" FontSize="20" Margin="0"></ui:SymbolIcon> | ||
</Button> | ||
<Button Height="28" Width="28" Grid.Column="2" Command="{Binding Rename}" Padding="0" Margin="4 0 0 0" ToolTip.Tip="Rename"> | ||
<ui:SymbolIcon Symbol="Rename" FontSize="20" Margin="0"></ui:SymbolIcon> | ||
</Button> | ||
</Grid> | ||
</DataTemplate> | ||
</ItemsControl.ItemTemplate> | ||
</ItemsControl> | ||
</StackPanel> | ||
</StackPanel> | ||
|
||
<!-- Options Definition --> | ||
<StackPanel VerticalAlignment="Top" HorizontalAlignment="Stretch" | ||
Spacing="10" IsVisible="{Binding HasOptionDefinition}"> | ||
<TextBlock FontSize="18" TextAlignment="Left" HorizontalAlignment="Center" FontWeight="DemiBold" Text="{Binding OptionTitle}"/> | ||
<StackPanel Orientation="Vertical" HorizontalAlignment="Stretch" Margin="5 8"> | ||
<ItemsControl ItemsSource="{Binding Options}"> | ||
<ItemsControl.ItemTemplate> | ||
<DataTemplate> | ||
<Grid ColumnDefinitions="*,Auto" Margin="0 2"> | ||
<TextBlock TextAlignment="Left" VerticalAlignment="Center" HorizontalAlignment="Left" Text="{Binding Name}"/> | ||
<Button Height="28" Width="28" Grid.Column="2" Command="{Binding Rename}" Padding="0" ToolTip.Tip="Rename"> | ||
<ui:SymbolIcon Symbol="Rename" FontSize="20" Margin="0"></ui:SymbolIcon> | ||
</Button> | ||
</Grid> | ||
</DataTemplate> | ||
</ItemsControl.ItemTemplate> | ||
</ItemsControl> | ||
</StackPanel> | ||
</StackPanel> | ||
|
||
</StackPanel > | ||
</Expander> | ||
</DataTemplate> | ||
</ItemsRepeater.ItemTemplate> | ||
</ItemsRepeater> | ||
</ScrollViewer> | ||
<StackPanel Margin="10" Name="CannotParseInfo" Grid.Row="3" VerticalAlignment="Center" HorizontalAlignment="Stretch" IsVisible="False"> | ||
<TextBlock Name="CannotParseInfoText" TextAlignment="Center" TextWrapping="Wrap"></TextBlock> | ||
</StackPanel> | ||
<StackPanel Grid.Row="4" HorizontalAlignment="Stretch" Margin="10"> | ||
<Button Name="ParseButton" Classes="accent" HorizontalAlignment="Center">Parse Code</Button> | ||
</StackPanel> | ||
</Grid> | ||
</Border> | ||
</UserControl> |
109 changes: 109 additions & 0 deletions
109
SkEditor/Controls/Sidebar/CodeParser/ParserSidebarPanel.axaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using Avalonia.Controls; | ||
using FluentAvalonia.UI.Controls; | ||
using SkEditor.API; | ||
using SkEditor.Utilities; | ||
using SkEditor.Utilities.Files; | ||
using SkEditor.Utilities.Parser; | ||
|
||
namespace SkEditor.Controls.Sidebar; | ||
|
||
public partial class ParserSidebarPanel : UserControl | ||
{ | ||
public bool CodeParserEnabled => ApiVault.Get().GetAppConfig().EnableCodeParser; | ||
public ObservableCollection<CodeSection> Sections { get; set; } = new (); | ||
|
||
public void Refresh(List<CodeSection> sections) | ||
{ | ||
Sections.Clear(); | ||
sections.ForEach(section => Sections.Add(section)); | ||
ItemsRepeater.ItemsSource = Sections; | ||
UpdateInformationBox(); | ||
} | ||
|
||
public ParserSidebarPanel() | ||
{ | ||
InitializeComponent(); | ||
|
||
ParserDisabled.IsVisible = !CodeParserEnabled; | ||
ScrollViewer.IsVisible = CodeParserEnabled; | ||
ParseButton.IsEnabled = CodeParserEnabled; | ||
|
||
ParseButton.Click += (_, _) => ParseCurrentFile(); | ||
EnableParser.Click += async (_, _) => | ||
{ | ||
var response = await ApiVault.Get().ShowMessageWithIcon("Enable Code Parser?", "The code parser let you navigate easily in your code and rename variables, options, and more.\n\nKeep in mind it is still in beta and may BREAK your scripts, so make sure to make a backup before that.", | ||
new SymbolIconSource() { Symbol = Symbol.Alert }); | ||
if (response == ContentDialogResult.Primary) | ||
{ | ||
ApiVault.Get().GetAppConfig().EnableCodeParser = true; | ||
ApiVault.Get().GetAppConfig().Save(); | ||
ParserDisabled.IsVisible = false; | ||
ScrollViewer.IsVisible = true; | ||
ParseButton.IsEnabled = true; | ||
} | ||
}; | ||
} | ||
|
||
public void ParseCurrentFile() | ||
{ | ||
var selectedItem = ApiVault.Get().GetTabView().SelectedItem as TabViewItem; | ||
if (selectedItem == null) | ||
return; | ||
var parser = FileHandler.OpenedFiles.Find(file => file.TabViewItem == selectedItem)?.Parser; | ||
if (parser == null) | ||
return; | ||
|
||
ParseButton.IsEnabled = false; | ||
parser.Parse(); | ||
} | ||
|
||
public void UpdateInformationBox(bool isToNotifyUnParsing = false) | ||
{ | ||
if (!CodeParserEnabled) | ||
{ | ||
Sections.Clear(); | ||
CannotParseInfoText.IsVisible = false; | ||
return; | ||
} | ||
|
||
if (isToNotifyUnParsing) | ||
{ | ||
Sections.Clear(); | ||
CannotParseInfo.IsVisible = true; | ||
CannotParseInfoText.Text = "File changed, you need to parse it again."; | ||
return; | ||
} | ||
|
||
if (Sections.Count == 0) | ||
{ | ||
CannotParseInfo.IsVisible = true; | ||
CannotParseInfoText.Text = "No sections found in the code. Maybe you should write something? :)"; | ||
return; | ||
} | ||
|
||
var parser = Sections[0].Parser; | ||
if (!parser.IsValid()) | ||
{ | ||
Sections.Clear(); | ||
CannotParseInfo.IsVisible = true; | ||
CannotParseInfoText.Text = "This file cannot be parsed, as it do not look likes a script file."; | ||
return; | ||
} | ||
|
||
CannotParseInfo.IsVisible = false; | ||
} | ||
|
||
public class ParserPanel : SidebarPanel | ||
{ | ||
public override UserControl Content => Panel; | ||
public override IconSource Icon => new SymbolIconSource() { Symbol = Symbol.Code }; | ||
public override bool IsDisabled => false; | ||
|
||
public readonly ParserSidebarPanel Panel = new (); | ||
|
||
public override int DesiredWidth { get; } = 350; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.