Exemple #1
0
        public void AddInspectorHighlighting(IHighlightingConsumer consumer, ICSharpDeclaration element,
                                             IDeclaredElement declaredElement, string baseDisplayName, string baseTooltip, string moreText, IconModel iconModel,
                                             IEnumerable <BulbMenuItem> items, List <CodeLensEntryExtraActionModel> extraActions)
        {
            string displayName = null;

            var solution = element.GetSolution();

            Assertion.Assert(solution.Locks.IsReadAccessAllowed(), "ReadLock required");

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

            if (containingType == null)
            {
                base.AddHighlighting(consumer, element, field, baseDisplayName, baseTooltip, moreText, iconModel, items, extraActions);
                return;
            }

            var(guidN, propertyNames) = GetAssetGuidAndPropertyName(solution, field);
            if (guidN == null || propertyNames.Length == 0)
            {
                base.AddHighlighting(consumer, element, field, baseDisplayName, baseTooltip, moreText, iconModel, items, extraActions);
                return;
            }

            var guid = guidN.Value;

            var presentationType = GetUnityPresentationType(type);

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

            if (presentationType == UnityPresentationType.UnityEvent)
            {
                var count = myUnityEventsElementContainer.GetUsageCountForEvent(field, out var estimated);
                var sb    = new StringBuilder();
                if (count == 0 && !estimated)
                {
                    sb.Append("No methods");
                }
                else
                {
                    sb.Append(count);
                    if (estimated)
                    {
                        sb.Append('+');
                    }
                    sb.Append(" ");
                    sb.Append("method");
                    if (estimated || count > 1)
                    {
                        sb.Append("s");
                    }
                }

                consumer.AddHighlighting(new UnityInspectorCodeInsightsHighlighting(element.GetNameDocumentRange(),
                                                                                    sb.ToString(), GetTooltip(count, estimated, false), "Methods", this,
                                                                                    declaredElement, iconModel, presentationType));
                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);

            int  changesCount;
            bool isEstimated    = false;
            bool isUniqueChange = false;

            if (myInspectorValuesContainer.IsIndexResultEstimated(guid, containingType, propertyNames))
            {
                changesCount = myInspectorValuesContainer.GetAffectedFiles(guid, propertyNames) - myInspectorValuesContainer.GetAffectedFilesWithSpecificValue(guid, propertyNames, initValueUnityPresentation);
                displayName  = $"Changed in {changesCount}+ assets";
                isEstimated  = true;
            }
            else
            {
                changesCount = 0;
                var initValueCount = myInspectorValuesContainer.GetValueCount(guid, propertyNames, initValueUnityPresentation);

                if (initValueCount == 0 && myInspectorValuesContainer.GetUniqueValuesCount(guid, propertyNames) == 1) // only modified value
                {
                    isUniqueChange = true;
                    var value = myInspectorValuesContainer.GetUniqueValueDifferTo(guid, propertyNames, null);
                    displayName = value.GetPresentation(solution, field, false);
                }
                else if (initValueCount > 0 && myInspectorValuesContainer.GetUniqueValuesCount(guid, propertyNames) == 2)
                {
                    isUniqueChange = true;
                    // original value & only one modified value
                    var anotherValueWithLocation = myInspectorValuesContainer.GetUniqueValueDifferTo(guid, propertyNames, initValueUnityPresentation);
                    displayName = anotherValueWithLocation.GetPresentation(solution, field, false);
                }

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

            consumer.AddHighlighting(new UnityInspectorCodeInsightsHighlighting(element.GetNameDocumentRange(),
                                                                                displayName, GetTooltip(changesCount, isEstimated, isUniqueChange), "Property Inspector values", this,
                                                                                declaredElement, iconModel, presentationType));
        }
        public void AddInspectorHighlighting(IHighlightingConsumer consumer, ICSharpDeclaration element,
                                             IDeclaredElement declaredElement, string baseDisplayName, string baseTooltip, string moreText, IconModel iconModel,
                                             IEnumerable <BulbMenuItem> items, List <CodeLensEntryExtraActionModel> extraActions)
        {
            string displayName = null;
            string tooltip     = "Values from Unity Editor Inspector";

            var solution = element.GetSolution();

            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 presentationType = GetUnityPresentationType(type);

            if (!myDeferredCacheController.CompletedOnce.Value || 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);

            if (myInspectorValuesContainer.IsIndexResultEstimated(guid, containingType, propertyNames))
            {
                var count = myInspectorValuesContainer.GetAffectedFiles(guid, propertyNames) - myInspectorValuesContainer.GetAffectedFilesWithSpecificValue(guid, propertyNames, initValueUnityPresentation);
                displayName = $"{count}+ changes";
            }
            else
            {
                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)
                {
                    // 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.FirstOrDefault(t => !t.Equals(initValueUnityPresentation));
                    displayName = anotherValueWithLocation?.GetPresentation(solution, field, false);
                }

                if (displayName == null || displayName.Equals("..."))
                {
                    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));
        }