Skip to content

Commit

Permalink
Merge branch 'master' into namespace-suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Takoooooo authored Oct 29, 2023
2 parents d116634 + c3dabfe commit 1155221
Show file tree
Hide file tree
Showing 11 changed files with 293 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class XmlParser
public enum ParserState
{
None,
InsideDeclaration,
InsideComment,
InsideCdata,
StartElement,
Expand Down Expand Up @@ -76,6 +77,9 @@ public XmlParser(ReadOnlyMemory<char> data, int start = 0)
private const string CdataStart = "![CDATA[";
private const string CdataEnd = "]]>";

private const string DeclarationStart = "?";
private const string DeclarationEnd = "?>";

private bool CheckPrev(int caret, string checkFor)
{
var startAt = caret - checkFor.Length + 1;
Expand All @@ -100,7 +104,6 @@ private bool ParseChar()
var i = _parserPos++;
var span = _data.Span;
var c = span[i];
char lastChar;
if (c == '<' && State == ParserState.None)
{
State = ParserState.StartElement;
Expand All @@ -110,13 +113,29 @@ private bool ParseChar()

_containingTagStart.Push(i);
}
else if (State == ParserState.StartElement && CheckPrev(i, DeclarationStart))
{
State = ParserState.InsideDeclaration;
}
else if (State == ParserState.InsideDeclaration && CheckPrev(i, DeclarationEnd))
{
State = ParserState.None;
if (_containingTagStart.Count > 0)
{
_containingTagStart.Pop();
}
}
else if (State == ParserState.StartElement && CheckPrev(i, CommentStart))
{
State = ParserState.InsideComment;
}
else if (State == ParserState.InsideComment && CheckPrev(i, CommentEnd))
{
State = ParserState.None;
if (_containingTagStart.Count > 0)
{
_containingTagStart.Pop();
}
}
else if (State == ParserState.StartElement && CheckPrev(i, CdataStart))
{
Expand All @@ -125,6 +144,10 @@ private bool ParseChar()
else if (State == ParserState.InsideCdata && CheckPrev(i, CdataEnd))
{
State = ParserState.None;
if (_containingTagStart.Count > 0)
{
_containingTagStart.Pop();
}
}
else if (State == ParserState.StartElement && char.IsWhiteSpace(c))
{
Expand Down Expand Up @@ -202,7 +225,11 @@ private bool ParseChar()

public string? GetParentTagName(int level)
{
if (NestingLevel - level - 1 < 0)
if (State == ParserState.None)
{
level--;
}
if (NestingLevel - level < 0)
return null;
var start = _containingTagStart.Skip(level).FirstOrDefault();
var m = Regex.Match(_data.Span.Slice(start).ToString(), @"^<[^\s/>]+");
Expand Down
17 changes: 17 additions & 0 deletions tests/CompletionEngineTests/CompletionEngineTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@
<AssemblyOriginatorKeyFile>..\..\Key.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>

<ItemGroup>
<None Remove="Parsing\Fragemts\GetParentTagName_Empty.xml" />
<None Remove="Parsing\Fragemts\Should_GetParentTagName_At_Level_One_Level.xml" />
<None Remove="Parsing\Fragemts\Should_GetParentTagName_At_Level_One_Level_With_CDATA.xml" />
<None Remove="Parsing\Fragemts\Should_GetParentTagName_At_Level_One_Level_With_Comment.xml" />
<None Remove="Parsing\Fragemts\Should_GetParentTagName_At_Level_Two_Level.xml" />
<None Remove="Parsing\Fragemts\Should_GetParentTagName_At_Level_Two_Level_With_CDATA.xml" />
<None Remove="Parsing\Fragemts\Should_GetParentTagName_At_Level_Two_Level_With_Comment.xml" />
<None Remove="Parsing\Test.xml" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
<PackageReference Include="xunit" Version="2.4.2" />
Expand All @@ -16,6 +27,12 @@
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Parsing\Fragemts\Should_GetParentTagName_At_Level_Two_Level.xml" />
<EmbeddedResource Include="Parsing\Fragemts\Should_GetParentTagName_At_Level_Two_Level_With_CDATA.xml" />
<EmbeddedResource Include="Parsing\Fragemts\Should_GetParentTagName_At_Level_One_Level_With_CDATA.xml" />
<EmbeddedResource Include="Parsing\Fragemts\Should_GetParentTagName_At_Level_Two_Level_With_Comment.xml" />
<EmbeddedResource Include="Parsing\Fragemts\Should_GetParentTagName_At_Level_One_Level_With_Comment.xml" />
<EmbeddedResource Include="Parsing\Fragemts\Should_GetParentTagName_At_Level_One_Level.xml" />
<EmbeddedResource Include="Test.bmp" />
<AvaloniaResource Include="Test.bmp" />
<AvaloniaResource Include="Test.xaml" />
Expand Down
103 changes: 0 additions & 103 deletions tests/CompletionEngineTests/Manipulator/XmlParserTests.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:ACTest.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:l="using:ACTest"
xmlns:v="using:ACTest.Views"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="ACTest.Views.MainWindow"
Icon="/Assets/avalonia-logo.ico"
Title="ACTest">

</Window>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:ACTest.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:l="using:ACTest"
xmlns:v="using:ACTest.Views"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="ACTest.Views.MainWindow"
Icon="/Assets/avalonia-logo.ico"
Title="ACTest">
<!--Start Here-->

</Window>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:ACTest.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:l="using:ACTest"
xmlns:v="using:ACTest.Views"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="ACTest.Views.MainWindow"
Icon="/Assets/avalonia-logo.ico"
Title="ACTest">

<![CDATA[
aaaa
]]>

</Window>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:ACTest.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:l="using:ACTest"
xmlns:v="using:ACTest.Views"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="ACTest.Views.MainWindow"
Icon="/Assets/avalonia-logo.ico"
Title="ACTest">
<!--Start Here-->

</Window>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:ACTest.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:l="using:ACTest"
xmlns:v="using:ACTest.Views"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="ACTest.Views.MainWindow"
Icon="/Assets/avalonia-logo.ico"
Title="ACTest">

<Window.Styles>

</Window.Styles>
</Window>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:ACTest.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:l="using:ACTest"
xmlns:v="using:ACTest.Views"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="ACTest.Views.MainWindow"
Icon="/Assets/avalonia-logo.ico"
Title="ACTest">

<Window.Styles>
<![CDATA[
aaaa
]]>
<Style/>

</Window.Styles>

</Window>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Window xmlns="/"
>
<!--Start Here-->
<Window.Styles>
<!--Styles-->
<Style/>

</Window.Styles>
</Window>
Loading

0 comments on commit 1155221

Please sign in to comment.