diff --git a/AiProgrammer/CodeProcessing/ClassExtractorCSharp.cs b/AiProgrammer/CodeProcessing/ClassExtractorCSharp.cs index 65e4079..37c497b 100644 --- a/AiProgrammer/CodeProcessing/ClassExtractorCSharp.cs +++ b/AiProgrammer/CodeProcessing/ClassExtractorCSharp.cs @@ -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 methods) - { - return GetClassContentWithSelectedMethodBodies(File.OpenText(fullFilePath), methods); - } - - //TODO: Longest method generated. Too long, refactor, preferably using AI itself. - public string GetClassContentWithSelectedMethodBodies(TextReader streamReader, IReadOnlyList 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 methods) { - stringBuilder.AppendLine(line); + return GetClassContentWithSelectedMethodBodies(File.OpenText(fullFilePath), methods); } - while ((line = streamReader.ReadLine()) != null) + public string GetClassContentWithSelectedMethodBodies(TextReader streamReader, IReadOnlyList 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 methods) + { if (line.Trim().EndsWith("{")) { braceCount++; @@ -99,7 +139,7 @@ void PrintLine(string line) { isInsideClass = false; } - + braceCount--; if (isInsideMethod) @@ -125,7 +165,5 @@ void PrintLine(string line) } } } - - return stringBuilder.ToString(); } -} +} \ No newline at end of file