Beispiel #1
0
        private static void ApplyRenameHotspots(
            [NotNull] LiveTemplatesManager liveTemplatesManager, [NotNull] ITextControl textControl,
            [NotNull] IForeachStatement statement, [NotNull] IList <string> namesCollection,
            [CanBeNull] IReferenceExpression extraReference = null)
        {
            var variableDeclaration = statement.IteratorDeclaration;
            var endSelectionRange   = new TextRange(textControl.Caret.Offset());

            var suggestTypeName = new MacroCallExpressionNew(new SuggestVariableTypeMacroDef());
            var typeNameInfo    = new HotspotInfo(
                new TemplateField("type", suggestTypeName, 0),
                variableDeclaration.VarKeyword.GetDocumentRange());

            var nameRanges = new LocalList <DocumentRange>();

            nameRanges.Add(variableDeclaration.NameIdentifier.GetDocumentRange());

            if (extraReference != null)
            {
                var documentRange = extraReference.GetDocumentRange();
                nameRanges.Add(documentRange);
                endSelectionRange = new TextRange(documentRange.TextRange.EndOffset);
            }

            var variableNameInfo = new HotspotInfo(
                new TemplateField("name", new NameSuggestionsExpression(namesCollection), 0),
                nameRanges.ToArray());

            var session = liveTemplatesManager.CreateHotspotSessionAtopExistingText(
                statement.GetSolution(), endSelectionRange, textControl,
                LiveTemplatesManager.EscapeAction.LeaveTextAndCaret, typeNameInfo, variableNameInfo);

            session.Execute();
        }
            private void InsertQualifierAsArgument([NotNull] IReferenceExpression referenceExpression, int existingArgumentsCount, [NotNull] ITextControl textControl)
            {
                var qualifierExpression = referenceExpression.QualifierExpression.NotNull("qualifierExpression != null");
                var qualifierText       = qualifierExpression.GetText();

                var enumerationType = FindReferencedEnumerationType(qualifierExpression);

                if (enumerationType != null)
                {
                    qualifierText = "typeof(" + qualifierText + ")";
                }

                if (IsFirstArgumentAlwaysPassedByRef())
                {
                    qualifierText = "ref " + qualifierText;
                }

                if (existingArgumentsCount > 0 || HasOnlyMultipleParameters())
                {
                    qualifierText += ", ";
                }

                TextRange argPosition;

                // todo: not reliable?
                var invokedExpression = InvocationExpressionNavigator.GetByInvokedExpression(referenceExpression);

                if (invokedExpression == null)
                {
                    qualifierText = "(" + qualifierText;
                    argPosition   = referenceExpression.GetDocumentRange().EndOffsetRange().TextRange;

                    // todo: check pars decoration setting
                    // todo: insert ')' if it's insertion is disabled
                }
                else
                {
                    argPosition = invokedExpression.LPar.GetDocumentRange().EndOffsetRange().TextRange;
                }

                var qualifierDocumentRange = qualifierExpression.GetDocumentRange();
                var qualifierWithDelimiter = qualifierDocumentRange.JoinRight(referenceExpression.NameIdentifier.GetDocumentStartOffset());

                textControl.Document.ReplaceText(argPosition, qualifierText);
                textControl.Document.DeleteText(qualifierWithDelimiter.TextRange);
            }
            private void PlaceCaretAfterCompletion(
                [NotNull] ITextControl textControl, [NotNull] IReferenceExpression referenceExpression, int existingArgumentsCount, LookupItemInsertType insertType)
            {
                var referenceRange = referenceExpression.GetDocumentRange();

                textControl.Caret.MoveTo(referenceRange.TextRange.EndOffset, CaretVisualPlacement.DontScrollIfVisible);

                var invocationExpression = InvocationExpressionNavigator.GetByInvokedExpression(referenceExpression);

                if (invocationExpression == null)
                {
                    return;
                }

                var invocationRange = invocationExpression.GetDocumentRange();

                textControl.Caret.MoveTo(invocationRange.TextRange.EndOffset, CaretVisualPlacement.DontScrollIfVisible);

                var settingsStore           = referenceExpression.GetSettingsStore();
                var parenthesesInsertType   = settingsStore.GetValue(CodeCompletionSettingsAccessor.ParenthesesInsertType);
                var hasMoreParametersToPass = HasMoreParametersToPass(existingArgumentsCount);

                switch (parenthesesInsertType)
                {
                case ParenthesesInsertType.Both:
                {
                    if (hasMoreParametersToPass)
                    {
                        var rightPar = invocationExpression.RPar;
                        if (rightPar != null)
                        {
                            var rightParRange = rightPar.GetDocumentRange().TextRange;
                            textControl.Caret.MoveTo(rightParRange.StartOffset, CaretVisualPlacement.DontScrollIfVisible);
                        }
                    }

                    break;
                }

                case ParenthesesInsertType.Left:
                case ParenthesesInsertType.None:
                {
                    // if in insert mode - drop right par and set caret to it's start offest
                    if (insertType == LookupItemInsertType.Insert)
                    {
                        var rightPar = invocationExpression.RPar;
                        if (rightPar != null)
                        {
                            var rightParRange = rightPar.GetDocumentRange().TextRange;

                            invocationExpression.GetPsiServices().Transactions.Execute(
                                commandName: typeof(StaticMethodBehavior).FullName,
                                handler: () =>
                                {
                                    using (WriteLockCookie.Create())
                                        LowLevelModificationUtil.DeleteChild(rightPar);
                                });

                            textControl.Caret.MoveTo(rightParRange.StartOffset, CaretVisualPlacement.DontScrollIfVisible);
                        }
                    }

                    break;
                }
                }
            }