FindReferencesProvider.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System.Linq;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using LLM.Editor.Core;
  5. using System.Collections.Generic;
  6. using IntelligentProjectAnalyzer.Analyzer;
  7. using Object = UnityEngine.Object;
  8. namespace LLM.Editor.Analysis
  9. {
  10. /// <summary>
  11. /// Finds all references to a specific method or class in the project using Roslyn for scripts,
  12. /// AssetDatabase for prefabs, and a scene search for GameObjects.
  13. /// </summary>
  14. public class FindReferencesProvider : IContextProvider
  15. {
  16. public object GetContext(Object target, string qualifier)
  17. {
  18. if (string.IsNullOrEmpty(qualifier))
  19. {
  20. return "Error: A method or class name (qualifier) is required for FindReferences.";
  21. }
  22. if (!qualifier.Contains(".")) // If it's just a method name...
  23. {
  24. // ...try to find the class from the working context.
  25. var workingContext = SessionManager.LoadWorkingContext();
  26. var lastScript = workingContext["lastScriptAnalyzed"]?["className"]?.ToString();
  27. if (!string.IsNullOrEmpty(lastScript))
  28. {
  29. var fullQualifier = $"{lastScript}.{qualifier}";
  30. Debug.Log($"[FindReferencesProvider] Inferred full qualifier as '{fullQualifier}' from working context.");
  31. }
  32. }
  33. // Let Roslyn handle the complex task of parsing the qualifier.
  34. var (scriptReferences, foundScriptGuid, scriptType) = RoslynReferenceFinder.FindReferences(qualifier);
  35. if (string.IsNullOrEmpty(foundScriptGuid) || scriptType == null)
  36. {
  37. return $"Error: Could not resolve '{qualifier}' to a known type or method in the project.";
  38. }
  39. var prefabReferences = FindPrefabReferences(foundScriptGuid);
  40. var sceneReferences = FindSceneReferences(scriptType);
  41. return new
  42. {
  43. scriptReferences,
  44. prefabReferences,
  45. sceneReferences
  46. };
  47. }
  48. /// <summary>
  49. /// Finds all prefabs that have the target script as a dependency.
  50. /// </summary>
  51. private static List<object> FindPrefabReferences(string scriptGuid)
  52. {
  53. var results = new List<object>();
  54. var allPrefabGuids = AssetDatabase.FindAssets("t:Prefab");
  55. foreach (var prefabGuid in allPrefabGuids)
  56. {
  57. var path = AssetDatabase.GUIDToAssetPath(prefabGuid);
  58. var dependencies = AssetDatabase.GetDependencies(path, false);
  59. if (dependencies.Any(dep => AssetDatabase.AssetPathToGUID(dep) == scriptGuid))
  60. {
  61. results.Add(new { path, guid = prefabGuid });
  62. }
  63. }
  64. return results;
  65. }
  66. /// <summary>
  67. /// Finds all GameObjects in the active scene that have the target script component.
  68. /// </summary>
  69. private static List<object> FindSceneReferences(System.Type scriptType)
  70. {
  71. var instances = Object.FindObjectsOfType(scriptType, true);
  72. return instances.Select(inst => new
  73. {
  74. inst.name,
  75. instanceId = inst.GetInstanceID()
  76. }).Cast<object>().ToList();
  77. }
  78. }
  79. }