mirror of
https://github.com/bloxstraplabs/bloxstrap.git
synced 2025-04-21 10:01:27 -07:00
add property element auto-complete
This commit is contained in:
parent
ed4700869d
commit
d924349a79
@ -11,7 +11,8 @@
|
|||||||
"Width": "double",
|
"Width": "double",
|
||||||
"HorizontalAlignment": "HorizontalAlignment",
|
"HorizontalAlignment": "HorizontalAlignment",
|
||||||
"VerticalAlignment": "VerticalAlignment",
|
"VerticalAlignment": "VerticalAlignment",
|
||||||
"ZIndex": "int"
|
"ZIndex": "int",
|
||||||
|
"RenderTransform": "Transform"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"Control": {
|
"Control": {
|
||||||
@ -183,13 +184,14 @@
|
|||||||
},
|
},
|
||||||
"int": {},
|
"int": {},
|
||||||
"double": {},
|
"double": {},
|
||||||
"object": {},
|
"object": { "CanHaveElement": true },
|
||||||
"Thickness": {},
|
"Thickness": {},
|
||||||
"Rect": {},
|
"Rect": {},
|
||||||
"Point": {},
|
"Point": {},
|
||||||
"Brush": {},
|
"Brush": { "CanHaveElement": true },
|
||||||
"Color": {},
|
"Color": {},
|
||||||
"ImageSource": {},
|
"ImageSource": {},
|
||||||
|
"Transform": { "CanHaveElement": true },
|
||||||
"Visibility": {
|
"Visibility": {
|
||||||
"Values": [
|
"Values": [
|
||||||
"Visible",
|
"Visible",
|
||||||
@ -252,6 +254,14 @@
|
|||||||
"MaxHeight"
|
"MaxHeight"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
"TextAlignment": {
|
||||||
|
"Values": [
|
||||||
|
"Left",
|
||||||
|
"Right",
|
||||||
|
"Center",
|
||||||
|
"Justify"
|
||||||
|
]
|
||||||
|
},
|
||||||
"TextTrimming": {
|
"TextTrimming": {
|
||||||
"Values": [
|
"Values": [
|
||||||
"None",
|
"None",
|
||||||
|
@ -33,6 +33,7 @@ namespace Bloxstrap.UI.Elements.Editor
|
|||||||
|
|
||||||
public class Type
|
public class Type
|
||||||
{
|
{
|
||||||
|
public bool CanHaveElement { get; set; } = false;
|
||||||
public List<string>? Values { get; set; } = null;
|
public List<string>? Values { get; set; } = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,6 +44,11 @@ namespace Bloxstrap.UI.Elements.Editor
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static SortedDictionary<string, SortedDictionary<string, string>> ElementInfo { get; set; } = new();
|
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>
|
/// <summary>
|
||||||
/// All type info
|
/// All type info
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -63,23 +69,42 @@ namespace Bloxstrap.UI.Elements.Editor
|
|||||||
PopulateElementInfo();
|
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))
|
if (ElementInfo.ContainsKey(name))
|
||||||
return ElementInfo[name];
|
return (ElementInfo[name], PropertyElements[name]);
|
||||||
|
|
||||||
|
List<string> properties = new List<string>();
|
||||||
SortedDictionary<string, string> attributes = new();
|
SortedDictionary<string, string> attributes = new();
|
||||||
|
|
||||||
foreach (var attribute in element.Attributes)
|
foreach (var attribute in element.Attributes)
|
||||||
|
{
|
||||||
attributes.Add(attribute.Key, attribute.Value);
|
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)
|
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);
|
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()
|
private static void PopulateElementInfo()
|
||||||
@ -88,7 +113,7 @@ namespace Bloxstrap.UI.Elements.Editor
|
|||||||
|
|
||||||
foreach (var element in _schema!.Elements)
|
foreach (var element in _schema!.Elements)
|
||||||
{
|
{
|
||||||
ElementInfo[element.Key] = GetElementAttributes(element.Key, element.Value);
|
GetElementAttributes(element.Key, element.Value);
|
||||||
|
|
||||||
if (!element.Value.IsCreatable)
|
if (!element.Value.IsCreatable)
|
||||||
toRemove.Add(element.Key);
|
toRemove.Add(element.Key);
|
||||||
@ -137,6 +162,9 @@ namespace Bloxstrap.UI.Elements.Editor
|
|||||||
case " ":
|
case " ":
|
||||||
OpenAttributeAutoComplete();
|
OpenAttributeAutoComplete();
|
||||||
break;
|
break;
|
||||||
|
case ".":
|
||||||
|
OpenPropertyElementAutoComplete();
|
||||||
|
break;
|
||||||
case "/":
|
case "/":
|
||||||
AddEndTag();
|
AddEndTag();
|
||||||
break;
|
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>
|
/// <summary>
|
||||||
/// Returns null if not eligible to auto complete there.
|
/// Returns null if not eligible to auto complete there.
|
||||||
/// Returns the name of the element to show the attributes for
|
/// 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)
|
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;
|
var typeValues = CustomBootstrapperSchema.Types[typeName].Values;
|
||||||
if (typeValues == null)
|
if (typeValues == null)
|
||||||
return;
|
return;
|
||||||
@ -346,6 +393,31 @@ namespace Bloxstrap.UI.Elements.Editor
|
|||||||
ShowCompletionWindow(data);
|
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()
|
private void CloseCompletionWindow()
|
||||||
{
|
{
|
||||||
if (_completionWindow != null)
|
if (_completionWindow != null)
|
||||||
|
Loading…
Reference in New Issue
Block a user