Skip to content

Commit

Permalink
Merge pull request #25 from ItsTheSky/features/config-indentation
Browse files Browse the repository at this point in the history
Configure the desired auto-indentation
  • Loading branch information
ItsTheSky committed Jan 24, 2024
2 parents 6da2221 + a15cbc6 commit 733541c
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 1 deletion.
3 changes: 3 additions & 0 deletions SkEditor/API/ISkEditorAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using SkEditor.Utilities;
using SkEditor.Views;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace SkEditor.API;
Expand Down Expand Up @@ -42,6 +43,8 @@ public interface ISkEditorAPI
public bool IsAddonEnabled(string addonName);

public void SaveData();

public List<TextEditor> GetOpenedEditors();


#region Events
Expand Down
4 changes: 4 additions & 0 deletions SkEditor/Languages/English.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@
<system:String x:Key="SettingsGeneralTitle">General</system:String>
<system:String x:Key="SettingsGeneralLanguage">Language</system:String>
<system:String x:Key="SettingsGeneralWrapping">Wrapping</system:String>
<system:String x:Key="SettingsGeneralTabType">Indentation Type</system:String>
<system:String x:Key="SettingsGeneralTabTypeDescription">Choose between tabs and spaces, and set the size of the tab.</system:String>
<system:String x:Key="SettingsGeneralTabTypeTabs">Tabs</system:String>
<system:String x:Key="SettingsGeneralTabTypeSpaces">Spaces</system:String>
<system:String x:Key="SettingsGeneralAutoIndent">Auto-indent</system:String>
<system:String x:Key="SettingsGeneralAutoIndentDescription">Automatically inserts a tab in a new line if the previous line ends with a colon.</system:String>
<system:String x:Key="SettingsGeneralAutoPairing">Auto-pairing</system:String>
Expand Down
8 changes: 8 additions & 0 deletions SkEditor/SkEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,14 @@ public void SaveData()
GetAppConfig().Save();
}

public List<TextEditor> GetOpenedEditors()
{
return GetTabView().TabItems
.OfType<TabViewItem>()
.Select(x => x.Content as TextEditor)
.Where(editor => editor != null)
.ToList();
}


#region Events
Expand Down
3 changes: 3 additions & 0 deletions SkEditor/Utilities/AppConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public class AppConfig
public Dictionary<string, object> CustomOptions { get; set; } = [];

public bool EnableAutoCompletionExperiment { get; set; } = false;

public bool UseSpacesInsteadOfTabs { get; set; } = false;
public int TabSize { get; set; } = 4;
public bool EnableProjectsExperiment { get; set; } = false;
public bool EnableHexPreview { get; set; } = false;

Expand Down
2 changes: 1 addition & 1 deletion SkEditor/Utilities/Editor/TextEditorEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public static void DoAutoIndent(object? sender, TextInputEventArgs e)

if (!previousLineText.EndsWith(':')) return;

textEditor.Document.Insert(line.Offset, "\t");
textEditor.Document.Insert(line.Offset, textEditor.Options.IndentationString);
}

public static void DoAutoPairing(object? sender, TextInputEventArgs e)
Expand Down
3 changes: 3 additions & 0 deletions SkEditor/Utilities/Files/FileBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ private static TextEditor SetOptions(TextEditor editor)

editor.Options.AllowScrollBelowDocument = true;
editor.Options.CutCopyWholeLine = true;

editor.Options.ConvertTabsToSpaces = ApiVault.Get().GetAppConfig().UseSpacesInsteadOfTabs;
editor.Options.IndentationSize = ApiVault.Get().GetAppConfig().TabSize;

return editor;
}
Expand Down
18 changes: 18 additions & 0 deletions SkEditor/Views/Settings/GeneralPage.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@
<ToggleSwitch Name="WrappingToggleSwitch" IsChecked="{Binding IsWrappingEnabled}"/>
</ui:SettingsExpander.Footer>
</ui:SettingsExpander>

<ui:SettingsExpander Header="{DynamicResource SettingsGeneralTabType}" IconSource="{StaticResource FunctionIcon}"
Description="{DynamicResource SettingsGeneralTabTypeDescription}">
<ui:SettingsExpander.Footer>
<StackPanel Orientation="Horizontal" Spacing="10">
<ComboBox Name="IndentationTypeComboBox">
<ComboBoxItem Tag="tabs" Content="{DynamicResource SettingsGeneralTabTypeTabs}"></ComboBoxItem>
<ComboBoxItem Tag="spaces" Content="{DynamicResource SettingsGeneralTabTypeSpaces}"></ComboBoxItem>
</ComboBox>
<ComboBox Name="IndentationAmountComboBox">
<ComboBoxItem Tag="1">1</ComboBoxItem>
<ComboBoxItem Tag="2">2</ComboBoxItem>
<ComboBoxItem Tag="3">3</ComboBoxItem>
<ComboBoxItem Tag="4">4</ComboBoxItem>
</ComboBox>
</StackPanel>
</ui:SettingsExpander.Footer>
</ui:SettingsExpander>

<ui:SettingsExpander Header="{DynamicResource SettingsGeneralAutoIndent}" IconSource="{StaticResource AutoIndentIcon}"
Description="{DynamicResource SettingsGeneralAutoIndentDescription}">
Expand Down
38 changes: 38 additions & 0 deletions SkEditor/Views/Settings/GeneralPage.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public GeneralPage()

AssignCommands();
LoadLanguages();
LoadIndentation();
}

private void LoadLanguages()
Expand All @@ -38,6 +39,31 @@ private void LoadLanguages()
Dispatcher.UIThread.InvokeAsync(() => Translation.ChangeLanguage(language));
};
}

private void LoadIndentation()
{
var appConfig = ApiVault.Get().GetAppConfig();
var tag = appConfig.UseSpacesInsteadOfTabs ? "spaces" : "tabs";
var amount = appConfig.TabSize;

foreach (var item in IndentationTypeComboBox.Items)
{
if ((item as ComboBoxItem).Tag.ToString() == tag)
{
IndentationTypeComboBox.SelectedItem = item;
break;
}
}

foreach (var item in IndentationAmountComboBox.Items)
{
if ((item as ComboBoxItem).Tag.ToString() == amount.ToString())
{
IndentationAmountComboBox.SelectedItem = item;
break;
}
}
}

private void AssignCommands()
{
Expand All @@ -49,6 +75,18 @@ private void AssignCommands()
AutoSaveToggleSwitch.Command = new RelayCommand(() => ToggleSetting("IsAutoSaveEnabled"));
CheckForUpdatesToggleSwitch.Command = new RelayCommand(() => ToggleSetting("CheckForUpdates"));
CheckForChangesToggleSwitch.Command = new RelayCommand(() => ToggleSetting("CheckForChanges"));
IndentationAmountComboBox.SelectionChanged += (s, e) =>
{
var appConfig = ApiVault.Get().GetAppConfig();
appConfig.TabSize = int.Parse((IndentationAmountComboBox.SelectedItem as ComboBoxItem).Tag.ToString());
ApiVault.Get().GetOpenedEditors().ForEach(e => e.Options.IndentationSize = appConfig.TabSize);
};
IndentationTypeComboBox.SelectionChanged += (s, e) =>
{
var appConfig = ApiVault.Get().GetAppConfig();
appConfig.UseSpacesInsteadOfTabs = (IndentationTypeComboBox.SelectedItem as ComboBoxItem).Tag.ToString() == "spaces";
ApiVault.Get().GetOpenedEditors().ForEach(e => e.Options.ConvertTabsToSpaces = appConfig.UseSpacesInsteadOfTabs);
};
}

private void ToggleRpc()
Expand Down

0 comments on commit 733541c

Please sign in to comment.