Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Core/Emitters/HTML/CSSProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public CSSProvider(string? userProvidedCSS)
UserProvidedCSS = userProvidedCSS;
}

public string GetMappedColour(string s) => ColorsMap[s];
public string GetColourHexValue(string s) => ColorsMap[s];

public string GetCSS(bool addLineNumber, bool optimize, List<NodeAfterProcessing> nodes, string mostCommonColourValue)
{
Expand Down Expand Up @@ -89,6 +89,7 @@ private string ApplyMostCommonColourToTemplateCSS(string template, string colour
{ NodeColors.Namespace, "#dfdfdf" },
{ NodeColors.EnumMemberName, "#dfdfdf" },
{ NodeColors.Identifier, "#dfdfdf" },
{ NodeColors.Default, "#dfdfdf" },
{ NodeColors.Punctuation, "#dfdfdf" },
{ NodeColors.Operator, "#dfdfdf" },
{ NodeColors.PropertyName, "#dfdfdf" },
Expand Down
26 changes: 17 additions & 9 deletions src/Core/Emitters/HTML/HTMLEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private string Escape(string textWithTrivia)

private int _LineCounter = 1;

private string _MostCommonColourValue = string.Empty;
private string _MostCommonColourHexValue = string.Empty;

// Public Stuff:

Expand Down Expand Up @@ -83,7 +83,7 @@ private string GenerateHtml(List<NodeAfterProcessing> nodes)
{
var sb = new StringBuilder();

sb.AppendLine(_cssHelper.GetCSS(AddLineNumber, Optimize, nodes, _MostCommonColourValue));
sb.AppendLine(_cssHelper.GetCSS(AddLineNumber, Optimize, nodes, _MostCommonColourHexValue));
sb.AppendLine(@"<pre class=""background"">");

for (int i = 0; i < nodes.Count; i++)
Expand All @@ -107,7 +107,7 @@ private string GenerateHtmlWithLineNumbers(List<NodeAfterProcessing> nodes)
{
var sb = new StringBuilder();

sb.AppendLine(_cssHelper.GetCSS(AddLineNumber, Optimize, nodes, _MostCommonColourValue));
sb.AppendLine(_cssHelper.GetCSS(AddLineNumber, Optimize, nodes, _MostCommonColourHexValue));
sb.AppendLine(@"<pre class=""background"">");

sb.AppendLine("<table>");
Expand Down Expand Up @@ -240,17 +240,25 @@ private void OptimizeNodes(List<NodeAfterProcessing> nodes)
if (!nodes.Any())
return;

var mostCommonColourName = nodes.Select(x => x.Colour).GroupBy(x => x).OrderByDescending(x => x.Count()).First().Key;
_MostCommonColourValue = _cssHelper.GetMappedColour(mostCommonColourName);
var mostCommonColourName = nodes
.Select(x => x.Colour)
.GroupBy(x => x)
.OrderByDescending(x => x.Count())
.First()
.Key;

_MostCommonColourHexValue = _cssHelper.GetColourHexValue(mostCommonColourName);

foreach (var node in nodes)
{
if (_cssHelper.GetColourHexValue(node.Colour) == _MostCommonColourHexValue)
node.UsesMostCommonColour = true;
}

for (int i = 0; i < nodes.Count; i++)
{
var current = nodes[i];

var mappedColour = _cssHelper.GetMappedColour(current.Colour);
if (mappedColour == _MostCommonColourValue)
nodes[i].UsesMostCommonColour = true;

if (i + 1 >= nodes.Count)
break;

Expand Down
122 changes: 118 additions & 4 deletions src/Core/HeuristicsGeneration/Backtrace.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using CsharpToColouredHTML.Core.Nodes;
using System.Linq;
using CsharpToColouredHTML.Core.Miscs;
using CsharpToColouredHTML.Core.Nodes;
using Microsoft.CodeAnalysis.Classification;

namespace CsharpToColouredHTML.Core.HeuristicsGeneration;
Expand Down Expand Up @@ -210,7 +212,7 @@ private bool TryReadIdentifierChain()
return false;
}

private bool TryReadTypeOfIdentifierChain()
private bool TryMarkTypeOfNamespaceAndClassChain()
{
var valid_identifiers = new List<string>
{
Expand Down Expand Up @@ -271,7 +273,7 @@ private bool TryReadTypeOfIdentifierChain()
operators.Count != identifiers.Count - 1)
return false;

foreach (var node in chainElements)
foreach (var node in chainWithoutLastElement)
{
if (node.ClassificationType == ClassificationTypeNames.Operator)
{
Expand All @@ -292,7 +294,7 @@ private bool TryReadTypeOfIdentifierChain()
}
}

_CurrentIndex = _OriginalNodes.IndexOf(peekedNode);
_CurrentIndex = _OriginalNodes.IndexOf(peekedNode) - 1;
return true;
}
}
Expand Down Expand Up @@ -726,4 +728,116 @@ private bool TryDetectCastAhead()

return false;
}

private bool CheckAndMarkNamedTupleType()
{
if (TryPeekAhead(out var peekedAhead) && peekedAhead.Text == "(" && CanMoveAhead(2))
{
MarkNodeAs(peekedAhead, NodeColors.Punctuation);
MoveNext();

var valid_identifiers = new List<string>
{
ClassificationTypeNames.Identifier,
ClassificationTypeNames.NamespaceName,
ClassificationTypeNames.ClassName,
ClassificationTypeNames.StructName,
ClassificationTypeNames.Keyword,
};

void MarkTupleElements(List<Node> chainElements)
{
foreach (var node in chainElements)
{
if (node.ClassificationType == ClassificationTypeNames.Operator)
{
MarkNodeAs(node, NodeColors.Operator);
}
else if (node.ClassificationType == ClassificationTypeNames.Punctuation)
{
MarkNodeAs(node, NodeColors.Punctuation);
}
else if (node.ClassificationType == ClassificationTypeNames.Keyword)
{
MarkNodeAs(node, NodeColors.Keyword);
}
else if (node.Id == chainElements[^2].Id)
{
MarkNodeAs(node, NodeColors.Identifier);
}
else if (node.Id == chainElements[^3].Id)
{
var color = ResolveClassOrStructName(node.Text);
MarkNodeAs(node, color);
}
else
{
MarkNodeAs(node, NodeColors.Namespace);
}
}
chainElements.Clear();
}

// 0 = currently at Identifier 1, expecting Identifier 2
// 1 = currently at Identifier 2, expecting Punctuation
// 2 = currently at Punctuation, expecting Identifier 1

var state = 0;
var indexAhead = 1;

var chainElements = new List<Node> { };

while (TryPeekAhead(out var peekedNode, indexAhead))
{
chainElements.Add(peekedNode);
if (state == 0 || state == 1)
{
if (valid_identifiers.Contains(peekedNode.ClassificationType))
{
state++;
indexAhead++;
continue;
}
else
{
if (state == 1 && peekedNode.Text.EqualsAnyOf(","))
{
MarkTupleElements(chainElements);
state = 0;
indexAhead++;
continue;
}
_CurrentIndex = _OriginalNodes.IndexOf(chainElements.First()) - 1;
return false;
}
}
else if (state == 2)
{
if (peekedNode.Text == ",")
{
MarkTupleElements(chainElements);
state = 0;
indexAhead++;
continue;
}
else
{
if (peekedNode.Text == ")")
{
MarkTupleElements(chainElements);
_CurrentIndex = _OriginalNodes.IndexOf(peekedNode);
return true;
}
else
{
_CurrentIndex = _OriginalNodes.IndexOf(chainElements.First()) - 1;
return false;
}
}
}
}
}

return false;
}
}
3 changes: 3 additions & 0 deletions src/Core/HeuristicsGeneration/HeuristicsGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ public void Reset()
_FoundStructs.Clear();
_FoundPropertiesOrFields.Clear();
_FoundLocalNames.Clear();
_FoundNamespaces.Clear();
_FoundNamespaceParts.Clear();
_OriginalNodes.Clear();
_Output.Clear();
_ParenthesisCounter = 0;
_CurrentIndex = 0;
Expand Down
6 changes: 3 additions & 3 deletions src/Core/HeuristicsGeneration/HeuristicsHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ private bool IsValidClassOrStructName(string text)
if (!char.IsLetter(text[0]) && text[0] != '_')
return false;

return text.Skip(1).All(x => char.IsLetter(x) || char.IsNumber(x));
return text.Skip(1).All(x => char.IsLetter(x) || char.IsNumber(x) || x == '_');
}

private bool IsPopularEnum(string text)
Expand Down Expand Up @@ -307,9 +307,9 @@ private bool IsPopularStruct(string text)
_Hints.ReallyPopularStructsSubstrings.Any(x => text.Contains(x, StringComparison.OrdinalIgnoreCase));
}

public static readonly List<string> AccessibilityModifiers = new List<string>
public static readonly List<string> CommonKeywordsBeforeTypeName = new List<string>
{
"public", "private", "internal", "sealed", "protected", "readonly", "static"
"public", "private", "internal", "sealed", "protected", "readonly", "static", "override", "event"
};

public static readonly List<string> Operators = new List<string>
Expand Down
Loading