Exemplo n.º 1
0
            protected override void AfterComplete(ITextControl textControl, IThrowStatement statement)
            {
                if (statement.Semicolon != null)
                {
                    FormatStatementOnSemicolon(statement);
                }

                var settingsStore = statement.GetSettingsStore();
                var expression    = (IObjectCreationExpression)statement.Exception;

                var parenthesesType = settingsStore.GetValue(CodeCompletionSettingsAccessor.ParenthesesInsertType);

                if (parenthesesType == ParenthesesInsertType.None)
                {
                    var endOffset = expression.GetDocumentRange().TextRange.EndOffset;
                    textControl.Caret.MoveTo(endOffset, CaretVisualPlacement.DontScrollIfVisible);
                }
                else
                {
                    var canInstantiate       = TypeUtils.CanInstantiateType(statement.Exception.Type(), statement);
                    var hasRequiredArguments = (canInstantiate & CanInstantiate.ConstructorWithParameters) != 0;

                    var caretNode = hasRequiredArguments ? expression.LPar : (statement.Semicolon ?? (ITreeNode)expression);
                    var endOffset = caretNode.GetDocumentRange().TextRange.EndOffset;

                    textControl.Caret.MoveTo(endOffset, CaretVisualPlacement.DontScrollIfVisible);

                    if (hasRequiredArguments && settingsStore.GetValue(PostfixTemplatesSettingsAccessor.InvokeParameterInfo))
                    {
                        var lookupItemsOwner = Info.ExecutionContext.LookupItemsOwner;
                        LookupUtil.ShowParameterInfo(statement.GetSolution(), textControl, lookupItemsOwner);
                    }
                }
            }
Exemplo n.º 2
0
            protected override void AfterComplete(ITextControl textControl, IInvocationExpression expression)
            {
                var invocationRange = expression.InvokedExpression.GetDocumentRange();
                var languageType    = expression.Language;
                var hotspotInfo     = new HotspotInfo(new TemplateField("Method", 0), invocationRange);

                var argumentRange = expression.Arguments[0].Value.GetDocumentRange();
                var solution      = expression.GetSolution();

                var marker = argumentRange.EndOffsetRange().CreateRangeMarker();
                var length = (marker.Range.EndOffset - invocationRange.TextRange.EndOffset);

                var liveTemplatesManager = Info.ExecutionContext.LiveTemplatesManager;
                var session = liveTemplatesManager.CreateHotspotSessionAtopExistingText(
                    expression.GetSolution(), TextRange.InvalidRange, textControl,
                    LiveTemplatesManager.EscapeAction.RestoreToOriginalText, hotspotInfo);

                var settings            = expression.GetSettingsStore();
                var invokeParameterInfo = settings.GetValue(PostfixTemplatesSettingsAccessor.InvokeParameterInfo);

                textControl.PutData(PostfixArgTemplateExpansion, string.Empty);

                session.Closed.Advise(EternalLifetime.Instance, _ =>
                {
                    textControl.PutData(PostfixArgTemplateExpansion, null);

                    using (ReadLockCookie.Create())
                    {
                        var hotspotRange = session.Hotspots[0].RangeMarker.Range;
                        if (!hotspotRange.IsValid)
                        {
                            return;
                        }

                        solution.GetPsiServices().Files.CommitAllDocuments();

                        if (TryPlaceCaretSmart(solution, textControl, languageType, hotspotRange))
                        {
                            return;
                        }

                        var endOffset = hotspotRange.EndOffset + length;
                        textControl.Caret.MoveTo(endOffset, CaretVisualPlacement.DontScrollIfVisible);

                        if (invokeParameterInfo)
                        {
                            var lookupItemsOwner = Info.ExecutionContext.LookupItemsOwner;
                            LookupUtil.ShowParameterInfo(solution, textControl, lookupItemsOwner);
                        }
                    }
                });

                session.Execute();
            }
            protected override void AfterComplete(ITextControl textControl, IInvocationExpression expression)
            {
                var parseReference = (IReferenceExpression)expression.InvokedExpression;
                var typeQualifier  = parseReference.QualifierExpression.NotNull();

                var solution  = expression.GetSolution();
                var psiModule = typeQualifier.GetPsiModule();

                var typesWithParsers   = GetTypesWithParsers(typeQualifier);
                var templateExpression = new TypeTemplateExpression(typesWithParsers, psiModule, CSharpLanguage.Instance);
                var templateField      = new TemplateField("type", templateExpression, 0);

                var hotspotInfo = new HotspotInfo(templateField, typeQualifier.GetDocumentRange());

                var argumentsRange = expression.ArgumentList.GetDocumentRange();

                var endSelectionRange = argumentsRange.EndOffsetRange().TextRange;
                var session           = myTemplatesManager.CreateHotspotSessionAtopExistingText(
                    solution, endSelectionRange, textControl, LiveTemplatesManager.EscapeAction.LeaveTextAndCaret, hotspotInfo);

                var settingsStore       = expression.GetSettingsStore();
                var invokeParameterInfo = settingsStore.GetValue(PostfixSettingsAccessor.InvokeParameterInfo);

                session.Closed.Advise(Lifetime, args =>
                {
                    if (myIsTryParse)
                    {
                        var shellLocks = solution.GetComponent <IShellLocks>();
                        shellLocks.QueueReadLock("Smart completion for .tryparse", () =>
                        {
#if RESHARPER8
                            var intellisenseManager = solution.GetComponent <IntellisenseManager>();
#elif RESHARPER9
                            var intellisenseManager = solution.GetComponent <ICodeCompletionSessionManager>();
#endif
                            intellisenseManager.ExecuteManualCompletion(
                                CodeCompletionType.SmartCompletion, textControl, solution, EmptyAction.Instance,
                                intellisenseManager.GetPrimaryEvaluationMode(CodeCompletionType.SmartCompletion),
                                AutocompletionBehaviour.DoNotAutocomplete);
                        });
                    }

                    if (invokeParameterInfo)
                    {
                        using (ReadLockCookie.Create())
                        {
                            LookupUtil.ShowParameterInfo(solution, textControl, myLookupItemsOwner);
                        }
                    }
                });

                session.Execute();
            }
Exemplo n.º 4
0
            protected override void AfterComplete(ITextControl textControl, IObjectCreationExpression expression)
            {
                var referencedType = CSharpTypeFactory.CreateDeclaredType(expression.CreatedTypeUsage);

                var canInstantiate = TypeUtils.CanInstantiateType(referencedType, expression);

                if ((canInstantiate & CanInstantiate.ConstructorWithParameters) != 0)
                {
                    var lparRange = expression.LPar.GetDocumentRange();
                    var rparRange = expression.RPar.GetDocumentRange();

                    var documentRange   = lparRange.SetEndTo(rparRange.TextRange.EndOffset);
                    var argumentsMarker = documentRange.CreateRangeMarker();

                    var settingsStore       = expression.GetSettingsStore();
                    var invokeParameterInfo = settingsStore.GetValue(PostfixTemplatesSettingsAccessor.InvokeParameterInfo);

                    var solution = expression.GetSolution();

                    ExecuteRefactoring(textControl, expression, executeAfter: () =>
                    {
                        var argumentsRange = argumentsMarker.Range;
                        if (!argumentsRange.IsValid)
                        {
                            return;
                        }

                        var offset = argumentsRange.StartOffset + argumentsRange.Length / 2; // EWW
                        textControl.Caret.MoveTo(offset, CaretVisualPlacement.DontScrollIfVisible);

                        if (invokeParameterInfo)
                        {
                            var lookupItemsOwner = Info.ExecutionContext.LookupItemsOwner;
                            LookupUtil.ShowParameterInfo(solution, textControl, lookupItemsOwner);
                        }
                    });
                }
                else
                {
                    ExecuteRefactoring(textControl, expression);
                }
            }
            protected override void AfterComplete(ITextControl textControl, IObjectCreationExpression expression)
            {
                var settingsStore = expression.GetSettingsStore();

                var parenthesesType = settingsStore.GetValue(CodeCompletionSettingsAccessor.ParenthesesInsertType);

                if (parenthesesType == ParenthesesInsertType.None)
                {
                    return;
                }

                var caretNode = myHasRequiredArguments ? expression.LPar : (ITreeNode)expression;
                var endOffset = caretNode.GetDocumentRange().TextRange.EndOffset;

                textControl.Caret.MoveTo(endOffset, CaretVisualPlacement.DontScrollIfVisible);

                if (myHasRequiredArguments && settingsStore.GetValue(PostfixSettingsAccessor.InvokeParameterInfo))
                {
                    var solution = expression.GetSolution();
                    LookupUtil.ShowParameterInfo(solution, textControl, myLookupItemsOwner);
                }
            }
            protected override void AfterComplete(ITextControl textControl, IObjectCreationExpression expression)
            {
                if (myHasRequiredArguments)
                {
                    var lparRange = expression.LPar.GetDocumentRange();
                    var rparRange = expression.RPar.GetDocumentRange();

                    var documentRange   = lparRange.SetEndTo(rparRange.TextRange.EndOffset);
                    var argumentsMarker = documentRange.CreateRangeMarker();

                    var settingsStore       = expression.GetSettingsStore();
                    var invokeParameterInfo = settingsStore.GetValue(PostfixSettingsAccessor.InvokeParameterInfo);

                    var solution = expression.GetSolution();

                    ExecuteRefactoring(textControl, expression, executeAfter: () =>
                    {
                        var argumentsRange = argumentsMarker.Range;
                        if (!argumentsRange.IsValid)
                        {
                            return;
                        }

                        var offset = argumentsRange.StartOffset + argumentsRange.Length / 2; // EWW
                        textControl.Caret.MoveTo(offset, CaretVisualPlacement.DontScrollIfVisible);

                        if (invokeParameterInfo)
                        {
                            LookupUtil.ShowParameterInfo(solution, textControl, myLookupItemsOwner);
                        }
                    });
                }
                else
                {
                    ExecuteRefactoring(textControl, expression);
                }
            }
Exemplo n.º 7
0
        private static AfterCompletionHandler BakeAfterComplete([NotNull] ILookupItem lookupItem, [NotNull] ISolution solution, int argumentsCount)
        {
            // sorry, ugly as f**k :(
            return((ITextControl textControl, ref TextRange range, ref TextRange decoration,
                    TailType tailType, ref Suffix suffix, ref IRangeMarker caretMarker) =>
            {
                var psiServices = solution.GetPsiServices();
                psiServices.CommitAllDocuments();

                var allMethods = GetAllTargetMethods(lookupItem);
                if (allMethods.Count == 0)
                {
                    return;
                }

                var reference = TextControlToPsi.GetElement <IReferenceExpression>(solution, textControl.Document, range.StartOffset);
                if (reference == null)
                {
                    return;
                }

                var decorationText = textControl.Document.GetText(decoration);
                var decorationRange = new DocumentRange(textControl.Document, decoration);

                var hasMoreParameters = HasMoreParameters(allMethods, argumentsCount);
                if (!hasMoreParameters) // put caret 'foo(arg){here};'
                {
                    caretMarker = decorationRange.EndOffsetRange().CreateRangeMarker();
                }
                else if (argumentsCount > 0)
                {
                    var parenthesisCloseIndex = decorationText.LastIndexOf(')');
                    if (parenthesisCloseIndex >= 0)
                    {
                        var delta = decoration.Length - parenthesisCloseIndex;
                        caretMarker = decorationRange.EndOffsetRange().Shift(-delta).CreateRangeMarker();
                    }
                }

                var qualifierExpression = reference.QualifierExpression.NotNull("qualifierExpression != null");
                var referencePointer = reference.CreateTreeElementPointer();

                var qualifierText = InsertQualifierAsArgument(
                    qualifierExpression, allMethods, argumentsCount, textControl, decoration, decorationText);

                // TODO: mmm?
                if (!hasMoreParameters && !decorationText.EndsWith(")", StringComparison.Ordinal))
                {
                    caretMarker = caretMarker.DocumentRange.Shift(+qualifierText.Length).CreateRangeMarker();
                }

                // replace qualifier with type (predefined/user type)
                var ownerType = allMethods[0].GetContainingType().NotNull("ownerType != null");
                FixQualifierExpression(textControl, qualifierExpression, ownerType);

                psiServices.CommitAllDocuments();

                var newReference = referencePointer.GetTreeNode();
                if (newReference != null)
                {
                    var keyword = CSharpTypeFactory.GetTypeKeyword(ownerType.GetClrName());
                    if (keyword == null) // bind user type
                    {
                        var newQualifier = (IReferenceExpression)newReference.QualifierExpression;
                        if (newQualifier != null)
                        {
                            var elementInstance = lookupItem.GetDeclaredElement().NotNull("elementInstance != null");
                            newQualifier.Reference.BindTo(ownerType, elementInstance.Substitution);
                        }

                        range = newReference.NameIdentifier.GetDocumentRange().TextRange;
                        decoration = TextRange.InvalidRange;
                    }

                    // show parameter info when needed
                    if (hasMoreParameters)
                    {
                        var factory = solution.GetComponent <LookupItemsOwnerFactory>();
                        var lookupItemsOwner = factory.CreateLookupItemsOwner(textControl);

                        LookupUtil.ShowParameterInfo(solution, textControl, lookupItemsOwner);
                    }
                }

                TipsManager.Instance.FeatureIsUsed(
                    "Plugin.ControlFlow.PostfixTemplates.<static>", textControl.Document, solution);
            });
        }