Browse Source

Add prepare text function support, replace underscores setting

pull/165/head
Ionite 1 year ago
parent
commit
e6604a8f6f
No known key found for this signature in database
  1. 20
      StabilityMatrix.Avalonia/Controls/CodeCompletion/CompletionData.cs
  2. 14
      StabilityMatrix.Avalonia/Controls/CodeCompletion/CompletionWindow.axaml.cs
  3. 5
      StabilityMatrix.Avalonia/Controls/CodeCompletion/ICompletionData.cs
  4. 3
      StabilityMatrix.Avalonia/DesignData/MockCompletionProvider.cs
  5. 11
      StabilityMatrix.Avalonia/Models/TagCompletion/CompletionProvider.cs
  6. 8
      StabilityMatrix.Avalonia/Models/TagCompletion/ICompletionProvider.cs
  7. 8
      StabilityMatrix.Avalonia/ViewModels/SettingsViewModel.cs
  8. 11
      StabilityMatrix.Avalonia/Views/SettingsPage.axaml
  9. 5
      StabilityMatrix.Core/Models/Settings/Settings.cs

20
StabilityMatrix.Avalonia/Controls/CodeCompletion/CompletionData.cs

@ -62,9 +62,25 @@ public class CompletionData : ICompletionData
}
/// <inheritdoc />
public void Complete(TextArea textArea, ISegment completionSegment, EventArgs insertionRequestEventArgs)
public void Complete(TextArea textArea, ISegment completionSegment, InsertionRequestEventArgs eventArgs, Func<string, string>? prepareText = null)
{
textArea.Document.Replace(completionSegment, Text);
var text = Text;
if (prepareText is not null)
{
text = prepareText(text);
}
// Replace text
textArea.Document.Replace(completionSegment, text);
// Append text if requested
if (eventArgs.AppendText is { } appendText)
{
var end = completionSegment.Offset + text.Length;
textArea.Document.Insert(end, appendText);
textArea.Caret.Offset = end + appendText.Length;
}
}
/// <inheritdoc />

14
StabilityMatrix.Avalonia/Controls/CodeCompletion/CompletionWindow.axaml.cs

@ -162,15 +162,11 @@ public class CompletionWindow : CompletionWindowBase
// The window must close before Complete() is called.
// If the Complete callback pushes stacked input handlers, we don't want to pop those when the CC window closes.
var length = EndOffset - StartOffset;
e.Item.Complete(TextArea, new AnchorSegment(TextArea.Document, StartOffset, length), e);
// Append text if requested
if (e.AppendText is { } appendText)
{
var end = StartOffset + e.Item.Text.Length;
TextArea.Document.Insert(end, appendText);
TextArea.Caret.Offset = end + appendText.Length;
}
e.Item.Complete(
TextArea,
new AnchorSegment(TextArea.Document, StartOffset, length),
e,
completionProvider.PrepareInsertionText);
}
private void CompletionList_CloseRequested(object? sender, EventArgs e)

5
StabilityMatrix.Avalonia/Controls/CodeCompletion/ICompletionData.cs

@ -72,10 +72,11 @@ public interface ICompletionData
/// <param name="textArea">The text area on which completion is performed.</param>
/// <param name="completionSegment">The text segment that was used by the completion window if
/// the user types (segment between CompletionWindow.StartOffset and CompletionWindow.EndOffset).</param>
/// <param name="insertionRequestEventArgs">The EventArgs used for the insertion request.
/// <param name="eventArgs">The EventArgs used for the insertion request.
/// These can be TextCompositionEventArgs, KeyEventArgs, MouseEventArgs, depending on how
/// the insertion was triggered.</param>
void Complete(TextArea textArea, ISegment completionSegment, EventArgs insertionRequestEventArgs);
/// <param name="prepareText">Optional function to transform the text to be inserted</param>
void Complete(TextArea textArea, ISegment completionSegment, InsertionRequestEventArgs eventArgs, Func<string, string>? prepareText = null);
/// <summary>
/// Update the text character highlighting

3
StabilityMatrix.Avalonia/DesignData/MockCompletionProvider.cs

@ -12,6 +12,9 @@ public class MockCompletionProvider : ICompletionProvider
/// <inheritdoc />
public bool IsLoaded => false;
/// <inheritdoc />
public Func<string, string>? PrepareInsertionText => null;
/// <inheritdoc />
public Task LoadFromFile(FilePath path, bool recreate = false)
{

11
StabilityMatrix.Avalonia/Models/TagCompletion/CompletionProvider.cs

@ -27,6 +27,7 @@ public class CompletionProvider : ICompletionProvider
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private readonly ISettingsManager settingsManager;
private readonly INotificationService notificationService;
private readonly AsyncLock loadLock = new();
@ -36,8 +37,13 @@ public class CompletionProvider : ICompletionProvider
public bool IsLoaded => searcher is not null;
public Func<string, string>? PrepareInsertionText
=> settingsManager.Settings.IsCompletionRemoveUnderscoresEnabled
? PrepareInsertionText_RemoveUnderscores : null;
public CompletionProvider(ISettingsManager settingsManager, INotificationService notificationService)
{
this.settingsManager = settingsManager;
this.notificationService = notificationService;
// Attach to load from set file on initial settings load
@ -69,6 +75,11 @@ public class CompletionProvider : ICompletionProvider
BackgroundLoadFromFile(fullPath);
}
}
private static string PrepareInsertionText_RemoveUnderscores(string text)
{
return text.Replace("_", " ");
}
/// <inheritdoc />
public void BackgroundLoadFromFile(FilePath path, bool recreate = false)

8
StabilityMatrix.Avalonia/Models/TagCompletion/ICompletionProvider.cs

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using StabilityMatrix.Avalonia.Controls.CodeCompletion;
using StabilityMatrix.Core.Models.FileInterfaces;
@ -11,6 +12,11 @@ public interface ICompletionProvider
/// Whether the completion provider is loaded.
/// </summary>
bool IsLoaded { get; }
/// <summary>
/// Optional function to transform the text to be inserted
/// </summary>
Func<string, string>? PrepareInsertionText => null;
/// <summary>
/// Load the completion provider from a file.

8
StabilityMatrix.Avalonia/ViewModels/SettingsViewModel.cs

@ -98,9 +98,10 @@ public partial class SettingsViewModel : PageViewModelBase
[ObservableProperty] private bool isPromptCompletionEnabled;
[ObservableProperty]
private IReadOnlyList<string> availableTagCompletionCsvs = Array.Empty<string>();
[ObservableProperty]
private string? selectedTagCompletionCsv;
[ObservableProperty]
private bool isCompletionRemoveUnderscoresEnabled;
// Integrations section
[ObservableProperty] private bool isDiscordRichPresenceEnabled;
@ -160,6 +161,11 @@ public partial class SettingsViewModel : PageViewModelBase
settings => settings.IsPromptCompletionEnabled,
true);
settingsManager.RelayPropertyFor(this,
vm => vm.IsCompletionRemoveUnderscoresEnabled,
settings => settings.IsCompletionRemoveUnderscoresEnabled,
true);
DebugThrowAsyncExceptionCommand.WithNotificationErrorHandler(notificationService, LogLevel.Warn);
ImportTagCsvCommand.WithNotificationErrorHandler(notificationService, LogLevel.Warn);
}

11
StabilityMatrix.Avalonia/Views/SettingsPage.axaml

@ -149,6 +149,17 @@
Content="Import"/>
</ui:SettingsExpanderItem.Footer>
</ui:SettingsExpanderItem>
<!-- Remove underscores -->
<ui:SettingsExpanderItem
Content="Replace underscores with spaces when inserting completions"
IconSource="Underline"
IsEnabled="{Binding IsPromptCompletionEnabled}">
<ui:SettingsExpanderItem.Footer>
<CheckBox Margin="8"
IsChecked="{Binding IsCompletionRemoveUnderscoresEnabled}"/>
</ui:SettingsExpanderItem.Footer>
</ui:SettingsExpanderItem>
</ui:SettingsExpander>
</Grid>

5
StabilityMatrix.Core/Models/Settings/Settings.cs

@ -54,6 +54,11 @@ public class Settings
/// </summary>
public string? TagCompletionCsv { get; set; }
/// <summary>
/// Whether to remove underscores from completions
/// </summary>
public bool IsCompletionRemoveUnderscoresEnabled { get; set; }
public bool RemoveFolderLinksOnShutdown { get; set; }
public bool IsDiscordRichPresenceEnabled { get; set; }

Loading…
Cancel
Save