public SignatureChange(ParameterConfiguration originalConfiguration, ParameterConfiguration updatedConfiguration)
        {
            OriginalConfiguration = originalConfiguration;
            UpdatedConfiguration = updatedConfiguration;

            // TODO: Could be better than O(n^2)
            var originalParameterList = originalConfiguration.ToListOfParameters();
            var updatedParameterList = updatedConfiguration.ToListOfParameters();

            for (var i = 0; i < originalParameterList.Count; i++)
            {
                int? index = null;
                var parameter = originalParameterList[i];
                if (parameter is ExistingParameter existingParameter)
                {
                    var updatedIndex = updatedParameterList.IndexOf(p => p is ExistingParameter ep && ep.Symbol == existingParameter.Symbol);
                    if (updatedIndex >= 0)
                    {
                        index = updatedIndex;
                    }
                }

                _originalIndexToUpdatedIndexMap.Add(i, index);
            }
        }
        internal ChangeSignatureDialogViewModel(INotificationService notificationService, ParameterConfiguration parameters, ISymbol symbol, ClassificationTypeMap classificationTypeMap)
        {
            _originalParameterConfiguration = parameters;
            _notificationService = notificationService;
            _classificationTypeMap = classificationTypeMap;

            int startingSelectedIndex = 0;

            if (parameters.ThisParameter != null)
            {
                startingSelectedIndex++;

                _thisParameter = new ParameterViewModel(this, parameters.ThisParameter);
                _disabledParameters.Add(parameters.ThisParameter);
            }

            if (parameters.ParamsParameter != null)
            {
                _paramsParameter = new ParameterViewModel(this, parameters.ParamsParameter);
            }

            _symbol = symbol;
            _declarationParts = symbol.ToDisplayParts(s_symbolDeclarationDisplayFormat);

            _parameterGroup1 = parameters.ParametersWithoutDefaultValues.Select(p => new ParameterViewModel(this, p)).ToList();
            _parameterGroup2 = parameters.RemainingEditableParameters.Select(p => new ParameterViewModel(this, p)).ToList();
            this.SelectedIndex = startingSelectedIndex;
        }
        private async Task <ChangeSignatureAnalyzedContext> GetContextAsync(Document document, int position, bool restrictToDeclarations, CancellationToken cancellationToken)
        {
            var symbol = GetInvocationSymbol(document, position, restrictToDeclarations, cancellationToken);

            // Cross-lang symbols will show as metadata, so map it to source if possible.
            symbol = await SymbolFinder.FindSourceDefinitionAsync(symbol, document.Project.Solution, cancellationToken).ConfigureAwait(false) ?? symbol;

            if (symbol == null)
            {
                return(new ChangeSignatureAnalyzedContext(CannotChangeSignatureReason.IncorrectKind));
            }

            if (symbol is IMethodSymbol)
            {
                var method         = symbol as IMethodSymbol;
                var containingType = method.ContainingType;

                if (method.Name == WellKnownMemberNames.DelegateBeginInvokeName &&
                    containingType != null &&
                    containingType.IsDelegateType() &&
                    containingType.DelegateInvokeMethod != null)
                {
                    symbol = containingType.DelegateInvokeMethod;
                }
            }

            if (symbol is IEventSymbol)
            {
                symbol = (symbol as IEventSymbol).Type;
            }

            if (symbol is INamedTypeSymbol)
            {
                var typeSymbol = symbol as INamedTypeSymbol;
                if (typeSymbol.IsDelegateType() && typeSymbol.DelegateInvokeMethod != null)
                {
                    symbol = typeSymbol.DelegateInvokeMethod;
                }
            }

            if (symbol.Locations.Any(loc => loc.IsInMetadata))
            {
                return(new ChangeSignatureAnalyzedContext(CannotChangeSignatureReason.DefinedInMetadata));
            }

            if (!symbol.MatchesKind(SymbolKind.Method, SymbolKind.Property, SymbolKind.NamedType))
            {
                return(new ChangeSignatureAnalyzedContext(CannotChangeSignatureReason.IncorrectKind));
            }

            var parameterConfiguration = ParameterConfiguration.Create(symbol.GetParameters().ToList(), symbol is IMethodSymbol && (symbol as IMethodSymbol).IsExtensionMethod);

            if (!parameterConfiguration.IsChangeable())
            {
                return(new ChangeSignatureAnalyzedContext(CannotChangeSignatureReason.InsufficientParameters));
            }

            return(new ChangeSignatureAnalyzedContext(document.Project.Solution, symbol, parameterConfiguration));
        }
 public ChangeSignatureAnalysisSucceededContext(
     Document document, int positionForTypeBinding, ISymbol symbol, ParameterConfiguration parameterConfiguration, CodeCleanupOptionsProvider fallbackOptions)
 {
     Document = document;
     Symbol   = symbol;
     ParameterConfiguration = parameterConfiguration;
     PositionForTypeBinding = positionForTypeBinding;
     FallbackOptions        = fallbackOptions;
 }
 public ParameterConfiguration(Microsoft.CodeAnalysis.ChangeSignature.ParameterConfiguration inner)
 {
     _inner        = inner;
     ThisParameter = inner.ThisParameter;
     ParametersWithoutDefaultValues = inner.ParametersWithoutDefaultValues;
     RemainingEditableParameters    = inner.RemainingEditableParameters;
     ParamsParameter = inner.ParamsParameter;
     SelectedIndex   = inner.SelectedIndex;
 }
        public ChangeSignatureOptionsResult GetChangeSignatureOptions(ISymbol symbol, ParameterConfiguration parameters, INotificationService notificationService)
        {
            var list = parameters.ToListOfParameters();

            return new ChangeSignatureOptionsResult
            {
                IsCancelled = IsCancelled,
                UpdatedSignature = new SignatureChange(
                    parameters,
                    UpdatedSignature == null ? parameters : ParameterConfiguration.Create(UpdatedSignature.Select(i => list[i]).ToList(), parameters.ThisParameter != null))
            };
        }
Exemple #7
0
        public SignatureChange(ParameterConfiguration originalConfiguration, ParameterConfiguration updatedConfiguration)
        {
            OriginalConfiguration = originalConfiguration;
            UpdatedConfiguration  = updatedConfiguration;

            // TODO: Could be better than O(n^2)
            var originalParameterList = originalConfiguration.ToListOfParameters();
            var updatedParameterList  = updatedConfiguration.ToListOfParameters();

            for (var i = 0; i < originalParameterList.Count; i++)
            {
                var parameter    = originalParameterList[i];
                var updatedIndex = updatedParameterList.IndexOf(parameter);
                _originalIndexToUpdatedIndexMap.Add(i, updatedIndex != -1 ? updatedIndex : (int?)null);
            }
        }
Exemple #8
0
        public SignatureChange(ParameterConfiguration originalConfiguration, ParameterConfiguration updatedConfiguration)
        {
            this.OriginalConfiguration = originalConfiguration;
            this.UpdatedConfiguration = updatedConfiguration;

            // TODO: Could be better than O(n^2)
            var originalParameterList = originalConfiguration.ToListOfParameters();
            var updatedParameterList = updatedConfiguration.ToListOfParameters();

            for (int i = 0; i < originalParameterList.Count; i++)
            {
                var parameter = originalParameterList[i];
                var updatedIndex = updatedParameterList.IndexOf(parameter);
                _originalIndexToUpdatedIndexMap.Add(i, updatedIndex != -1 ? updatedIndex : (int?)null);
            }
        }
        public ChangeSignatureOptionsResult GetChangeSignatureOptions(ISymbol symbol, ParameterConfiguration parameters, INotificationService notificationService)
        {
            var viewModel = new ChangeSignatureDialogViewModel(notificationService, parameters, symbol, _classificationTypeMap);

            var dialog = new ChangeSignatureDialog(viewModel);
            var result = dialog.ShowModal();

            if (result.HasValue && result.Value)
            {
                return new ChangeSignatureOptionsResult { IsCancelled = false, UpdatedSignature = new SignatureChange(parameters, viewModel.GetParameterConfiguration()), PreviewChanges = viewModel.PreviewChanges };
            }
            else
            {
                return new ChangeSignatureOptionsResult { IsCancelled = true };
            }
        }
        private static SignatureChange CreateCompensatingSignatureChange(ISymbol declarationSymbol, SignatureChange updatedSignature)
        {
            if (declarationSymbol.GetParameters().Length > updatedSignature.OriginalConfiguration.ToListOfParameters().Count)
            {
                var origStuff = updatedSignature.OriginalConfiguration.ToListOfParameters();
                var newStuff  = updatedSignature.UpdatedConfiguration.ToListOfParameters();

                var realStuff = declarationSymbol.GetParameters();

                var bonusParameters = realStuff.Skip(origStuff.Count);

                origStuff.AddRange(bonusParameters);
                newStuff.AddRange(bonusParameters);

                var newOrigParams    = ParameterConfiguration.Create(origStuff, updatedSignature.OriginalConfiguration.ThisParameter != null);
                var newUpdatedParams = ParameterConfiguration.Create(newStuff, updatedSignature.OriginalConfiguration.ThisParameter != null);
                updatedSignature = new SignatureChange(newOrigParams, newUpdatedParams);
            }

            return(updatedSignature);
        }
        internal async Task <ChangeSignatureAnalyzedContext> GetContextAsync(
            Document document, int position, bool restrictToDeclarations, CancellationToken cancellationToken)
        {
            var(symbol, selectedIndex) = await GetInvocationSymbolAsync(
                document, position, restrictToDeclarations, cancellationToken).ConfigureAwait(false);

            // Cross-language symbols will show as metadata, so map it to source if possible.
            symbol = await SymbolFinder.FindSourceDefinitionAsync(symbol, document.Project.Solution, cancellationToken).ConfigureAwait(false) ?? symbol;

            if (symbol == null)
            {
                return(new CannotChangeSignatureAnalyzedContext(CannotChangeSignatureReason.IncorrectKind));
            }

            if (symbol is IMethodSymbol method)
            {
                var containingType = method.ContainingType;

                if (method.Name == WellKnownMemberNames.DelegateBeginInvokeName &&
                    containingType != null &&
                    containingType.IsDelegateType() &&
                    containingType.DelegateInvokeMethod != null)
                {
                    symbol = containingType.DelegateInvokeMethod;
                }
            }

            if (symbol is IEventSymbol ev)
            {
                symbol = ev.Type;
            }

            if (symbol is INamedTypeSymbol typeSymbol)
            {
                if (typeSymbol.IsDelegateType() && typeSymbol.DelegateInvokeMethod != null)
                {
                    symbol = typeSymbol.DelegateInvokeMethod;
                }
            }

            if (!symbol.MatchesKind(SymbolKind.Method, SymbolKind.Property))
            {
                return(new CannotChangeSignatureAnalyzedContext(CannotChangeSignatureReason.IncorrectKind));
            }

            if (symbol.Locations.Any(loc => loc.IsInMetadata))
            {
                return(new CannotChangeSignatureAnalyzedContext(CannotChangeSignatureReason.DefinedInMetadata));
            }

            // This should be called after the metadata check above to avoid looking for nodes in metadata.
            var declarationLocation = symbol.Locations.FirstOrDefault();

            if (declarationLocation == null)
            {
                return(new CannotChangeSignatureAnalyzedContext(CannotChangeSignatureReason.DefinedInMetadata));
            }

            var solution            = document.Project.Solution;
            var declarationDocument = solution.GetRequiredDocument(declarationLocation.SourceTree !);
            var declarationChangeSignatureService = declarationDocument.GetRequiredLanguageService <AbstractChangeSignatureService>();

            int insertPosition;
            var reference = symbol.DeclaringSyntaxReferences.FirstOrDefault();

            if (reference != null)
            {
                insertPosition = declarationChangeSignatureService.GetPositionBeforeParameterListClosingBrace(reference.GetSyntax(cancellationToken));
            }
            else
            {
                // There may be no declaring syntax reference, for example delegate Invoke methods. Use an
                // insertPosition of 0 and continue on.
                insertPosition = 0;
            }

            var parameterConfiguration = ParameterConfiguration.Create(
                symbol.GetParameters().Select(p => new ExistingParameter(p)).ToImmutableArray <Parameter>(),
                symbol.IsExtensionMethod(), selectedIndex);

            return(new ChangeSignatureAnalysisSucceededContext(
                       declarationDocument, insertPosition, symbol, parameterConfiguration));
        }
        internal async Task <ChangeSignatureAnalyzedContext> GetContextAsync(
            Document document, int position, bool restrictToDeclarations, CancellationToken cancellationToken)
        {
            var(symbol, selectedIndex) = await GetInvocationSymbolAsync(
                document, position, restrictToDeclarations, cancellationToken).ConfigureAwait(false);

            // Cross-language symbols will show as metadata, so map it to source if possible.
            symbol = await SymbolFinder.FindSourceDefinitionAsync(symbol, document.Project.Solution, cancellationToken).ConfigureAwait(false) ?? symbol;

            if (symbol == null)
            {
                return(new CannotChangeSignatureAnalyzedContext(CannotChangeSignatureReason.IncorrectKind));
            }

            if (symbol is IMethodSymbol method)
            {
                var containingType = method.ContainingType;

                if (method.Name == WellKnownMemberNames.DelegateBeginInvokeName &&
                    containingType != null &&
                    containingType.IsDelegateType() &&
                    containingType.DelegateInvokeMethod != null)
                {
                    symbol = containingType.DelegateInvokeMethod;
                }
            }

            if (symbol is IEventSymbol ev)
            {
                symbol = ev.Type;
            }

            if (symbol is INamedTypeSymbol typeSymbol)
            {
                if (typeSymbol.IsDelegateType() && typeSymbol.DelegateInvokeMethod != null)
                {
                    symbol = typeSymbol.DelegateInvokeMethod;
                }
            }

            if (symbol.Locations.Any(loc => loc.IsInMetadata))
            {
                return(new CannotChangeSignatureAnalyzedContext(CannotChangeSignatureReason.DefinedInMetadata));
            }

            // This should be called after the metadata check above to avoid looking for nodes in metadata.
            var declarationLocation = symbol.Locations.FirstOrDefault();

            if (declarationLocation == null)
            {
                return(new CannotChangeSignatureAnalyzedContext(CannotChangeSignatureReason.DefinedInMetadata));
            }

            var solution            = document.Project.Solution;
            var documentId          = solution.GetDocumentId(declarationLocation.SourceTree);
            var declarationDocument = solution.GetDocument(documentId);
            var declarationChangeSignatureService = declarationDocument?.GetRequiredLanguageService <AbstractChangeSignatureService>();

            if (declarationChangeSignatureService == null)
            {
                return(new CannotChangeSignatureAnalyzedContext(CannotChangeSignatureReason.DeclarationLanguageServiceNotFound));
            }

            int?insertPositionOpt = declarationChangeSignatureService.TryGetInsertPositionFromDeclaration(symbol.Locations.FirstOrDefault().FindNode(cancellationToken));

            if (insertPositionOpt == null)
            {
                return(new CannotChangeSignatureAnalyzedContext(CannotChangeSignatureReason.DeclarationMethodPositionNotFound));
            }

            if (!symbol.MatchesKind(SymbolKind.Method, SymbolKind.Property, SymbolKind.NamedType))
            {
                return(new CannotChangeSignatureAnalyzedContext(CannotChangeSignatureReason.IncorrectKind));
            }

            var parameterConfiguration = ParameterConfiguration.Create(
                symbol.GetParameters().Select(p => new ExistingParameter(p)),
                symbol.IsExtensionMethod(), selectedIndex);

            return(new ChangeSignatureAnalyzedSucceedContext(
                       declarationDocument ?? document, insertPositionOpt.Value, symbol, parameterConfiguration));
        }
        public ChangeSignatureOptionsResult?GetChangeSignatureOptions(Document document, int positionForTypeBinding, ISymbol symbol, Microsoft.CodeAnalysis.ChangeSignature.ParameterConfiguration parameters)
        {
            var viewModel = new ChangeSignatureDialogViewModel(new ParameterConfiguration(parameters), symbol);

            var dialog = _dialogFactory.CreateExport().Value;

            dialog.ViewModel = viewModel;
            var result = dialog.Show();

            return(result == true
                ? new ChangeSignatureOptionsResult(new SignatureChange(new ParameterConfiguration(parameters), viewModel.GetParameterConfiguration()).ToInternal(), previewChanges: false)
                : null);
        }
Exemple #14
0
        public ChangeSignatureOptionsResult GetChangeSignatureOptions(ISymbol symbol, Microsoft.CodeAnalysis.ChangeSignature.ParameterConfiguration parameters,
                                                                      INotificationService notificationService)
        {
            var viewModel = new ChangeSignatureDialogViewModel(new ParameterConfiguration(parameters), symbol);

            var dialog = new ChangeSignatureDialog(viewModel);

            dialog.SetOwnerToActive();
            var result = dialog.ShowDialog();

            return(result == true
                ? new ChangeSignatureOptionsResult {
                IsCancelled = false, UpdatedSignature = new SignatureChange(new ParameterConfiguration(parameters), viewModel.GetParameterConfiguration()).ToInternal()
            }
                : new ChangeSignatureOptionsResult {
                IsCancelled = true
            });
        }
Exemple #15
0
        public ChangeSignatureOptionsResult GetChangeSignatureOptions(ISymbol symbol, Microsoft.CodeAnalysis.ChangeSignature.ParameterConfiguration parameters,
                                                                      INotificationService notificationService)
        {
            var viewModel = new ChangeSignatureDialogViewModel(new ParameterConfiguration(parameters), symbol);

            var dialog = _dialogFactory.CreateExport().Value;

            dialog.ViewModel = viewModel;
            var result = dialog.Show();

            return(result == true
                ? new ChangeSignatureOptionsResult {
                IsCancelled = false, UpdatedSignature = new SignatureChange(new ParameterConfiguration(parameters), viewModel.GetParameterConfiguration()).ToInternal()
            }
                : new ChangeSignatureOptionsResult {
                IsCancelled = true
            });
        }