Skip to content

Commit

Permalink
fixes for find usages & counters presentation
Browse files Browse the repository at this point in the history
  • Loading branch information
krasnotsvetov committed Mar 2, 2020
1 parent ea13f29 commit b0d4190
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,57 +126,75 @@ public override void OnClick(CodeInsightsHighlighting highlighting, ISolution so
Assertion.Assert(solution.Locks.IsReadAccessAllowed(), "ReadLock required");

var field = (declaredElement as IField).NotNull();
var type = field.Type;
var containingType = field.GetContainingType();
if (containingType == null)
return;

var (guid, propertyNames) = GetAssetGuidAndPropertyName(solution, field);
if (guid == null || propertyNames == null || propertyNames.Length == 0)
return;

var type = field.Type;

var presentationType = GetUnityPresentationType(type);

if (myDeferredCacheController.IsProcessingFiles() || ShouldShowUnknownPresentation(presentationType))
{
base.AddHighlighting(consumer, element, field, baseDisplayName, baseTooltip, moreText, iconModel, items, extraActions);
return;
}

var initializer = (element as IFieldDeclaration).NotNull("element as IFieldDeclaration != null").Initial;
var initValue = (initializer as IExpressionInitializer)?.Value?.ConstantValue.Value;

var initValueUnityPresentation = GetUnitySerializedPresentation(presentationType, initValue);
var initValueCount = myInspectorValuesContainer.GetValueCount(guid, propertyNames, initValueUnityPresentation);

if (initValueCount == 0 && myInspectorValuesContainer.GetUniqueValuesCount(guid, propertyNames) == 1) // only modified value
{
var values = myInspectorValuesContainer.GetUniqueValues(guid, propertyNames).ToArray();
Assertion.Assert(values.Length == 1, "valueWithLocations.Length == 1"); //performance assertion
var value = values[0];
displayName = value.GetPresentation(solution, field, false);
} else if (initValueCount > 0 && myInspectorValuesContainer.GetUniqueValuesCount(guid, propertyNames) == 2)
var initValueUnityPresentation = GetUnitySerializedPresentation(presentationType, initValue);

if (myInspectorValuesContainer.IsIndexResultEstimated(guid, containingType, propertyNames))
{

// original value & only one modified value
var values = myInspectorValuesContainer.GetUniqueValues(guid, propertyNames).ToArray();
Assertion.Assert(values.Length == 2, "values.Length == 2"); //performance assertion

var anotherValueWithLocation = values.First(t => !t.Equals(initValueUnityPresentation));
displayName = anotherValueWithLocation.GetPresentation(solution, field, false);
var count = myInspectorValuesContainer.GetAffectedFiles(guid, propertyNames) - myInspectorValuesContainer.GetAffectedFilesWithSpecificValue(guid, propertyNames, initValueUnityPresentation);
displayName = $"{count}+ changes";
}

if (displayName == null || displayName.Equals("..."))
else
{
var count = myInspectorValuesContainer.GetAffectedFiles(guid, propertyNames) -
myInspectorValuesContainer.GetAffectedFilesWithSpecificValue(guid, propertyNames, initValueUnityPresentation);
if (count == 0)
var initValueCount =
myInspectorValuesContainer.GetValueCount(guid, propertyNames, initValueUnityPresentation);

if (initValueCount == 0 && myInspectorValuesContainer.GetUniqueValuesCount(guid, propertyNames) == 1
) // only modified value
{
var values = myInspectorValuesContainer.GetUniqueValues(guid, propertyNames).ToArray();
Assertion.Assert(values.Length == 1, "valueWithLocations.Length == 1"); //performance assertion
var value = values[0];
displayName = value.GetPresentation(solution, field, false);
}
else if (initValueCount > 0 &&
myInspectorValuesContainer.GetUniqueValuesCount(guid, propertyNames) == 2)
{
displayName = "Unchanged";

// original value & only one modified value
var values = myInspectorValuesContainer.GetUniqueValues(guid, propertyNames).ToArray();
Assertion.Assert(values.Length == 2, "values.Length == 2"); //performance assertion

var anotherValueWithLocation = values.First(t => !t.Equals(initValueUnityPresentation));
displayName = anotherValueWithLocation.GetPresentation(solution, field, false);
}
else

if (displayName == null || displayName.Equals("..."))
{
var word = count == 1 ? "asset" : "assets";
displayName = $"Changed in {count} {word}";
var count = myInspectorValuesContainer.GetAffectedFiles(guid, propertyNames) -
myInspectorValuesContainer.GetAffectedFilesWithSpecificValue(guid, propertyNames,
initValueUnityPresentation);
if (count == 0)
{
displayName = "Unchanged";
}
else
{
var word = count == 1 ? "asset" : "assets";
displayName = $"Changed in {count} {word}";
}
}
}

consumer.AddHighlighting(new CodeInsightsHighlighting(element.GetNameDocumentRange(),
displayName, tooltip, "Property Inspector values", this,
declaredElement, iconModel));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public override bool Navigate(ISolution solution, IDeclaredElementPointer<IDecla
return true;

var tooltipManager = solution.GetComponent<ITooltipManager>();
tooltipManager.Show("Start the Unity Editor to view changes in the Inspector", lifetime => textControl.PopupWindowContextFactory.CreatePopupWindowContext(lifetime));
tooltipManager.Show("Start the Unity Editor to view results", lifetime => textControl.PopupWindowContextFactory.CreatePopupWindowContext(lifetime));
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
using JetBrains.Diagnostics;
using JetBrains.Lifetimes;
using JetBrains.ProjectModel;
using JetBrains.ReSharper.Daemon;
using JetBrains.ReSharper.Daemon.SolutionAnalysis;
using JetBrains.ReSharper.Daemon.UsageChecking;
using JetBrains.ReSharper.Plugins.Unity.Feature.Caches;
using JetBrains.ReSharper.Plugins.Unity.Yaml.Psi.DeferredCaches.AssetHierarchy;
using JetBrains.ReSharper.Plugins.Unity.Yaml.Psi.DeferredCaches.AssetHierarchy.References;
Expand Down Expand Up @@ -36,8 +39,10 @@ public class AssetInspectorValuesContainer : IUnityAssetDataElementContainer
private readonly CountingSet<MonoBehaviourFieldWithValue> myValuesWhichAreUniqueInWholeFile = new CountingSet<MonoBehaviourFieldWithValue>();
private readonly Dictionary<IPsiSourceFile, OneToListMap<string, InspectorVariableUsage>> myPsiSourceFileToInspectorValues = new Dictionary<IPsiSourceFile, OneToListMap<string, InspectorVariableUsage>>();

private readonly OneToCompactCountingSet<string, string> myNameToGuids = new OneToCompactCountingSet<string, string>();

public AssetInspectorValuesContainer(DeferredCachesLocks deferredCachesLocks, IEnumerable<IAssetInspectorValueDeserializer> assetInspectorValueDeserializer, ILogger logger)

public AssetInspectorValuesContainer(DeferredCachesLocks deferredCachesLocks,IEnumerable<IAssetInspectorValueDeserializer> assetInspectorValueDeserializer, ILogger logger)
{
myDeferredCachesLocks = deferredCachesLocks;
myLogger = logger;
Expand Down Expand Up @@ -113,6 +118,9 @@ public void Drop(IPsiSourceFile sourceFile, AssetDocumentHierarchyElement assetD
RemoveUniqueValue(mbField, variableUsage);
myChangesInFiles.Remove(mbField, sourceFile);
RemoveChangesPerFile(new MonoBehaviourField(guid, variableUsage.Name, sourceFile), variableUsage);

if (variableUsage.ScriptReference is ExternalReference externalReference)
myNameToGuids.Remove(variableUsage.Name, externalReference.ExternalAssetGuid);
}

myPsiSourceFileToInspectorValues.Remove(sourceFile);
Expand Down Expand Up @@ -168,6 +176,9 @@ public void Merge(IPsiSourceFile sourceFile, AssetDocumentHierarchyElement asset
AddChangesPerFile(new MonoBehaviourField(guid, variableUsage.Name, sourceFile), variableUsage);

inspectorUsages.Add(variableUsage.Name, variableUsage);

if (variableUsage.ScriptReference is ExternalReference externalReference)
myNameToGuids.Add(variableUsage.Name, externalReference.ExternalAssetGuid);
}

myPsiSourceFileToInspectorValues.Add(sourceFile, inspectorUsages);
Expand Down Expand Up @@ -235,6 +246,35 @@ public int GetUniqueValuesCount(string guid, IEnumerable<string> possibleNames)
return count;
}

public bool IsIndexResultEstimated(string ownerGuid, ITypeElement containingType, IEnumerable<string> possibleNames)
{
// TODO: prefab modifications
// TODO: drop daemon dependency and inject compoentns in consructor
var configuration = containingType.GetSolution().GetComponent<SolutionAnalysisConfiguration>();
if (configuration.Enabled.Value && configuration.CompletedOnceAfterStart.Value && configuration.Loaded.Value)
{
var service = containingType.GetSolution().GetComponent<SolutionAnalysisService>();
var id = service.GetElementId(containingType);
if (id.HasValue && service.UsageChecker is IGlobalUsageChecker checker)
{
// no inheritors
if (checker.GetDerivedTypeElementsCount(id.Value) == 0)
return false;
}
}

var count = 0;
foreach (var possibleName in possibleNames)
{
count += myNameToGuids.GetValues(possibleName).Length;
if (count == 1 && !myNameToGuids.GetValues(possibleName)[0].Equals(ownerGuid))
count++;
}

return count > 1;
}


public IEnumerable<IAssetValue> GetUniqueValues(string guid, IEnumerable<string> possibleNames)
{
var result = new List<IAssetValue>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ public int GetAssetUsagesCountInner(IDeclaredElement declaredElement, out bool e
// we have already cache guid in merge method for methodData in myLocalUsages
var guid = (assetMethodData.TargetScriptReference as ExternalReference).NotNull("Expected External Reference").ExternalAssetGuid;
var symbolTable = GetReferenceSymbolTable(solution, module, assetMethodData, guid);
if (symbolTable.GetResolveResult(assetMethodData.MethodName).ResolveErrorType == ResolveErrorType.OK)
var resolveResult = symbolTable.GetResolveResult(assetMethodData.MethodName);
if (resolveResult.ResolveErrorType == ResolveErrorType.OK && Equals(resolveResult.DeclaredElement, declaredElement))
{
usageCount += c;
}
Expand Down Expand Up @@ -294,7 +295,8 @@ public IEnumerable<AssetMethodData> GetAssetUsagesFor(IPsiSourceFile psiSourceFi
foreach (var methodData in assetMethodData)
{
var symbolTable = GetReferenceSymbolTable(psiSourceFile.GetSolution(), psiSourceFile.GetPsiModule(), methodData, GetScriptGuid(methodData));
if (symbolTable.GetResolveResult(methodData.MethodName).ResolveErrorType == ResolveErrorType.OK)
var resolveResult = symbolTable.GetResolveResult(methodData.MethodName);
if (resolveResult.ResolveErrorType == ResolveErrorType.OK && Equals(resolveResult.DeclaredElement, declaredElement))
{
result.Add(methodData);
}
Expand Down

0 comments on commit b0d4190

Please sign in to comment.