Skip to content

Commit

Permalink
Merge pull request #26 from ItsTheSky/features/code-parsing
Browse files Browse the repository at this point in the history
Code parsing & basic refactoring for options and variables
  • Loading branch information
ItsTheSky authored Feb 5, 2024
2 parents 7562dd1 + c52352f commit af69352
Show file tree
Hide file tree
Showing 22 changed files with 1,194 additions and 5 deletions.
3 changes: 3 additions & 0 deletions SkEditor/API/ISkEditorAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using SkEditor.Utilities.Files;

namespace SkEditor.API;

Expand All @@ -23,6 +24,8 @@ public interface ISkEditorAPI

public TextEditor? GetTextEditor();

public OpenedFile? GetOpenedFile();

public TabView GetTabView();

public void OpenUrl(string url);
Expand Down
31 changes: 28 additions & 3 deletions SkEditor/Controls/SideBarControl.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Avalonia.Animation;
using Avalonia.Controls;
using Avalonia.Media;
using CommunityToolkit.Mvvm.Input;
Expand All @@ -12,6 +15,7 @@ public partial class SideBarControl : UserControl
private static readonly List<SidebarPanel> Panels = new();

public readonly ExplorerSidebarPanel.ExplorerPanel ProjectPanel = new();
public readonly ParserSidebarPanel.ParserPanel ParserPanel = new();

public static void RegisterPanel(SidebarPanel panel)
{
Expand All @@ -24,21 +28,38 @@ public SideBarControl()
InitializeComponent();

RegisterPanel(ProjectPanel);
RegisterPanel(ParserPanel);
}

public void LoadPanels()
public static long TransitionDuration = 100L;
public async void LoadPanels()

Check warning on line 35 in SkEditor/Controls/SideBarControl.axaml.cs

View workflow job for this annotation

GitHub Actions / build-macos

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
foreach (SidebarPanel panel in Panels)
{
var btn = CreatePanelButton(panel);
var content = panel.Content;
content.Width = 0;
content.Opacity = 0;

content.Transitions = new Transitions() {
new DoubleTransition()
{
Property = WidthProperty,
Duration = TimeSpan.FromMilliseconds(TransitionDuration)
},
new DoubleTransition()
{
Property = OpacityProperty,
Duration = TimeSpan.FromMilliseconds(150)
}
};

btn.Command = new RelayCommand(() =>
btn.Command = new RelayCommand(async () =>
{
if (_currentPanel == panel)
{
_currentPanel.Content.Width = 0; // Close current panel
_currentPanel.Content.Opacity = 0;
_currentPanel.OnClose();
_currentPanel = null;
Expand All @@ -53,11 +74,15 @@ public void LoadPanels()
{
_currentPanel.OnClose();
_currentPanel.Content.Width = 0; // Close current panel
_currentPanel.Content.Opacity = 0;
_currentPanel = null;
await Task.Delay((int) TransitionDuration);
}
_currentPanel = panel;
_currentPanel.Content.Width = 250;
_currentPanel.Content.Width = _currentPanel.DesiredWidth;
_currentPanel.Content.Opacity = 1;
_currentPanel.OnOpen();
});

Expand Down
152 changes: 152 additions & 0 deletions SkEditor/Controls/Sidebar/CodeParser/ParserSidebarPanel.axaml
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 SkEditor/Controls/Sidebar/CodeParser/ParserSidebarPanel.axaml.cs
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;
}
}
1 change: 1 addition & 0 deletions SkEditor/Languages/English.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
<system:String x:Key="RefactorWindowRemoveComments">Remove comments</system:String>
<system:String x:Key="RefactorWindowTabsToSpaces">Convert tabs to spaces</system:String>
<system:String x:Key="RefactorWindowSpacesToTabs">Convert spaces to tabs</system:String>
<system:String x:Key="RefactorWindowRefactorBoxName">Renaming '{0}' into ...</system:String>

<!-- File Association Selection Window -->
<system:String x:Key="FileAssociationSelectionWindowTitle">Select file association</system:String>
Expand Down
Loading

0 comments on commit af69352

Please sign in to comment.