using System.Linq;
using UnityEditor;
using UnityEngine;
using LLM.Editor.Core;
using System.Collections.Generic;
using IntelligentProjectAnalyzer.Analyzer;
using Object = UnityEngine.Object;
namespace LLM.Editor.Analysis
{
///
/// Finds all references to a specific method or class in the project using Roslyn for scripts,
/// AssetDatabase for prefabs, and a scene search for GameObjects.
///
public class FindReferencesProvider : IContextProvider
{
public object GetContext(Object target, string qualifier)
{
if (string.IsNullOrEmpty(qualifier))
{
return "Error: A method or class name (qualifier) is required for FindReferences.";
}
if (!qualifier.Contains(".")) // If it's just a method name...
{
// ...try to find the class from the working context.
var workingContext = SessionManager.LoadWorkingContext();
var lastScript = workingContext["lastScriptAnalyzed"]?["className"]?.ToString();
if (!string.IsNullOrEmpty(lastScript))
{
var fullQualifier = $"{lastScript}.{qualifier}";
Debug.Log($"[FindReferencesProvider] Inferred full qualifier as '{fullQualifier}' from working context.");
}
}
// Let Roslyn handle the complex task of parsing the qualifier.
var (scriptReferences, foundScriptGuid, scriptType) = RoslynReferenceFinder.FindReferences(qualifier);
if (string.IsNullOrEmpty(foundScriptGuid) || scriptType == null)
{
return $"Error: Could not resolve '{qualifier}' to a known type or method in the project.";
}
var prefabReferences = FindPrefabReferences(foundScriptGuid);
var sceneReferences = FindSceneReferences(scriptType);
return new
{
scriptReferences,
prefabReferences,
sceneReferences
};
}
///
/// Finds all prefabs that have the target script as a dependency.
///
private static List