add end tag auto complete

This commit is contained in:
bluepilledgreat 2024-10-20 16:42:56 +01:00
parent 467dd5c85a
commit d678d8c9e4

View File

@ -138,7 +138,7 @@ namespace Bloxstrap.UI.Elements.Editor
OpenAttributeAutoComplete(); OpenAttributeAutoComplete();
break; break;
case "/": case "/":
CloseCompletionWindow(); AddEndTag();
break; break;
case ">": case ">":
CloseCompletionWindow(); CloseCompletionWindow();
@ -152,8 +152,8 @@ namespace Bloxstrap.UI.Elements.Editor
private (string, int) GetLineAndPosAtCaretPosition() private (string, int) GetLineAndPosAtCaretPosition()
{ {
// this assumes the file was saved as CSLF (\r\n newlines) // this assumes the file was saved as CSLF (\r\n newlines)
int lineStartIdx = UIXML.Text.LastIndexOf('\n', UIXML.CaretOffset); int lineStartIdx = UIXML.Text.LastIndexOf('\n', UIXML.CaretOffset - 1);
int lineEndIdx = UIXML.Text.IndexOf('\n', UIXML.CaretOffset); int lineEndIdx = UIXML.Text.IndexOf('\n', UIXML.CaretOffset - 1);
string line; string line;
int pos; int pos;
@ -187,7 +187,7 @@ namespace Bloxstrap.UI.Elements.Editor
/// <param name="xml"></param> /// <param name="xml"></param>
/// <param name="offset"></param> /// <param name="offset"></param>
/// <returns></returns> /// <returns></returns>
public static string? GetElementAtCursor(string xml, int offset) public static string? GetElementAtCursor(string xml, int offset, bool onlyAllowInside = false)
{ {
if (offset == xml.Length) if (offset == xml.Length)
{ {
@ -211,7 +211,7 @@ namespace Bloxstrap.UI.Elements.Editor
} }
else else
{ {
if (endIdx2 < offset) if (onlyAllowInside && endIdx2 < offset)
return null; // we dont want attribute auto complete to show outside of elements return null; // we dont want attribute auto complete to show outside of elements
if (endIdx2 < xml.Length && xml[endIdx2 - 1] == '/') if (endIdx2 < xml.Length && xml[endIdx2 - 1] == '/')
@ -247,10 +247,15 @@ namespace Bloxstrap.UI.Elements.Editor
// we have an equal number, let's check if pos is in between the speech marks // we have an equal number, let's check if pos is in between the speech marks
int count = -1; int count = -1;
int idx = pos; int idx = pos;
int size = line.Length - 1;
while (idx != -1) while (idx != -1)
{ {
count++; count++;
if (size > idx + 1)
idx = line.IndexOf('"', idx + 1); idx = line.IndexOf('"', idx + 1);
else
idx = -1;
} }
if (count % 2 != 0) if (count % 2 != 0)
@ -261,7 +266,27 @@ namespace Bloxstrap.UI.Elements.Editor
} }
} }
return GetElementAtCursor(UIXML.Text, UIXML.CaretOffset); return GetElementAtCursor(UIXML.Text, UIXML.CaretOffset, true);
}
private void AddEndTag()
{
CloseCompletionWindow();
if (UIXML.Text.Length > 2 && UIXML.Text[UIXML.CaretOffset - 2] == '<')
{
var elementName = GetElementAtCursor(UIXML.Text, UIXML.CaretOffset - 3);
if (elementName == null)
return;
UIXML.TextArea.Document.Insert(UIXML.CaretOffset, $"{elementName}>");
}
else
{
var elementName = ShowAttributesForElementName(); // re-using functions :)
if (elementName != null)
UIXML.TextArea.Document.Insert(UIXML.CaretOffset, ">");
}
} }
private void OpenElementAutoComplete() private void OpenElementAutoComplete()