Skip to content
Open
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
156 changes: 97 additions & 59 deletions AiProgrammer/CodeProcessing/ClassExtractorCSharp.cs
Original file line number Diff line number Diff line change
@@ -1,73 +1,113 @@
using System.Text;
using System.Text;
using System.IO;
using System.Collections.Generic;
using System.Linq;

namespace AiProgrammer.CodeProcessing;

public class ClassExtractorCSharp : IClassExtractor
namespace AiProgrammer.CodeProcessing
{
public string GetClassContentWithSelectedMethodBodies(string fullFilePath, IReadOnlyList<string> methods)
{
return GetClassContentWithSelectedMethodBodies(File.OpenText(fullFilePath), methods);
}

//TODO: Longest method generated. Too long, refactor, preferably using AI itself.
public string GetClassContentWithSelectedMethodBodies(TextReader streamReader, IReadOnlyList<string> methods)
public class ClassExtractorCSharp : IClassExtractor
{
StringBuilder stringBuilder = new StringBuilder();

string line;
int braceCount = 0;
int insideMethodBraceCount = -1;

int classStartBraceCount = 0;
bool isInsideClass = false;
bool isInsideMethod = false;
bool nextLineIsNamespace = false;
bool nextLineIsClass = false;
bool waitForNamespaceEnd = false;
bool waitForClassEnd = false;

void PrintLine(string line)
public string GetClassContentWithSelectedMethodBodies(string fullFilePath, IReadOnlyList<string> methods)
{
stringBuilder.AppendLine(line);
return GetClassContentWithSelectedMethodBodies(File.OpenText(fullFilePath), methods);
}

while ((line = streamReader.ReadLine()) != null)
public string GetClassContentWithSelectedMethodBodies(TextReader streamReader, IReadOnlyList<string> methods)
{
if (!line.Contains(";") && !line.Contains("\"")
&& (line.Contains(" namespace ") || line.Contains("\tnamespace ") || line.StartsWith("namespace ")))
{
PrintLine(line);
nextLineIsNamespace = true;
continue;
}
StringBuilder stringBuilder = new StringBuilder();

if (!line.Contains("\"") &&
(line.Contains(" class ") || line.Contains("\tclass ")))
{
PrintLine(line);
nextLineIsClass = true;
isInsideClass = true;
braceCount++;
classStartBraceCount = braceCount;
continue;
}
string line;
int braceCount = 0;
int insideMethodBraceCount = -1;

int classStartBraceCount = 0;
bool isInsideClass = false;
bool isInsideMethod = false;
bool nextLineIsNamespace = false;
bool nextLineIsClass = false;
bool waitForNamespaceEnd = false;
bool waitForClassEnd = false;

if (nextLineIsNamespace)
void PrintLine(string line)
{
waitForNamespaceEnd = true;
nextLineIsNamespace = false;
PrintLine(line);
continue;
stringBuilder.AppendLine(line);
}

if (nextLineIsClass)
while ((line = streamReader.ReadLine()) != null)
{
waitForClassEnd = true;
nextLineIsClass = false;
PrintLine(line);
continue;
if (IsNamespaceDeclaration(line))
{
HandleNamespaceDeclaration(line, ref nextLineIsNamespace);
continue;
}

if (IsClassDeclaration(line))
{
HandleClassDeclaration(line, ref isInsideClass, ref braceCount, ref classStartBraceCount, ref nextLineIsClass);
continue;
}

if (nextLineIsNamespace)
{
HandleNextLineNamespace(line, ref waitForNamespaceEnd, ref nextLineIsNamespace);
continue;
}

if (nextLineIsClass)
{
HandleNextLineClass(line, ref waitForClassEnd, ref nextLineIsClass);
continue;
}

ProcessLine(line, ref braceCount, ref insideMethodBraceCount, ref isInsideClass, ref isInsideMethod, classStartBraceCount, methods);
}

return stringBuilder.ToString();
}

private bool IsNamespaceDeclaration(string line)
{
return !line.Contains(";") && !line.Contains("\"")
&& (line.Contains(" namespace ") || line.Contains("\tnamespace ") || line.StartsWith("namespace "));
}

private void HandleNamespaceDeclaration(string line, ref bool nextLineIsNamespace)
{
PrintLine(line);
nextLineIsNamespace = true;
}

private bool IsClassDeclaration(string line)
{
return !line.Contains("\"") &&
(line.Contains(" class ") || line.Contains("\tclass "));
}

private void HandleClassDeclaration(string line, ref bool isInsideClass, ref int braceCount, ref int classStartBraceCount, ref bool nextLineIsClass)
{
PrintLine(line);
nextLineIsClass = true;
isInsideClass = true;
braceCount++;
classStartBraceCount = braceCount;
}

private void HandleNextLineNamespace(string line, ref bool waitForNamespaceEnd, ref bool nextLineIsNamespace)
{
waitForNamespaceEnd = true;
nextLineIsNamespace = false;
PrintLine(line);
}

private void HandleNextLineClass(string line, ref bool waitForClassEnd, ref bool nextLineIsClass)
{
waitForClassEnd = true;
nextLineIsClass = false;
PrintLine(line);
}

private void ProcessLine(string line, ref int braceCount, ref int insideMethodBraceCount, ref bool isInsideClass, ref bool isInsideMethod, int classStartBraceCount, IReadOnlyList<string> methods)
{
if (line.Trim().EndsWith("{"))
{
braceCount++;
Expand Down Expand Up @@ -99,7 +139,7 @@ void PrintLine(string line)
{
isInsideClass = false;
}

braceCount--;

if (isInsideMethod)
Expand All @@ -125,7 +165,5 @@ void PrintLine(string line)
}
}
}

return stringBuilder.ToString();
}
}
}