Skip to content

Commit

Permalink
add property element auto-complete
Browse files Browse the repository at this point in the history
  • Loading branch information
bluepilledgreat committed Oct 21, 2024
1 parent ed47008 commit d924349
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 11 deletions.
16 changes: 13 additions & 3 deletions Bloxstrap/Resources/CustomBootstrapperSchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"Width": "double",
"HorizontalAlignment": "HorizontalAlignment",
"VerticalAlignment": "VerticalAlignment",
"ZIndex": "int"
"ZIndex": "int",
"RenderTransform": "Transform"
}
},
"Control": {
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -252,6 +254,14 @@
"MaxHeight"
]
},
"TextAlignment": {
"Values": [
"Left",
"Right",
"Center",
"Justify"
]
},
"TextTrimming": {
"Values": [
"None",
Expand Down
88 changes: 80 additions & 8 deletions Bloxstrap/UI/Elements/Editor/BootstrapperEditorWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ private class Element

public class Type
{
public bool CanHaveElement { get; set; } = false;
public List<string>? Values { get; set; } = null;
}

Expand All @@ -43,6 +44,11 @@ public class Type
/// </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>
Expand All @@ -63,23 +69,42 @@ public static void ParseSchema()
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()
Expand All @@ -88,7 +113,7 @@ private static void PopulateElementInfo()

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);
Expand Down Expand Up @@ -137,6 +162,9 @@ private void OnTextAreaTextEntered(object sender, TextCompositionEventArgs e)
case " ":
OpenAttributeAutoComplete();
break;
case ".":
OpenPropertyElementAutoComplete();
break;
case "/":
AddEndTag();
break;
Expand Down Expand Up @@ -233,6 +261,28 @@ private void OnTextAreaTextEntered(object sender, TextCompositionEventArgs e)
}
}

/// <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
Expand Down Expand Up @@ -331,9 +381,6 @@ private void OpenAttributeAutoComplete()

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;
Expand All @@ -346,6 +393,31 @@ private void OpenTypeValueAutoComplete(string typeName)
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)
Expand Down

0 comments on commit d924349

Please sign in to comment.