63 lines
2.5 KiB
C#
63 lines
2.5 KiB
C#
using CodeBase.Models;
|
||
using Microsoft.CodeAnalysis.CSharp;
|
||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||
|
||
namespace CodeBase.Services
|
||
{
|
||
public class ChunkService(VectorizationService vectorizationService)
|
||
{
|
||
public List<CodeChunk> ChunkCSharpFile(string filePath, string fileContent)
|
||
{
|
||
var chunks = new List<CodeChunk>();
|
||
var syntaxTree = CSharpSyntaxTree.ParseText(fileContent);
|
||
var root = syntaxTree.GetRoot();
|
||
|
||
// 1. Собираем методы (как и раньше)
|
||
var methods = root.DescendantNodes().OfType<MethodDeclarationSyntax>();
|
||
foreach (var method in methods)
|
||
{
|
||
chunks.Add(new CodeChunk
|
||
{
|
||
FilePath = filePath,
|
||
MethodName = method.Identifier.Text,
|
||
Content = method.ToFullString().Trim()
|
||
});
|
||
}
|
||
|
||
// 2. ДОБАВЛЯЕМ СБОР ENUM (Перечислений)
|
||
var enums = root.DescendantNodes().OfType<EnumDeclarationSyntax>();
|
||
foreach (var enumSyntax in enums)
|
||
{
|
||
chunks.Add(new CodeChunk
|
||
{
|
||
FilePath = filePath,
|
||
MethodName = enumSyntax.Identifier.Text, // Сохраняем имя енама
|
||
Content = enumSyntax.ToFullString().Trim()
|
||
});
|
||
}
|
||
|
||
// 3. ДОБАВЛЯЕМ СБОР КЛАССОВ-МОДЕЛЕЙ (без методов)
|
||
var classes = root.DescendantNodes().OfType<ClassDeclarationSyntax>();
|
||
foreach (var classSyntax in classes)
|
||
{
|
||
// Берем только свойства, чтобы понимать структуру модели
|
||
var properties = classSyntax.Members.OfType<PropertyDeclarationSyntax>();
|
||
if (properties.Any())
|
||
{
|
||
chunks.Add(new CodeChunk
|
||
{
|
||
FilePath = filePath,
|
||
MethodName = classSyntax.Identifier.Text,
|
||
// Сохраняем объявление класса и его свойства
|
||
Content = $"class {classSyntax.Identifier.Text} {{\n" +
|
||
string.Join("\n", properties.Select(p => p.ToFullString().Trim())) +
|
||
"\n}"
|
||
});
|
||
}
|
||
}
|
||
|
||
return chunks;
|
||
}
|
||
}
|
||
}
|