From d924349a79292f239ecc28d1b3035473915b0193 Mon Sep 17 00:00:00 2001 From: bluepilledgreat <97983689+bluepilledgreat@users.noreply.github.com> Date: Mon, 21 Oct 2024 20:29:31 +0100 Subject: [PATCH] add property element auto-complete --- .../Resources/CustomBootstrapperSchema.json | 16 +++- .../Editor/BootstrapperEditorWindow.xaml.cs | 88 +++++++++++++++++-- 2 files changed, 93 insertions(+), 11 deletions(-) diff --git a/Bloxstrap/Resources/CustomBootstrapperSchema.json b/Bloxstrap/Resources/CustomBootstrapperSchema.json index b3206cb..5bd2b58 100644 --- a/Bloxstrap/Resources/CustomBootstrapperSchema.json +++ b/Bloxstrap/Resources/CustomBootstrapperSchema.json @@ -11,7 +11,8 @@ "Width": "double", "HorizontalAlignment": "HorizontalAlignment", "VerticalAlignment": "VerticalAlignment", - "ZIndex": "int" + "ZIndex": "int", + "RenderTransform": "Transform" } }, "Control": { @@ -183,13 +184,14 @@ }, "int": {}, "double": {}, - "object": {}, + "object": { "CanHaveElement": true }, "Thickness": {}, "Rect": {}, "Point": {}, - "Brush": {}, + "Brush": { "CanHaveElement": true }, "Color": {}, "ImageSource": {}, + "Transform": { "CanHaveElement": true }, "Visibility": { "Values": [ "Visible", @@ -252,6 +254,14 @@ "MaxHeight" ] }, + "TextAlignment": { + "Values": [ + "Left", + "Right", + "Center", + "Justify" + ] + }, "TextTrimming": { "Values": [ "None", diff --git a/Bloxstrap/UI/Elements/Editor/BootstrapperEditorWindow.xaml.cs b/Bloxstrap/UI/Elements/Editor/BootstrapperEditorWindow.xaml.cs index 573b41a..a35dc8f 100644 --- a/Bloxstrap/UI/Elements/Editor/BootstrapperEditorWindow.xaml.cs +++ b/Bloxstrap/UI/Elements/Editor/BootstrapperEditorWindow.xaml.cs @@ -33,6 +33,7 @@ namespace Bloxstrap.UI.Elements.Editor public class Type { + public bool CanHaveElement { get; set; } = false; public List? Values { get; set; } = null; } @@ -43,6 +44,11 @@ namespace Bloxstrap.UI.Elements.Editor /// public static SortedDictionary> ElementInfo { get; set; } = new(); + /// + /// Attributes of elements that can have property elements + /// + public static Dictionary> PropertyElements { get; set; } = new(); + /// /// All type info /// @@ -63,23 +69,42 @@ namespace Bloxstrap.UI.Elements.Editor PopulateElementInfo(); } - private static SortedDictionary GetElementAttributes(string name, Element element) + private static (SortedDictionary, List) GetElementAttributes(string name, Element element) { if (ElementInfo.ContainsKey(name)) - return ElementInfo[name]; + return (ElementInfo[name], PropertyElements[name]); + List properties = new List(); SortedDictionary attributes = new(); foreach (var attribute in element.Attributes) + { attributes.Add(attribute.Key, attribute.Value); + if (!Types.ContainsKey(attribute.Value)) + throw new Exception($"Schema for type {attribute.Value} is missing. Blame Matt!"); + + Type type = Types[attribute.Value]; + if (type.CanHaveElement) + properties.Add(attribute.Key); + } + if (element.SuperClass != null) { - foreach (var attribute in GetElementAttributes(element.SuperClass, _schema!.Elements[element.SuperClass])) + (SortedDictionary superAttributes, List superProperties) = GetElementAttributes(element.SuperClass, _schema!.Elements[element.SuperClass]); + foreach (var attribute in superAttributes) attributes.Add(attribute.Key, attribute.Value); + + foreach (var property in superProperties) + properties.Add(property); } - return attributes; + properties.Sort(); + + ElementInfo[name] = attributes; + PropertyElements[name] = properties; + + return (attributes, properties); } private static void PopulateElementInfo() @@ -88,7 +113,7 @@ namespace Bloxstrap.UI.Elements.Editor foreach (var element in _schema!.Elements) { - ElementInfo[element.Key] = GetElementAttributes(element.Key, element.Value); + GetElementAttributes(element.Key, element.Value); if (!element.Value.IsCreatable) toRemove.Add(element.Key); @@ -137,6 +162,9 @@ namespace Bloxstrap.UI.Elements.Editor case " ": OpenAttributeAutoComplete(); break; + case ".": + OpenPropertyElementAutoComplete(); + break; case "/": AddEndTag(); break; @@ -233,6 +261,28 @@ namespace Bloxstrap.UI.Elements.Editor } } + /// + /// A space between the cursor and the element will completely cancel this function + /// + private string? GetElementAtCursorNoSpaces(string xml, int offset) + { + (string line, int pos) = GetLineAndPosAtCaretPosition(); + + string curr = ""; + while (pos != -1) + { + char c = line[pos]; + if (c == ' ' || c == '\t') + return null; + if (c == '<') + return curr; + curr = c + curr; + pos--; + } + + return null; + } + /// /// Returns null if not eligible to auto complete there. /// Returns the name of the element to show the attributes for @@ -331,9 +381,6 @@ namespace Bloxstrap.UI.Elements.Editor private void OpenTypeValueAutoComplete(string typeName) { - if (!CustomBootstrapperSchema.Types.ContainsKey(typeName)) - throw new Exception($"Schema for type {typeName} is missing. Blame Matt!"); - var typeValues = CustomBootstrapperSchema.Types[typeName].Values; if (typeValues == null) return; @@ -346,6 +393,31 @@ namespace Bloxstrap.UI.Elements.Editor ShowCompletionWindow(data); } + private void OpenPropertyElementAutoComplete() + { + string? element = GetElementAtCursorNoSpaces(UIXML.Text, UIXML.CaretOffset); + if (element == null) + { + CloseCompletionWindow(); + return; + } + + if (!CustomBootstrapperSchema.PropertyElements.ContainsKey(element)) + { + CloseCompletionWindow(); + return; + } + + var properties = CustomBootstrapperSchema.PropertyElements[element]; + + var data = new List(); + + foreach (var property in properties) + data.Add(new TypeValueCompletionData(property)); + + ShowCompletionWindow(data); + } + private void CloseCompletionWindow() { if (_completionWindow != null)