add property element auto-complete

This commit is contained in:
bluepilledgreat 2024-10-21 20:29:31 +01:00
parent ed4700869d
commit d924349a79
2 changed files with 93 additions and 11 deletions

View File

@ -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",

View File

@ -33,6 +33,7 @@ namespace Bloxstrap.UI.Elements.Editor
public class Type
{
public bool CanHaveElement { get; set; } = false;
public List<string>? Values { get; set; } = null;
}
@ -43,6 +44,11 @@ namespace Bloxstrap.UI.Elements.Editor
/// </summary>
public static SortedDictionary<string, SortedDictionary<string, string>> ElementInfo { get; set; } = new();
/// <summary>
/// Attributes of elements that can have property elements
/// </summary>
public static Dictionary<string, List<string>> PropertyElements { get; set; } = new();
/// <summary>
/// All type info
/// </summary>
@ -63,23 +69,42 @@ namespace Bloxstrap.UI.Elements.Editor
PopulateElementInfo();
}
private static SortedDictionary<string, string> GetElementAttributes(string name, Element element)
private static (SortedDictionary<string, string>, List<string>) GetElementAttributes(string name, Element element)
{
if (ElementInfo.ContainsKey(name))
return ElementInfo[name];
return (ElementInfo[name], PropertyElements[name]);
List<string> properties = new List<string>();
SortedDictionary<string, string> 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<string, string> superAttributes, List<string> 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
}
}
/// <summary>
/// A space between the cursor and the element will completely cancel this function
/// </summary>
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;
}
/// <summary>
/// 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<ICompletionData>();
foreach (var property in properties)
data.Add(new TypeValueCompletionData(property));
ShowCompletionWindow(data);
}
private void CloseCompletionWindow()
{
if (_completionWindow != null)